blob: 8a67e26e88e41bf7d6d15f661c1096072953a852 [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 Walkin866cff12005-03-05 00:50:53 +000020 FILE *mkf;
21 int i;
Lev Walkinf15320b2004-06-03 03:38:44 +000022
Lev Walkinacd9f8b2004-08-19 13:29:03 +000023 deps = asn1c_read_file_dependencies(arg, datadir);
24 if(!deps && datadir) {
25 WARNING("Cannot read file-dependencies information "
26 "from %s\n", datadir);
27 }
Lev Walkinf15320b2004-06-03 03:38:44 +000028
29 TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) {
30 TQ_FOR(arg->expr, &(arg->mod->members), next) {
31 if(asn1_lang_map[arg->expr->meta_type]
32 [arg->expr->expr_type].type_cb) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000033 if(asn1c_dump_streams(arg, deps))
Lev Walkinf15320b2004-06-03 03:38:44 +000034 return -1;
35 }
36 }
37 }
38
39 /*
40 * Dump out the Makefile template and the rest of the support code.
41 */
Lev Walkinacd9f8b2004-08-19 13:29:03 +000042 if((arg->flags & A1C_PRINT_COMPILED)
43 || (arg->flags & A1C_OMIT_SUPPORT_CODE)) {
44 return 0; /* Finished */
45 }
Lev Walkinf15320b2004-06-03 03:38:44 +000046
Lev Walkin4604d032005-03-04 08:52:50 +000047 mkf = asn1c_open_file("Makefile.am", ".sample", 0);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000048 if(mkf == NULL) {
49 perror("Makefile.am.sample");
50 return -1;
51 }
Lev Walkinf15320b2004-06-03 03:38:44 +000052
Lev Walkinacd9f8b2004-08-19 13:29:03 +000053 fprintf(mkf, "ASN_MODULE_SOURCES=");
54 TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) {
55 TQ_FOR(arg->expr, &(arg->mod->members), next) {
56 if(asn1_lang_map[arg->expr->meta_type]
57 [arg->expr->expr_type].type_cb) {
Lev Walkind29bbce2004-09-23 22:19:14 +000058 fprintf(mkf, "\t\\\n\t%s.c",
Lev Walkinacd9f8b2004-08-19 13:29:03 +000059 arg->expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +000060 }
61 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +000062 }
Lev Walkind29bbce2004-09-23 22:19:14 +000063 fprintf(mkf, "\n\nASN_MODULE_HEADERS=");
64 TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) {
65 TQ_FOR(arg->expr, &(arg->mod->members), next) {
66 if(asn1_lang_map[arg->expr->meta_type]
67 [arg->expr->expr_type].type_cb) {
68 fprintf(mkf, "\t\\\n\t%s.h",
69 arg->expr->Identifier);
70 }
71 }
72 }
73 fprintf(mkf, "\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +000074
Lev Walkinacd9f8b2004-08-19 13:29:03 +000075 /*
76 * Move necessary skeleton files and add them to Makefile.am.sample.
77 */
78 dlist = asn1c_deps_makelist(deps);
79 if(dlist) {
80 char buf[8129];
81 char *dir_end;
Lev Walkin4efbfb72005-02-25 14:20:30 +000082 size_t dlen = strlen(datadir);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000083
Lev Walkin4efbfb72005-02-25 14:20:30 +000084 assert(dlen < (sizeof(buf) / 2 - 2));
85 memcpy(buf, datadir, dlen);
86 dir_end = buf + dlen;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000087 *dir_end++ = '/';
88
89 for(i = 0; i < dlist->el_count; i++) {
90 char *fname = dlist->elements[i]->filename;
Lev Walkind29bbce2004-09-23 22:19:14 +000091 char *dotH;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000092
93 assert(strlen(fname) < (sizeof(buf) / 2));
94 strcpy(dir_end, fname);
95
96 if(asn1c_copy_over(arg, buf) == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +000097 fprintf(mkf, ">>>ABORTED<<<");
98 fclose(mkf);
Lev Walkinf15320b2004-06-03 03:38:44 +000099 return -1;
Lev Walkind29bbce2004-09-23 22:19:14 +0000100 }
101 dotH = strrchr(fname, 'h');
102 if(dotH && fname<dotH && dotH[-1] == '.' && !dotH[1]) {
103 fprintf(mkf, "ASN_MODULE_HEADERS+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000104 } else {
Lev Walkind29bbce2004-09-23 22:19:14 +0000105 fprintf(mkf, "ASN_MODULE_SOURCES+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000106 }
107 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000108 }
109
Lev Walkind29bbce2004-09-23 22:19:14 +0000110 fprintf(mkf, "\n\n"
111 "lib_LTLIBRARIES=libsomething.la\n"
112 "libsomething_la_SOURCES="
113 "$(ASN_MODULE_SOURCES) $(ASN_MODULE_HEADERS)\n"
114 "\n"
115 "# This file may be used as an input for make(3)\n"
116 "# Remove the lines below to convert it into a pure .am file\n"
117 "TARGET = progname\n"
118 "CFLAGS += -I.\n"
119 "OBJS=${ASN_MODULE_SOURCES:.c=.o} $(TARGET).o\n"
120 "\nall: $(TARGET)\n"
121 "\n$(TARGET): ${OBJS}"
122 "\n\t$(CC) $(CFLAGS) -o $(TARGET) ${OBJS} $(LDFLAGS) $(LIBS)\n"
123 "\n.SUFFIXES:"
124 "\n.SUFFIXES: .c .o\n"
125 "\n.c.o:"
126 "\n\t$(CC) $(CFLAGS) -o $@ -c $<\n"
127 "\nclean:"
128 "\n\trm -f $(TARGET)"
129 "\n\trm -f $(OBJS)\n"
Lev Walkin866cff12005-03-05 00:50:53 +0000130 "\nregen: regenerate-from-asn1-source\n"
131 "\nregenerate-from-asn1-source:\n\t"
Lev Walkind29bbce2004-09-23 22:19:14 +0000132 );
Lev Walkin866cff12005-03-05 00:50:53 +0000133
134 for(i = 0; i < argc; i++)
135 fprintf(mkf, "%s%s", i ? " " : "", argv[i]);
136 fprintf(mkf, "\n\n");
137
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000138 fclose(mkf);
139 fprintf(stderr, "Generated Makefile.am.sample\n");
140
Lev Walkinf15320b2004-06-03 03:38:44 +0000141 return 0;
142}
143
144/*
145 * Dump the streams.
146 */
147static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000148asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000149 if(arg->flags & A1C_PRINT_COMPILED) {
150 return asn1c_print_streams(arg);
151 } else {
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000152 return asn1c_save_streams(arg, deps);
Lev Walkinf15320b2004-06-03 03:38:44 +0000153 }
154}
155
156static int
157asn1c_print_streams(arg_t *arg) {
158 compiler_streams_t *cs = arg->expr->data;
159 asn1p_expr_t *expr = arg->expr;
160 int i;
161
Lev Walkin64399722004-08-11 07:17:22 +0000162 for(i = 1; i < OT_MAX; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000163 out_chunk_t *ot;
Lev Walkin59004fa2004-08-20 13:37:01 +0000164 if(TQ_FIRST(&cs->destination[i].chunks) == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +0000165 continue;
166
167 printf("\n/*** <<< %s [%s] >>> ***/\n\n",
168 _compiler_stream2str[i],
169 expr->Identifier);
170
Lev Walkin59004fa2004-08-20 13:37:01 +0000171 TQ_FOR(ot, &(cs->destination[i].chunks), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000172 fwrite(ot->buf, ot->len, 1, stdout);
173 }
174 }
175
176 return 0;
177}
178
179static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000180asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 asn1p_expr_t *expr = arg->expr;
182 compiler_streams_t *cs = expr->data;
183 out_chunk_t *ot;
184 FILE *fp_c, *fp_h;
Lev Walkin4604d032005-03-04 08:52:50 +0000185 char *tmpname_c, *tmpname_h;
186 char *name_buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 char *header_id;
Lev Walkin4604d032005-03-04 08:52:50 +0000188 const char *c_retained = "";
189 const char *h_retained = "";
Lev Walkinf15320b2004-06-03 03:38:44 +0000190
191 if(cs == NULL) {
192 fprintf(stderr, "Cannot compile %s at line %d\n",
193 expr->Identifier, expr->_lineno);
194 return -1;
195 }
196
Lev Walkin4604d032005-03-04 08:52:50 +0000197 fp_c = asn1c_open_file(expr->Identifier, ".c", &tmpname_c);
198 fp_h = asn1c_open_file(expr->Identifier, ".h", &tmpname_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000199 if(fp_c == NULL || fp_h == NULL) {
Lev Walkin4604d032005-03-04 08:52:50 +0000200 if(fp_c) { unlink(tmpname_c); free(tmpname_c); fclose(fp_c); }
201 if(fp_h) { unlink(tmpname_h); free(tmpname_h); fclose(fp_h); }
Lev Walkinf15320b2004-06-03 03:38:44 +0000202 return -1;
203 }
204
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000205 fprintf(fp_c,
206 "/*\n"
207 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000208 " * From ASN.1 module \"%s\"\n"
209 " * \tfound in \"%s\"\n"
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000210 " */\n\n",
211 arg->mod->Identifier,
212 arg->mod->source_file_name
213 );
214 fprintf(fp_h,
215 "/*\n"
216 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000217 " * From ASN.1 module \"%s\"\n"
218 " * \tfound in \"%s\"\n"
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000219 " */\n\n",
220 arg->mod->Identifier,
221 arg->mod->source_file_name
222 );
223
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000224 header_id = asn1c_make_identifier(0, expr->Identifier, NULL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000225 fprintf(fp_h,
226 "#ifndef\t_%s_H_\n"
227 "#define\t_%s_H_\n"
228 "\n", header_id, header_id);
229
Lev Walkin3d551c02005-07-15 18:49:41 +0000230 fprintf(fp_h, "\n#include <asn_application.h>\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000231
Lev Walkinb9b8b952005-03-05 00:33:27 +0000232#define SAVE_STREAM(fp, idx, msg, actdep) do { \
Lev Walkin866cff12005-03-05 00:50:53 +0000233 if(TQ_FIRST(&(cs->destination[idx].chunks)) && *msg) \
Lev Walkinb9b8b952005-03-05 00:33:27 +0000234 fprintf(fp, "\n/* %s */\n", msg); \
Lev Walkinc8285712005-03-04 22:18:20 +0000235 TQ_FOR(ot, &(cs->destination[idx].chunks), next) { \
236 if(actdep) asn1c_activate_dependency(deps, 0, ot->buf); \
Lev Walkinb9b8b952005-03-05 00:33:27 +0000237 fwrite(ot->buf, ot->len, 1, fp); \
Lev Walkinc8285712005-03-04 22:18:20 +0000238 } \
239} while(0)
240
Lev Walkinb9b8b952005-03-05 00:33:27 +0000241 SAVE_STREAM(fp_h, OT_INCLUDES, "Including external dependencies", 1);
Lev Walkin3d551c02005-07-15 18:49:41 +0000242
243 fprintf(fp_h, "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
244
Lev Walkinb9b8b952005-03-05 00:33:27 +0000245 SAVE_STREAM(fp_h, OT_DEPS, "Dependencies", 0);
246 SAVE_STREAM(fp_h, OT_FWD_DECLS, "Forward declarations", 0);
247 SAVE_STREAM(fp_h, OT_TYPE_DECLS, expr->Identifier, 0);
248 SAVE_STREAM(fp_h, OT_FUNC_DECLS,"Implementation", 0);
249 if(!(arg->flags & A1C_NO_INCLUDE_DEPS))
250 SAVE_STREAM(fp_h, OT_POST_INCLUDE, "Referred external types", 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000251
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000252 fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n\n"
253 "#endif\t/* _%s_H_ */\n",
254 header_id);
255
Lev Walkin0ffabe22004-09-22 16:02:03 +0000256 fprintf(fp_c, "#include <asn_internal.h>\n\n");
Lev Walkine8318b82005-03-06 09:29:03 +0000257 fprintf(fp_c, "#include \"%s.h\"\n\n", expr->Identifier);
Lev Walkinb9b8b952005-03-05 00:33:27 +0000258 if(arg->flags & A1C_NO_INCLUDE_DEPS)
Lev Walkin866cff12005-03-05 00:50:53 +0000259 SAVE_STREAM(fp_c, OT_POST_INCLUDE, "", 1);
Lev Walkin59004fa2004-08-20 13:37:01 +0000260 TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000261 fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000262 TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
263 fwrite(ot->buf, ot->len, 1, fp_c);
264 TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000265 fwrite(ot->buf, ot->len, 1, fp_c);
266
Lev Walkinc8285712005-03-04 22:18:20 +0000267 assert(OT_MAX == 10); /* Protection from reckless changes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000268
269 fclose(fp_c);
270 fclose(fp_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000271
272 name_buf = alloca(strlen(expr->Identifier) + 3);
273
274 sprintf(name_buf, "%s.c", expr->Identifier);
275 if(identical_files(name_buf, tmpname_c)) {
276 c_retained = " (contents unchanged)";
277 unlink(tmpname_c);
278 } else {
279 if(rename(tmpname_c, name_buf)) {
280 unlink(tmpname_c);
281 perror(tmpname_c);
282 return -1;
283 }
284 }
285
286 sprintf(name_buf, "%s.h", expr->Identifier);
287 if(identical_files(name_buf, tmpname_h)) {
288 h_retained = " (contents unchanged)";
289 unlink(tmpname_h);
290 } else {
291 if(rename(tmpname_h, name_buf)) {
292 unlink(tmpname_h);
293 perror(tmpname_h);
294 return -1;
295 }
296 }
297
298 free(tmpname_c);
299 free(tmpname_h);
300
301 fprintf(stderr, "Compiled %s.c%s\n",
302 expr->Identifier, c_retained);
303 fprintf(stderr, "Compiled %s.h%s\n",
304 expr->Identifier, h_retained);
Lev Walkinf15320b2004-06-03 03:38:44 +0000305 return 0;
306}
307
Lev Walkin4604d032005-03-04 08:52:50 +0000308static int
309identical_files(const char *fname1, const char *fname2) {
310 char buf[2][8192];
311 FILE *fp1, *fp2;
312 size_t olen, nlen;
313 int retval = 1; /* Files are identical */
314
315 fp1 = fopen(fname1, "r");
316 if(!fp1) { return 0; }
317 fp2 = fopen(fname2, "r");
318 if(!fp2) { fclose(fp1); return 0; }
319
320 while((olen = fread(buf[0], 1, sizeof(buf[0]), fp1))) {
321 nlen = fread(buf[1], 1, olen, fp2);
322 if(nlen != olen || memcmp(buf[0], buf[1], nlen)) {
323 retval = 0;
324 break;
325 }
326 }
327 nlen = fread(buf[1], 1, 1, fp2);
328 if(nlen) retval = 0;
329
330 fclose(fp1);
331 fclose(fp2);
332 return retval;
333}
334
Lev Walkin4efbfb72005-02-25 14:20:30 +0000335/*
336 * Copy file for real.
337 */
338static int
339real_copy(const char *src, const char *dst) {
340 unsigned char buf[8192];
341 FILE *fpsrc, *fpdst;
342 size_t len;
343 int retval = 0;
344
Lev Walkin4604d032005-03-04 08:52:50 +0000345 if(identical_files(src, dst))
346 return retval; /* Success, no need to copy for real. */
347
348 fpsrc = fopen(src, "r");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000349 if(!fpsrc) { errno = EIO; return -1; }
Lev Walkin4604d032005-03-04 08:52:50 +0000350 fpdst = asn1c_open_file(dst, "", 0);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000351 if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; }
352
353 while(!feof(fpsrc)) {
354 len = fread(buf, 1, sizeof(buf), fpsrc);
355 if(fwrite(buf, 1, len, fpsrc) != len) {
356 errno = EIO;
357 retval = -1;
358 break;
359 }
360 }
361
362 fclose(fpsrc);
363 fclose(fpdst);
364 return retval;
365}
366
Lev Walkinf15320b2004-06-03 03:38:44 +0000367static int
368asn1c_copy_over(arg_t *arg, char *path) {
Lev Walkin79f54952004-08-13 16:58:19 +0000369 char *fname;
Lev Walkinf15320b2004-06-03 03:38:44 +0000370
Lev Walkind9bd7752004-06-05 08:17:50 +0000371 (void)arg; /* Unused argument */
372
Lev Walkin79f54952004-08-13 16:58:19 +0000373 fname = a1c_basename(path);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000374 if(!fname
375#ifdef WIN32
376 || real_copy(path, fname)
377#else
378 || (1 ? symlink(path, fname) : real_copy(path, fname))
379#endif
380 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000381 if(errno == EEXIST) {
382 struct stat sb1, sb2;
383 if(stat(path, &sb1) == 0
384 && stat(fname, &sb2) == 0
385 && sb1.st_dev == sb2.st_dev
386 && sb1.st_ino == sb2.st_ino) {
387 /*
388 * Nothing to do.
389 */
390 fprintf(stderr,
391 "File %s is already here as %s\n",
392 path, fname);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000393 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000394 } else {
395 fprintf(stderr,
396 "Retaining local %s (%s suggested)\n",
397 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000398 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000399 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000400 } else if(errno == ENOENT) {
401 /* Ignore this */
402 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000403 } else {
404 fprintf(stderr, "Symlink %s -> %s failed: %s\n",
405 path, fname, strerror(errno));
406 return -1;
407 }
408 }
409
410 fprintf(stderr, "Symlinked %s\t-> %s\n", path, fname);
411
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000412 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000413}
414