compile.c 1.2 KB

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