blob: 0f3ed044c9f1ee5ef03898f98ba077ea60577745 [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
16asn1c_save_compiled_output(arg_t *arg, const char *datadir) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000017 asn1c_fdeps_t *deps = 0;
18 FILE *mkf;
19 asn1c_fdeps_t *dlist;
Lev Walkinf15320b2004-06-03 03:38:44 +000020
Lev Walkinacd9f8b2004-08-19 13:29:03 +000021 deps = asn1c_read_file_dependencies(arg, datadir);
22 if(!deps && datadir) {
23 WARNING("Cannot read file-dependencies information "
24 "from %s\n", datadir);
25 }
Lev Walkinf15320b2004-06-03 03:38:44 +000026
27 TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) {
28 TQ_FOR(arg->expr, &(arg->mod->members), next) {
29 if(asn1_lang_map[arg->expr->meta_type]
30 [arg->expr->expr_type].type_cb) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000031 if(asn1c_dump_streams(arg, deps))
Lev Walkinf15320b2004-06-03 03:38:44 +000032 return -1;
33 }
34 }
35 }
36
37 /*
38 * Dump out the Makefile template and the rest of the support code.
39 */
Lev Walkinacd9f8b2004-08-19 13:29:03 +000040 if((arg->flags & A1C_PRINT_COMPILED)
41 || (arg->flags & A1C_OMIT_SUPPORT_CODE)) {
42 return 0; /* Finished */
43 }
Lev Walkinf15320b2004-06-03 03:38:44 +000044
Lev Walkin4604d032005-03-04 08:52:50 +000045 mkf = asn1c_open_file("Makefile.am", ".sample", 0);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000046 if(mkf == NULL) {
47 perror("Makefile.am.sample");
48 return -1;
49 }
Lev Walkinf15320b2004-06-03 03:38:44 +000050
Lev Walkinacd9f8b2004-08-19 13:29:03 +000051 fprintf(mkf, "ASN_MODULE_SOURCES=");
52 TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) {
53 TQ_FOR(arg->expr, &(arg->mod->members), next) {
54 if(asn1_lang_map[arg->expr->meta_type]
55 [arg->expr->expr_type].type_cb) {
Lev Walkind29bbce2004-09-23 22:19:14 +000056 fprintf(mkf, "\t\\\n\t%s.c",
Lev Walkinacd9f8b2004-08-19 13:29:03 +000057 arg->expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +000058 }
59 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +000060 }
Lev Walkind29bbce2004-09-23 22:19:14 +000061 fprintf(mkf, "\n\nASN_MODULE_HEADERS=");
62 TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) {
63 TQ_FOR(arg->expr, &(arg->mod->members), next) {
64 if(asn1_lang_map[arg->expr->meta_type]
65 [arg->expr->expr_type].type_cb) {
66 fprintf(mkf, "\t\\\n\t%s.h",
67 arg->expr->Identifier);
68 }
69 }
70 }
71 fprintf(mkf, "\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +000072
Lev Walkinacd9f8b2004-08-19 13:29:03 +000073 /*
74 * Move necessary skeleton files and add them to Makefile.am.sample.
75 */
76 dlist = asn1c_deps_makelist(deps);
77 if(dlist) {
78 char buf[8129];
79 char *dir_end;
Lev Walkin4efbfb72005-02-25 14:20:30 +000080 size_t dlen = strlen(datadir);
81 int i;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000082
Lev Walkin4efbfb72005-02-25 14:20:30 +000083 assert(dlen < (sizeof(buf) / 2 - 2));
84 memcpy(buf, datadir, dlen);
85 dir_end = buf + dlen;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000086 *dir_end++ = '/';
87
88 for(i = 0; i < dlist->el_count; i++) {
89 char *fname = dlist->elements[i]->filename;
Lev Walkind29bbce2004-09-23 22:19:14 +000090 char *dotH;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000091
92 assert(strlen(fname) < (sizeof(buf) / 2));
93 strcpy(dir_end, fname);
94
95 if(asn1c_copy_over(arg, buf) == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +000096 fprintf(mkf, ">>>ABORTED<<<");
97 fclose(mkf);
Lev Walkinf15320b2004-06-03 03:38:44 +000098 return -1;
Lev Walkind29bbce2004-09-23 22:19:14 +000099 }
100 dotH = strrchr(fname, 'h');
101 if(dotH && fname<dotH && dotH[-1] == '.' && !dotH[1]) {
102 fprintf(mkf, "ASN_MODULE_HEADERS+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000103 } else {
Lev Walkind29bbce2004-09-23 22:19:14 +0000104 fprintf(mkf, "ASN_MODULE_SOURCES+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000105 }
106 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000107 }
108
Lev Walkind29bbce2004-09-23 22:19:14 +0000109 fprintf(mkf, "\n\n"
110 "lib_LTLIBRARIES=libsomething.la\n"
111 "libsomething_la_SOURCES="
112 "$(ASN_MODULE_SOURCES) $(ASN_MODULE_HEADERS)\n"
113 "\n"
114 "# This file may be used as an input for make(3)\n"
115 "# Remove the lines below to convert it into a pure .am file\n"
116 "TARGET = progname\n"
117 "CFLAGS += -I.\n"
118 "OBJS=${ASN_MODULE_SOURCES:.c=.o} $(TARGET).o\n"
119 "\nall: $(TARGET)\n"
120 "\n$(TARGET): ${OBJS}"
121 "\n\t$(CC) $(CFLAGS) -o $(TARGET) ${OBJS} $(LDFLAGS) $(LIBS)\n"
122 "\n.SUFFIXES:"
123 "\n.SUFFIXES: .c .o\n"
124 "\n.c.o:"
125 "\n\t$(CC) $(CFLAGS) -o $@ -c $<\n"
126 "\nclean:"
127 "\n\trm -f $(TARGET)"
128 "\n\trm -f $(OBJS)\n"
129 "\n"
130 );
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000131 fclose(mkf);
132 fprintf(stderr, "Generated Makefile.am.sample\n");
133
Lev Walkinf15320b2004-06-03 03:38:44 +0000134 return 0;
135}
136
137/*
138 * Dump the streams.
139 */
140static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000141asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000142 if(arg->flags & A1C_PRINT_COMPILED) {
143 return asn1c_print_streams(arg);
144 } else {
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000145 return asn1c_save_streams(arg, deps);
Lev Walkinf15320b2004-06-03 03:38:44 +0000146 }
147}
148
149static int
150asn1c_print_streams(arg_t *arg) {
151 compiler_streams_t *cs = arg->expr->data;
152 asn1p_expr_t *expr = arg->expr;
153 int i;
154
Lev Walkin64399722004-08-11 07:17:22 +0000155 for(i = 1; i < OT_MAX; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000156 out_chunk_t *ot;
Lev Walkin59004fa2004-08-20 13:37:01 +0000157 if(TQ_FIRST(&cs->destination[i].chunks) == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +0000158 continue;
159
160 printf("\n/*** <<< %s [%s] >>> ***/\n\n",
161 _compiler_stream2str[i],
162 expr->Identifier);
163
Lev Walkin59004fa2004-08-20 13:37:01 +0000164 TQ_FOR(ot, &(cs->destination[i].chunks), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000165 fwrite(ot->buf, ot->len, 1, stdout);
166 }
167 }
168
169 return 0;
170}
171
172static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000173asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000174 asn1p_expr_t *expr = arg->expr;
175 compiler_streams_t *cs = expr->data;
176 out_chunk_t *ot;
177 FILE *fp_c, *fp_h;
Lev Walkin4604d032005-03-04 08:52:50 +0000178 char *tmpname_c, *tmpname_h;
179 char *name_buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 char *header_id;
Lev Walkin4604d032005-03-04 08:52:50 +0000181 const char *c_retained = "";
182 const char *h_retained = "";
Lev Walkinf15320b2004-06-03 03:38:44 +0000183
184 if(cs == NULL) {
185 fprintf(stderr, "Cannot compile %s at line %d\n",
186 expr->Identifier, expr->_lineno);
187 return -1;
188 }
189
Lev Walkin4604d032005-03-04 08:52:50 +0000190 fp_c = asn1c_open_file(expr->Identifier, ".c", &tmpname_c);
191 fp_h = asn1c_open_file(expr->Identifier, ".h", &tmpname_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 if(fp_c == NULL || fp_h == NULL) {
Lev Walkin4604d032005-03-04 08:52:50 +0000193 if(fp_c) { unlink(tmpname_c); free(tmpname_c); fclose(fp_c); }
194 if(fp_h) { unlink(tmpname_h); free(tmpname_h); fclose(fp_h); }
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 return -1;
196 }
197
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000198 fprintf(fp_c,
199 "/*\n"
200 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000201 " * From ASN.1 module \"%s\"\n"
202 " * \tfound in \"%s\"\n"
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000203 " */\n\n",
204 arg->mod->Identifier,
205 arg->mod->source_file_name
206 );
207 fprintf(fp_h,
208 "/*\n"
209 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000210 " * From ASN.1 module \"%s\"\n"
211 " * \tfound in \"%s\"\n"
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000212 " */\n\n",
213 arg->mod->Identifier,
214 arg->mod->source_file_name
215 );
216
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000217 header_id = asn1c_make_identifier(0, expr->Identifier, NULL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000218 fprintf(fp_h,
219 "#ifndef\t_%s_H_\n"
220 "#define\t_%s_H_\n"
221 "\n", header_id, header_id);
222
Lev Walkin43634792004-08-10 01:14:29 +0000223 fprintf(fp_h, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
224
Lev Walkinc8285712005-03-04 22:18:20 +0000225 fprintf(fp_h, "#include <asn_application.h>\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000226
Lev Walkinc8285712005-03-04 22:18:20 +0000227#define SAVE_STREAM(idx, msg, actdep) do { \
228 if(TQ_FIRST(&(cs->destination[idx].chunks))) \
229 fprintf(fp_h, "\n/* %s */\n", msg); \
230 TQ_FOR(ot, &(cs->destination[idx].chunks), next) { \
231 if(actdep) asn1c_activate_dependency(deps, 0, ot->buf); \
232 fwrite(ot->buf, ot->len, 1, fp_h); \
233 } \
234} while(0)
235
236 SAVE_STREAM(OT_INCLUDES, "Including external dependencies", 1);
237 SAVE_STREAM(OT_DEPS, "Dependencies", 0);
238 SAVE_STREAM(OT_FWD_DECLS, "Forward declarations", 0);
239 SAVE_STREAM(OT_TYPE_DECLS, expr->Identifier, 0);
240 SAVE_STREAM(OT_FUNC_DECLS, "Implementation", 0);
241 SAVE_STREAM(OT_POST_INCLUDE, "Referred external types", 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000242
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000243 fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n\n"
244 "#endif\t/* _%s_H_ */\n",
245 header_id);
246
Lev Walkin0ffabe22004-09-22 16:02:03 +0000247 fprintf(fp_c, "#include <asn_internal.h>\n\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000248 fprintf(fp_c, "#include <%s.h>\n\n", expr->Identifier); /* Myself */
Lev Walkin59004fa2004-08-20 13:37:01 +0000249 TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000250 fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000251 TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
252 fwrite(ot->buf, ot->len, 1, fp_c);
253 TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000254 fwrite(ot->buf, ot->len, 1, fp_c);
255
Lev Walkinc8285712005-03-04 22:18:20 +0000256 assert(OT_MAX == 10); /* Protection from reckless changes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000257
258 fclose(fp_c);
259 fclose(fp_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000260
261 name_buf = alloca(strlen(expr->Identifier) + 3);
262
263 sprintf(name_buf, "%s.c", expr->Identifier);
264 if(identical_files(name_buf, tmpname_c)) {
265 c_retained = " (contents unchanged)";
266 unlink(tmpname_c);
267 } else {
268 if(rename(tmpname_c, name_buf)) {
269 unlink(tmpname_c);
270 perror(tmpname_c);
271 return -1;
272 }
273 }
274
275 sprintf(name_buf, "%s.h", expr->Identifier);
276 if(identical_files(name_buf, tmpname_h)) {
277 h_retained = " (contents unchanged)";
278 unlink(tmpname_h);
279 } else {
280 if(rename(tmpname_h, name_buf)) {
281 unlink(tmpname_h);
282 perror(tmpname_h);
283 return -1;
284 }
285 }
286
287 free(tmpname_c);
288 free(tmpname_h);
289
290 fprintf(stderr, "Compiled %s.c%s\n",
291 expr->Identifier, c_retained);
292 fprintf(stderr, "Compiled %s.h%s\n",
293 expr->Identifier, h_retained);
Lev Walkinf15320b2004-06-03 03:38:44 +0000294 return 0;
295}
296
Lev Walkin4604d032005-03-04 08:52:50 +0000297static int
298identical_files(const char *fname1, const char *fname2) {
299 char buf[2][8192];
300 FILE *fp1, *fp2;
301 size_t olen, nlen;
302 int retval = 1; /* Files are identical */
303
304 fp1 = fopen(fname1, "r");
305 if(!fp1) { return 0; }
306 fp2 = fopen(fname2, "r");
307 if(!fp2) { fclose(fp1); return 0; }
308
309 while((olen = fread(buf[0], 1, sizeof(buf[0]), fp1))) {
310 nlen = fread(buf[1], 1, olen, fp2);
311 if(nlen != olen || memcmp(buf[0], buf[1], nlen)) {
312 retval = 0;
313 break;
314 }
315 }
316 nlen = fread(buf[1], 1, 1, fp2);
317 if(nlen) retval = 0;
318
319 fclose(fp1);
320 fclose(fp2);
321 return retval;
322}
323
Lev Walkin4efbfb72005-02-25 14:20:30 +0000324/*
325 * Copy file for real.
326 */
327static int
328real_copy(const char *src, const char *dst) {
329 unsigned char buf[8192];
330 FILE *fpsrc, *fpdst;
331 size_t len;
332 int retval = 0;
333
Lev Walkin4604d032005-03-04 08:52:50 +0000334 if(identical_files(src, dst))
335 return retval; /* Success, no need to copy for real. */
336
337 fpsrc = fopen(src, "r");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000338 if(!fpsrc) { errno = EIO; return -1; }
Lev Walkin4604d032005-03-04 08:52:50 +0000339 fpdst = asn1c_open_file(dst, "", 0);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000340 if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; }
341
342 while(!feof(fpsrc)) {
343 len = fread(buf, 1, sizeof(buf), fpsrc);
344 if(fwrite(buf, 1, len, fpsrc) != len) {
345 errno = EIO;
346 retval = -1;
347 break;
348 }
349 }
350
351 fclose(fpsrc);
352 fclose(fpdst);
353 return retval;
354}
355
Lev Walkinf15320b2004-06-03 03:38:44 +0000356static int
357asn1c_copy_over(arg_t *arg, char *path) {
Lev Walkin79f54952004-08-13 16:58:19 +0000358 char *fname;
Lev Walkinf15320b2004-06-03 03:38:44 +0000359
Lev Walkind9bd7752004-06-05 08:17:50 +0000360 (void)arg; /* Unused argument */
361
Lev Walkin79f54952004-08-13 16:58:19 +0000362 fname = a1c_basename(path);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000363 if(!fname
364#ifdef WIN32
365 || real_copy(path, fname)
366#else
367 || (1 ? symlink(path, fname) : real_copy(path, fname))
368#endif
369 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000370 if(errno == EEXIST) {
371 struct stat sb1, sb2;
372 if(stat(path, &sb1) == 0
373 && stat(fname, &sb2) == 0
374 && sb1.st_dev == sb2.st_dev
375 && sb1.st_ino == sb2.st_ino) {
376 /*
377 * Nothing to do.
378 */
379 fprintf(stderr,
380 "File %s is already here as %s\n",
381 path, fname);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000382 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000383 } else {
384 fprintf(stderr,
385 "Retaining local %s (%s suggested)\n",
386 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000387 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000388 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000389 } else if(errno == ENOENT) {
390 /* Ignore this */
391 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000392 } else {
393 fprintf(stderr, "Symlink %s -> %s failed: %s\n",
394 path, fname, strerror(errno));
395 return -1;
396 }
397 }
398
399 fprintf(stderr, "Symlinked %s\t-> %s\n", path, fname);
400
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000401 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000402}
403