]> git.ozlabs.org Git - ccan/blobdiff - tools/ccanlint/tests/examples_compile.c
tools/ccanlint: add args to main.
[ccan] / tools / ccanlint / tests / examples_compile.c
index 19cbec187ca338441939a125170ace956feb3e66..2ca2f34c6970a520c7b8cc3b18cbfc132bdf8c85 100644 (file)
@@ -4,6 +4,7 @@
 #include <ccan/tal/str/str.h>
 #include <ccan/take/take.h>
 #include <ccan/cast/cast.h>
+#include <ccan/tal/path/path.h>
 #include <ccan/str/str.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -125,6 +126,17 @@ static char *example_lib_list(const void *ctx, struct manifest **deps)
        return list;
 }
 
+static char *cflags_list(const struct manifest *m)
+{
+       unsigned int i;
+       char *ret = tal_strdup(m, cflags);
+
+       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;
+}
+
 /* FIXME: Test with reduced features! */
 static bool compile(const void *ctx,
                    struct manifest *m,
@@ -132,11 +144,12 @@ static bool compile(const void *ctx,
                    char **output)
 {
        struct manifest **deps = get_example_deps(m, file);
+       const char *flags = cflags_list(m);
 
        file->compiled[COMPILE_NORMAL] = temp_file(ctx, "", file->fullname);
        if (!compile_and_link(ctx, file->fullname, ccan_dir,
                              example_obj_list(file, deps),
-                             compiler, cflags,
+                             compiler, flags,
                              example_lib_list(file, deps),
                              file->compiled[COMPILE_NORMAL],
                              output)) {
@@ -429,7 +442,7 @@ static char *mangle(struct manifest *m, char **lines)
        if (!has_main) {
                ret = tal_strcat(m, take(ret),
                             "/* Need a main to link successfully. */\n"
-                            "int main(void)\n{\n");
+                            "int main(int argc, char *argv[])\n{\n");
                fake_function = true;
        }
 
@@ -447,8 +460,13 @@ static char *mangle(struct manifest *m, char **lines)
                tal_append_fmt(&ret, "  %s\n", use_funcs);
        }
 
+       if (!has_main)
+               ret = tal_strcat(m, take(ret),
+                                "(void)argc; (void)argv;\n");
+       
        if (fake_function)
-               ret = tal_strcat(m, take(ret), "return 0;\n}\n");
+               ret = tal_strcat(m, take(ret),
+                                "return 0;\n}\n");
        return ret;
 }
 
@@ -461,10 +479,10 @@ static struct ccan_file *mangle_example(struct manifest *m,
        struct ccan_file *f;
 
        name = temp_file(example, ".c",
-                        tal_fmt(m, "%s/mangled-%s", m->dir, example->name));
+                        take(tal_fmt(NULL, "mangled-%s", example->name)));
        f = new_ccan_file(example,
-                         tal_dirname(example, name),
-                         tal_basename(example, name));
+                         take(path_dirname(example, name)),
+                         take(path_basename(example, name)));
        tal_steal(f, name);
 
        fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
@@ -472,7 +490,7 @@ static struct ccan_file *mangle_example(struct manifest *m,
                return NULL;
 
        contents = mangle(m, lines);
-       if (write(fd, contents, strlen(contents)) != strlen(contents)) {
+       if (write(fd, contents, strlen(contents)) != (int)strlen(contents)) {
                close(fd);
                return NULL;
        }
@@ -537,7 +555,7 @@ static unsigned int try_compiling(struct manifest *m,
 }
 
 static void build_examples(struct manifest *m,
-                          unsigned int *timeleft, struct score *score)
+                          unsigned int *timeleft UNNEEDED, struct score *score)
 {
        struct ccan_file *i;
        char **prev = NULL;