]> git.ozlabs.org Git - ccan/blob - tools/compile.c
ccanlint: put generated _info in correct directory.
[ccan] / tools / compile.c
1 #include "tools.h"
2 #include <ccan/talloc/talloc.h>
3 #include <stdlib.h>
4
5 /* Compile multiple object files into a single.  Returns errmsg if fails. */
6 char *link_objects(const void *ctx, const char *objs, char **errmsg)
7 {
8         char *file = temp_file(ctx, ".o");
9
10         *errmsg = run_command(ctx, NULL, "ld -r -o %s %s", file, objs);
11         if (*errmsg) {
12                 talloc_free(file);
13                 return NULL;
14         }
15         return file;
16 }
17
18 /* Compile a single C file to an object file.  Returns errmsg if fails. */
19 char *compile_object(const void *ctx, const char *cfile, const char *ccandir,
20                      char **errmsg)
21 {
22         char *file = temp_file(ctx, ".o");
23
24         *errmsg = run_command(ctx, NULL, "cc " CFLAGS " -I%s -c -o %s %s",
25                               ccandir, file, cfile);
26         if (*errmsg) {
27                 talloc_free(file);
28                 return NULL;
29         }
30         return file;
31 }
32
33 /* Compile and link single C file, with object files.
34  * Returns name of result, or NULL (and fills in errmsg). */
35 char *compile_and_link(const void *ctx, const char *cfile, const char *ccandir,
36                        const char *objs, const char *extra_cflags,
37                        const char *libs, char **errmsg)
38 {
39         char *file = temp_file(ctx, "");
40
41         *errmsg = run_command(ctx, NULL, "cc " CFLAGS " -I%s %s -o %s %s %s %s",
42                               ccandir, extra_cflags, file, cfile, objs, libs);
43         if (*errmsg) {
44                 talloc_free(file);
45                 return NULL;
46         }
47         return file;
48 }