]> git.ozlabs.org Git - ccan/blobdiff - tools/configurator/configurator.c
tools/configurator: support --header-file if we don't want to write to stdout.
[ccan] / tools / configurator / configurator.c
index b8cb6f7a7cd4c42f2fd9701bd1d84a71ceba2ee0..dae15446e67417ef8f02d3d07b259dbe33925963 100644 (file)
@@ -32,6 +32,7 @@
 #include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #ifdef _MSC_VER
 #define popen _popen
@@ -65,6 +66,7 @@
 
 static const char *progname = "";
 static int verbose;
+static bool like_a_libtool = false;
 
 struct test {
        const char *name;
@@ -437,6 +439,20 @@ static void c12r_errx(int eval, const char *fmt, ...)
        exit(eval);
 }
 
+static void start_test(const char *what, const char *why)
+{
+       if (like_a_libtool) {
+               printf("%s%s... ", what, why);
+               fflush(stdout);
+       }
+}
+
+static void end_test(bool result)
+{
+       if (like_a_libtool)
+               printf("%s\n", result ? "yes" : "no");
+}
+
 static size_t fcopy(FILE *fsrc, FILE *fdst)
 {
        char buffer[BUFSIZ];
@@ -634,6 +650,7 @@ static bool run_test(const char *cmd, struct test *test)
                        printf("Extra link line: %s", newcmd);
        }
 
+       start_test("checking for ", test->name);
        output = run(newcmd, &status);
 
        free(newcmd);
@@ -666,6 +683,7 @@ static bool run_test(const char *cmd, struct test *test)
                test->answer = (status == 0);
        }
        test->done = true;
+       end_test(test->answer);
 
        if (test->answer && test->overrides) {
                struct test *override = find_test(test->overrides);
@@ -684,13 +702,16 @@ int main(int argc, const char *argv[])
        const char *outflag = DEFAULT_OUTPUT_EXE_FLAG;
        const char *configurator_cc = NULL;
        const char *orig_cc;
+       const char *varfile = NULL;
+       const char *headerfile = NULL;
+       FILE *outf;
 
        if (argc > 0)
                progname = argv[0];
 
        while (argc > 1) {
                if (strcmp(argv[1], "--help") == 0) {
-                       printf("Usage: configurator [-v] [-O<outflag>] [--configurator-cc=<compiler-for-tests>] [<compiler> <flags>...]\n"
+                       printf("Usage: configurator [-v] [--var-file=<filename>] [-O<outflag>] [--configurator-cc=<compiler-for-tests>] [--autotools-style] [<compiler> <flags>...]\n"
                               "  <compiler> <flags> will have \"<outflag> <outfile> <infile.c>\" appended\n"
                               "Default: %s %s %s\n",
                               DEFAULT_COMPILER, DEFAULT_FLAGS,
@@ -719,6 +740,18 @@ int main(int argc, const char *argv[])
                        configurator_cc = argv[1] + 18;
                        argc--;
                        argv++;
+               } else if (strncmp(argv[1], "--var-file=", 11) == 0) {
+                       varfile = argv[1] + 11;
+                       argc--;
+                       argv++;
+               } else if (strcmp(argv[1], "--autotools-style") == 0) {
+                       like_a_libtool = true;
+                       argc--;
+                       argv++;
+               } else if (strncmp(argv[1], "--header-file=", 14) == 0) {
+                       headerfile = argv[1] + 14;
+                       argc--;
+                       argv++;
                } else {
                        break;
                }
@@ -732,6 +765,11 @@ int main(int argc, const char *argv[])
                argv[1] = configurator_cc;
 
        cmd = connect_args(argv, outflag, OUTPUT_FILE " " INPUT_FILE);
+       if (like_a_libtool) {
+               start_test("Making autoconf users comfortable", "");
+               sleep(1);
+               end_test(1);
+       }
        for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
                run_test(cmd, &tests[i]);
        free(cmd);
@@ -739,21 +777,56 @@ int main(int argc, const char *argv[])
        remove(OUTPUT_FILE);
        remove(INPUT_FILE);
 
-       printf("/* Generated by CCAN configurator */\n"
+       if (varfile) {
+               FILE *vars;
+
+               if (strcmp(varfile, "-") == 0)
+                       vars = stdout;
+               else {
+                       start_test("Writing variables to ", varfile);
+                       vars = fopen(varfile, "a");
+                       if (!vars)
+                               c12r_err(2, "Could not open %s", varfile);
+               }
+               for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
+                       fprintf(vars, "%s=%u\n", tests[i].name, tests[i].answer);
+               if (vars != stdout) {
+                       if (fclose(vars) != 0)
+                               c12r_err(2, "Closing %s", varfile);
+                       end_test(1);
+               }
+       }
+
+       if (headerfile) {
+               start_test("Writing header to ", headerfile);
+               outf = fopen(headerfile, "w");
+               if (!outf)
+                       c12r_err(2, "Could not open %s", headerfile);
+       } else
+               outf = stdout;
+
+       fprintf(outf, "/* Generated by CCAN configurator */\n"
               "#ifndef CCAN_CONFIG_H\n"
               "#define CCAN_CONFIG_H\n");
-       printf("#ifndef _GNU_SOURCE\n");
-       printf("#define _GNU_SOURCE /* Always use GNU extensions. */\n");
-       printf("#endif\n");
-       printf("#define CCAN_COMPILER \"%s\"\n", orig_cc);
+       fprintf(outf, "#ifndef _GNU_SOURCE\n");
+       fprintf(outf, "#define _GNU_SOURCE /* Always use GNU extensions. */\n");
+       fprintf(outf, "#endif\n");
+       fprintf(outf, "#define CCAN_COMPILER \"%s\"\n", orig_cc);
        cmd = connect_args(argv + 1, "", "");
-       printf("#define CCAN_CFLAGS \"%s\"\n", cmd);
+       fprintf(outf, "#define CCAN_CFLAGS \"%s\"\n", cmd);
        free(cmd);
-       printf("#define CCAN_OUTPUT_EXE_CFLAG \"%s\"\n\n", outflag);
+       fprintf(outf, "#define CCAN_OUTPUT_EXE_CFLAG \"%s\"\n\n", outflag);
        /* This one implies "#include <ccan/..." works, eg. for tdb2.h */
-       printf("#define HAVE_CCAN 1\n");
+       fprintf(outf, "#define HAVE_CCAN 1\n");
        for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
-               printf("#define %s %u\n", tests[i].name, tests[i].answer);
-       printf("#endif /* CCAN_CONFIG_H */\n");
+               fprintf(outf, "#define %s %u\n", tests[i].name, tests[i].answer);
+       fprintf(outf, "#endif /* CCAN_CONFIG_H */\n");
+
+       if (headerfile) {
+               if (fclose(outf) != 0)
+                       c12r_err(2, "Closing %s", headerfile);
+               end_test(1);
+       }
+
        return 0;
 }