]> git.ozlabs.org Git - ccan/blob - tools/compile.c
ccan_tokenizer: Corrected LICENSE link so it points to BSD-3CLAUSE.
[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 NULL if fails. */
8 char *link_objects(const void *ctx, const char *basename, bool in_pwd,
9                    const char *objs, char **errmsg)
10 {
11         char *file = maybe_temp_file(ctx, ".o", in_pwd, basename);
12
13         if (compile_verbose)
14                 printf("Linking objects into %s\n", file);
15
16         if (run_command(ctx, NULL, errmsg, "ld -r -o %s %s", file, objs))
17                 return file;
18
19         talloc_free(file);
20         return NULL;
21 }
22
23 /* Compile a single C file to an object file. */
24 bool compile_object(const void *ctx, const char *cfile, const char *ccandir,
25                     const char *compiler,
26                     const char *cflags,
27                     const char *outfile, char **output)
28 {
29         if (compile_verbose)
30                 printf("Compiling %s\n", outfile);
31         return run_command(ctx, NULL, output,
32                            "%s %s -I%s -c -o %s %s",
33                            compiler, cflags, ccandir, outfile, cfile);
34 }
35
36 /* Compile and link single C file, with object files.
37  * Returns false on failure. */
38 bool compile_and_link(const void *ctx, const char *cfile, const char *ccandir,
39                       const char *objs, const char *compiler,
40                       const char *cflags,
41                       const char *libs, const char *outfile, char **output)
42 {
43         if (compile_verbose)
44                 printf("Compiling and linking %s\n", outfile);
45         return run_command(ctx, NULL, output,
46                            "%s %s -I%s -o %s %s %s %s",
47                            compiler, cflags,
48                            ccandir, outfile, cfile, objs, libs);
49 }