]> git.ozlabs.org Git - ccan/commitdiff
Now we compile everything into the tmp dir.
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 25 Sep 2009 08:21:06 +0000 (17:51 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Fri, 25 Sep 2009 08:21:06 +0000 (17:51 +0930)
tools/ccanlint/tests/build.c
tools/ccanlint/tests/build_objs.c
tools/ccanlint/tests/check_includes_build.c
tools/ccanlint/tests/compile_test_helpers.c
tools/ccanlint/tests/compile_tests.c
tools/compile.c
tools/tools.c
tools/tools.h

index 0a5e11e66fbca66281c138c9de084a6ea592525b..1b3d878928b53ca738fba2c13b2f2f394a508d19 100644 (file)
@@ -26,23 +26,32 @@ static char *obj_list(const struct manifest *m)
        char *list = talloc_strdup(m, "");
        struct ccan_file *i;
 
-       /* Object from all the C files. */
+       /* Objects from all the C files. */
        list_for_each(&m->c_files, i, list)
-               list = talloc_asprintf_append(list, "%.*s.o ",
-                                             strlen(i->name) - 2, i->name);
+               list = talloc_asprintf_append(list, "%s ", i->compiled);
 
        return list;
 }
 
-/* We leave this object file around after ccanlint runs, all built. */
 static void *do_build(struct manifest *m)
 {
+       char *filename, *err;
+
        if (list_empty(&m->c_files)) {
                /* No files?  No score, but we "pass". */
                build.total_score = 0;
                return NULL;
        }
-       return run_command(m, "ld -r -o ../%s.o %s", m->basename, obj_list(m));
+       filename = link_objects(m, obj_list(m), &err);
+       if (filename) {
+               char *realname = talloc_asprintf(m, "../%s.o", m->basename);
+               /* We leave this object file around, all built. */
+               if (rename(filename, realname) != 0)
+                       return talloc_asprintf(m, "Failed to rename %s to %s",
+                                              filename, realname);
+               return NULL;
+       }
+       return err;
 }
 
 static const char *describe_build(struct manifest *m, void *check_result)
index e0ea222350bc981ed3075270a0db3ee7459cf1f1..2f189e859f25a8b523505609cafd31c429e3a443 100644 (file)
@@ -21,50 +21,31 @@ static const char *can_build(struct manifest *m)
        return NULL;
 }
 
-static bool compile_obj(struct ccan_file *c_file, char *objfile, char **report)
-{
-       char *err;
-
-       err = compile_object(objfile, objfile, c_file->name);
-       if (err) {
-               if (*report)
-                       *report = talloc_append_string(*report, err);
-               else
-                       *report = err;
-               return false;
-       }
-       return true;
-}
-
-static int cleanup_obj(char *objfile)
-{
-       unlink(objfile);
-       return 0;
-}
-
 static void *check_objs_build(struct manifest *m)
 {
        char *report = NULL;
        struct ccan_file *i;
 
-       /* One point for each obj file. */
-       list_for_each(&m->c_files, i, list)
-               build_objs.total_score++;
-
        list_for_each(&m->c_files, i, list) {
-               char *objfile = talloc_strdup(m, i->name);
-               objfile[strlen(objfile)-1] = 'o';
+               char *err;
+
+               /* One point for each obj file. */
+               build_objs.total_score++;
 
-               if (compile_obj(i, objfile, &report))
-                       talloc_set_destructor(objfile, cleanup_obj);
+               i->compiled = compile_object(m, i->name, &err);
+               if (!i->compiled) {
+                       if (report)
+                               report = talloc_append_string(report, err);
+                       else
+                               report = err;
+               }
        }
        return report;
 }
 
 static const char *describe_objs_build(struct manifest *m, void *check_result)
 {
-       return talloc_asprintf(check_result, 
-                              "%s", (char *)check_result);
+       return check_result;
 }
 
 struct ccanlint build_objs = {
index d9c6f495be86f5b48a543a12ca3a5c9de7eb5b1f..7e0ab945f290dfaaa83e373b3255520a4e61fc20 100644 (file)
@@ -25,11 +25,10 @@ static const char *can_build(struct manifest *m)
 static void *check_includes_build(struct manifest *m)
 {
        char *contents;
-       char *tmpfile, *objfile;
+       char *tmpfile, *err;
        int fd;
 
        tmpfile = temp_file(m, ".c");
-       objfile = temp_file(m, ".o");
 
        fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0600);
        if (fd < 0)
@@ -44,7 +43,9 @@ static void *check_includes_build(struct manifest *m)
        }
        close(fd);
 
-       return compile_object(m, objfile, tmpfile);
+       if (compile_object(m, tmpfile, &err))
+               return NULL;
+       return err;
 }
 
 static const char *describe_includes_build(struct manifest *m,
index 2aa8d8639800a379bad6d42f70652b09b5393cda..de8d4632c9fbea9aa9ff992a2b0a0eb78a879d91 100644 (file)
@@ -21,17 +21,14 @@ static const char *can_build(struct manifest *m)
        return NULL;
 }
 
-static char *objname(const void *ctx, const char *cfile)
+static char *compile(struct manifest *m, struct ccan_file *cfile)
 {
-       return talloc_asprintf(ctx, "%.*s.o ", strlen(cfile) - 2, cfile);
-}
-
-static char *compile(struct manifest *m, const char *cfile)
-{
-       char *obj;
+       char *err;
 
-       obj = objname(m, cfile);
-       return compile_object(m, obj, cfile);
+       cfile->compiled = compile_object(m, cfile->name, &err);
+       if (cfile->compiled)
+               return NULL;
+       return err;
 }
 
 static void *do_compile_test_helpers(struct manifest *m)
