#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);
 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)
 {
                                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;
                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),
 
 /* 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 */
 
                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,
 
                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 {
 
                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;
 
                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",
 
        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);
 
                    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;
 
                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);
                         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;
 
                    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);
 }
 
 
 
 /* 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);
 }
 
        *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;
 
                   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. */