check_includes_build.c 1.8 KB

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