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 | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 4 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 5 | static int asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | static int asn1c_print_streams(arg_t *arg); |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 7 | static int asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 8 | static int asn1c_copy_over(arg_t *arg, char *path); |
| 9 | |
| 10 | int |
| 11 | asn1c_save_compiled_output(arg_t *arg, const char *datadir) { |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 12 | asn1c_fdeps_t *deps = 0; |
| 13 | FILE *mkf; |
| 14 | asn1c_fdeps_t *dlist; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 15 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 16 | deps = asn1c_read_file_dependencies(arg, datadir); |
| 17 | if(!deps && datadir) { |
| 18 | WARNING("Cannot read file-dependencies information " |
| 19 | "from %s\n", datadir); |
| 20 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 21 | |
| 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) { |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 26 | if(asn1c_dump_streams(arg, deps)) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 27 | return -1; |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /* |
| 33 | * Dump out the Makefile template and the rest of the support code. |
| 34 | */ |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 35 | if((arg->flags & A1C_PRINT_COMPILED) |
| 36 | || (arg->flags & A1C_OMIT_SUPPORT_CODE)) { |
| 37 | return 0; /* Finished */ |
| 38 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 39 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 40 | mkf = asn1c_open_file("Makefile.am", ".sample"); |
| 41 | if(mkf == NULL) { |
| 42 | perror("Makefile.am.sample"); |
| 43 | return -1; |
| 44 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 45 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 46 | 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); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 56 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 57 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 58 | /* |
| 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) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 79 | fprintf(mkf, ">>>ABORTED<<<"); |
| 80 | fclose(mkf); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 81 | return -1; |
| 82 | } else { |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 83 | fprintf(mkf, "\t\\\n\t%s", fname); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 86 | } |
| 87 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 88 | 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 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Dump the streams. |
| 99 | */ |
| 100 | static int |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 101 | asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *deps) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 102 | if(arg->flags & A1C_PRINT_COMPILED) { |
| 103 | return asn1c_print_streams(arg); |
| 104 | } else { |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 105 | return asn1c_save_streams(arg, deps); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | static int |
| 110 | asn1c_print_streams(arg_t *arg) { |
| 111 | compiler_streams_t *cs = arg->expr->data; |
| 112 | asn1p_expr_t *expr = arg->expr; |
| 113 | int i; |
| 114 | |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 115 | for(i = 1; i < OT_MAX; i++) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 116 | 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 | |
| 132 | static int |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 133 | asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 134 | 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 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 146 | fp_c = asn1c_open_file(expr->Identifier, ".c"); |
| 147 | fp_h = asn1c_open_file(expr->Identifier, ".h"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 148 | 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 | |
vlm | f18d856 | 2004-06-03 05:06:25 +0000 | [diff] [blame] | 154 | 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 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 171 | header_id = asn1c_make_identifier(0, expr->Identifier, NULL); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 172 | fprintf(fp_h, |
| 173 | "#ifndef\t_%s_H_\n" |
| 174 | "#define\t_%s_H_\n" |
| 175 | "\n", header_id, header_id); |
| 176 | |
vlm | 8098252 | 2004-08-10 01:14:29 +0000 | [diff] [blame] | 177 | fprintf(fp_h, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n"); |
| 178 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 179 | fprintf(fp_h, "#include <constr_TYPE.h>\n\n"); |
| 180 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 181 | TQ_FOR(ot, &(cs->targets[OT_INCLUDES]), next) { |
| 182 | asn1c_activate_dependency(deps, 0, ot->buf); |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 183 | fwrite(ot->buf, ot->len, 1, fp_h); |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 184 | } |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 185 | fprintf(fp_h, "\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 186 | 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 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 195 | 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 */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 200 | 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 | |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 205 | assert(OT_MAX == 7); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 206 | |
| 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 | |
| 214 | static int |
| 215 | asn1c_copy_over(arg_t *arg, char *path) { |
vlm | 1f1d8cb | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 216 | char *fname; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 217 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 218 | (void)arg; /* Unused argument */ |
| 219 | |
vlm | 1f1d8cb | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 220 | fname = a1c_basename(path); |
| 221 | if(!fname || symlink(path, fname)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 222 | 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); |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 234 | return 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 235 | } else { |
| 236 | fprintf(stderr, |
| 237 | "Retaining local %s (%s suggested)\n", |
| 238 | fname, path); |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 239 | return 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 240 | } |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 241 | } else if(errno == ENOENT) { |
| 242 | /* Ignore this */ |
| 243 | return 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 244 | } 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 | |
vlm | bfb3131 | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 253 | return 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 254 | } |
| 255 | |