]> git.ozlabs.org Git - ccan/blob - tools/compile.c
opt: Don't segfault if a string's default is NULL
[ccan] / tools / compile.c
1 #include "tools.h"
2 #include <stdlib.h>
3
4 #ifndef CCAN_COMPILER
5 #define CCAN_COMPILER DEFAULT_CCAN_COMPILER
6 #endif
7 #ifndef CCAN_CFLAGS
8 #define CCAN_CFLAGS DEFAULT_CCAN_CFLAGS
9 #endif
10 const char *compiler = CCAN_COMPILER;
11 const char *cflags = CCAN_CFLAGS;
12
13 bool compile_verbose = false;
14
15 /* Compile multiple object files into a single.  Returns NULL if fails. */
16 char *link_objects(const void *ctx, const char *basename,
17                    const char *objs, char **errmsg)
18 {
19         char *file = temp_file(ctx, ".o", basename);
20
21         if (compile_verbose)
22                 printf("Linking objects into %s\n", file);
23
24         if (run_command(ctx, NULL, errmsg, "ld -r -o %s %s", file, objs))
25                 return file;
26
27         tal_free(file);
28         return NULL;
29 }
30
31 /* Compile a single C file to an object file. */
32 bool compile_object(const void *ctx, const char *cfile, const char *ccandir,
33                     const char *compiler,
34                     const char *cflags,
35                     const char *outfile, char **output)
36 {
37         if (compile_verbose)
38                 printf("Compiling %s\n", outfile);
39         return run_command(ctx, NULL, output,
40                            "%s %s -I%s -c -o %s %s",
41                            compiler, cflags, ccandir, outfile, cfile);
42 }
43
44 /* Compile and link single C file, with object files.
45  * Returns false on failure. */
46 bool compile_and_link(const void *ctx, const char *cfile, const char *ccandir,
47                       const char *objs, const char *compiler,
48                       const char *cflags,
49                       const char *libs, const char *outfile, char **output)
50 {
51         if (compile_verbose)
52                 printf("Compiling and linking %s\n", outfile);
53         return run_command(ctx, NULL, output,
54                            "%s %s -I%s -o %s %s %s %s",
55                            compiler, cflags,
56                            ccandir, outfile, cfile, objs, libs);
57 }