compile.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #include "tools.h"
  2. #include <ccan/talloc/talloc.h>
  3. #include <stdlib.h>
  4. /* Compile multiple object files into a single. Returns errmsg if fails. */
  5. char *link_objects(const void *ctx, const char *objs, char **errmsg)
  6. {
  7. char *file = temp_file(ctx, ".o");
  8. *errmsg = run_command(ctx, NULL, "ld -r -o %s %s", file, objs);
  9. if (*errmsg) {
  10. talloc_free(file);
  11. return NULL;
  12. }
  13. return file;
  14. }
  15. /* Compile a single C file to an object file. Returns errmsg if fails. */
  16. char *compile_object(const void *ctx, const char *cfile, const char *ccandir,
  17. const char *extra_cflags,
  18. const char *outfile)
  19. {
  20. return run_command(ctx, NULL, "cc " CFLAGS " -I%s %s -c -o %s %s",
  21. ccandir, extra_cflags, outfile, cfile);
  22. }
  23. /* Compile and link single C file, with object files.
  24. * Returns error message or NULL on success. */
  25. char *compile_and_link(const void *ctx, const char *cfile, const char *ccandir,
  26. const char *objs, const char *extra_cflags,
  27. const char *libs, const char *outfile)
  28. {
  29. return run_command(ctx, NULL, "cc " CFLAGS " -I%s %s -o %s %s %s %s",
  30. ccandir, extra_cflags, outfile, cfile, objs, libs);
  31. }