Commit 6431b1c9 authored by Jon Ringle's avatar Jon Ringle Committed by Lev Walkin

Add '-D destdir' option for generated files

parent 7871abf7
......@@ -63,16 +63,19 @@ main(int ac, char **av) {
int print_arg__fix_n_print = 0; /* Fix and print */
int warnings_as_errors = 0; /* Treat warnings as errors */
char *skeletons_dir = NULL; /* Directory with supplementary stuff */
char destdir[PATH_MAX]; /* Destination directory for generated files */
asn1p_t *asn = 0; /* An ASN.1 parsed tree */
int ret; /* Return value from misc functions */
int ch; /* Command line character */
int i; /* Index in some loops */
int exit_code = 0; /* Exit code */
destdir[0] = '\0';
/*
* Process command-line options.
*/
while((ch = getopt(ac, av, "EFf:g:hn:LPp:RS:vW:X")) != -1) switch(ch) {
while((ch = getopt(ac, av, "EFf:g:hn:LPp:RS:D:vW:X")) != -1) switch(ch) {
case 'E':
print_arg__print_out = 1;
break;
......@@ -179,6 +182,11 @@ main(int ac, char **av) {
case 'S':
skeletons_dir = optarg;
break;
case 'D':
strncat(destdir, optarg, PATH_MAX - 2); /* leave room for possible trailing '/' */
if(destdir[strlen(destdir)-1] != '/')
strcat(destdir, "/");
break;
case 'v':
fprintf(stderr, "ASN.1 Compiler, v" VERSION "\n" COPYRIGHT);
exit(0);
......@@ -241,7 +249,7 @@ main(int ac, char **av) {
ac -= optind;
av += optind;
} else {
const char *bin_name = a1c_basename(av[0]);
const char *bin_name = a1c_basename(av[0], NULL);
fprintf(stderr,
"%s: No input files specified. "
"Try '%s -h' for more information\n",
......@@ -370,7 +378,7 @@ main(int ac, char **av) {
* Compile the ASN.1 tree into a set of source files
* of another language.
*/
if(asn1_compile(asn, skeletons_dir, asn1_compiler_flags, ac + optind,
if(asn1_compile(asn, skeletons_dir, destdir, asn1_compiler_flags, ac + optind,
optind, av - optind)) {
exit_code = EX_SOFTWARE;
}
......@@ -499,6 +507,7 @@ usage(const char *av0) {
" -R Restrict output (tables only, no support code)\n"
" -S <dir> Directory with support (skeleton?) files\n"
" (Default is \"%s\")\n"
" -D <dir> Destination directory for generated files (default current dir)\n"
" -X Generate and print the XML DTD\n"
"\n"
......@@ -532,7 +541,7 @@ usage(const char *av0) {
" -print-lines Generate \"-- #line\" comments in -E output\n"
,
a1c_basename(av0), DATADIR);
a1c_basename(av0, NULL), DATADIR);
/* clang-format on */
exit(EX_USAGE);
}
......@@ -36,7 +36,7 @@ int mkstemp(char *template) {
#endif
FILE *
asn1c_open_file(const char *name, const char *ext, char **opt_tmpname) {
asn1c_open_file(const char* destdir, const char *name, const char *ext, char **opt_tmpname) {
char fname[PATH_MAX];
int created = 1;
#ifndef _WIN32
......@@ -49,7 +49,9 @@ asn1c_open_file(const char *name, const char *ext, char **opt_tmpname) {
/*
* Compute filenames.
*/
ret = snprintf(fname, sizeof(fname), "%s%s%s", name, ext,
ret = snprintf(fname, sizeof(fname), "%s%s%s%s",
opt_tmpname ? "" : destdir,
name, ext,
opt_tmpname ? ".XXXXXX" : "");
assert(ret > 0 && ret < (ssize_t)sizeof(fname));
......@@ -124,14 +126,20 @@ asn1c_open_file(const char *name, const char *ext, char **opt_tmpname) {
}
const char *
a1c_basename(const char *path) {
a1c_basename(const char *path, const char *destdir) {
static char strbuf[PATH_MAX];
const char *pend;
const char *name;
char *sbuf = strbuf;
if(destdir) {
strncpy(strbuf, destdir, PATH_MAX - 1);
strbuf[PATH_MAX - 1] = '\0';
sbuf = strbuf + strlen(strbuf);
}
pend = path + strlen(path);
if(pend == path) {
strcpy(strbuf, ".");
strcpy(sbuf, ".");
return strbuf;
}
......@@ -139,7 +147,7 @@ a1c_basename(const char *path) {
for(pend--; pend > path && *pend == '/'; pend--);
if(pend == path && *path == '/') {
strcpy(strbuf, "/");
strcpy(sbuf, "/");
return strbuf;
}
......@@ -150,8 +158,8 @@ a1c_basename(const char *path) {
return 0;
}
memcpy(strbuf, name, pend - name + 1);
strbuf[pend - name + 1] = '\0';
memcpy(sbuf, name, pend - name + 1);
sbuf[pend - name + 1] = '\0';
return strbuf;
}
......
......@@ -7,14 +7,14 @@
* its name returned in (*opt_tmpname).
* The (*opt_tmpname) should then be subsequently freed by free(3).
*/
FILE *asn1c_open_file(const char *base_part, const char *extension,
FILE *asn1c_open_file(const char *destdir, const char *base_part, const char *extension,
char **opt_tmpname);
/*
* Obtain base name and directory name of a path.
* Some systems have them in <libgen.h> as dirname(3) and basename(3).
*/
const char *a1c_basename(const char *path);
const char *a1c_basename(const char *path, const char* destdir);
const char *a1c_dirname(const char *path);
#endif /* ASN1C_COMPAT_H */
......@@ -38,14 +38,14 @@ enum include_type_result {
TI_INCLUDED_FROM_CMDLINE
};
static int asn1c_dump_streams(arg_t *arg, asn1c_dep_chainset *, int, char **);
static int asn1c_dump_streams(arg_t *arg, asn1c_dep_chainset *, const char *, int, char **);
static int asn1c_print_streams(arg_t *arg);
static int asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *, int, char **);
static int asn1c_copy_over(arg_t *arg, const char *path, const char *msg);
static int asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *, const char *, int, char **);
static int asn1c_copy_over(arg_t *arg, const char *destdir, const char *path, const char *msg);
static int identical_files(const char *fname1, const char *fname2);
static int need_to_generate_pdu_collection(arg_t *arg);
static abuf *generate_pdu_collection(arg_t *arg);
static int generate_pdu_collection_file(arg_t *arg);
static int generate_pdu_collection_file(arg_t *arg, const char *destdir);
static int generate_preamble(arg_t *, FILE *, int optc, char **argv);
static enum include_type_result include_type_to_pdu_collection(arg_t *arg);
static int pdu_collection_has_unused_types(arg_t *arg);
......@@ -55,11 +55,11 @@ static int asn1c__pdu_type_lookup(const char *typename);
static int
asn1c__save_library_makefile(arg_t *arg, const asn1c_dep_chainset *deps,
const char *datadir, const char *makefile_name) {
const char *datadir, const char *destdir, const char *makefile_name) {
asn1p_module_t *mod;
FILE *mkf;
mkf = asn1c_open_file(makefile_name, "", 0);
mkf = asn1c_open_file(destdir, makefile_name, "", 0);
if(mkf == NULL) {
perror(makefile_name);
return -1;
......@@ -70,7 +70,7 @@ asn1c__save_library_makefile(arg_t *arg, const asn1c_dep_chainset *deps,
TQ_FOR(arg->expr, &(mod->members), next) {
if(asn1_lang_map[arg->expr->meta_type]
[arg->expr->expr_type].type_cb) {
safe_fprintf(mkf, "\t\\\n\t%s.c",
safe_fprintf(mkf, "\t\\\n\t%s%s.c", destdir,
asn1c_make_identifier(AMI_MASK_ONLY_SPACES, arg->expr, 0));
}
}
......@@ -80,7 +80,7 @@ asn1c__save_library_makefile(arg_t *arg, const asn1c_dep_chainset *deps,
TQ_FOR(arg->expr, &(mod->members), next) {
if(asn1_lang_map[arg->expr->meta_type]
[arg->expr->expr_type].type_cb) {
safe_fprintf(mkf, "\t\\\n\t%s.h",
safe_fprintf(mkf, "\t\\\n\t%s%s.h", destdir,
asn1c_make_identifier(AMI_MASK_ONLY_SPACES, arg->expr, 0));
}
}
......@@ -118,7 +118,7 @@ asn1c__save_library_makefile(arg_t *arg, const asn1c_dep_chainset *deps,
where[0] = '\0';
}
if(asn1c_copy_over(arg, dstpath, where) == -1) {
if(asn1c_copy_over(arg, destdir, dstpath, where) == -1) {
safe_fprintf(mkf, ">>>ABORTED<<<");
fclose(mkf);
return -1;
......@@ -131,7 +131,7 @@ asn1c__save_library_makefile(arg_t *arg, const asn1c_dep_chainset *deps,
} else {
what_kind = "SRCS";
}
safe_fprintf(mkf, "ASN_MODULE_%s+=%s\n", what_kind, fname);
safe_fprintf(mkf, "ASN_MODULE_%s+=%s%s\n", what_kind, destdir, fname);
}
asn1c_dep_chain_free(dlist);
......@@ -152,57 +152,54 @@ asn1c__save_library_makefile(arg_t *arg, const asn1c_dep_chainset *deps,
"$(ASN_MODULE_SRCS) $(ASN_MODULE_HDRS)\n"
"libasncodec_la_CFLAGS=$(ASN_MODULE_CFLAGS)\n");
fclose(mkf);
safe_fprintf(stderr, "Generated Makefile.am.targets\n");
safe_fprintf(stderr, "Generated %s%s\n", destdir, makefile_name);
return 0;
}
static int
asn1c__save_example_makefile(arg_t *arg, const asn1c_dep_chainset *deps,
const char *datadir, const char *makefile_name,
asn1c__save_example_makefile(arg_t *arg, const asn1c_dep_chainset *deps, const char *datadir,
const char *destdir, const char *makefile_name,
const char *library_makefile_name, int argc,
char **argv) {
FILE *mkf;
asn1c_dep_chain *dlist = asn1c_deps_flatten(deps, FDEP_CONVERTER);
mkf = asn1c_open_file(makefile_name, "", 0);
mkf = asn1c_open_file(destdir, makefile_name, "", 0);
if(mkf == NULL) {
perror(makefile_name);
return -1;
}
safe_fprintf(
mkf,
"include %s\n\n"
safe_fprintf(mkf,
"include %s%s\n\n"
"LIBS += -lm\n"
"CFLAGS += $(ASN_MODULE_CFLAGS) %s%s-I.\n"
"ASN_LIBRARY ?= libasncodec.a\n"
"ASN_PROGRAM ?= converter-example\n"
"ASN_PROGRAM_SRCS ?= ",
library_makefile_name,
destdir, library_makefile_name,
(arg->flags & A1C_PDU_TYPE) ? generate_pdu_C_definition() : "",
need_to_generate_pdu_collection(arg) ? "-DASN_PDU_COLLECTION " : "");
if(arg->flags & A1C_GEN_EXAMPLE) {
asn1c_dep_chain *dlist = asn1c_deps_flatten(deps, FDEP_CONVERTER);
if(dlist) {
for(size_t i = 0; i < dlist->deps_count; i++) {
char dstpath[PATH_MAX];
int ret = snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir,
dlist->deps[i]->filename);
assert(ret > 0 && (size_t)ret < sizeof(dstpath));
if(asn1c_copy_over(arg, dstpath, "implicit") == -1) {
if(asn1c_copy_over(arg, destdir, dstpath, "implicit") == -1) {
safe_fprintf(mkf, ">>>ABORTED<<<");
fclose(mkf);
return -1;
}
safe_fprintf(mkf, "\\\n\t%s", dlist->deps[i]->filename);
safe_fprintf(mkf, "\\\n\t%s%s", destdir, dlist->deps[i]->filename);
}
asn1c_dep_chain_free(dlist);
}
}
if(need_to_generate_pdu_collection(arg)) {
safe_fprintf(mkf, "\\\n\tpdu_collection.c");
if(generate_pdu_collection_file(arg))
safe_fprintf(mkf, "\\\n\t%spdu_collection.c", destdir);
if(generate_pdu_collection_file(arg, destdir))
return -1;
}
......@@ -228,7 +225,7 @@ asn1c__save_example_makefile(arg_t *arg, const asn1c_dep_chainset *deps,
safe_fprintf(mkf, "\n\n");
fclose(mkf);
safe_fprintf(stderr, "Generated %s\n", makefile_name);
safe_fprintf(stderr, "Generated %s%s\n", destdir, makefile_name);
return 0;
}
......@@ -243,10 +240,13 @@ can_generate_pdu_collection(arg_t *arg) {
}
int
asn1c_save_compiled_output(arg_t *arg, const char *datadir,
asn1c_save_compiled_output(arg_t *arg, const char *datadir, const char *destdir,
int argc, int optc, char **argv) {
int ret = -1;
const char* program_makefile = "Makefile.am.example";
const char* library_makefile = "Makefile.am.libasncodec";
/*
* Early check that we can properly generate PDU collection.
*/
......@@ -270,7 +270,7 @@ asn1c_save_compiled_output(arg_t *arg, const char *datadir,
TQ_FOR(arg->expr, &(mod->members), next) {
if(asn1_lang_map[arg->expr->meta_type][arg->expr->expr_type]
.type_cb) {
if(asn1c_dump_streams(arg, deps, optc, argv)) break;
if(asn1c_dump_streams(arg, deps, destdir, optc, argv)) break;
}
}
}
......@@ -284,14 +284,14 @@ asn1c_save_compiled_output(arg_t *arg, const char *datadir,
break;
}
ret = asn1c__save_library_makefile(arg, deps, datadir,
"Makefile.am.libasncodec");
ret = asn1c__save_library_makefile(arg, deps, datadir, destdir,
library_makefile);
if(ret) break;
if(arg->flags & A1C_GEN_EXAMPLE) {
ret = asn1c__save_example_makefile(
arg, deps, datadir, "Makefile.am.example",
"Makefile.am.libasncodec", argc, argv);
ret = asn1c__save_example_makefile(arg, deps, datadir, destdir,
program_makefile,
library_makefile, argc, argv);
if(ret) break;
}
} while(0);
......@@ -306,12 +306,12 @@ asn1c_save_compiled_output(arg_t *arg, const char *datadir,
* Dump the streams.
*/
static int
asn1c_dump_streams(arg_t *arg, asn1c_dep_chainset *deps, int optc,
asn1c_dump_streams(arg_t *arg, asn1c_dep_chainset *deps, const char* destdir, int optc,
char **argv) {
if(arg->flags & A1C_PRINT_COMPILED) {
return asn1c_print_streams(arg);
} else {
return asn1c_save_streams(arg, deps, optc, argv);
return asn1c_save_streams(arg, deps, destdir, optc, argv);
}
}
......@@ -339,7 +339,7 @@ asn1c_print_streams(arg_t *arg) {
}
static int
asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, int optc,
asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, const char* destdir, int optc,
char **argv) {
asn1p_expr_t *expr = arg->expr;
compiler_streams_t *cs = expr->data;
......@@ -359,8 +359,8 @@ asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, int optc,
}
filename = strdup(asn1c_make_identifier(AMI_MASK_ONLY_SPACES, expr, (char*)0));
fp_c = asn1c_open_file(filename, ".c", &tmpname_c);
fp_h = asn1c_open_file(filename, ".h", &tmpname_h);
fp_c = asn1c_open_file(destdir, filename, ".c", &tmpname_c);
fp_h = asn1c_open_file(destdir, filename, ".h", &tmpname_h);
if(fp_c == NULL || fp_h == NULL) {
if(fp_c) { unlink(tmpname_c); free(tmpname_c); fclose(fp_c); }
if(fp_h) { unlink(tmpname_h); free(tmpname_h); fclose(fp_h); }
......@@ -423,7 +423,7 @@ asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, int optc,
fclose(fp_c);
fclose(fp_h);
int ret = snprintf(name_buf, sizeof(name_buf), "%s.c", filename);
int ret = snprintf(name_buf, sizeof(name_buf), "%s%s.c", destdir, filename);
assert(ret > 0 && ret < (ssize_t)sizeof(name_buf));
if(identical_files(name_buf, tmpname_c)) {
......@@ -439,7 +439,7 @@ asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, int optc,
}
}
sprintf(name_buf, "%s.h", filename);
sprintf(name_buf, "%s%s.h", destdir, filename);
if(identical_files(name_buf, tmpname_h)) {
h_retained = " (contents unchanged)";
unlink(tmpname_h);
......@@ -456,10 +456,10 @@ asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, int optc,
free(tmpname_c);
free(tmpname_h);
safe_fprintf(stderr, "Compiled %s.c%s\n",
filename, c_retained);
safe_fprintf(stderr, "Compiled %s.h%s\n",
filename, h_retained);
safe_fprintf(stderr, "Compiled %s%s.c%s\n",
destdir, filename, c_retained);
safe_fprintf(stderr, "Compiled %s%s.h%s\n",
destdir, filename, h_retained);
free(filename);
return 0;
}
......@@ -536,7 +536,7 @@ real_copy(const char *src, const char *dst) {
fpsrc = fopen(src, "r");
if(!fpsrc) { errno = EIO; return -1; }
fpdst = asn1c_open_file(dst, "", &tmpname);
fpdst = asn1c_open_file(NULL, dst, "", &tmpname);
if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; }
while(!feof(fpsrc)) {
......@@ -564,14 +564,14 @@ real_copy(const char *src, const char *dst) {
}
static int
asn1c_copy_over(arg_t *arg, const char *path, const char *msg) {
asn1c_copy_over(arg_t *arg, const char* destdir, const char *path, const char *msg) {
#ifdef _WIN32
int use_real_copy = 1;
#else
int use_real_copy = !(arg->flags & A1C_LINK_SKELETONS);
#endif
const char *fname = a1c_basename(path);
const char *fname = a1c_basename(path, destdir);
if(!fname
|| (use_real_copy ? real_copy(path, fname) : symlink(path, fname))
) {
......@@ -615,11 +615,11 @@ asn1c_copy_over(arg_t *arg, const char *path, const char *msg) {
static int
generate_pdu_collection_file(arg_t *arg) {
generate_pdu_collection_file(arg_t *arg, const char* destdir) {
abuf *buf = generate_pdu_collection(arg);
assert(buf);
FILE *fp = asn1c_open_file("pdu_collection", ".c", 0);
FILE *fp = asn1c_open_file(destdir, "pdu_collection", ".c", 0);
if(fp == NULL) {
perror("pdu_collection.c");
return -1;
......
#ifndef ASN1C_SAVE_H
#define ASN1C_SAVE_H
int asn1c_save_compiled_output(arg_t *arg, const char *datadir,
int asn1c_save_compiled_output(arg_t *arg, const char *datadir, const char* destdir,
int argc, int optc, char **argv);
#endif /* ASN1C_SAVE_H */
......@@ -11,7 +11,7 @@ static int asn1c_attach_streams(asn1p_expr_t *expr);
static int asn1c_detach_streams(asn1p_expr_t *expr);
int
asn1_compile(asn1p_t *asn, const char *datadir, enum asn1c_flags flags,
asn1_compile(asn1p_t *asn, const char *datadir, const char *destdir, enum asn1c_flags flags,
int argc, int optc, char **argv) {
arg_t arg_s;
arg_t *arg = &arg_s;
......@@ -84,7 +84,7 @@ asn1_compile(asn1p_t *asn, const char *datadir, enum asn1c_flags flags,
/*
* Save or print out the compiled result.
*/
if(asn1c_save_compiled_output(arg, datadir, argc, optc, argv))
if(asn1c_save_compiled_output(arg, datadir, destdir, argc, optc, argv))
return -1;
TQ_FOR(mod, &(asn->modules), mod_next) {
......
......@@ -93,7 +93,7 @@ enum asn1c_flags {
/*
* Compile the ASN.1 specification.
*/
int asn1_compile(asn1p_t *asn, const char *datadir, enum asn1c_flags,
int asn1_compile(asn1p_t *asn, const char *datadir, const char *destdir, enum asn1c_flags,
int argc, int optc, char **argv);
void asn1c__add_pdu_type(const char *typename);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment