main_header_compiles.c 1.9 KB

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