blob: 00a4496edf7b5716462c15508b662452803df460 [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;
79 int i = strlen(datadir);
80
81 assert(i < (int)(sizeof(buf) / 2 - 2));
82 memcpy(buf, datadir, i);
83 dir_end = buf + i;
84 *dir_end++ = '/';
85
86 for(i = 0; i < dlist->el_count; i++) {
87 char *fname = dlist->elements[i]->filename;
Lev Walkind29bbce2004-09-23 22:19:14 +000088 char *dotH;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000089
90 assert(strlen(fname) < (sizeof(buf) / 2));
91 strcpy(dir_end, fname);
92
93 if(asn1c_copy_over(arg, buf) == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +000094 fprintf(mkf, ">>>ABORTED<<<");
95 fclose(mkf);
Lev Walkinf15320b2004-06-03 03:38:44 +000096 return -1;
Lev Walkind29bbce2004-09-23 22:19:14 +000097 }
98 dotH = strrchr(fname, 'h');
99 if(dotH && fname<dotH && dotH[-1] == '.' && !dotH[1]) {
100 fprintf(mkf, "ASN_MODULE_HEADERS+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000101 } else {
Lev Walkind29bbce2004-09-23 22:19:14 +0000102 fprintf(mkf, "ASN_MODULE_SOURCES+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000103 }
104 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000105 }
106
Lev Walkind29bbce2004-09-23 22:19:14 +0000107 fprintf(mkf, "\n\n"
108 "lib_LTLIBRARIES=libsomething.la\n"
109 "libsomething_la_SOURCES="
110 "$(ASN_MODULE_SOURCES) $(ASN_MODULE_HEADERS)\n"
111 "\n"
112 "# This file may be used as an input for make(3)\n"
113 "# Remove the lines below to convert it into a pure .am file\n"
114 "TARGET = progname\n"
115 "CFLAGS += -I.\n"
116 "OBJS=${ASN_MODULE_SOURCES:.c=.o} $(TARGET).o\n"
117 "\nall: $(TARGET)\n"
118 "\n$(TARGET): ${OBJS}"
119 "\n\t$(CC) $(CFLAGS) -o $(TARGET) ${OBJS} $(LDFLAGS) $(LIBS)\n"
120 "\n.SUFFIXES:"
121 "\n.SUFFIXES: .c .o\n"
122 "\n.c.o:"
123 "\n\t$(CC) $(CFLAGS) -o $@ -c $<\n"
124 "\nclean:"
125 "\n\trm -f $(TARGET)"
126 "\n\trm -f $(OBJS)\n"
127 "\n"
128 );
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000129 fclose(mkf);
130 fprintf(stderr, "Generated Makefile.am.sample\n");
131
Lev Walkinf15320b2004-06-03 03:38:44 +0000132 return 0;
133}
134
135/*
136 * Dump the streams.
137 */
138static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000139asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000140 if(arg->flags & A1C_PRINT_COMPILED) {
141 return asn1c_print_streams(arg);
142 } else {
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000143 return asn1c_save_streams(arg, deps);
Lev Walkinf15320b2004-06-03 03:38:44 +0000144 }
145}
146
147static int
148asn1c_print_streams(arg_t *arg) {
149 compiler_streams_t *cs = arg->expr->data;
150 asn1p_expr_t *expr = arg->expr;
151 int i;
152
Lev Walkin64399722004-08-11 07:17:22 +0000153 for(i = 1; i < OT_MAX; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 out_chunk_t *ot;
Lev Walkin59004fa2004-08-20 13:37:01 +0000155 if(TQ_FIRST(&cs->destination[i].chunks) == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +0000156 continue;
157
158 printf("\n/*** <<< %s [%s] >>> ***/\n\n",
159 _compiler_stream2str[i],
160 expr->Identifier);
161
Lev Walkin59004fa2004-08-20 13:37:01 +0000162 TQ_FOR(ot, &(cs->destination[i].chunks), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000163 fwrite(ot->buf, ot->len, 1, stdout);
164 }
165 }
166
167 return 0;
168}
169
170static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000171asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000172 asn1p_expr_t *expr = arg->expr;
173 compiler_streams_t *cs = expr->data;
174 out_chunk_t *ot;
175 FILE *fp_c, *fp_h;
176 char *header_id;
177
178 if(cs == NULL) {
179 fprintf(stderr, "Cannot compile %s at line %d\n",
180 expr->Identifier, expr->_lineno);
181 return -1;
182 }
183
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000184 fp_c = asn1c_open_file(expr->Identifier, ".c");
185 fp_h = asn1c_open_file(expr->Identifier, ".h");
Lev Walkinf15320b2004-06-03 03:38:44 +0000186 if(fp_c == NULL || fp_h == NULL) {
187 if(fp_c) fclose(fp_c); /* lacks unlink() */
188 if(fp_h) fclose(fp_h); /* lacks unlink() */
189 return -1;
190 }
191
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000192 fprintf(fp_c,
193 "/*\n"
194 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000195 " * From ASN.1 module \"%s\"\n"
196 " * \tfound in \"%s\"\n"
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000197 " */\n\n",
198 arg->mod->Identifier,
199 arg->mod->source_file_name
200 );
201 fprintf(fp_h,
202 "/*\n"
203 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000204 " * From ASN.1 module \"%s\"\n"
205 " * \tfound in \"%s\"\n"
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000206 " */\n\n",
207 arg->mod->Identifier,
208 arg->mod->source_file_name
209 );
210
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000211 header_id = asn1c_make_identifier(0, expr->Identifier, NULL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000212 fprintf(fp_h,
213 "#ifndef\t_%s_H_\n"
214 "#define\t_%s_H_\n"
215 "\n", header_id, header_id);
216
Lev Walkin43634792004-08-10 01:14:29 +0000217 fprintf(fp_h, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
218
Lev Walkinf15320b2004-06-03 03:38:44 +0000219 fprintf(fp_h, "#include <constr_TYPE.h>\n\n");
220
Lev Walkin59004fa2004-08-20 13:37:01 +0000221 TQ_FOR(ot, &(cs->destination[OT_INCLUDES].chunks), next) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000222 asn1c_activate_dependency(deps, 0, ot->buf);
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000223 fwrite(ot->buf, ot->len, 1, fp_h);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000224 }
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000225 fprintf(fp_h, "\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000226 TQ_FOR(ot, &(cs->destination[OT_DEPS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000227 fwrite(ot->buf, ot->len, 1, fp_h);
228 fprintf(fp_h, "\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000229 TQ_FOR(ot, &(cs->destination[OT_TYPE_DECLS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000230 fwrite(ot->buf, ot->len, 1, fp_h);
231 fprintf(fp_h, "\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000232 TQ_FOR(ot, &(cs->destination[OT_FUNC_DECLS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000233 fwrite(ot->buf, ot->len, 1, fp_h);
234
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000235 fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n\n"
236 "#endif\t/* _%s_H_ */\n",
237 header_id);
238
Lev Walkin0ffabe22004-09-22 16:02:03 +0000239 fprintf(fp_c, "#include <asn_internal.h>\n\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000240 fprintf(fp_c, "#include <%s.h>\n\n", expr->Identifier); /* Myself */
Lev Walkin59004fa2004-08-20 13:37:01 +0000241 TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000242 fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000243 TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
244 fwrite(ot->buf, ot->len, 1, fp_c);
245 TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000246 fwrite(ot->buf, ot->len, 1, fp_c);
247
Lev Walkin59004fa2004-08-20 13:37:01 +0000248 assert(OT_MAX == 8);
Lev Walkinf15320b2004-06-03 03:38:44 +0000249
250 fclose(fp_c);
251 fclose(fp_h);
252 fprintf(stderr, "Compiled %s.c\n", expr->Identifier);
253 fprintf(stderr, "Compiled %s.h\n", expr->Identifier);
254 return 0;
255}
256
257static int
258asn1c_copy_over(arg_t *arg, char *path) {
Lev Walkin79f54952004-08-13 16:58:19 +0000259 char *fname;
Lev Walkinf15320b2004-06-03 03:38:44 +0000260
Lev Walkind9bd7752004-06-05 08:17:50 +0000261 (void)arg; /* Unused argument */
262
Lev Walkin79f54952004-08-13 16:58:19 +0000263 fname = a1c_basename(path);
264 if(!fname || symlink(path, fname)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000265 if(errno == EEXIST) {
266 struct stat sb1, sb2;
267 if(stat(path, &sb1) == 0
268 && stat(fname, &sb2) == 0
269 && sb1.st_dev == sb2.st_dev
270 && sb1.st_ino == sb2.st_ino) {
271 /*
272 * Nothing to do.
273 */
274 fprintf(stderr,
275 "File %s is already here as %s\n",
276 path, fname);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000277 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000278 } else {
279 fprintf(stderr,
280 "Retaining local %s (%s suggested)\n",
281 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000282 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000283 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000284 } else if(errno == ENOENT) {
285 /* Ignore this */
286 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000287 } else {
288 fprintf(stderr, "Symlink %s -> %s failed: %s\n",
289 path, fname, strerror(errno));
290 return -1;
291 }
292 }
293
294 fprintf(stderr, "Symlinked %s\t-> %s\n", path, fname);
295
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000296 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000297}
298