]> git.ozlabs.org Git - ccan/commitdiff
ccanlint: use familiar names for temporary files, show them with -vv.
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 10 Sep 2010 03:06:26 +0000 (12:36 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Fri, 10 Sep 2010 03:06:26 +0000 (12:36 +0930)
tools/ccanlint/compulsory_tests/build.c
tools/ccanlint/compulsory_tests/check_build.c
tools/ccanlint/compulsory_tests/compile_test_helpers.c
tools/compile.c
tools/depends.c
tools/tools.c
tools/tools.h

index a8f7bcb6aac6303344837c59eab1648f1e84fea2..c78e500b238f4e0953e7084f46a36deba03c0f44 100644 (file)
@@ -44,7 +44,7 @@ static void *do_build(struct manifest *m,
                build.total_score = 0;
                return NULL;
        }
-       filename = link_objects(m, obj_list(m), &err);
+       filename = link_objects(m, m->basename, false, obj_list(m), &err);
        if (filename && keep) {
                char *realname = talloc_asprintf(m, "%s.o", m->dir);
                /* We leave this object file around, all built. */
index 5bdec5533f5920b09f54b42f3197e6fef9cd5bd5..42eda13ae56aede90e2fd9df85927e9a1260129c 100644 (file)
@@ -51,9 +51,10 @@ static void *check_use_build(struct manifest *m,
 {
        char *contents;
        char *tmpfile;
+       char *basename = talloc_asprintf(m, "%s/example.c", m->dir);
        int fd;
 
-       tmpfile = temp_file(m, ".c");
+       tmpfile = maybe_temp_file(m, ".c", keep, basename);
 
        fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0600);
        if (fd < 0)
@@ -74,7 +75,8 @@ static void *check_use_build(struct manifest *m,
        close(fd);
 
        return compile_and_link(m, tmpfile, ccan_dir, obj_list(m), "",
-                               lib_list(m), temp_file(m, ""));
+                               lib_list(m),
+                               maybe_temp_file(m, "", keep, tmpfile));
 }
 
 static const char *describe_use_build(struct manifest *m, void *check_result)
index 94daaa1bf4bbe1128d234cf767e228f08bd1680b..207c0b2d346378bbdf0a79b9755484e1df18f524 100644 (file)
@@ -25,7 +25,7 @@ static char *compile(struct manifest *m,
                     bool keep,
                     struct ccan_file *cfile)
 {
-       cfile->compiled = maybe_temp_file(m, "", keep, cfile->fullname);
+       cfile->compiled = maybe_temp_file(m, ".o", keep, cfile->fullname);
        return compile_object(m, cfile->fullname, ccan_dir, "",
                              cfile->compiled);
 }
index 7ff04225dc3e42a7af2dc170711c804498529baa..6707c4a68c99af055efe47a5428d6a6cf74faf5a 100644 (file)
@@ -5,9 +5,10 @@
 bool compile_verbose = false;
 
 /* Compile multiple object files into a single.  Returns errmsg if fails. */
-char *link_objects(const void *ctx, const char *objs, char **errmsg)
+char *link_objects(const void *ctx, const char *basename, bool in_pwd,
+                  const char *objs, char **errmsg)
 {
-       char *file = temp_file(ctx, ".o");
+       char *file = maybe_temp_file(ctx, ".o", in_pwd, basename);
 
        if (compile_verbose)
                printf("Linking objects into %s\n", file);
index 6ac31cdc5ebce5e973c1b34b9c506c3672fa7ada..e2666d97f778ae2007127561b22415117cba9875 100644 (file)
@@ -48,7 +48,7 @@ static char *compile_info(const void *ctx, const char *dir)
        if (!info)
                return NULL;
 
-       info_c_file = temp_file(ctx, ".c");
+       info_c_file = maybe_temp_file(ctx, ".c", false, "_info");
        fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600);
        if (fd < 0)
                return NULL;
@@ -61,7 +61,7 @@ static char *compile_info(const void *ctx, const char *dir)
        ccandir = talloc_dirname(ctx, dir);
        *strrchr(ccandir, '/') = '\0';
 
-       compiled = temp_file(ctx, "");
+       compiled = maybe_temp_file(ctx, "", false, "info");
        if (compile_and_link(ctx, info_c_file, ccandir, "", "", "",
                             compiled))
                return NULL;
index a4944bd1f8988b43cf63e63ac42460a0d1643529..d27db719e973034183a8fb7a5e837b2a481cf6e7 100644 (file)
 #include <errno.h>
 #include <err.h>
 #include <unistd.h>
+#include <assert.h>
 #include "tools.h"
 
 static char *tmpdir = NULL;
-static unsigned int count;
 bool tools_verbose = false;
 
 /* Ten minutes. */
@@ -210,28 +210,37 @@ char *temp_dir(const void *ctx)
        return tmpdir;
 }
 
-char *temp_file(const void *ctx, const char *extension)
-{
-       char *f = talloc_asprintf(ctx, "%s/%u%s",
-                                 temp_dir(ctx), count++, extension);
-       if (tools_verbose)
-               printf("Created temporary file %s\n", f);
-       return f;
-}
-
 char *maybe_temp_file(const void *ctx, const char *extension, bool keep,
                      const char *srcname)
 {
        size_t baselen;
-       char *f;
+       char *f, *suffix = talloc_strdup(ctx, "");
+       struct stat st;
+       unsigned int count = 0;
 
        if (!keep)
-               return temp_file(ctx, extension);
+               srcname = talloc_basename(ctx, srcname);
+       else
+               assert(srcname[0] == '/');
+
+       if (strrchr(srcname, '.'))
+               baselen = strrchr(srcname, '.') - srcname;
+       else
+               baselen = strlen(srcname);
+
+       do {
+               f = talloc_asprintf(ctx, "%s/%.*s%s%s",
+                                   keep ? "" : temp_dir(ctx),
+                                   baselen, srcname,
+                                   suffix, extension);
+               talloc_free(suffix);
+               suffix = talloc_asprintf(ctx, "-%u", ++count);
+       } while (!keep && lstat(f, &st) == 0);
 
-       baselen = strrchr(srcname, '.') - srcname;
-       f = talloc_asprintf(ctx, "%.*s%s", baselen, srcname, extension);
        if (tools_verbose)
                printf("Creating file %s\n", f);
+
+       talloc_free(suffix);
        return f;
 }
 
index a7a25c99cd756e0c68dedc9da2247c7be0ad7371..9bc7e1fcfda7e2f1148d0db40f545ecb3d5a362f 100644 (file)
@@ -35,7 +35,6 @@ char *run_command(const void *ctx, unsigned int *time_ms, const char *fmt, ...);
 char *run_with_timeout(const void *ctx, const char *cmd,
                       bool *ok, unsigned *timeout_ms);
 char *temp_dir(const void *ctx);
-char *temp_file(const void *ctx, const char *extension);
 bool move_file(const char *oldname, const char *newname);
 
 /* From compile.c.
@@ -46,7 +45,8 @@ bool move_file(const char *oldname, const char *newname);
 /* If set, say what we're compiling to. */
 extern bool compile_verbose;
 /* Compile multiple object files into a single. */
-char *link_objects(const void *ctx, const char *objs, char **errmsg);
+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 errmsg if fails. */
 char *compile_object(const void *ctx, const char *cfile, const char *ccandir,
                     const char *extra_cflags,
@@ -57,8 +57,8 @@ char *compile_and_link(const void *ctx, const char *cfile, const char *ccandir,
                       const char *objs, const char *extra_cflags,
                       const char *libs, const char *outfile);
 
-/* If keep is false, return a temporary file.  Otherwise, base it on srcname */
-char *maybe_temp_file(const void *ctx, const char *extension, bool keep,
+/* If in_pwd is false, return a file int temp_dir, otherwise a local file. */
+char *maybe_temp_file(const void *ctx, const char *extension, bool in_pwd,
                      const char *srcname);
 
 /* Default wait for run_command.  Should never time out. */