]> git.ozlabs.org Git - ccan/commitdiff
tools: Consolidate gcov handling
authorDavid Gibson <david@gibson.dropbear.id.au>
Wed, 18 Jan 2017 03:09:29 +0000 (14:09 +1100)
committerDavid Gibson <david@gibson.dropbear.id.au>
Wed, 18 Jan 2017 05:33:17 +0000 (16:33 +1100)
At the moment, invocation of the 'gcov' tool for coverage analysis
from ccanlint is put directly into the tests_compile_coverage.c and
tests_coverage.c files.  This makes it awkard to extend.

So, this patch moves the invocation of gcov into a new tools/gcov.v
file, analagous to tools/compile.c.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
tools/ccanlint/tests/tests_compile_coverage.c
tools/ccanlint/tests/tests_coverage.c
tools/gcov.c [new file with mode: 0644]
tools/tools.h

index 89308fe80ae9ae4a26ba97979bb11f7bf2725785..c8bc35d93b1ab1fe37320b18fb79f2bb7023ef0b 100644 (file)
 /* Note: we already test safe_mode in run_tests.c */
 static const char *can_run_coverage(struct manifest *m)
 {
-#ifdef __GNUC__
-       unsigned int timeleft = default_timeout_ms;
-       char *output;
-
-       if (!run_command(m, &timeleft, &output, "gcov -h"))
-               return tal_fmt(m, "No gcov support: %s", output);
-       return NULL;
-#else
-       return "No coverage support for this compiler";
-#endif
+       return gcov_unavailable(m);
 }
 
 static char *cflags_list(const struct manifest *m)
index d877553e68b950b7bcc7a2562731f8349bf53536..e29572019141e81fd8b77a4abc4e1d7931170860 100644 (file)
@@ -138,7 +138,7 @@ static void do_run_coverage_tests(struct manifest *m,
 {
        struct ccan_file *i;
        char *cmdout, *outdir;
-       char *covcmd;
+       char *covargs;
        bool full_gcov = (verbose > 1);
        struct list_head *list;
        bool ran_some = false;
@@ -146,16 +146,14 @@ static void do_run_coverage_tests(struct manifest *m,
        /* This tells gcov where we put those .gcno files. */
        outdir = path_dirname(score,
                              m->info_file->compiled[COMPILE_NORMAL]);
-       covcmd = tal_fmt(m, "gcov %s -o %s",
-                        full_gcov ? "" : "-n",
-                        outdir);
+       covargs = tal_fmt(m, "%s -o %s", full_gcov ? "" : "-n", outdir);
 
        /* Run them all. */
        foreach_ptr(list, &m->run_tests, &m->api_tests) {
                list_for_each(list, i, list) {
                        if (run_command(score, timeleft, &cmdout,
                                        "%s", i->compiled[COMPILE_COVERAGE])) {
-                               tal_append_fmt(&covcmd, " %s", i->fullname);
+                               tal_append_fmt(&covargs, " %s", i->fullname);
                        } else {
                                score_file_error(score, i, 0,
                                                 "Running test with coverage"
@@ -174,7 +172,7 @@ static void do_run_coverage_tests(struct manifest *m,
        }
 
        /* Now run gcov: we want output even if it succeeds. */
-       if (!run_command(score, timeleft, &cmdout, "%s", covcmd)) {
+       if (!run_gcov(score, timeleft, &cmdout, "%s", covargs)) {
                score->error = tal_fmt(score, "Running gcov: %s", cmdout);
                return;
        }
diff --git a/tools/gcov.c b/tools/gcov.c
new file mode 100644 (file)
index 0000000..f922d8d
--- /dev/null
@@ -0,0 +1,36 @@
+#include "tools.h"
+#include <stdlib.h>
+#include <stdarg.h>
+
+bool run_gcov(const void *ctx, unsigned int *time_ms, char **output,
+             const char *fmt, ...)
+{
+       char *args;
+       va_list ap;
+       bool rc;
+
+       va_start(ap, fmt);
+       args = tal_vfmt(ctx, fmt, ap);
+       rc = run_command(ctx, time_ms, output, "gcov %s", args);
+       tal_free(args);
+       return rc;
+}
+
+const char *gcov_unavailable(void *ctx)
+{
+       const char *err = NULL;
+
+#ifdef __GNUC__
+       unsigned int timeleft = default_timeout_ms;
+       char *output;
+
+       if (!run_gcov(ctx, &timeleft, &output, "-h")) {
+               err = tal_fmt(ctx, "No gcov support: %s", output);
+               tal_free(output);
+       }
+#else
+       err = "No coverage support for this compiler";
+#endif
+
+       return err;
+}
index d29a25fbdfaf6697c5fb9827f2aa7a38a01bac03..e5aaee2f9c4d0d898bcd7840b66c3bc9c8fcd8d8 100644 (file)
@@ -101,4 +101,10 @@ extern const unsigned int default_timeout_ms;
 
 /* Get ccan/ top dir, given a directory within it. */
 const char *find_ccan_dir(const char *base);
+
+/* Run gcov coverage tool */
+const char *gcov_unavailable(void *ctx);
+bool run_gcov(const void *ctx, unsigned int *time_ms, char **output,
+             const char *fmt, ...);
+
 #endif /* CCAN_TOOLS_H */