]> git.ozlabs.org Git - ccan/commitdiff
ccanlint: -k should not pollute module directory.
authorRusty Russell <rusty@rustcorp.com.au>
Sun, 9 Jan 2011 02:49:15 +0000 (13:19 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Sun, 9 Jan 2011 02:49:15 +0000 (13:19 +1030)
It leads to numerous problems, such as the next ccanlint getting confused
trying to compile examples, and "-k examples_compile -k examples_exist"
giving bogus errors.

So instead we leave the temporary dir lying around and delete
individual files which aren't marked "keep".

tools/ccanlint/ccanlint.c
tools/ccanlint/tests/examples_compile.c
tools/ccanlint/tests/tests_coverage.c
tools/tools.c
tools/tools.h

index 8acad9329e4c7280d0fef58eb8b833cf9ca7cd72..42da05afad85719a09ebe66f9bb0d9849608d96b 100644 (file)
@@ -292,12 +292,21 @@ static void init_tests(void)
        }
 }
 
+static int show_tmpdir(char *dir)
+{
+       printf("You can find ccanlint working files in '%s'\n", dir);
+       return 0;
+}
+
 static char *keep_test(const char *testname, void *unused)
 {
        struct ccanlint *i = find_test(testname);
        if (!i)
                errx(1, "No test %s to --keep", testname);
        i->keep_results = true;
+
+       /* Don't automatically destroy temporary dir. */
+       talloc_set_destructor(temp_dir(NULL), show_tmpdir);
        return NULL;
 }
 
@@ -495,6 +504,10 @@ int main(int argc, char *argv[])
                           " of CCAN modules.",
                           "This usage message");
 
+       /* We move into temporary directory, so gcov dumps its files there. */
+       if (chdir(temp_dir(talloc_autofree_context())) != 0)
+               err(1, "Error changing to %s temporary dir", temp_dir(NULL));
+
        opt_parse(&argc, argv, opt_log_stderr_exit);
 
        if (dir[0] != '/')
@@ -508,10 +521,6 @@ int main(int argc, char *argv[])
        if (verbose >= 4)
                tools_verbose = true;
 
-       /* We move into temporary directory, so gcov dumps its files there. */
-       if (chdir(temp_dir(talloc_autofree_context())) != 0)
-               err(1, "Error changing to %s temporary dir", temp_dir(NULL));
-
        m = get_manifest(talloc_autofree_context(), dir);
 
        /* Create a symlink from temp dir back to src dir's test directory. */
index 1ccc620248cadd3b0723de8ee01fcbef8732e687..8ab806438f1b4aab25e033cda63fca6915017e0c 100644 (file)
@@ -122,6 +122,9 @@ static bool compile(const void *ctx,
        if (!compile_and_link(ctx, file->fullname, ccan_dir,
                              obj_list(m, file),
                              "", lib_list(m), file->compiled, output)) {
+               /* Don't keep failures. */
+               if (keep)
+                       unlink(file->compiled);
                talloc_free(file->compiled);
                file->compiled = NULL;
                return false;
index 7943071b76634bfc50ce88e962ba8dfffc4480e6..ec9774dae53650778aaef9c02688e0b01e02c26e 100644 (file)
@@ -127,15 +127,26 @@ static void do_run_coverage_tests(struct manifest *m,
                                  unsigned int *timeleft, struct score *score)
 {
        struct ccan_file *i;
-       char *cmdout;
+       char *cmdout, *outdir;
        char *covcmd;
        bool full_gcov = (verbose > 1);
        struct list_head *list;
 
        /* This tells gcov where we put those .gcno files. */
+       outdir = talloc_dirname(score, m->info_file->compiled);
        covcmd = talloc_asprintf(m, "gcov %s -o %s",
                                 full_gcov ? "" : "-n",
-                                talloc_dirname(score, m->info_file->compiled));
+                                outdir);
+
+       /* Unlink these files afterwards. */
+       if (!keep) {
+               talloc_set_destructor(talloc_asprintf(score,
+                                                     "%s/run.gcno", outdir),
+                                     unlink_file_destructor);
+               talloc_set_destructor(talloc_asprintf(score,
+                                                     "%s/run.gcda", outdir),
+                                     unlink_file_destructor);
+       }
 
        /* Run them all. */
        foreach_ptr(list, &m->run_tests, &m->api_tests) {
index eeaff6f17f644233f7aa1772ab27c69aad69f81f..dd7243c2c6a7ff6d213a291642e700a661e90627 100644 (file)
@@ -210,6 +210,12 @@ char *temp_dir(const void *ctx)
        return tmpdir;
 }
 
+int unlink_file_destructor(char *filename)
+{
+       unlink(filename);
+       return 0;
+}
+
 char *maybe_temp_file(const void *ctx, const char *extension, bool keep,
                      const char *srcname)
 {
@@ -218,11 +224,7 @@ char *maybe_temp_file(const void *ctx, const char *extension, bool keep,
        struct stat st;
        unsigned int count = 0;
 
-       if (!keep)
-               srcname = talloc_basename(ctx, srcname);
-       else
-               assert(srcname[0] == '/');
-
+       srcname = talloc_basename(ctx, srcname);
        if (strrchr(srcname, '.'))
                baselen = strrchr(srcname, '.') - srcname;
        else
@@ -230,7 +232,7 @@ char *maybe_temp_file(const void *ctx, const char *extension, bool keep,
 
        do {
                f = talloc_asprintf(ctx, "%s/%.*s%s%s",
-                                   keep ? "" : temp_dir(ctx),
+                                   temp_dir(ctx),
                                    baselen, srcname,
                                    suffix, extension);
                talloc_free(suffix);
@@ -238,7 +240,10 @@ char *maybe_temp_file(const void *ctx, const char *extension, bool keep,
        } while (lstat(f, &st) == 0);
 
        if (tools_verbose)
-               printf("Creating file %s\n", f);
+               printf("Creating %sfile %s\n", keep ? "" : "temporary ", f);
+
+       if (!keep)
+               talloc_set_destructor(f, unlink_file_destructor);
 
        talloc_free(suffix);
        return f;
index d39f01977683ef605798da2a7c6fdb40ed907a9c..a4c4cfa06b8f3bd79ff3b5e7ed274143b8634c51 100644 (file)
@@ -72,4 +72,7 @@ char *maybe_temp_file(const void *ctx, const char *extension, bool in_pwd,
 /* Default wait for run_command.  Should never time out. */
 extern const unsigned int default_timeout_ms;
 
+/* Talloc destructor which unlinks file. */
+int unlink_file_destructor(char *filename);
+
 #endif /* CCAN_TOOLS_H */