blob: 1abf242770c76fc95d675de8bc703c94cd7ce49c [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);
13
14int
15asn1c_save_compiled_output(arg_t *arg, const char *datadir) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000016 asn1c_fdeps_t *deps = 0;
17 FILE *mkf;
18 asn1c_fdeps_t *dlist;
Lev Walkinf15320b2004-06-03 03:38:44 +000019
Lev Walkinacd9f8b2004-08-19 13:29:03 +000020 deps = asn1c_read_file_dependencies(arg, datadir);
21 if(!deps && datadir) {
22 WARNING("Cannot read file-dependencies information "
23 "from %s\n", datadir);
24 }
Lev Walkinf15320b2004-06-03 03:38:44 +000025
26 TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) {
27 TQ_FOR(arg->expr, &(arg->mod->members), next) {
28 if(asn1_lang_map[arg->expr->meta_type]
29 [arg->expr->expr_type].type_cb) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000030 if(asn1c_dump_streams(arg, deps))
Lev Walkinf15320b2004-06-03 03:38:44 +000031 return -1;
32 }
33 }
34 }
35
36 /*
37 * Dump out the Makefile template and the rest of the support code.
38 */
Lev Walkinacd9f8b2004-08-19 13:29:03 +000039 if((arg->flags & A1C_PRINT_COMPILED)
40 || (arg->flags & A1C_OMIT_SUPPORT_CODE)) {
41 return 0; /* Finished */
42 }
Lev Walkinf15320b2004-06-03 03:38:44 +000043
Lev Walkinacd9f8b2004-08-19 13:29:03 +000044 mkf = asn1c_open_file("Makefile.am", ".sample");
45 if(mkf == NULL) {
46 perror("Makefile.am.sample");
47 return -1;
48 }
Lev Walkinf15320b2004-06-03 03:38:44 +000049
Lev Walkinacd9f8b2004-08-19 13:29:03 +000050 fprintf(mkf, "ASN_MODULE_SOURCES=");
51 TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) {
52 TQ_FOR(arg->expr, &(arg->mod->members), next) {
53 if(asn1_lang_map[arg->expr->meta_type]
54 [arg->expr->expr_type].type_cb) {
Lev Walkind29bbce2004-09-23 22:19:14 +000055 fprintf(mkf, "\t\\\n\t%s.c",
Lev Walkinacd9f8b2004-08-19 13:29:03 +000056 arg->expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +000057 }
58 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +000059 }
Lev Walkind29bbce2004-09-23 22:19:14 +000060 fprintf(mkf, "\n\nASN_MODULE_HEADERS=");
61 TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) {
62 TQ_FOR(arg->expr, &(arg->mod->members), next) {
63 if(asn1_lang_map[arg->expr->meta_type]
64 [arg->expr->expr_type].type_cb) {
65 fprintf(mkf, "\t\\\n\t%s.h",
66 arg->expr->Identifier);
67 }
68 }
69 }
70 fprintf(mkf, "\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +000071
Lev Walkinacd9f8b2004-08-19 13:29:03 +000072 /*
73 * Move necessary skeleton files and add them to Makefile.am.sample.
74 */
75 dlist = asn1c_deps_makelist(deps);
76 if(dlist) {
77 char buf[8129];
78 char *dir_end;
Lev Walkin4efbfb72005-02-25 14:20:30 +000079 size_t dlen = strlen(datadir);
80 int i;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000081
Lev Walkin4efbfb72005-02-25 14:20:30 +000082 assert(dlen < (sizeof(buf) / 2 - 2));
83 memcpy(buf, datadir, dlen);
84 dir_end = buf + dlen;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000085 *dir_end++ = '/';
86
87 for(i = 0; i < dlist->el_count; i++) {
88 char *fname = dlist->elements[i]->filename;
Lev Walkind29bbce2004-09-23 22:19:14 +000089 char *dotH;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000090
91 assert(strlen(fname) < (sizeof(buf) / 2));
92 strcpy(dir_end, fname);
93
94 if(asn1c_copy_over(arg, buf) == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +000095 fprintf(mkf, ">>>ABORTED<<<");
96 fclose(mkf);
Lev Walkinf15320b2004-06-03 03:38:44 +000097 return -1;
Lev Walkind29bbce2004-09-23 22:19:14 +000098 }
99 dotH = strrchr(fname, 'h');
100 if(dotH && fname<dotH && dotH[-1] == '.' && !dotH[1]) {
101 fprintf(mkf, "ASN_MODULE_HEADERS+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000102 } else {
Lev Walkind29bbce2004-09-23 22:19:14 +0000103 fprintf(mkf, "ASN_MODULE_SOURCES+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000104 }
105 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000106 }
107
Lev Walkind29bbce2004-09-23 22:19:14 +0000108 fprintf(mkf, "\n\n"
109 "lib_LTLIBRARIES=libsomething.la\n"
110 "libsomething_la_SOURCES="
111 "$(ASN_MODULE_SOURCES) $(ASN_MODULE_HEADERS)\n"
112 "\n"
113 "# This file may be used as an input for make(3)\n"
114 "# Remove the lines below to convert it into a pure .am file\n"
115 "TARGET = progname\n"
116 "CFLAGS += -I.\n"
117 "OBJS=${ASN_MODULE_SOURCES:.c=.o} $(TARGET).o\n"
118 "\nall: $(TARGET)\n"
119 "\n$(TARGET): ${OBJS}"
120 "\n\t$(CC) $(CFLAGS) -o $(TARGET) ${OBJS} $(LDFLAGS) $(LIBS)\n"
121 "\n.SUFFIXES:"
122 "\n.SUFFIXES: .c .o\n"
123 "\n.c.o:"
124 "\n\t$(CC) $(CFLAGS) -o $@ -c $<\n"
125 "\nclean:"
126 "\n\trm -f $(TARGET)"
127 "\n\trm -f $(OBJS)\n"
128 "\n"
129 );
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000130 fclose(mkf);
131 fprintf(stderr, "Generated Makefile.am.sample\n");
132
Lev Walkinf15320b2004-06-03 03:38:44 +0000133 return 0;
134}
135
136/*
137 * Dump the streams.
138 */
139static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000140asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000141 if(arg->flags & A1C_PRINT_COMPILED) {
142 return asn1c_print_streams(arg);
143 } else {
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000144 return asn1c_save_streams(arg, deps);
Lev Walkinf15320b2004-06-03 03:38:44 +0000145 }
146}
147
148static int
149asn1c_print_streams(arg_t *arg) {
150 compiler_streams_t *cs = arg->expr->data;
151 asn1p_expr_t *expr = arg->expr;
152 int i;
153
Lev Walkin64399722004-08-11 07:17:22 +0000154 for(i = 1; i < OT_MAX; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000155 out_chunk_t *ot;
Lev Walkin59004fa2004-08-20 13:37:01 +0000156 if(TQ_FIRST(&cs->destination[i].chunks) == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +0000157 continue;
158
159 printf("\n/*** <<< %s [%s] >>> ***/\n\n",
160 _compiler_stream2str[i],
161 expr->Identifier);
162
Lev Walkin59004fa2004-08-20 13:37:01 +0000163 TQ_FOR(ot, &(cs->destination[i].chunks), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000164 fwrite(ot->buf, ot->len, 1, stdout);
165 }
166 }
167
168 return 0;
169}
170
171static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000172asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000173 asn1p_expr_t *expr = arg->expr;
174 compiler_streams_t *cs = expr->data;
175 out_chunk_t *ot;
176 FILE *fp_c, *fp_h;
177 char *header_id;
178
179 if(cs == NULL) {
180 fprintf(stderr, "Cannot compile %s at line %d\n",
181 expr->Identifier, expr->_lineno);
182 return -1;
183 }
184
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000185 fp_c = asn1c_open_file(expr->Identifier, ".c");
186 fp_h = asn1c_open_file(expr->Identifier, ".h");
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 if(fp_c == NULL || fp_h == NULL) {
188 if(fp_c) fclose(fp_c); /* lacks unlink() */
189 if(fp_h) fclose(fp_h); /* lacks unlink() */
190 return -1;
191 }
192
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000193 fprintf(fp_c,
194 "/*\n"
195 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000196 " * From ASN.1 module \"%s\"\n"
197 " * \tfound in \"%s\"\n"
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000198 " */\n\n",
199 arg->mod->Identifier,
200 arg->mod->source_file_name
201 );
202 fprintf(fp_h,
203 "/*\n"
204 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000205 " * From ASN.1 module \"%s\"\n"
206 " * \tfound in \"%s\"\n"
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000207 " */\n\n",
208 arg->mod->Identifier,
209 arg->mod->source_file_name
210 );
211
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000212 header_id = asn1c_make_identifier(0, expr->Identifier, NULL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000213 fprintf(fp_h,
214 "#ifndef\t_%s_H_\n"
215 "#define\t_%s_H_\n"
216 "\n", header_id, header_id);
217
Lev Walkin43634792004-08-10 01:14:29 +0000218 fprintf(fp_h, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
219
Lev Walkin11c3e172004-09-24 21:00:50 +0000220 fprintf(fp_h, "#include <asn_application.h>\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000221
Lev Walkin59004fa2004-08-20 13:37:01 +0000222 TQ_FOR(ot, &(cs->destination[OT_INCLUDES].chunks), next) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000223 asn1c_activate_dependency(deps, 0, ot->buf);
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000224 fwrite(ot->buf, ot->len, 1, fp_h);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000225 }
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000226 fprintf(fp_h, "\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000227 TQ_FOR(ot, &(cs->destination[OT_DEPS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000228 fwrite(ot->buf, ot->len, 1, fp_h);
229 fprintf(fp_h, "\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000230 TQ_FOR(ot, &(cs->destination[OT_TYPE_DECLS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000231 fwrite(ot->buf, ot->len, 1, fp_h);
232 fprintf(fp_h, "\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000233 TQ_FOR(ot, &(cs->destination[OT_FUNC_DECLS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000234 fwrite(ot->buf, ot->len, 1, fp_h);
235
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000236 fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n\n"
237 "#endif\t/* _%s_H_ */\n",
238 header_id);
239
Lev Walkin0ffabe22004-09-22 16:02:03 +0000240 fprintf(fp_c, "#include <asn_internal.h>\n\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000241 fprintf(fp_c, "#include <%s.h>\n\n", expr->Identifier); /* Myself */
Lev Walkin59004fa2004-08-20 13:37:01 +0000242 TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000243 fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000244 TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
245 fwrite(ot->buf, ot->len, 1, fp_c);
246 TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000247 fwrite(ot->buf, ot->len, 1, fp_c);
248
Lev Walkin59004fa2004-08-20 13:37:01 +0000249 assert(OT_MAX == 8);
Lev Walkinf15320b2004-06-03 03:38:44 +0000250
251 fclose(fp_c);
252 fclose(fp_h);
253 fprintf(stderr, "Compiled %s.c\n", expr->Identifier);
254 fprintf(stderr, "Compiled %s.h\n", expr->Identifier);
255 return 0;
256}
257
Lev Walkin4efbfb72005-02-25 14:20:30 +0000258/*
259 * Copy file for real.
260 */
261static int
262real_copy(const char *src, const char *dst) {
263 unsigned char buf[8192];
264 FILE *fpsrc, *fpdst;
265 size_t len;
266 int retval = 0;
267
268 fpsrc = fopen(src, "rb");
269 if(!fpsrc) { errno = EIO; return -1; }
270 fpdst = fopen(src, "wb");
271 if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; }
272
273 while(!feof(fpsrc)) {
274 len = fread(buf, 1, sizeof(buf), fpsrc);
275 if(fwrite(buf, 1, len, fpsrc) != len) {
276 errno = EIO;
277 retval = -1;
278 break;
279 }
280 }
281
282 fclose(fpsrc);
283 fclose(fpdst);
284 return retval;
285}
286
Lev Walkinf15320b2004-06-03 03:38:44 +0000287static int
288asn1c_copy_over(arg_t *arg, char *path) {
Lev Walkin79f54952004-08-13 16:58:19 +0000289 char *fname;
Lev Walkinf15320b2004-06-03 03:38:44 +0000290
Lev Walkind9bd7752004-06-05 08:17:50 +0000291 (void)arg; /* Unused argument */
292
Lev Walkin79f54952004-08-13 16:58:19 +0000293 fname = a1c_basename(path);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000294 if(!fname
295#ifdef WIN32
296 || real_copy(path, fname)
297#else
298 || (1 ? symlink(path, fname) : real_copy(path, fname))
299#endif
300 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000301 if(errno == EEXIST) {
302 struct stat sb1, sb2;
303 if(stat(path, &sb1) == 0
304 && stat(fname, &sb2) == 0
305 && sb1.st_dev == sb2.st_dev
306 && sb1.st_ino == sb2.st_ino) {
307 /*
308 * Nothing to do.
309 */
310 fprintf(stderr,
311 "File %s is already here as %s\n",
312 path, fname);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000313 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000314 } else {
315 fprintf(stderr,
316 "Retaining local %s (%s suggested)\n",
317 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000318 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000319 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000320 } else if(errno == ENOENT) {
321 /* Ignore this */
322 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000323 } else {
324 fprintf(stderr, "Symlink %s -> %s failed: %s\n",
325 path, fname, strerror(errno));
326 return -1;
327 }
328 }
329
330 fprintf(stderr, "Symlinked %s\t-> %s\n", path, fname);
331
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000332 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000333}
334