main_header_compiles.c 1.8 KB

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