blob: 0eb70c986a67ebf67ea411ee7e3e0822cf270171 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#include "asn1c_internal.h"
Lev Walkin79f54952004-08-13 16:58:19 +00002#include "asn1c_compat.h"
Lev Walkinacd9f8b2004-08-19 13:29:03 +00003#include "asn1c_fdeps.h"
Lev Walkin59004fa2004-08-20 13:37:01 +00004#include "asn1c_lang.h"
5#include "asn1c_misc.h"
6#include "asn1c_save.h"
7#include "asn1c_out.h"
Lev Walkinf15320b2004-06-03 03:38:44 +00008
Lev Walkinacd9f8b2004-08-19 13:29:03 +00009static int asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *);
Lev Walkinf15320b2004-06-03 03:38:44 +000010static int asn1c_print_streams(arg_t *arg);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000011static int asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *);
Lev Walkinf15320b2004-06-03 03:38:44 +000012static int asn1c_copy_over(arg_t *arg, char *path);
Lev Walkin4604d032005-03-04 08:52:50 +000013static int identical_files(const char *fname1, const char *fname2);
Lev Walkinf15320b2004-06-03 03:38:44 +000014
15int
Lev Walkin866cff12005-03-05 00:50:53 +000016asn1c_save_compiled_output(arg_t *arg, const char *datadir,
17 int argc, char **argv) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000018 asn1c_fdeps_t *deps = 0;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000019 asn1c_fdeps_t *dlist;
Lev Walkinb85a8132005-08-18 13:38:19 +000020 asn1p_module_t *mod;
Lev Walkin866cff12005-03-05 00:50:53 +000021 FILE *mkf;
22 int i;
Lev Walkinf15320b2004-06-03 03:38:44 +000023
Lev Walkinacd9f8b2004-08-19 13:29:03 +000024 deps = asn1c_read_file_dependencies(arg, datadir);
25 if(!deps && datadir) {
26 WARNING("Cannot read file-dependencies information "
27 "from %s\n", datadir);
28 }
Lev Walkinf15320b2004-06-03 03:38:44 +000029
Lev Walkinb85a8132005-08-18 13:38:19 +000030 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
31 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +000032 if(asn1_lang_map[arg->expr->meta_type]
33 [arg->expr->expr_type].type_cb) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000034 if(asn1c_dump_streams(arg, deps))
Lev Walkinf15320b2004-06-03 03:38:44 +000035 return -1;
36 }
37 }
38 }
39
40 /*
41 * Dump out the Makefile template and the rest of the support code.
42 */
Lev Walkinacd9f8b2004-08-19 13:29:03 +000043 if((arg->flags & A1C_PRINT_COMPILED)
44 || (arg->flags & A1C_OMIT_SUPPORT_CODE)) {
45 return 0; /* Finished */
46 }
Lev Walkinf15320b2004-06-03 03:38:44 +000047
Lev Walkin4604d032005-03-04 08:52:50 +000048 mkf = asn1c_open_file("Makefile.am", ".sample", 0);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000049 if(mkf == NULL) {
50 perror("Makefile.am.sample");
51 return -1;
52 }
Lev Walkinf15320b2004-06-03 03:38:44 +000053
Lev Walkinacd9f8b2004-08-19 13:29:03 +000054 fprintf(mkf, "ASN_MODULE_SOURCES=");
Lev Walkinb85a8132005-08-18 13:38:19 +000055 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
56 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000057 if(asn1_lang_map[arg->expr->meta_type]
58 [arg->expr->expr_type].type_cb) {
Lev Walkind29bbce2004-09-23 22:19:14 +000059 fprintf(mkf, "\t\\\n\t%s.c",
Lev Walkinacd9f8b2004-08-19 13:29:03 +000060 arg->expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +000061 }
62 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +000063 }
Lev Walkind29bbce2004-09-23 22:19:14 +000064 fprintf(mkf, "\n\nASN_MODULE_HEADERS=");
Lev Walkinb85a8132005-08-18 13:38:19 +000065 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
66 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkind29bbce2004-09-23 22:19:14 +000067 if(asn1_lang_map[arg->expr->meta_type]
68 [arg->expr->expr_type].type_cb) {
69 fprintf(mkf, "\t\\\n\t%s.h",
70 arg->expr->Identifier);
71 }
72 }
73 }
74 fprintf(mkf, "\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +000075
Lev Walkinacd9f8b2004-08-19 13:29:03 +000076 /*
77 * Move necessary skeleton files and add them to Makefile.am.sample.
78 */
79 dlist = asn1c_deps_makelist(deps);
80 if(dlist) {
81 char buf[8129];
82 char *dir_end;
Lev Walkin4efbfb72005-02-25 14:20:30 +000083 size_t dlen = strlen(datadir);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000084
Lev Walkin4efbfb72005-02-25 14:20:30 +000085 assert(dlen < (sizeof(buf) / 2 - 2));
86 memcpy(buf, datadir, dlen);
87 dir_end = buf + dlen;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000088 *dir_end++ = '/';
89
90 for(i = 0; i < dlist->el_count; i++) {
91 char *fname = dlist->elements[i]->filename;
Lev Walkind29bbce2004-09-23 22:19:14 +000092 char *dotH;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000093
94 assert(strlen(fname) < (sizeof(buf) / 2));
95 strcpy(dir_end, fname);
96
97 if(asn1c_copy_over(arg, buf) == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +000098 fprintf(mkf, ">>>ABORTED<<<");
99 fclose(mkf);
Lev Walkinf15320b2004-06-03 03:38:44 +0000100 return -1;
Lev Walkind29bbce2004-09-23 22:19:14 +0000101 }
102 dotH = strrchr(fname, 'h');
103 if(dotH && fname<dotH && dotH[-1] == '.' && !dotH[1]) {
104 fprintf(mkf, "ASN_MODULE_HEADERS+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000105 } else {
Lev Walkind29bbce2004-09-23 22:19:14 +0000106 fprintf(mkf, "ASN_MODULE_SOURCES+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000107 }
108 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000109 }
110
Lev Walkind29bbce2004-09-23 22:19:14 +0000111 fprintf(mkf, "\n\n"
112 "lib_LTLIBRARIES=libsomething.la\n"
113 "libsomething_la_SOURCES="
114 "$(ASN_MODULE_SOURCES) $(ASN_MODULE_HEADERS)\n"
115 "\n"
116 "# This file may be used as an input for make(3)\n"
117 "# Remove the lines below to convert it into a pure .am file\n"
118 "TARGET = progname\n"
119 "CFLAGS += -I.\n"
120 "OBJS=${ASN_MODULE_SOURCES:.c=.o} $(TARGET).o\n"
121 "\nall: $(TARGET)\n"
122 "\n$(TARGET): ${OBJS}"
123 "\n\t$(CC) $(CFLAGS) -o $(TARGET) ${OBJS} $(LDFLAGS) $(LIBS)\n"
124 "\n.SUFFIXES:"
125 "\n.SUFFIXES: .c .o\n"
126 "\n.c.o:"
127 "\n\t$(CC) $(CFLAGS) -o $@ -c $<\n"
128 "\nclean:"
129 "\n\trm -f $(TARGET)"
130 "\n\trm -f $(OBJS)\n"
Lev Walkin866cff12005-03-05 00:50:53 +0000131 "\nregen: regenerate-from-asn1-source\n"
132 "\nregenerate-from-asn1-source:\n\t"
Lev Walkind29bbce2004-09-23 22:19:14 +0000133 );
Lev Walkin866cff12005-03-05 00:50:53 +0000134
135 for(i = 0; i < argc; i++)
136 fprintf(mkf, "%s%s", i ? " " : "", argv[i]);
137 fprintf(mkf, "\n\n");
138
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000139 fclose(mkf);
140 fprintf(stderr, "Generated Makefile.am.sample\n");
141
Lev Walkinf15320b2004-06-03 03:38:44 +0000142 return 0;
143}
144
145/*
146 * Dump the streams.
147 */
148static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000149asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000150 if(arg->flags & A1C_PRINT_COMPILED) {
151 return asn1c_print_streams(arg);
152 } else {
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000153 return asn1c_save_streams(arg, deps);
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 }
155}
156
157static int
158asn1c_print_streams(arg_t *arg) {
159 compiler_streams_t *cs = arg->expr->data;
160 asn1p_expr_t *expr = arg->expr;
161 int i;
162
Lev Walkin64399722004-08-11 07:17:22 +0000163 for(i = 1; i < OT_MAX; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000164 out_chunk_t *ot;
Lev Walkin59004fa2004-08-20 13:37:01 +0000165 if(TQ_FIRST(&cs->destination[i].chunks) == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +0000166 continue;
167
168 printf("\n/*** <<< %s [%s] >>> ***/\n\n",
169 _compiler_stream2str[i],
170 expr->Identifier);
171
Lev Walkin59004fa2004-08-20 13:37:01 +0000172 TQ_FOR(ot, &(cs->destination[i].chunks), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000173 fwrite(ot->buf, ot->len, 1, stdout);
174 }
175 }
176
177 return 0;
178}
179
180static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000181asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 asn1p_expr_t *expr = arg->expr;
183 compiler_streams_t *cs = expr->data;
184 out_chunk_t *ot;
185 FILE *fp_c, *fp_h;
Lev Walkin4604d032005-03-04 08:52:50 +0000186 char *tmpname_c, *tmpname_h;
187 char *name_buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000188 char *header_id;
Lev Walkin4604d032005-03-04 08:52:50 +0000189 const char *c_retained = "";
190 const char *h_retained = "";
Lev Walkinf15320b2004-06-03 03:38:44 +0000191
192 if(cs == NULL) {
193 fprintf(stderr, "Cannot compile %s at line %d\n",
194 expr->Identifier, expr->_lineno);
195 return -1;
196 }
197
Lev Walkin4604d032005-03-04 08:52:50 +0000198 fp_c = asn1c_open_file(expr->Identifier, ".c", &tmpname_c);
199 fp_h = asn1c_open_file(expr->Identifier, ".h", &tmpname_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000200 if(fp_c == NULL || fp_h == NULL) {
Lev Walkin4604d032005-03-04 08:52:50 +0000201 if(fp_c) { unlink(tmpname_c); free(tmpname_c); fclose(fp_c); }
202 if(fp_h) { unlink(tmpname_h); free(tmpname_h); fclose(fp_h); }
Lev Walkinf15320b2004-06-03 03:38:44 +0000203 return -1;
204 }
205
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000206 fprintf(fp_c,
207 "/*\n"
208 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000209 " * From ASN.1 module \"%s\"\n"
210 " * \tfound in \"%s\"\n"
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000211 " */\n\n",
Lev Walkinb85a8132005-08-18 13:38:19 +0000212 expr->module->ModuleName,
213 expr->module->source_file_name
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000214 );
215 fprintf(fp_h,
216 "/*\n"
217 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000218 " * From ASN.1 module \"%s\"\n"
219 " * \tfound in \"%s\"\n"
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000220 " */\n\n",
Lev Walkinb85a8132005-08-18 13:38:19 +0000221 expr->module->ModuleName,
222 expr->module->source_file_name
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000223 );
224
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000225 header_id = asn1c_make_identifier(0, expr->Identifier, NULL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000226 fprintf(fp_h,
227 "#ifndef\t_%s_H_\n"
228 "#define\t_%s_H_\n"
229 "\n", header_id, header_id);
230
Lev Walkin3d551c02005-07-15 18:49:41 +0000231 fprintf(fp_h, "\n#include <asn_application.h>\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000232
Lev Walkinb9b8b952005-03-05 00:33:27 +0000233#define SAVE_STREAM(fp, idx, msg, actdep) do { \
Lev Walkin866cff12005-03-05 00:50:53 +0000234 if(TQ_FIRST(&(cs->destination[idx].chunks)) && *msg) \
Lev Walkinb9b8b952005-03-05 00:33:27 +0000235 fprintf(fp, "\n/* %s */\n", msg); \
Lev Walkinc8285712005-03-04 22:18:20 +0000236 TQ_FOR(ot, &(cs->destination[idx].chunks), next) { \
237 if(actdep) asn1c_activate_dependency(deps, 0, ot->buf); \
Lev Walkinb9b8b952005-03-05 00:33:27 +0000238 fwrite(ot->buf, ot->len, 1, fp); \
Lev Walkinc8285712005-03-04 22:18:20 +0000239 } \
240} while(0)
241
Lev Walkinb9b8b952005-03-05 00:33:27 +0000242 SAVE_STREAM(fp_h, OT_INCLUDES, "Including external dependencies", 1);
Lev Walkin3d551c02005-07-15 18:49:41 +0000243
244 fprintf(fp_h, "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
245
Lev Walkinb9b8b952005-03-05 00:33:27 +0000246 SAVE_STREAM(fp_h, OT_DEPS, "Dependencies", 0);
247 SAVE_STREAM(fp_h, OT_FWD_DECLS, "Forward declarations", 0);
248 SAVE_STREAM(fp_h, OT_TYPE_DECLS, expr->Identifier, 0);
249 SAVE_STREAM(fp_h, OT_FUNC_DECLS,"Implementation", 0);
250 if(!(arg->flags & A1C_NO_INCLUDE_DEPS))
251 SAVE_STREAM(fp_h, OT_POST_INCLUDE, "Referred external types", 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000252
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000253 fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n\n"
254 "#endif\t/* _%s_H_ */\n",
255 header_id);
256
Lev Walkin0ffabe22004-09-22 16:02:03 +0000257 fprintf(fp_c, "#include <asn_internal.h>\n\n");
Lev Walkine8318b82005-03-06 09:29:03 +0000258 fprintf(fp_c, "#include \"%s.h\"\n\n", expr->Identifier);
Lev Walkinb9b8b952005-03-05 00:33:27 +0000259 if(arg->flags & A1C_NO_INCLUDE_DEPS)
Lev Walkin866cff12005-03-05 00:50:53 +0000260 SAVE_STREAM(fp_c, OT_POST_INCLUDE, "", 1);
Lev Walkin59004fa2004-08-20 13:37:01 +0000261 TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000262 fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000263 TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
264 fwrite(ot->buf, ot->len, 1, fp_c);
265 TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000266 fwrite(ot->buf, ot->len, 1, fp_c);
267
Lev Walkinc8285712005-03-04 22:18:20 +0000268 assert(OT_MAX == 10); /* Protection from reckless changes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000269
270 fclose(fp_c);
271 fclose(fp_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000272
273 name_buf = alloca(strlen(expr->Identifier) + 3);
274
275 sprintf(name_buf, "%s.c", expr->Identifier);
276 if(identical_files(name_buf, tmpname_c)) {
277 c_retained = " (contents unchanged)";
278 unlink(tmpname_c);
279 } else {
280 if(rename(tmpname_c, name_buf)) {
281 unlink(tmpname_c);
282 perror(tmpname_c);
283 return -1;
284 }
285 }
286
287 sprintf(name_buf, "%s.h", expr->Identifier);
288 if(identical_files(name_buf, tmpname_h)) {
289 h_retained = " (contents unchanged)";
290 unlink(tmpname_h);
291 } else {
292 if(rename(tmpname_h, name_buf)) {
293 unlink(tmpname_h);
294 perror(tmpname_h);
295 return -1;
296 }
297 }
298
299 free(tmpname_c);
300 free(tmpname_h);
301
302 fprintf(stderr, "Compiled %s.c%s\n",
303 expr->Identifier, c_retained);
304 fprintf(stderr, "Compiled %s.h%s\n",
305 expr->Identifier, h_retained);
Lev Walkinf15320b2004-06-03 03:38:44 +0000306 return 0;
307}
308
Lev Walkin4604d032005-03-04 08:52:50 +0000309static int
310identical_files(const char *fname1, const char *fname2) {
311 char buf[2][8192];
312 FILE *fp1, *fp2;
313 size_t olen, nlen;
314 int retval = 1; /* Files are identical */
315
316 fp1 = fopen(fname1, "r");
317 if(!fp1) { return 0; }
318 fp2 = fopen(fname2, "r");
319 if(!fp2) { fclose(fp1); return 0; }
320
321 while((olen = fread(buf[0], 1, sizeof(buf[0]), fp1))) {
322 nlen = fread(buf[1], 1, olen, fp2);
323 if(nlen != olen || memcmp(buf[0], buf[1], nlen)) {
324 retval = 0;
325 break;
326 }
327 }
328 nlen = fread(buf[1], 1, 1, fp2);
329 if(nlen) retval = 0;
330
331 fclose(fp1);
332 fclose(fp2);
333 return retval;
334}
335
Lev Walkin4efbfb72005-02-25 14:20:30 +0000336/*
337 * Copy file for real.
338 */
339static int
340real_copy(const char *src, const char *dst) {
341 unsigned char buf[8192];
342 FILE *fpsrc, *fpdst;
343 size_t len;
344 int retval = 0;
345
Lev Walkin4604d032005-03-04 08:52:50 +0000346 if(identical_files(src, dst))
347 return retval; /* Success, no need to copy for real. */
348
349 fpsrc = fopen(src, "r");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000350 if(!fpsrc) { errno = EIO; return -1; }
Lev Walkin4604d032005-03-04 08:52:50 +0000351 fpdst = asn1c_open_file(dst, "", 0);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000352 if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; }
353
354 while(!feof(fpsrc)) {
355 len = fread(buf, 1, sizeof(buf), fpsrc);
356 if(fwrite(buf, 1, len, fpsrc) != len) {
357 errno = EIO;
358 retval = -1;
359 break;
360 }
361 }
362
363 fclose(fpsrc);
364 fclose(fpdst);
365 return retval;
366}
367
Lev Walkinf15320b2004-06-03 03:38:44 +0000368static int
369asn1c_copy_over(arg_t *arg, char *path) {
Lev Walkin79f54952004-08-13 16:58:19 +0000370 char *fname;
Lev Walkinf15320b2004-06-03 03:38:44 +0000371
Lev Walkind9bd7752004-06-05 08:17:50 +0000372 (void)arg; /* Unused argument */
373
Lev Walkin79f54952004-08-13 16:58:19 +0000374 fname = a1c_basename(path);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000375 if(!fname
376#ifdef WIN32
377 || real_copy(path, fname)
378#else
379 || (1 ? symlink(path, fname) : real_copy(path, fname))
380#endif
381 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000382 if(errno == EEXIST) {
383 struct stat sb1, sb2;
384 if(stat(path, &sb1) == 0
385 && stat(fname, &sb2) == 0
386 && sb1.st_dev == sb2.st_dev
387 && sb1.st_ino == sb2.st_ino) {
388 /*
389 * Nothing to do.
390 */
391 fprintf(stderr,
392 "File %s is already here as %s\n",
393 path, fname);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000394 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000395 } else {
396 fprintf(stderr,
397 "Retaining local %s (%s suggested)\n",
398 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000399 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000400 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000401 } else if(errno == ENOENT) {
402 /* Ignore this */
403 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000404 } else {
405 fprintf(stderr, "Symlink %s -> %s failed: %s\n",
406 path, fname, strerror(errno));
407 return -1;
408 }
409 }
410
411 fprintf(stderr, "Symlinked %s\t-> %s\n", path, fname);
412
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000413 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000414}
415