check_includes_build.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 void *check_includes_build(struct manifest *m)
  24. {
  25. char *contents;
  26. char *tmpfile, *objfile;
  27. int fd;
  28. tmpfile = temp_file(m, ".c");
  29. objfile = temp_file(m, ".o");
  30. fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0600);
  31. if (fd < 0)
  32. return talloc_asprintf(m, "Creating temporary file: %s",
  33. strerror(errno));
  34. contents = talloc_asprintf(tmpfile, "#include <ccan/%s/%s.h>\n",
  35. m->basename, m->basename);
  36. if (write(fd, contents, strlen(contents)) != strlen(contents)) {
  37. close(fd);
  38. return "Failure writing to temporary file";
  39. }
  40. close(fd);
  41. return compile_object(m, objfile, tmpfile);
  42. }
  43. static const char *describe_includes_build(struct manifest *m,
  44. void *check_result)
  45. {
  46. return talloc_asprintf(check_result,
  47. "#include of the main header file:\n"
  48. "%s", (char *)check_result);
  49. }
  50. struct ccanlint includes_build = {
  51. .name = "Can compile against main header",
  52. .total_score = 1,
  53. .check = check_includes_build,
  54. .describe = describe_includes_build,
  55. .can_run = can_build,
  56. };
  57. REGISTER_TEST(includes_build, &depends_exist, NULL);