compile.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "tools.h"
  2. #include <ccan/talloc/talloc.h>
  3. #include <stdlib.h>
  4. bool compile_verbose = false;
  5. /* Compile multiple object files into a single. Returns NULL if fails. */
  6. char *link_objects(const void *ctx, const char *basename,
  7. const char *objs, char **errmsg)
  8. {
  9. char *file = temp_file(ctx, ".o", basename);
  10. if (compile_verbose)
  11. printf("Linking objects into %s\n", file);
  12. if (run_command(ctx, NULL, errmsg, "ld -r -o %s %s", file, objs))
  13. return file;
  14. talloc_free(file);
  15. return NULL;
  16. }
  17. /* Compile a single C file to an object file. */
  18. bool compile_object(const void *ctx, const char *cfile, const char *ccandir,
  19. const char *compiler,
  20. const char *cflags,
  21. const char *outfile, char **output)
  22. {
  23. if (compile_verbose)
  24. printf("Compiling %s\n", outfile);
  25. return run_command(ctx, NULL, output,
  26. "%s %s -I%s -c -o %s %s",
  27. compiler, cflags, ccandir, outfile, cfile);
  28. }
  29. /* Compile and link single C file, with object files.
  30. * Returns false on failure. */
  31. bool compile_and_link(const void *ctx, const char *cfile, const char *ccandir,
  32. const char *objs, const char *compiler,
  33. const char *cflags,
  34. const char *libs, const char *outfile, char **output)
  35. {
  36. if (compile_verbose)
  37. printf("Compiling and linking %s\n", outfile);
  38. return run_command(ctx, NULL, output,
  39. "%s %s -I%s -o %s %s %s %s",
  40. compiler, cflags,
  41. ccandir, outfile, cfile, objs, libs);
  42. }