]> git.ozlabs.org Git - ccan/blob - tools/compile.c
ccanlint: print out compile targets for -vv.
[ccan] / tools / compile.c
1 #include "tools.h"
2 #include <ccan/talloc/talloc.h>
3 #include <stdlib.h>
4
5 bool compile_verbose = false;
6
7 /* Compile multiple object files into a single.  Returns errmsg if fails. */
8 char *link_objects(const void *ctx, const char *objs, char **errmsg)
9 {
10         char *file = temp_file(ctx, ".o");
11
12         if (compile_verbose)
13                 printf("Linking objects into %s\n", file);
14
15         *errmsg = run_command(ctx, NULL, "ld -r -o %s %s", file, objs);
16         if (*errmsg) {
17                 talloc_free(file);
18                 return NULL;
19         }
20         return file;
21 }
22
23 /* Compile a single C file to an object file.  Returns errmsg if fails. */
24 char *compile_object(const void *ctx, const char *cfile, const char *ccandir,
25                      const char *extra_cflags,
26                      const char *outfile)
27 {
28         if (compile_verbose)
29                 printf("Compiling %s\n", outfile);
30         return run_command(ctx, NULL, "cc " CFLAGS " -I%s %s -c -o %s %s",
31                            ccandir, extra_cflags, outfile, cfile);
32 }
33
34 /* Compile and link single C file, with object files.
35  * Returns error message or NULL on success. */
36 char *compile_and_link(const void *ctx, const char *cfile, const char *ccandir,
37                        const char *objs, const char *extra_cflags,
38                        const char *libs, const char *outfile)
39 {
40         if (compile_verbose)
41                 printf("Compiling and linking %s\n", outfile);
42         return run_command(ctx, NULL, "cc " CFLAGS " -I%s %s -o %s %s %s %s",
43                            ccandir, extra_cflags, outfile, cfile, objs, libs);
44 }