From: Rusty Russell Date: Tue, 1 Mar 2011 05:38:57 +0000 (+1030) Subject: ccanlint: read config.h to get compilation flags at runtime. X-Git-Url: http://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=104125b2dd0a044fb3d68f9bc3b1b18c9f32ae2b ccanlint: read config.h to get compilation flags at runtime. This means you don't have to recompile ccanlint to get the new flags; it's a small step towards making ccanlint useful outside the ccan repo. --- diff --git a/tools/ccanlint/ccanlint.c b/tools/ccanlint/ccanlint.c index 1c7ee389..231ae7c0 100644 --- a/tools/ccanlint/ccanlint.c +++ b/tools/ccanlint/ccanlint.c @@ -31,6 +31,7 @@ #include #include #include +#include int verbose = 0; static LIST_HEAD(compulsory_tests); @@ -41,6 +42,12 @@ static struct btree *cmdline_exclude; static struct btree *info_exclude; static unsigned int timeout; +/* These are overridden at runtime if we can find config.h */ +const char *compiler = CCAN_COMPILER; +const char *cflags = CCAN_CFLAGS; + +const char *config_header; + #if 0 static void indent_print(const char *string) { @@ -475,6 +482,92 @@ static void skip_unrelated_tests(struct ccanlint *target) i->skip = "not relevant to target"; } +static char *demangle_string(char *string) +{ + unsigned int i; + const char mapfrom[] = "abfnrtv"; + const char mapto[] = "\a\b\f\n\r\t\v"; + + if (!strchr(string, '"')) + return NULL; + string = strchr(string, '"') + 1; + if (!strrchr(string, '"')) + return NULL; + *strrchr(string, '"') = '\0'; + + for (i = 0; i < strlen(string); i++) { + if (string[i] == '\\') { + char repl; + unsigned len = 0; + char *p = strchr(mapfrom, string[i+1]); + if (p) { + repl = mapto[p - mapfrom]; + len = 1; + } else if (strlen(string+i+1) >= 3) { + if (string[i+1] == 'x') { + repl = (string[i+2]-'0')*16 + + string[i+3]-'0'; + len = 3; + } else if (isdigit(string[i+1])) { + repl = (string[i+2]-'0')*8*8 + + (string[i+3]-'0')*8 + + (string[i+4]-'0'); + len = 3; + } + } + if (len == 0) { + repl = string[i+1]; + len = 1; + } + + string[i] = repl; + memmove(string + i + 1, string + i + len + 1, + strlen(string + i + len + 1) + 1); + } + } + + return string; +} + + +static void read_config_header(void) +{ + char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir); + char **lines; + unsigned int i; + + config_header = grab_file(NULL, fname, NULL); + if (!config_header) { + talloc_free(fname); + return; + } + + lines = strsplit(config_header, config_header, "\n"); + for (i = 0; i < talloc_array_length(lines) - 1; i++) { + char *sym; + const char **line = (const char **)&lines[i]; + + if (!get_token(line, "#")) + continue; + if (!get_token(line, "define")) + continue; + sym = get_symbol_token(lines, line); + if (streq(sym, "CCAN_COMPILER")) { + compiler = demangle_string(lines[i]); + if (verbose > 1) + printf("%s: compiler set to '%s'\n", + fname, compiler); + } else if (streq(sym, "CCAN_CFLAGS")) { + cflags = demangle_string(lines[i]); + if (verbose > 1) + printf("%s: compiler flags set to '%s'\n", + fname, cflags); + } + } + if (!compiler || !cflags) + errx(1, "Problem parsing %s", fname); +} + int main(int argc, char *argv[]) { bool summary = false; @@ -535,6 +628,7 @@ int main(int argc, char *argv[]) tools_verbose = true; m = get_manifest(talloc_autofree_context(), dir); + read_config_header(); /* Create a symlink from temp dir back to src dir's test directory. */ if (symlink(talloc_asprintf(m, "%s/test", dir), diff --git a/tools/ccanlint/ccanlint.h b/tools/ccanlint/ccanlint.h index 41aaec93..33db1257 100644 --- a/tools/ccanlint/ccanlint.h +++ b/tools/ccanlint/ccanlint.h @@ -221,4 +221,10 @@ extern bool safe_mode; /* Where is the ccan dir? Available after first manifest. */ extern const char *ccan_dir; +/* Compiler and CFLAGS, from config.h if available. */ +extern const char *compiler, *cflags; + +/* Contents of config.h (or NULL if not found) */ +extern const char *config_header; + #endif /* CCAN_LINT_H */ diff --git a/tools/ccanlint/compulsory_tests/depends_build.c b/tools/ccanlint/compulsory_tests/depends_build.c index bb1f6ca3..00f75b37 100644 --- a/tools/ccanlint/compulsory_tests/depends_build.c +++ b/tools/ccanlint/compulsory_tests/depends_build.c @@ -37,8 +37,8 @@ static char *build_subdir_objs(struct manifest *m) char *output; i->compiled = maybe_temp_file(m, "", false, fullfile); - if (!compile_object(m, fullfile, ccan_dir, "", i->compiled, - &output)) { + if (!compile_object(m, fullfile, ccan_dir, compiler, cflags, + i->compiled, &output)) { talloc_free(i->compiled); i->compiled = NULL; return talloc_asprintf(m, diff --git a/tools/ccanlint/compulsory_tests/main_header_compiles.c b/tools/ccanlint/compulsory_tests/main_header_compiles.c index 296b0a39..99f3aa38 100644 --- a/tools/ccanlint/compulsory_tests/main_header_compiles.c +++ b/tools/ccanlint/compulsory_tests/main_header_compiles.c @@ -57,7 +57,8 @@ static void check_includes_build(struct manifest *m, err(1, "writing to temporary file %s", tmpsrc); close(fd); - if (compile_object(score, tmpsrc, ccan_dir, "", tmpobj, &cmdout)) { + if (compile_object(score, tmpsrc, ccan_dir, compiler, cflags, + tmpobj, &cmdout)) { score->pass = true; score->score = score->total; } else { diff --git a/tools/ccanlint/compulsory_tests/module_links.c b/tools/ccanlint/compulsory_tests/module_links.c index 83b1c780..9c1bce79 100644 --- a/tools/ccanlint/compulsory_tests/module_links.c +++ b/tools/ccanlint/compulsory_tests/module_links.c @@ -72,8 +72,8 @@ static void check_use_build(struct manifest *m, err(1, "Failure writing to temporary file %s", tmpfile); close(fd); - if (compile_and_link(score, tmpfile, ccan_dir, obj_list(m), "", - lib_list(m), + if (compile_and_link(score, tmpfile, ccan_dir, obj_list(m), + compiler, cflags, lib_list(m), maybe_temp_file(m, "", keep, tmpfile), &cmdout)) { score->pass = true; diff --git a/tools/ccanlint/compulsory_tests/objects_build.c b/tools/ccanlint/compulsory_tests/objects_build.c index 32546e59..91bc12ce 100644 --- a/tools/ccanlint/compulsory_tests/objects_build.c +++ b/tools/ccanlint/compulsory_tests/objects_build.c @@ -38,8 +38,8 @@ static void check_objs_build(struct manifest *m, char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name); i->compiled = maybe_temp_file(m, "", keep, fullfile); - if (!compile_object(score, fullfile, ccan_dir, "", i->compiled, - &output)) { + if (!compile_object(score, fullfile, ccan_dir, compiler, cflags, + i->compiled, &output)) { talloc_free(i->compiled); score_file_error(score, i, 0, "Compiling object files:\n%s", diff --git a/tools/ccanlint/tests/examples_compile.c b/tools/ccanlint/tests/examples_compile.c index 578805fe..67412df3 100644 --- a/tools/ccanlint/tests/examples_compile.c +++ b/tools/ccanlint/tests/examples_compile.c @@ -121,7 +121,8 @@ static bool compile(const void *ctx, file->compiled = maybe_temp_file(ctx, "", keep, file->fullname); if (!compile_and_link(ctx, file->fullname, ccan_dir, obj_list(m, file), - "", lib_list(m), file->compiled, output)) { + compiler, cflags, + lib_list(m), file->compiled, output)) { /* Don't keep failures. */ if (keep) unlink(file->compiled); diff --git a/tools/ccanlint/tests/tests_compile.c b/tools/ccanlint/tests/tests_compile.c index 473d55f8..13a8f0db 100644 --- a/tools/ccanlint/tests/tests_compile.c +++ b/tools/ccanlint/tests/tests_compile.c @@ -66,10 +66,12 @@ static bool compile(const void *ctx, bool link_with_module, bool keep, char **output) { + char *f = talloc_asprintf(ctx, "%s%s", + fail ? "-DFAIL " : "", cflags); + file->compiled = maybe_temp_file(ctx, "", keep, file->fullname); if (!compile_and_link(ctx, file->fullname, ccan_dir, - obj_list(m, link_with_module), - fail ? "-DFAIL" : "", + obj_list(m, link_with_module), compiler, f, lib_list(m), file->compiled, output)) { talloc_free(file->compiled); return false; diff --git a/tools/ccanlint/tests/tests_compile_coverage.c b/tools/ccanlint/tests/tests_compile_coverage.c index 508057fa..ea46e79e 100644 --- a/tools/ccanlint/tests/tests_compile_coverage.c +++ b/tools/ccanlint/tests/tests_compile_coverage.c @@ -38,7 +38,7 @@ static bool build_module_objs_with_coverage(struct manifest *m, bool keep, char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name); i->cov_compiled = maybe_temp_file(m, "", keep, fullfile); - if (!compile_object(m, fullfile, ccan_dir, "", + if (!compile_object(m, fullfile, ccan_dir, compiler, cflags, i->cov_compiled, &err)) { score_file_error(score, i, 0, "%s", err); talloc_free(i->cov_compiled); @@ -93,11 +93,12 @@ static char *cov_compile(const void *ctx, bool keep) { char *output; + char *f = talloc_asprintf(ctx, "%s %s", cflags, COVERAGE_CFLAGS); file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname); if (!compile_and_link(ctx, file->fullname, ccan_dir, obj_list(m, modobjs), - COVERAGE_CFLAGS, + compiler, f, lib_list(m), file->cov_compiled, &output)) { talloc_free(file->cov_compiled); file->cov_compiled = NULL; diff --git a/tools/ccanlint/tests/tests_helpers_compile.c b/tools/ccanlint/tests/tests_helpers_compile.c index 67ca8d8a..7aad1dcb 100644 --- a/tools/ccanlint/tests/tests_helpers_compile.c +++ b/tools/ccanlint/tests/tests_helpers_compile.c @@ -27,7 +27,7 @@ static bool compile(struct manifest *m, char **output) { cfile->compiled = maybe_temp_file(m, ".o", keep, cfile->fullname); - return compile_object(m, cfile->fullname, ccan_dir, "", + return compile_object(m, cfile->fullname, ccan_dir, compiler, cflags, cfile->compiled, output); } diff --git a/tools/compile.c b/tools/compile.c index 3ab1afaa..ec7e2147 100644 --- a/tools/compile.c +++ b/tools/compile.c @@ -22,25 +22,28 @@ char *link_objects(const void *ctx, const char *basename, bool in_pwd, /* Compile a single C file to an object file. */ bool compile_object(const void *ctx, const char *cfile, const char *ccandir, - const char *extra_cflags, + const char *compiler, + const char *cflags, const char *outfile, char **output) { if (compile_verbose) printf("Compiling %s\n", outfile); - return run_command(ctx, NULL, output, CCAN_COMPILER " " CCAN_CFLAGS - " -I%s %s -c -o %s %s", - ccandir, extra_cflags, outfile, cfile); + return run_command(ctx, NULL, output, + "%s %s -I%s -c -o %s %s", + compiler, cflags, ccandir, outfile, cfile); } /* Compile and link single C file, with object files. * Returns false on failure. */ bool compile_and_link(const void *ctx, const char *cfile, const char *ccandir, - const char *objs, const char *extra_cflags, + const char *objs, const char *compiler, + const char *cflags, const char *libs, const char *outfile, char **output) { if (compile_verbose) printf("Compiling and linking %s\n", outfile); - return run_command(ctx, NULL, output, CCAN_COMPILER " " CCAN_CFLAGS - " -I%s %s -o %s %s %s %s", - ccandir, extra_cflags, outfile, cfile, objs, libs); + return run_command(ctx, NULL, output, + "%s %s -I%s -o %s %s %s %s", + compiler, cflags, + ccandir, outfile, cfile, objs, libs); } diff --git a/tools/depends.c b/tools/depends.c index ee39a12f..ca1b6fc8 100644 --- a/tools/depends.c +++ b/tools/depends.c @@ -62,7 +62,8 @@ static char *compile_info(const void *ctx, const char *dir) *strrchr(ccandir, '/') = '\0'; compiled = maybe_temp_file(ctx, "", false, "info"); - if (compile_and_link(ctx, info_c_file, ccandir, "", "", "", + if (compile_and_link(ctx, info_c_file, ccandir, "", + CCAN_COMPILER, CCAN_CFLAGS, "", compiled, &output)) return compiled; return NULL; diff --git a/tools/tools.h b/tools/tools.h index a4c4cfa0..0a6ef8cc 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -58,11 +58,13 @@ char *link_objects(const void *ctx, const char *basename, bool in_pwd, const char *objs, char **errmsg); /* Compile a single C file to an object file. Returns false if fails. */ bool compile_object(const void *ctx, const char *cfile, const char *ccandir, - const char *extra_cflags, + const char *compiler, + const char *cflags, const char *outfile, char **output); /* Compile and link single C file, with object files, libs, etc. */ bool compile_and_link(const void *ctx, const char *cfile, const char *ccandir, - const char *objs, const char *extra_cflags, + const char *objs, + const char *compiler, const char *cflags, const char *libs, const char *outfile, char **output); /* If in_pwd is false, return a file int temp_dir, otherwise a local file. */