build.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/str/str.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <limits.h>
  10. #include <errno.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <err.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. static const char *can_build(struct manifest *m)
  17. {
  18. if (list_empty(&m->c_files))
  19. return "No C files in module";
  20. if (safe_mode)
  21. return "Safe mode enabled";
  22. return NULL;
  23. }
  24. static char *obj_list(const struct manifest *m)
  25. {
  26. char *list = talloc_strdup(m, "");
  27. struct ccan_file *i;
  28. /* Object from all the C files. */
  29. list_for_each(&m->c_files, i, list)
  30. list = talloc_asprintf_append(list, "%.*s.o ",
  31. strlen(i->name) - 2, i->name);
  32. return list;
  33. }
  34. /* We leave this object file around after ccanlint runs, all built. */
  35. static void *do_build(struct manifest *m)
  36. {
  37. return run_command(m, "ld -r -o ../%s.o %s", m->basename, obj_list(m));
  38. }
  39. static const char *describe_build(struct manifest *m, void *check_result)
  40. {
  41. return talloc_asprintf(check_result,
  42. "The object file for the module didn't build:\n"
  43. "%s", (char *)check_result);
  44. }
  45. struct ccanlint build = {
  46. .name = "Module can be built",
  47. .total_score = 1,
  48. .check = do_build,
  49. .describe = describe_build,
  50. .can_run = can_build,
  51. };
  52. REGISTER_TEST(build, &depends_built, NULL);