blob: 2db1c2ef82711c4e3608a336383e2462383a2a08 [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) {
55 fprintf(mkf, "\t\\\n\t%s.c %s.h",
56 arg->expr->Identifier,
57 arg->expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +000058 }
59 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +000060 }
Lev Walkinf15320b2004-06-03 03:38:44 +000061
Lev Walkinacd9f8b2004-08-19 13:29:03 +000062 /*
63 * Move necessary skeleton files and add them to Makefile.am.sample.
64 */
65 dlist = asn1c_deps_makelist(deps);
66 if(dlist) {
67 char buf[8129];
68 char *dir_end;
69 int i = strlen(datadir);
70
71 assert(i < (int)(sizeof(buf) / 2 - 2));
72 memcpy(buf, datadir, i);
73 dir_end = buf + i;
74 *dir_end++ = '/';
75
76 for(i = 0; i < dlist->el_count; i++) {
77 char *fname = dlist->elements[i]->filename;
78
79 assert(strlen(fname) < (sizeof(buf) / 2));
80 strcpy(dir_end, fname);
81
82 if(asn1c_copy_over(arg, buf) == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +000083 fprintf(mkf, ">>>ABORTED<<<");
84 fclose(mkf);
Lev Walkinf15320b2004-06-03 03:38:44 +000085 return -1;
86 } else {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000087 fprintf(mkf, "\t\\\n\t%s", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +000088 }
89 }
Lev Walkinf15320b2004-06-03 03:38:44 +000090 }
91
Lev Walkinacd9f8b2004-08-19 13:29:03 +000092 fprintf(mkf, "\n\n");
93 fprintf(mkf, "lib_LTLIBRARIES=libsomething.la\n");
94 fprintf(mkf, "libsomething_la_SOURCES=$(ASN_MODULE_SOURCES)\n");
95 fclose(mkf);
96 fprintf(stderr, "Generated Makefile.am.sample\n");
97
Lev Walkinf15320b2004-06-03 03:38:44 +000098 return 0;
99}
100
101/*
102 * Dump the streams.
103 */
104static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000105asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000106 if(arg->flags & A1C_PRINT_COMPILED) {
107 return asn1c_print_streams(arg);
108 } else {
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000109 return asn1c_save_streams(arg, deps);
Lev Walkinf15320b2004-06-03 03:38:44 +0000110 }
111}
112
113static int
114asn1c_print_streams(arg_t *arg) {
115 compiler_streams_t *cs = arg->expr->data;
116 asn1p_expr_t *expr = arg->expr;
117 int i;
118
Lev Walkin64399722004-08-11 07:17:22 +0000119 for(i = 1; i < OT_MAX; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000120 out_chunk_t *ot;
Lev Walkin59004fa2004-08-20 13:37:01 +0000121 if(TQ_FIRST(&cs->destination[i].chunks) == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +0000122 continue;
123
124 printf("\n/*** <<< %s [%s] >>> ***/\n\n",
125 _compiler_stream2str[i],
126 expr->Identifier);
127
Lev Walkin59004fa2004-08-20 13:37:01 +0000128 TQ_FOR(ot, &(cs->destination[i].chunks), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000129 fwrite(ot->buf, ot->len, 1, stdout);
130 }
131 }
132
133 return 0;
134}
135
136static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000137asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000138 asn1p_expr_t *expr = arg->expr;
139 compiler_streams_t *cs = expr->data;
140 out_chunk_t *ot;
141 FILE *fp_c, *fp_h;
142 char *header_id;
143
144 if(cs == NULL) {
145 fprintf(stderr, "Cannot compile %s at line %d\n",
146 expr->Identifier, expr->_lineno);
147 return -1;
148 }
149
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000150 fp_c = asn1c_open_file(expr->Identifier, ".c");
151 fp_h = asn1c_open_file(expr->Identifier, ".h");
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 if(fp_c == NULL || fp_h == NULL) {
153 if(fp_c) fclose(fp_c); /* lacks unlink() */
154 if(fp_h) fclose(fp_h); /* lacks unlink() */
155 return -1;
156 }
157
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000158 fprintf(fp_c,
159 "/*\n"
160 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
161 " * From ASN.1 module \"%s\" found in \"%s\"\n"
162 " */\n\n",
163 arg->mod->Identifier,
164 arg->mod->source_file_name
165 );
166 fprintf(fp_h,
167 "/*\n"
168 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
169 " * From ASN.1 module \"%s\" found in \"%s\"\n"
170 " */\n\n",
171 arg->mod->Identifier,
172 arg->mod->source_file_name
173 );
174
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000175 header_id = asn1c_make_identifier(0, expr->Identifier, NULL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000176 fprintf(fp_h,
177 "#ifndef\t_%s_H_\n"
178 "#define\t_%s_H_\n"
179 "\n", header_id, header_id);
180
Lev Walkin43634792004-08-10 01:14:29 +0000181 fprintf(fp_h, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
182
Lev Walkinf15320b2004-06-03 03:38:44 +0000183 fprintf(fp_h, "#include <constr_TYPE.h>\n\n");
184
Lev Walkin59004fa2004-08-20 13:37:01 +0000185 TQ_FOR(ot, &(cs->destination[OT_INCLUDES].chunks), next) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000186 asn1c_activate_dependency(deps, 0, ot->buf);
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000187 fwrite(ot->buf, ot->len, 1, fp_h);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000188 }
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000189 fprintf(fp_h, "\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000190 TQ_FOR(ot, &(cs->destination[OT_DEPS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 fwrite(ot->buf, ot->len, 1, fp_h);
192 fprintf(fp_h, "\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000193 TQ_FOR(ot, &(cs->destination[OT_TYPE_DECLS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 fwrite(ot->buf, ot->len, 1, fp_h);
195 fprintf(fp_h, "\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000196 TQ_FOR(ot, &(cs->destination[OT_FUNC_DECLS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000197 fwrite(ot->buf, ot->len, 1, fp_h);
198
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000199 fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n\n"
200 "#endif\t/* _%s_H_ */\n",
201 header_id);
202
Lev Walkin0ffabe22004-09-22 16:02:03 +0000203 fprintf(fp_c, "#include <asn_internal.h>\n\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000204 fprintf(fp_c, "#include <%s.h>\n\n", expr->Identifier); /* Myself */
Lev Walkin59004fa2004-08-20 13:37:01 +0000205 TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000206 fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000207 TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
208 fwrite(ot->buf, ot->len, 1, fp_c);
209 TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000210 fwrite(ot->buf, ot->len, 1, fp_c);
211
Lev Walkin59004fa2004-08-20 13:37:01 +0000212 assert(OT_MAX == 8);
Lev Walkinf15320b2004-06-03 03:38:44 +0000213
214 fclose(fp_c);
215 fclose(fp_h);
216 fprintf(stderr, "Compiled %s.c\n", expr->Identifier);
217 fprintf(stderr, "Compiled %s.h\n", expr->Identifier);
218 return 0;
219}
220
221static int
222asn1c_copy_over(arg_t *arg, char *path) {
Lev Walkin79f54952004-08-13 16:58:19 +0000223 char *fname;
Lev Walkinf15320b2004-06-03 03:38:44 +0000224
Lev Walkind9bd7752004-06-05 08:17:50 +0000225 (void)arg; /* Unused argument */
226
Lev Walkin79f54952004-08-13 16:58:19 +0000227 fname = a1c_basename(path);
228 if(!fname || symlink(path, fname)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000229 if(errno == EEXIST) {
230 struct stat sb1, sb2;
231 if(stat(path, &sb1) == 0
232 && stat(fname, &sb2) == 0
233 && sb1.st_dev == sb2.st_dev
234 && sb1.st_ino == sb2.st_ino) {
235 /*
236 * Nothing to do.
237 */
238 fprintf(stderr,
239 "File %s is already here as %s\n",
240 path, fname);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000241 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000242 } else {
243 fprintf(stderr,
244 "Retaining local %s (%s suggested)\n",
245 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000246 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000247 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000248 } else if(errno == ENOENT) {
249 /* Ignore this */
250 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000251 } else {
252 fprintf(stderr, "Symlink %s -> %s failed: %s\n",
253 path, fname, strerror(errno));
254 return -1;
255 }
256 }
257
258 fprintf(stderr, "Symlinked %s\t-> %s\n", path, fname);
259
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000260 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000261}
262