compile.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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, bool in_pwd,
  7. const char *objs, char **errmsg)
  8. {
  9. char *file = maybe_temp_file(ctx, ".o", in_pwd, 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 *extra_cflags,
  20. const char *outfile, char **output)
  21. {
  22. if (compile_verbose)
  23. printf("Compiling %s\n", outfile);
  24. return run_command(ctx, NULL, output, CCAN_COMPILER " " CCAN_CFLAGS
  25. " -I%s %s -c -o %s %s",
  26. ccandir, extra_cflags, outfile, cfile);
  27. }
  28. /* Compile and link single C file, with object files.
  29. * Returns false on failure. */
  30. bool compile_and_link(const void *ctx, const char *cfile, const char *ccandir,
  31. const char *objs, const char *extra_cflags,
  32. const char *libs, const char *outfile, char **output)
  33. {
  34. if (compile_verbose)
  35. printf("Compiling and linking %s\n", outfile);
  36. return run_command(ctx, NULL, output, CCAN_COMPILER " " CCAN_CFLAGS
  37. " -I%s %s -o %s %s %s %s",
  38. ccandir, extra_cflags, outfile, cfile, objs, libs);
  39. }