compile.c 1.8 KB

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