check_includes_build.c 1.5 KB

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