check_includes_build.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. return NULL;
  33. }
  34. static void *check_includes_build(struct manifest *m,
  35. bool keep,
  36. unsigned int *timeleft)
  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. return talloc_asprintf(m, "Creating temporary file %s: %s",
  47. tmpsrc, strerror(errno));
  48. contents = talloc_asprintf(tmpsrc, "#include <ccan/%s/%s.h>\n",
  49. m->basename, m->basename);
  50. if (write(fd, contents, strlen(contents)) != strlen(contents)) {
  51. close(fd);
  52. return "Failure writing to temporary file";
  53. }
  54. close(fd);
  55. return compile_object(m, tmpsrc, ccan_dir, tmpobj);
  56. }
  57. static const char *describe_includes_build(struct manifest *m,
  58. void *check_result)
  59. {
  60. return talloc_asprintf(check_result,
  61. "#include of the main header file:\n"
  62. "%s", (char *)check_result);
  63. }
  64. struct ccanlint includes_build = {
  65. .key = "include-main",
  66. .name = "Modules main header compiles",
  67. .total_score = 1,
  68. .check = check_includes_build,
  69. .describe = describe_includes_build,
  70. .can_run = can_build,
  71. };
  72. REGISTER_TEST(includes_build, &depends_exist, &has_main_header, NULL);