]> git.ozlabs.org Git - ccan/blob - tools/compile.c
tools: new "configurator" tool.
[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 *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         *errmsg = run_command(ctx, NULL, "ld -r -o %s %s", file, objs);
17         if (*errmsg) {
18                 talloc_free(file);
19                 return NULL;
20         }
21         return file;
22 }
23
24 /* Compile a single C file to an object file.  Returns errmsg if fails. */
25 char *compile_object(const void *ctx, const char *cfile, const char *ccandir,
26                      const char *extra_cflags,
27                      const char *outfile)
28 {
29         if (compile_verbose)
30                 printf("Compiling %s\n", outfile);
31         return run_command(ctx, NULL, CCAN_COMPILER " " CCAN_CFLAGS
32                            " -I%s %s -c -o %s %s",
33                            ccandir, extra_cflags, outfile, cfile);
34 }
35
36 /* Compile and link single C file, with object files.
37  * Returns error message or NULL on success. */
38 char *compile_and_link(const void *ctx, const char *cfile, const char *ccandir,
39                        const char *objs, const char *extra_cflags,
40                        const char *libs, const char *outfile)
41 {
42         if (compile_verbose)
43                 printf("Compiling and linking %s\n", outfile);
44         return run_command(ctx, NULL, CCAN_COMPILER " " CCAN_CFLAGS
45                            " -I%s %s -o %s %s %s %s",
46                            ccandir, extra_cflags, outfile, cfile, objs, libs);
47 }