check_includes_build.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/grab_file/grab_file.h>
  5. #include <ccan/str/str.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <limits.h>
  11. #include <errno.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <err.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. static const char *can_build(struct manifest *m)
  18. {
  19. if (safe_mode)
  20. return "Safe mode enabled";
  21. return NULL;
  22. }
  23. static struct ccan_file *main_header(struct manifest *m)
  24. {
  25. struct ccan_file *f;
  26. list_for_each(&m->h_files, f, list) {
  27. if (strstarts(f->name, m->basename)
  28. && strlen(f->name) == strlen(m->basename) + 2)
  29. return f;
  30. }
  31. /* Should not happen: we depend on has_main_header */
  32. abort();
  33. }
  34. static void check_includes_build(struct manifest *m,
  35. bool keep,
  36. unsigned int *timeleft, struct score *score)
  37. {
  38. char *contents;
  39. char *tmpsrc, *tmpobj;
  40. int fd;
  41. struct ccan_file *mainh = main_header(m);
  42. tmpsrc = maybe_temp_file(m, "-included.c", keep, mainh->fullname);
  43. tmpobj = maybe_temp_file(m, ".o", keep, tmpsrc);
  44. fd = open(tmpsrc, O_WRONLY | O_CREAT | O_EXCL, 0600);
  45. if (fd < 0)
  46. err(1, "Creating temporary file %s", tmpsrc);
  47. contents = talloc_asprintf(tmpsrc, "#include <ccan/%s/%s.h>\n",
  48. m->basename, m->basename);
  49. if (write(fd, contents, strlen(contents)) != strlen(contents))
  50. err(1, "writing to temporary file %s", tmpsrc);
  51. close(fd);
  52. score->error = compile_object(m, tmpsrc, ccan_dir, "", tmpobj);
  53. if (score->error) {
  54. score->error = talloc_asprintf(score,
  55. "#include of the main header file:\n%s",
  56. score->error);
  57. } else {
  58. score->pass = true;
  59. score->score = score->total;
  60. }
  61. }
  62. struct ccanlint includes_build = {
  63. .key = "include-main",
  64. .name = "Modules main header compiles",
  65. .check = check_includes_build,
  66. .can_run = can_build,
  67. };
  68. REGISTER_TEST(includes_build, &depends_exist, &has_main_header, NULL);