ccanlint: read config.h to get compilation flags at runtime.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 1 Mar 2011 05:38:57 +0000 (16:08 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 1 Mar 2011 05:38:57 +0000 (16:08 +1030)
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.

13 files changed:
tools/ccanlint/ccanlint.c
tools/ccanlint/ccanlint.h
tools/ccanlint/compulsory_tests/depends_build.c
tools/ccanlint/compulsory_tests/main_header_compiles.c
tools/ccanlint/compulsory_tests/module_links.c
tools/ccanlint/compulsory_tests/objects_build.c
tools/ccanlint/tests/examples_compile.c
tools/ccanlint/tests/tests_compile.c
tools/ccanlint/tests/tests_compile_coverage.c
tools/ccanlint/tests/tests_helpers_compile.c
tools/compile.c
tools/depends.c
tools/tools.h

index 1c7ee389dbc75728d35091e23a7d0c5a4463a469..231ae7c05c45e0b1050b834b096b48ddb634f23d 100644 (file)
@@ -31,6 +31,7 @@
 #include <ccan/talloc/talloc.h>
 #include <ccan/opt/opt.h>
 #include <ccan/foreach/foreach.h>
+#include <ccan/grab_file/grab_file.h>
 
 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),
index 41aaec9353ae9b1d09bf063f38b4e9cd24507881..33db1257eb126bbf9f59299175af05f13c5edaf6 100644 (file)
@@ -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 */
index bb1f6ca358a821d8f14b9172f8bd737cdc107c5f..00f75b3733301f75083a74bda1b128f2c909ed2f 100644 (file)
@@ -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,
index 296b0a3961c10b93d2109ada855d21d2a570fddf..99f3aa38855928e8611dc0967f4ff854a65e6ed9 100644 (file)
@@ -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 {
index 83b1c780e067046cc715fc479f7e79c64e22cdda..9c1bce79d9e608c31e6b1fefefc38a56fb72f9bc 100644 (file)
@@ -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;
index 32546e591732ba080ed439d89b34d53e7dfd86da..91bc12ce9b3965a37afa8c327d864d36145e649d 100644 (file)
@@ -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",
index 578805fe0c62a23db549a6e54e6fb0018980a15a..67412df34e184301cd3894e78080ab546cb8c480 100644 (file)
@@ -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);
index 473d55f821b668368a2aa673b16ded2096ca49e9..13a8f0dbaadfb92c22232a3ccf802d2cbc157e04 100644 (file)
@@ -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;
index 508057fa0d3b2d9ad61361211c01e92a94c48dc8..ea46e79e1bc3d8df15cac0aeb2889ca526ff4e81 100644 (file)
@@ -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;
index 67ca8d8ae332f192f568272909befb7ac2281aaa..7aad1dcb9291b48a96bce6688af2291dcc016fa9 100644 (file)
@@ -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);
 }
 
index 3ab1afaa3158cd44fa050da15892a355f476db6a..ec7e2147b239ddb568acee8a87973dc05757db85 100644 (file)
@@ -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);
 }
index ee39a12f134e4405c98f45f10cefe9fff829752a..ca1b6fc88b0f893814cb279f26b8ec4be3e77e9b 100644 (file)
@@ -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;
index a4c4cfa06b8f3bd79ff3b5e7ed274143b8634c51..0a6ef8ccc4ac2b9a54fba7bfe9223bb48c4da068 100644 (file)
@@ -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. */