From: Rusty Russell Date: Fri, 25 Sep 2009 08:21:06 +0000 (+0930) Subject: Now we compile everything into the tmp dir. X-Git-Url: https://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=687cde2a8c561b68f17609b615eb930ea02f5aac Now we compile everything into the tmp dir. --- diff --git a/tools/ccanlint/tests/build.c b/tools/ccanlint/tests/build.c index 0a5e11e6..1b3d8789 100644 --- a/tools/ccanlint/tests/build.c +++ b/tools/ccanlint/tests/build.c @@ -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) diff --git a/tools/ccanlint/tests/build_objs.c b/tools/ccanlint/tests/build_objs.c index e0ea2223..2f189e85 100644 --- a/tools/ccanlint/tests/build_objs.c +++ b/tools/ccanlint/tests/build_objs.c @@ -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 = { diff --git a/tools/ccanlint/tests/check_includes_build.c b/tools/ccanlint/tests/check_includes_build.c index d9c6f495..7e0ab945 100644 --- a/tools/ccanlint/tests/check_includes_build.c +++ b/tools/ccanlint/tests/check_includes_build.c @@ -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, diff --git a/tools/ccanlint/tests/compile_test_helpers.c b/tools/ccanlint/tests/compile_test_helpers.c index 2aa8d863..de8d4632 100644 --- a/tools/ccanlint/tests/compile_test_helpers.c +++ b/tools/ccanlint/tests/compile_test_helpers.c @@ -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" diff --git a/tools/ccanlint/tests/compile_tests.c b/tools/ccanlint/tests/compile_tests.c index 1d24f655..c8f303b7 100644 --- a/tools/ccanlint/tests/compile_tests.c +++ b/tools/ccanlint/tests/compile_tests.c @@ -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; diff --git a/tools/compile.c b/tools/compile.c index 5ec41228..910b9462 100644 --- a/tools/compile.c +++ b/tools/compile.c @@ -3,15 +3,29 @@ #include /* 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. diff --git a/tools/tools.c b/tools/tools.c index 2da37234..4fbc6857 100644 --- a/tools/tools.c +++ b/tools/tools.c @@ -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; } diff --git a/tools/tools.h b/tools/tools.h index c311809e..6f732378 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -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);