compile.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 *objs, char **errmsg)
  7. {
  8. char *file = temp_file(ctx, ".o");
  9. if (compile_verbose)
  10. printf("Linking objects into %s\n", file);
  11. *errmsg = run_command(ctx, NULL, "ld -r -o %s %s", file, objs);
  12. if (*errmsg) {
  13. talloc_free(file);
  14. return NULL;
  15. }
  16. return file;
  17. }
  18. /* Compile a single C file to an object file. Returns errmsg if fails. */
  19. char *compile_object(const void *ctx, const char *cfile, const char *ccandir,
  20. const char *extra_cflags,
  21. const char *outfile)
  22. {
  23. if (compile_verbose)
  24. printf("Compiling %s\n", outfile);
  25. return run_command(ctx, NULL, "cc " CFLAGS " -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 error message or NULL on success. */
  30. char *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)
  33. {
  34. if (compile_verbose)
  35. printf("Compiling and linking %s\n", outfile);
  36. return run_command(ctx, NULL, "cc " CFLAGS " -I%s %s -o %s %s %s %s",
  37. ccandir, extra_cflags, outfile, cfile, objs, libs);
  38. }