compile.c 1.6 KB

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