vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include "asn1c_internal.h" |
vlm | 1f1d8cb | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 2 | #include "asn1c_compat.h" |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 3 | #include "asn1c_fdeps.h" |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 4 | #include "asn1c_lang.h" |
| 5 | #include "asn1c_misc.h" |
| 6 | #include "asn1c_save.h" |
| 7 | #include "asn1c_out.h" |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 8 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 9 | static int asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 10 | static int asn1c_print_streams(arg_t *arg); |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 11 | static int asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 12 | static int asn1c_copy_over(arg_t *arg, char *path); |
| 13 | |
| 14 | int |
| 15 | asn1c_save_compiled_output(arg_t *arg, const char *datadir) { |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 16 | asn1c_fdeps_t *deps = 0; |
| 17 | FILE *mkf; |
| 18 | asn1c_fdeps_t *dlist; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 19 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 20 | deps = asn1c_read_file_dependencies(arg, datadir); |
| 21 | if(!deps && datadir) { |
| 22 | WARNING("Cannot read file-dependencies information " |
| 23 | "from %s\n", datadir); |
| 24 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 25 | |
| 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) { |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 30 | if(asn1c_dump_streams(arg, deps)) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 31 | return -1; |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * Dump out the Makefile template and the rest of the support code. |
| 38 | */ |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 39 | if((arg->flags & A1C_PRINT_COMPILED) |
| 40 | || (arg->flags & A1C_OMIT_SUPPORT_CODE)) { |
| 41 | return 0; /* Finished */ |
| 42 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 43 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 44 | mkf = asn1c_open_file("Makefile.am", ".sample"); |
| 45 | if(mkf == NULL) { |
| 46 | perror("Makefile.am.sample"); |
| 47 | return -1; |
| 48 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 49 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 50 | 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); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 60 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 61 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 62 | /* |
| 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) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 83 | fprintf(mkf, ">>>ABORTED<<<"); |
| 84 | fclose(mkf); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 85 | return -1; |
| 86 | } else { |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 87 | fprintf(mkf, "\t\\\n\t%s", fname); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 90 | } |
| 91 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 92 | 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 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Dump the streams. |
| 103 | */ |
| 104 | static int |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 105 | asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *deps) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 106 | if(arg->flags & A1C_PRINT_COMPILED) { |
| 107 | return asn1c_print_streams(arg); |
| 108 | } else { |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 109 | return asn1c_save_streams(arg, deps); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | static int |
| 114 | asn1c_print_streams(arg_t *arg) { |
| 115 | compiler_streams_t *cs = arg->expr->data; |
| 116 | asn1p_expr_t *expr = arg->expr; |
| 117 | int i; |
| 118 | |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 119 | for(i = 1; i < OT_MAX; i++) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 120 | out_chunk_t *ot; |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 121 | if(TQ_FIRST(&cs->destination[i].chunks) == NULL) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 122 | continue; |
| 123 | |
| 124 | printf("\n/*** <<< %s [%s] >>> ***/\n\n", |
| 125 | _compiler_stream2str[i], |
| 126 | expr->Identifier); |
| 127 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 128 | TQ_FOR(ot, &(cs->destination[i].chunks), next) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 129 | fwrite(ot->buf, ot->len, 1, stdout); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 137 | asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 138 | 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 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 150 | fp_c = asn1c_open_file(expr->Identifier, ".c"); |
| 151 | fp_h = asn1c_open_file(expr->Identifier, ".h"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 152 | 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 | |
vlm | f18d856 | 2004-06-03 05:06:25 +0000 | [diff] [blame] | 158 | 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 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 175 | header_id = asn1c_make_identifier(0, expr->Identifier, NULL); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 176 | fprintf(fp_h, |
| 177 | "#ifndef\t_%s_H_\n" |
| 178 | "#define\t_%s_H_\n" |
| 179 | "\n", header_id, header_id); |
| 180 | |
vlm | 8098252 | 2004-08-10 01:14:29 +0000 | [diff] [blame] | 181 | fprintf(fp_h, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n"); |
| 182 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 183 | fprintf(fp_h, "#include <constr_TYPE.h>\n\n"); |
| 184 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 185 | TQ_FOR(ot, &(cs->destination[OT_INCLUDES].chunks), next) { |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 186 | asn1c_activate_dependency(deps, 0, ot->buf); |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 187 | fwrite(ot->buf, ot->len, 1, fp_h); |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 188 | } |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 189 | fprintf(fp_h, "\n"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 190 | TQ_FOR(ot, &(cs->destination[OT_DEPS].chunks), next) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 191 | fwrite(ot->buf, ot->len, 1, fp_h); |
| 192 | fprintf(fp_h, "\n"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 193 | TQ_FOR(ot, &(cs->destination[OT_TYPE_DECLS].chunks), next) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 194 | fwrite(ot->buf, ot->len, 1, fp_h); |
| 195 | fprintf(fp_h, "\n"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 196 | TQ_FOR(ot, &(cs->destination[OT_FUNC_DECLS].chunks), next) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 197 | fwrite(ot->buf, ot->len, 1, fp_h); |
| 198 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 199 | fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n\n" |
| 200 | "#endif\t/* _%s_H_ */\n", |
| 201 | header_id); |
| 202 | |
| 203 | fprintf(fp_c, "#include <%s.h>\n\n", expr->Identifier); /* Myself */ |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 204 | TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 205 | fwrite(ot->buf, ot->len, 1, fp_c); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 206 | TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next) |
| 207 | fwrite(ot->buf, ot->len, 1, fp_c); |
| 208 | TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 209 | fwrite(ot->buf, ot->len, 1, fp_c); |
| 210 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 211 | assert(OT_MAX == 8); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 212 | |
| 213 | fclose(fp_c); |
| 214 | fclose(fp_h); |
| 215 | fprintf(stderr, "Compiled %s.c\n", expr->Identifier); |
| 216 | fprintf(stderr, "Compiled %s.h\n", expr->Identifier); |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static int |
| 221 | asn1c_copy_over(arg_t *arg, char *path) { |
vlm | 1f1d8cb | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 222 | char *fname; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 223 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 224 | (void)arg; /* Unused argument */ |
| 225 | |
vlm | 1f1d8cb | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 226 | fname = a1c_basename(path); |
| 227 | if(!fname || symlink(path, fname)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 228 | if(errno == EEXIST) { |
| 229 | struct stat sb1, sb2; |
| 230 | if(stat(path, &sb1) == 0 |
| 231 | && stat(fname, &sb2) == 0 |
| 232 | && sb1.st_dev == sb2.st_dev |
| 233 | && sb1.st_ino == sb2.st_ino) { |
| 234 | /* |
| 235 | * Nothing to do. |
| 236 | */ |
| 237 | fprintf(stderr, |
| 238 | "File %s is already here as %s\n", |
| 239 | path, fname); |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 240 | return 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 241 | } else { |
| 242 | fprintf(stderr, |
| 243 | "Retaining local %s (%s suggested)\n", |
| 244 | fname, path); |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 245 | return 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 246 | } |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 247 | } else if(errno == ENOENT) { |
| 248 | /* Ignore this */ |
| 249 | return 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 250 | } else { |
| 251 | fprintf(stderr, "Symlink %s -> %s failed: %s\n", |
| 252 | path, fname, strerror(errno)); |
| 253 | return -1; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | fprintf(stderr, "Symlinked %s\t-> %s\n", path, fname); |
| 258 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 259 | return 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 260 | } |
| 261 | |