@@ -41,7 +38,7 @@ static void *do_compile_test_helpers(struct manifest *m)
 
        list_for_each(&m->other_test_c_files, i, list) {
                compile_tests.total_score++;
-               cmdout = compile(m, i->name);
+               cmdout = compile(m, i);
                if (cmdout)
                        return talloc_asprintf(m,
                                               "Failed to compile helper C"
index 1d24f655a1ede9d952c5a86c4067cc94477c9af2..c8f303b797844f2141173c325778243fa855d6be 100644 (file)
@@ -23,13 +23,18 @@ static const char *can_build(struct manifest *m)
 
 static char *obj_list(const struct manifest *m, bool link_with_module)
 {
-       char *list = talloc_strdup(m, "../tap.o");
+       char *list;
        struct ccan_file *i;
 
+       /* We expect to be linked with tap, unless that's us. */
+       if (!streq(m->basename, "tap"))
+               list = talloc_strdup(m, "../tap.o");
+       else
+               list = talloc_strdup(m, "");
+
        /* Objects from any other C files. */
        list_for_each(&m->other_test_c_files, i, list)
-               list = talloc_asprintf_append(list, " %.*s.o",
-                                             strlen(i->name) - 2, i->name);
+               list = talloc_asprintf_append(list, " %s", i->compiled);
 
        if (link_with_module)
                list = talloc_asprintf_append(list, " ../%s.o", m->basename);
@@ -118,7 +123,7 @@ static void *do_compile_tests(struct manifest *m)
 
        list_for_each(&m->compile_fail_tests, i, list) {
                compile_tests.total_score++;
-               cmdout = compile(list, m, i, true, false);
+               cmdout = compile(list, m, i, false, false);
                if (cmdout) {
                        res = talloc(list, struct compile_tests_result);
                        res->filename = i->name;
@@ -126,7 +131,7 @@ static void *do_compile_tests(struct manifest *m)
                        res->output = talloc_steal(res, cmdout);
                        list_add_tail(list, &res->list);
                } else {
-                       cmdout = compile(list, m, i, false, false);
+                       cmdout = compile(list, m, i, true, false);
                        if (!cmdout) {
                                res = talloc(list, struct compile_tests_result);
                                res->filename = i->name;
index 5ec41228f59e51de3d2b5819a34a0eede4fcfa22..910b9462b1a3d452245db88deacf509cdb5e2bfe 100644 (file)
@@ -3,15 +3,29 @@
 #include <stdlib.h>
 
 /* Compile multiple object files into a single.  Returns errmsg if fails. */
-char *link_objects(const void *ctx, const char *outfile, const char *objs)
+char *link_objects(const void *ctx, const char *objs, char **errmsg)
 {
-       return run_command(ctx, "cc " CFLAGS " -c -o %s %s", outfile, objs);
+       char *file = temp_file(ctx, ".o");
+
+       *errmsg = run_command(ctx, "ld -r -o %s %s", file, objs);
+       if (*errmsg) {
+               talloc_free(file);
+               return NULL;
+       }
+       return file;
 }
 
 /* Compile a single C file to an object file.  Returns errmsg if fails. */
-char *compile_object(const void *ctx, const char *outfile, const char *cfile)
+char *compile_object(const void *ctx, const char *cfile, char **errmsg)
 {
-       return run_command(ctx, "cc " CFLAGS " -c -o %s %s", outfile, cfile);
+       char *file = temp_file(ctx, ".o");
+
+       *errmsg = run_command(ctx, "cc " CFLAGS " -c -o %s %s", file, cfile);
+       if (*errmsg) {
+               talloc_free(file);
+               return NULL;
+       }
+       return file;
 }
 
 /* Compile and link single C file, with object files.
index 2da37234e2dd034ea8d5dd0f108eadcb8caf86bf..4fbc6857c2b7e72fb1300e5102cc89289dda9f7c 100644 (file)
@@ -61,7 +61,7 @@ char *run_command(const void *ctx, const char *fmt, ...)
 
        /* Ensure stderr gets to us too. */
        cmd = talloc_asprintf_append(cmd, " 2>&1");
-       
+
        pipe = popen(cmd, "r");
        if (!pipe)
                return talloc_asprintf(ctx, "Failed to run '%s'", cmd);
@@ -79,7 +79,7 @@ static int unlink_all(char *dir)
 {
        char cmd[strlen(dir) + sizeof("rm -rf ")];
        sprintf(cmd, "rm -rf %s", dir);
-//     system(cmd);
+       system(cmd);
        return 0;
 }
 
index c311809eed446a5dabaad171fe87eb46f3743b9e..6f732378d331d51bae5e2820934df6cc728acb95 100644 (file)
@@ -30,13 +30,16 @@ char *talloc_getcwd(const void *ctx);
 char *run_command(const void *ctx, const char *fmt, ...);
 char *temp_file(const void *ctx, const char *extension);
 
-/* From compile.c. */
-/* Compile multiple object files into a single.  Returns errmsg if fails. */
-char *link_objects(const void *ctx, const char *outfile, const char *objs);
+/* From compile.c.
+ *
+ * These all compile into a temporary dir, and return the filename.
+ * On failure they return NULL, and errmsg is set to compiler output.
+ */
+/* Compile multiple object files into a single. */
+char *link_objects(const void *ctx, 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 *outfile, const char *cfile);
-/* Compile and link single C file, with object files.
- * Returns name of result, or NULL (and fills in errmsg). */
+char *compile_object(const void *ctx, const char *cfile, char **errmsg);
+/* Compile and link single C file, with object files, libs, etc. */
 char *compile_and_link(const void *ctx, const char *cfile, const char *objs,
                       const char *extra_cflags, const char *libs,
                       char **errmsg);