blob: 444d2c744148dab0e4e3ce2eca5237d8a01b7d21 [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 Walkin43634792004-08-10 01:14:29 +0000230 fprintf(fp_h, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
231
Lev Walkinc8285712005-03-04 22:18:20 +0000232 fprintf(fp_h, "#include <asn_application.h>\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000233
Lev Walkinb9b8b952005-03-05 00:33:27 +0000234#define SAVE_STREAM(fp, idx, msg, actdep) do { \
Lev Walkin866cff12005-03-05 00:50:53 +0000235 if(TQ_FIRST(&(cs->destination[idx].chunks)) && *msg) \
Lev Walkinb9b8b952005-03-05 00:33:27 +0000236 fprintf(fp, "\n/* %s */\n", msg); \
Lev Walkinc8285712005-03-04 22:18:20 +0000237 TQ_FOR(ot, &(cs->destination[idx].chunks), next) { \
238 if(actdep) asn1c_activate_dependency(deps, 0, ot->buf); \
Lev Walkinb9b8b952005-03-05 00:33:27 +0000239 fwrite(ot->buf, ot->len, 1, fp); \
Lev Walkinc8285712005-03-04 22:18:20 +0000240 } \
241} while(0)
242
Lev Walkinb9b8b952005-03-05 00:33:27 +0000243 SAVE_STREAM(fp_h, OT_INCLUDES, "Including external dependencies", 1);
244 SAVE_STREAM(fp_h, OT_DEPS, "Dependencies", 0);
245 SAVE_STREAM(fp_h, OT_FWD_DECLS, "Forward declarations", 0);
246 SAVE_STREAM(fp_h, OT_TYPE_DECLS, expr->Identifier, 0);
247 SAVE_STREAM(fp_h, OT_FUNC_DECLS,"Implementation", 0);
248 if(!(arg->flags & A1C_NO_INCLUDE_DEPS))
249 SAVE_STREAM(fp_h, OT_POST_INCLUDE, "Referred external types", 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000250
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000251 fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n\n"
252 "#endif\t/* _%s_H_ */\n",
253 header_id);
254
Lev Walkin0ffabe22004-09-22 16:02:03 +0000255 fprintf(fp_c, "#include <asn_internal.h>\n\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000256 fprintf(fp_c, "#include <%s.h>\n\n", expr->Identifier); /* Myself */
Lev Walkinb9b8b952005-03-05 00:33:27 +0000257 if(arg->flags & A1C_NO_INCLUDE_DEPS)
Lev Walkin866cff12005-03-05 00:50:53 +0000258 SAVE_STREAM(fp_c, OT_POST_INCLUDE, "", 1);
Lev Walkin59004fa2004-08-20 13:37:01 +0000259 TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000260 fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000261 TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
262 fwrite(ot->buf, ot->len, 1, fp_c);
263 TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000264 fwrite(ot->buf, ot->len, 1, fp_c);
265
Lev Walkinc8285712005-03-04 22:18:20 +0000266 assert(OT_MAX == 10); /* Protection from reckless changes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000267
268 fclose(fp_c);
269 fclose(fp_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000270
271 name_buf = alloca(strlen(expr->Identifier) + 3);
272
273 sprintf(name_buf, "%s.c", expr->Identifier);
274 if(identical_files(name_buf, tmpname_c)) {
275 c_retained = " (contents unchanged)";
276 unlink(tmpname_c);
277 } else {
278 if(rename(tmpname_c, name_buf)) {
279 unlink(tmpname_c);
280 perror(tmpname_c);
281 return -1;
282 }
283 }
284
285 sprintf(name_buf, "%s.h", expr->Identifier);
286 if(identical_files(name_buf, tmpname_h)) {
287 h_retained = " (contents unchanged)";
288 unlink(tmpname_h);
289 } else {
290 if(rename(tmpname_h, name_buf)) {
291 unlink(tmpname_h);
292 perror(tmpname_h);
293 return -1;
294 }
295 }
296
297 free(tmpname_c);
298 free(tmpname_h);
299
300 fprintf(stderr, "Compiled %s.c%s\n",
301 expr->Identifier, c_retained);
302 fprintf(stderr, "Compiled %s.h%s\n",
303 expr->Identifier, h_retained);
Lev Walkinf15320b2004-06-03 03:38:44 +0000304 return 0;
305}
306
Lev Walkin4604d032005-03-04 08:52:50 +0000307static int
308identical_files(const char *fname1, const char *fname2) {
309 char buf[2][8192];
310 FILE *fp1, *fp2;
311 size_t olen, nlen;
312 int retval = 1; /* Files are identical */
313
314 fp1 = fopen(fname1, "r");
315 if(!fp1) { return 0; }
316 fp2 = fopen(fname2, "r");
317 if(!fp2) { fclose(fp1); return 0; }
318
319 while((olen = fread(buf[0], 1, sizeof(buf[0]), fp1))) {
320 nlen = fread(buf[1], 1, olen, fp2);
321 if(nlen != olen || memcmp(buf[0], buf[1], nlen)) {
322 retval = 0;
323 break;
324 }
325 }
326 nlen = fread(buf[1], 1, 1, fp2);
327 if(nlen) retval = 0;
328
329 fclose(fp1);
330 fclose(fp2);
331 return retval;
332}
333
Lev Walkin4efbfb72005-02-25 14:20:30 +0000334/*
335 * Copy file for real.
336 */
337static int
338real_copy(const char *src, const char *dst) {
339 unsigned char buf[8192];
340 FILE *fpsrc, *fpdst;
341 size_t len;
342 int retval = 0;
343
Lev Walkin4604d032005-03-04 08:52:50 +0000344 if(identical_files(src, dst))
345 return retval; /* Success, no need to copy for real. */
346
347 fpsrc = fopen(src, "r");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000348 if(!fpsrc) { errno = EIO; return -1; }
Lev Walkin4604d032005-03-04 08:52:50 +0000349 fpdst = asn1c_open_file(dst, "", 0);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000350 if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; }
351
352 while(!feof(fpsrc)) {
353 len = fread(buf, 1, sizeof(buf), fpsrc);
354 if(fwrite(buf, 1, len, fpsrc) != len) {
355 errno = EIO;
356 retval = -1;
357 break;
358 }
359 }
360
361 fclose(fpsrc);
362 fclose(fpdst);
363 return retval;
364}
365
Lev Walkinf15320b2004-06-03 03:38:44 +0000366static int
367asn1c_copy_over(arg_t *arg, char *path) {
Lev Walkin79f54952004-08-13 16:58:19 +0000368 char *fname;
Lev Walkinf15320b2004-06-03 03:38:44 +0000369
Lev Walkind9bd7752004-06-05 08:17:50 +0000370 (void)arg; /* Unused argument */
371
Lev Walkin79f54952004-08-13 16:58:19 +0000372 fname = a1c_basename(path);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000373 if(!fname
374#ifdef WIN32
375 || real_copy(path, fname)
376#else
377 || (1 ? symlink(path, fname) : real_copy(path, fname))
378#endif
379 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000380 if(errno == EEXIST) {
381 struct stat sb1, sb2;
382 if(stat(path, &sb1) == 0
383 && stat(fname, &sb2) == 0
384 && sb1.st_dev == sb2.st_dev
385 && sb1.st_ino == sb2.st_ino) {
386 /*
387 * Nothing to do.
388 */
389 fprintf(stderr,
390 "File %s is already here as %s\n",
391 path, fname);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000392 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000393 } else {
394 fprintf(stderr,
395 "Retaining local %s (%s suggested)\n",
396 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000397 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000398 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000399 } else if(errno == ENOENT) {
400 /* Ignore this */
401 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000402 } else {
403 fprintf(stderr, "Symlink %s -> %s failed: %s\n",
404 path, fname, strerror(errno));
405 return -1;
406 }
407 }
408
409 fprintf(stderr, "Symlinked %s\t-> %s\n", path, fname);
410
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000411 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000412}
413