]> git.ozlabs.org Git - ccan/blob - tools/compile.c
Fix valgrind running with debugger.
[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, "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, char **errmsg)
20 {
21         char *file = temp_file(ctx, ".o");
22
23         *errmsg = run_command(ctx, "cc " CFLAGS " -c -o %s %s", file, cfile);
24         if (*errmsg) {
25                 talloc_free(file);
26                 return NULL;
27         }
28         return file;
29 }
30
31 /* Compile and link single C file, with object files.
32  * Returns name of result, or NULL (and fills in errmsg). */
33 char *compile_and_link(const void *ctx, const char *cfile, const char *objs,
34                        const char *extra_cflags, const char *libs,
35                        char **errmsg)
36 {
37         char *file = temp_file(ctx, "");
38
39         *errmsg = run_command(ctx, "cc " CFLAGS " %s -o %s %s %s %s",
40                               extra_cflags, file, cfile, objs, libs);
41         if (*errmsg) {
42                 talloc_free(file);
43                 return NULL;
44         }
45         return file;
46 }