]> git.ozlabs.org Git - ccan/blob - tools/compile.c
Makefile: fix fastcheck.
[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 #ifndef CCAN_OUTPUT_EXE_CFLAG
11 #define CCAN_OUTPUT_EXE_CFLAG DEFAULT_CCAN_OUTPUT_EXE_CFLAG
12 #endif
13 const char *compiler = CCAN_COMPILER;
14 const char *cflags = CCAN_CFLAGS;
15 const char *outexecflag = CCAN_OUTPUT_EXE_CFLAG;
16
17 bool compile_verbose = false;
18
19 /* Compile multiple object files into a single.  Returns NULL if fails. */
20 char *link_objects(const void *ctx, const char *basename,
21                    const char *objs, char **errmsg)
22 {
23         char *file = temp_file(ctx, ".o", basename);
24
25         if (compile_verbose)
26                 printf("Linking objects into %s\n", file);
27
28         if (run_command(ctx, NULL, errmsg, "ld -r -o %s %s", file, objs))
29                 return file;
30
31         tal_free(file);
32         return NULL;
33 }
34
35 /* Compile a single C file to an object file. */
36 bool compile_object(const void *ctx, const char *cfile, const char *ccandir,
37                     const char *compiler,
38                     const char *cflags,
39                     const char *outfile, char **output)
40 {
41         if (compile_verbose)
42                 printf("Compiling %s\n", outfile);
43         return run_command(ctx, NULL, output,
44                            "%s %s -I%s -c %s%s %s",
45                            compiler, cflags, ccandir,
46                            outexecflag, outfile, cfile);
47 }
48
49 /* Compile and link single C file, with object files.
50  * Returns false on failure. */
51 bool compile_and_link(const void *ctx, const char *cfile, const char *ccandir,
52                       const char *objs, const char *compiler,
53                       const char *cflags,
54                       const char *libs, const char *outfile, char **output)
55 {
56         if (compile_verbose)
57                 printf("Compiling and linking %s\n", outfile);
58         return run_command(ctx, NULL, output,
59                            "%s %s -I%s %s%s %s %s %s",
60                            compiler, cflags, ccandir,
61                            outexecflag, outfile, cfile, objs, libs);
62 }