compile.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. char **errmsg)
  18. {
  19. char *file = temp_file(ctx, ".o");
  20. *errmsg = run_command(ctx, NULL, "cc " CFLAGS " -I%s -c -o %s %s",
  21. ccandir, file, cfile);
  22. if (*errmsg) {
  23. talloc_free(file);
  24. return NULL;
  25. }
  26. return file;
  27. }
  28. /* Compile and link single C file, with object files.
  29. * Returns name of result, or NULL (and fills in errmsg). */
  30. char *compile_and_link(const void *ctx, const char *cfile, const char *ccandir,
  31. const char *objs, const char *extra_cflags,
  32. const char *libs, char **errmsg)
  33. {
  34. char *file = temp_file(ctx, "");
  35. *errmsg = run_command(ctx, NULL, "cc " CFLAGS " -I%s %s -o %s %s %s %s",
  36. ccandir, extra_cflags, file, cfile, objs, libs);
  37. if (*errmsg) {
  38. talloc_free(file);
  39. return NULL;
  40. }
  41. return file;
  42. }