compile.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "tools.h"
  2. #include <stdlib.h>
  3. bool compile_verbose = false;
  4. /* Compile multiple object files into a single. Returns NULL if fails. */
  5. char *link_objects(const void *ctx, const char *basename,
  6. const char *objs, char **errmsg)
  7. {
  8. char *file = temp_file(ctx, ".o", basename);
  9. if (compile_verbose)
  10. printf("Linking objects into %s\n", file);
  11. if (run_command(ctx, NULL, errmsg, "ld -r -o %s %s", file, objs))
  12. return file;
  13. tal_free(file);
  14. return NULL;
  15. }
  16. /* Compile a single C file to an object file. */
  17. bool compile_object(const void *ctx, const char *cfile, const char *ccandir,
  18. const char *compiler,
  19. const char *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,
  25. "%s %s -I%s -c -o %s %s",
  26. compiler, cflags, ccandir, 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 *compiler,
  32. const char *cflags,
  33. const char *libs, const char *outfile, char **output)
  34. {
  35. if (compile_verbose)
  36. printf("Compiling and linking %s\n", outfile);
  37. return run_command(ctx, NULL, output,
  38. "%s %s -I%s -o %s %s %s %s",
  39. compiler, cflags,
  40. ccandir, outfile, cfile, objs, libs);
  41. }