]> git.ozlabs.org Git - ccan/blobdiff - tools/ccanlint/tests/tests_compile_coverage.c
tools: Consolidate gcov handling
[ccan] / tools / ccanlint / tests / tests_compile_coverage.c
index f41edc0bc6f36588a334a07c923707bbc10421d5..c8bc35d93b1ab1fe37320b18fb79f2bb7023ef0b 100644 (file)
@@ -1,6 +1,5 @@
 #include <tools/ccanlint/ccanlint.h>
 #include <tools/tools.h>
-#include <ccan/talloc/talloc.h>
 #include <ccan/str/str.h>
 #include <ccan/foreach/foreach.h>
 #include <sys/types.h>
 #include <err.h>
 #include <string.h>
 #include <ctype.h>
+#include "build.h"
+#include "tests_compile.h"
 
 /* Note: we already test safe_mode in run_tests.c */
 static const char *can_run_coverage(struct manifest *m)
 {
-       unsigned int timeleft = default_timeout_ms;
-       char *output;
-
-       if (!run_command(m, &timeleft, &output, "gcov -h"))
-               return talloc_asprintf(m, "No gcov support: %s", output);
-       return NULL;
-}
-
-static bool build_module_objs_with_coverage(struct manifest *m, bool keep,
-                                           struct score *score,
-                                           char **modobjs)
-{
-       struct ccan_file *i;
-
-       *modobjs = talloc_strdup(m, "");
-       list_for_each(&m->c_files, i, list) {
-               char *err;
-               char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
-
-               i->cov_compiled = maybe_temp_file(m, "", keep, fullfile);
-               if (!compile_object(m, fullfile, ccan_dir, "",
-                                   i->cov_compiled, &err)) {
-                       score_file_error(score, i, 0, err);
-                       talloc_free(i->cov_compiled);
-                       i->cov_compiled = NULL;
-                       return false;
-               }
-               *modobjs = talloc_asprintf_append(*modobjs,
-                                                 " %s", i->cov_compiled);
-       }
-       return true;
+       return gcov_unavailable(m);
 }
 
-/* FIXME: Merge this into one place. */
-static char *obj_list(const struct manifest *m, const char *modobjs)
+static char *cflags_list(const struct manifest *m)
 {
-       char *list = talloc_strdup(m, "");
-       struct ccan_file *i;
-       struct manifest *subm;
-
-       /* Objects from any other C files. */
-       list_for_each(&m->other_test_c_files, i, list)
-               list = talloc_asprintf_append(list, " %s", i->compiled);
-
-       if (modobjs)
-               list = talloc_append_string(list, modobjs);
+       unsigned int i;
+       char *ret = tal_strdup(m, cflags);
 
-       /* Other ccan modules (don't need coverage versions of those). */
-       list_for_each(&m->deps, subm, list) {
-               if (subm->compiled)
-                       list = talloc_asprintf_append(list, " %s",
-                                                     subm->compiled);
-       }
-
-       return list;
+       char **flags = get_cflags(m, m->dir, get_or_compile_info);
+       for (i = 0; flags[i]; i++)
+               tal_append_fmt(&ret, " %s", flags[i]);
+       return ret;
 }
 
-static char *lib_list(const struct manifest *m)
+static char *cflags_list_append(const struct manifest *m, char *iflags)
 {
-       unsigned int i, num;
-       char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
-       char *ret = talloc_strdup(m, "");
+       unsigned int i;
 
-       for (i = 0; i < num; i++)
-               ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
-       return ret;
+       char **flags = get_cflags(m, m->dir, get_or_compile_info);
+       for (i = 0; flags[i]; i++)
+               tal_append_fmt(&iflags, " %s", flags[i]);
+       return iflags;
 }
 
-static char *cov_compile(const void *ctx,
-                        struct manifest *m,
-                        struct ccan_file *file,
-                        const char *modobjs,
-                        bool keep)
+static void cov_compile(const void *ctx,
+                       unsigned int time_ms,
+                       struct manifest *m,
+                       struct ccan_file *file,
+                       bool link_with_module)
 {
-       char *output;
-
-       file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
-       if (!compile_and_link(ctx, file->fullname, ccan_dir,
-                             obj_list(m, modobjs),
-                             COVERAGE_CFLAGS,
-                             lib_list(m), file->cov_compiled, &output)) {
-               talloc_free(file->cov_compiled);
-               file->cov_compiled = NULL;
-               return output;
-       }
-       talloc_free(output);
-       return NULL;
+       char *flags = tal_fmt(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
+       flags = cflags_list_append(m, flags);
+
+       file->compiled[COMPILE_COVERAGE] = temp_file(ctx, "", file->fullname);
+       compile_and_link_async(file, time_ms, file->fullname, ccan_dir,
+                              test_obj_list(m, link_with_module,
+                                            COMPILE_NORMAL,
+                                            COMPILE_COVERAGE),
+                              compiler, flags,
+                              test_lib_list(m, COMPILE_NORMAL),
+                              file->compiled[COMPILE_COVERAGE]);
 }
 
 /* FIXME: Coverage from testable examples as well. */
 static void do_compile_coverage_tests(struct manifest *m,
-                                     bool keep,
                                      unsigned int *timeleft,
                                      struct score *score)
 {
-       char *cmdout, *modobjs = NULL;
+       char *cmdout;
        struct ccan_file *i;
        struct list_head *h;
-
-       if (!list_empty(&m->api_tests)
-           && !build_module_objs_with_coverage(m, keep, score, &modobjs)) {
-               score->error = talloc_strdup(score,
-                            "Failed to compile module objects with coverage");
-               return;
+       bool ok;
+       char *f = cflags_list(m);
+       tal_append_fmt(&f, " %s", COVERAGE_CFLAGS);
+
+       /* For API tests, we need coverage version of module. */
+       if (!list_empty(&m->api_tests)) {
+               build_objects(m, score, f, COMPILE_COVERAGE);
+               if (!score->pass) {
+                       score->error = tal_strdup(score,
+                                                    "Failed to compile module objects with coverage");
+                       return;
+               }
        }
 
        foreach_ptr(h, &m->run_tests, &m->api_tests) {
                list_for_each(h, i, list) {
-                       cmdout = cov_compile(m, m, i, NULL, keep);
-                       if (cmdout) {
-                               score_file_error(score, i, 0,
-                                 "Failed to compile test with coverage: %s",
-                                 cmdout);
-                       }
+                       cov_compile(m, *timeleft, m, i, h == &m->api_tests);
+               }
+       }
+
+       while ((i = collect_command(&ok, &cmdout)) != NULL) {
+               if (!ok) {
+                       score_file_error(score, i, 0,
+                                        "Failed to compile test with coverage:"
+                                        " %s", cmdout);
                }
        }
        if (!score->error) {