compile.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 errmsg 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. *errmsg = run_command(ctx, NULL, "ld -r -o %s %s", file, objs);
  13. if (*errmsg) {
  14. talloc_free(file);
  15. return NULL;
  16. }
  17. return file;
  18. }
  19. /* Compile a single C file to an object file. Returns errmsg if fails. */
  20. char *compile_object(const void *ctx, const char *cfile, const char *ccandir,
  21. const char *extra_cflags,
  22. const char *outfile)
  23. {
  24. if (compile_verbose)
  25. printf("Compiling %s\n", outfile);
  26. return run_command(ctx, NULL, "cc " CFLAGS " -I%s %s -c -o %s %s",
  27. ccandir, extra_cflags, outfile, cfile);
  28. }
  29. /* Compile and link single C file, with object files.
  30. * Returns error message or NULL on success. */
  31. char *compile_and_link(const void *ctx, const char *cfile, const char *ccandir,
  32. const char *objs, const char *extra_cflags,
  33. const char *libs, const char *outfile)
  34. {
  35. if (compile_verbose)
  36. printf("Compiling and linking %s\n", outfile);
  37. return run_command(ctx, NULL, "cc " CFLAGS " -I%s %s -o %s %s %s %s",
  38. ccandir, extra_cflags, outfile, cfile, objs, libs);
  39. }