blob: 3c630538de33666a508f525ad0bf753063ce1771 [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 Walkinf15320b2004-06-03 03:38:44 +00004
Lev Walkinacd9f8b2004-08-19 13:29:03 +00005static int asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *);
Lev Walkinf15320b2004-06-03 03:38:44 +00006static int asn1c_print_streams(arg_t *arg);
Lev Walkinacd9f8b2004-08-19 13:29:03 +00007static int asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *);
Lev Walkinf15320b2004-06-03 03:38:44 +00008static int asn1c_copy_over(arg_t *arg, char *path);
9
10int
11asn1c_save_compiled_output(arg_t *arg, const char *datadir) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000012 asn1c_fdeps_t *deps = 0;
13 FILE *mkf;
14 asn1c_fdeps_t *dlist;
Lev Walkinf15320b2004-06-03 03:38:44 +000015
Lev Walkinacd9f8b2004-08-19 13:29:03 +000016 deps = asn1c_read_file_dependencies(arg, datadir);
17 if(!deps && datadir) {
18 WARNING("Cannot read file-dependencies information "
19 "from %s\n", datadir);
20 }
Lev Walkinf15320b2004-06-03 03:38:44 +000021
22 TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) {
23 TQ_FOR(arg->expr, &(arg->mod->members), next) {
24 if(asn1_lang_map[arg->expr->meta_type]
25 [arg->expr->expr_type].type_cb) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000026 if(asn1c_dump_streams(arg, deps))
Lev Walkinf15320b2004-06-03 03:38:44 +000027 return -1;
28 }
29 }
30 }
31
32 /*
33 * Dump out the Makefile template and the rest of the support code.
34 */
Lev Walkinacd9f8b2004-08-19 13:29:03 +000035 if((arg->flags & A1C_PRINT_COMPILED)
36 || (arg->flags & A1C_OMIT_SUPPORT_CODE)) {
37 return 0; /* Finished */
38 }
Lev Walkinf15320b2004-06-03 03:38:44 +000039
Lev Walkinacd9f8b2004-08-19 13:29:03 +000040 mkf = asn1c_open_file("Makefile.am", ".sample");
41 if(mkf == NULL) {
42 perror("Makefile.am.sample");
43 return -1;
44 }
Lev Walkinf15320b2004-06-03 03:38:44 +000045
Lev Walkinacd9f8b2004-08-19 13:29:03 +000046 fprintf(mkf, "ASN_MODULE_SOURCES=");
47 TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) {
48 TQ_FOR(arg->expr, &(arg->mod->members), next) {
49 if(asn1_lang_map[arg->expr->meta_type]
50 [arg->expr->expr_type].type_cb) {
51 fprintf(mkf, "\t\\\n\t%s.c %s.h",
52 arg->expr->Identifier,
53 arg->expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +000054 }
55 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +000056 }
Lev Walkinf15320b2004-06-03 03:38:44 +000057
Lev Walkinacd9f8b2004-08-19 13:29:03 +000058 /*
59 * Move necessary skeleton files and add them to Makefile.am.sample.
60 */
61 dlist = asn1c_deps_makelist(deps);
62 if(dlist) {
63 char buf[8129];
64 char *dir_end;
65 int i = strlen(datadir);
66
67 assert(i < (int)(sizeof(buf) / 2 - 2));
68 memcpy(buf, datadir, i);
69 dir_end = buf + i;
70 *dir_end++ = '/';
71
72 for(i = 0; i < dlist->el_count; i++) {
73 char *fname = dlist->elements[i]->filename;
74
75 assert(strlen(fname) < (sizeof(buf) / 2));
76 strcpy(dir_end, fname);
77
78 if(asn1c_copy_over(arg, buf) == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +000079 fprintf(mkf, ">>>ABORTED<<<");
80 fclose(mkf);
Lev Walkinf15320b2004-06-03 03:38:44 +000081 return -1;
82 } else {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000083 fprintf(mkf, "\t\\\n\t%s", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +000084 }
85 }
Lev Walkinf15320b2004-06-03 03:38:44 +000086 }
87
Lev Walkinacd9f8b2004-08-19 13:29:03 +000088 fprintf(mkf, "\n\n");
89 fprintf(mkf, "lib_LTLIBRARIES=libsomething.la\n");
90 fprintf(mkf, "libsomething_la_SOURCES=$(ASN_MODULE_SOURCES)\n");
91 fclose(mkf);
92 fprintf(stderr, "Generated Makefile.am.sample\n");
93
Lev Walkinf15320b2004-06-03 03:38:44 +000094 return 0;
95}
96
97/*
98 * Dump the streams.
99 */
100static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000101asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000102 if(arg->flags & A1C_PRINT_COMPILED) {
103 return asn1c_print_streams(arg);
104 } else {
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000105 return asn1c_save_streams(arg, deps);
Lev Walkinf15320b2004-06-03 03:38:44 +0000106 }
107}
108
109static int
110asn1c_print_streams(arg_t *arg) {
111 compiler_streams_t *cs = arg->expr->data;
112 asn1p_expr_t *expr = arg->expr;
113 int i;
114
Lev Walkin64399722004-08-11 07:17:22 +0000115 for(i = 1; i < OT_MAX; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000116 out_chunk_t *ot;
117 if(TQ_FIRST(&cs->targets[i]) == NULL)
118 continue;
119
120 printf("\n/*** <<< %s [%s] >>> ***/\n\n",
121 _compiler_stream2str[i],
122 expr->Identifier);
123
124 TQ_FOR(ot, &(cs->targets[i]), next) {
125 fwrite(ot->buf, ot->len, 1, stdout);
126 }
127 }
128
129 return 0;
130}
131
132static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000133asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000134 asn1p_expr_t *expr = arg->expr;
135 compiler_streams_t *cs = expr->data;
136 out_chunk_t *ot;
137 FILE *fp_c, *fp_h;
138 char *header_id;
139
140 if(cs == NULL) {
141 fprintf(stderr, "Cannot compile %s at line %d\n",
142 expr->Identifier, expr->_lineno);
143 return -1;
144 }
145
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000146 fp_c = asn1c_open_file(expr->Identifier, ".c");
147 fp_h = asn1c_open_file(expr->Identifier, ".h");
Lev Walkinf15320b2004-06-03 03:38:44 +0000148 if(fp_c == NULL || fp_h == NULL) {
149 if(fp_c) fclose(fp_c); /* lacks unlink() */
150 if(fp_h) fclose(fp_h); /* lacks unlink() */
151 return -1;
152 }
153
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000154 fprintf(fp_c,
155 "/*\n"
156 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
157 " * From ASN.1 module \"%s\" found in \"%s\"\n"
158 " */\n\n",
159 arg->mod->Identifier,
160 arg->mod->source_file_name
161 );
162 fprintf(fp_h,
163 "/*\n"
164 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
165 " * From ASN.1 module \"%s\" found in \"%s\"\n"
166 " */\n\n",
167 arg->mod->Identifier,
168 arg->mod->source_file_name
169 );
170
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000171 header_id = asn1c_make_identifier(0, expr->Identifier, NULL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000172 fprintf(fp_h,
173 "#ifndef\t_%s_H_\n"
174 "#define\t_%s_H_\n"
175 "\n", header_id, header_id);
176
Lev Walkin43634792004-08-10 01:14:29 +0000177 fprintf(fp_h, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
178
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 fprintf(fp_h, "#include <constr_TYPE.h>\n\n");
180
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000181 TQ_FOR(ot, &(cs->targets[OT_INCLUDES]), next) {
182 asn1c_activate_dependency(deps, 0, ot->buf);
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000183 fwrite(ot->buf, ot->len, 1, fp_h);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000184 }
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000185 fprintf(fp_h, "\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000186 TQ_FOR(ot, &(cs->targets[OT_DEPS]), next)
187 fwrite(ot->buf, ot->len, 1, fp_h);
188 fprintf(fp_h, "\n");
189 TQ_FOR(ot, &(cs->targets[OT_TYPE_DECLS]), next)
190 fwrite(ot->buf, ot->len, 1, fp_h);
191 fprintf(fp_h, "\n");
192 TQ_FOR(ot, &(cs->targets[OT_FUNC_DECLS]), next)
193 fwrite(ot->buf, ot->len, 1, fp_h);
194
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000195 fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n\n"
196 "#endif\t/* _%s_H_ */\n",
197 header_id);
198
199 fprintf(fp_c, "#include <%s.h>\n\n", expr->Identifier); /* Myself */
Lev Walkinf15320b2004-06-03 03:38:44 +0000200 TQ_FOR(ot, &(cs->targets[OT_STAT_DEFS]), next)
201 fwrite(ot->buf, ot->len, 1, fp_c);
202 TQ_FOR(ot, &(cs->targets[OT_CODE]), next)
203 fwrite(ot->buf, ot->len, 1, fp_c);
204
Lev Walkin64399722004-08-11 07:17:22 +0000205 assert(OT_MAX == 7);
Lev Walkinf15320b2004-06-03 03:38:44 +0000206
207 fclose(fp_c);
208 fclose(fp_h);
209 fprintf(stderr, "Compiled %s.c\n", expr->Identifier);
210 fprintf(stderr, "Compiled %s.h\n", expr->Identifier);
211 return 0;
212}
213
214static int
215asn1c_copy_over(arg_t *arg, char *path) {
Lev Walkin79f54952004-08-13 16:58:19 +0000216 char *fname;
Lev Walkinf15320b2004-06-03 03:38:44 +0000217
Lev Walkind9bd7752004-06-05 08:17:50 +0000218 (void)arg; /* Unused argument */
219
Lev Walkin79f54952004-08-13 16:58:19 +0000220 fname = a1c_basename(path);
221 if(!fname || symlink(path, fname)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000222 if(errno == EEXIST) {
223 struct stat sb1, sb2;
224 if(stat(path, &sb1) == 0
225 && stat(fname, &sb2) == 0
226 && sb1.st_dev == sb2.st_dev
227 && sb1.st_ino == sb2.st_ino) {
228 /*
229 * Nothing to do.
230 */
231 fprintf(stderr,
232 "File %s is already here as %s\n",
233 path, fname);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000234 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000235 } else {
236 fprintf(stderr,
237 "Retaining local %s (%s suggested)\n",
238 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000239 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000240 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000241 } else if(errno == ENOENT) {
242 /* Ignore this */
243 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000244 } else {
245 fprintf(stderr, "Symlink %s -> %s failed: %s\n",
246 path, fname, strerror(errno));
247 return -1;
248 }
249 }
250
251 fprintf(stderr, "Symlinked %s\t-> %s\n", path, fname);
252
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000253 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000254}
255