main_header_compiles.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. unsigned int *timeleft, struct score *score)
  36. {
  37. char *contents;
  38. char *tmpsrc, *tmpobj, *cmdout;
  39. int fd;
  40. struct ccan_file *mainh = main_header(m);
  41. tmpsrc = temp_file(m, "-included.c", mainh->fullname);
  42. tmpobj = temp_file(m, ".o", tmpsrc);
  43. fd = open(tmpsrc, O_WRONLY | O_CREAT | O_EXCL, 0600);
  44. if (fd < 0)
  45. err(1, "Creating temporary file %s", tmpsrc);
  46. contents = talloc_asprintf(tmpsrc, "#include <ccan/%s/%s.h>\n",
  47. m->basename, m->basename);
  48. if (write(fd, contents, strlen(contents)) != strlen(contents))
  49. err(1, "writing to temporary file %s", tmpsrc);
  50. close(fd);
  51. if (compile_object(score, tmpsrc, ccan_dir, compiler, cflags,
  52. tmpobj, &cmdout)) {
  53. score->pass = true;
  54. score->score = score->total;
  55. } else {
  56. score->error = talloc_asprintf(score,
  57. "#include of the main header file:\n%s",
  58. cmdout);
  59. }
  60. }
  61. struct ccanlint main_header_compiles = {
  62. .key = "main_header_compiles",
  63. .name = "Modules main header compiles",
  64. .check = check_includes_build,
  65. .can_run = can_build,
  66. .needs = "depends_exist main_header_exists"
  67. };
  68. REGISTER_TEST(main_header_compiles);