check_depends_built.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/str/str.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <limits.h>
  10. #include <errno.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <err.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. static const char *can_build(struct manifest *m)
  17. {
  18. if (safe_mode)
  19. return "Safe mode enabled";
  20. return NULL;
  21. }
  22. /* FIXME: recursive ccanlint if they ask for it. */
  23. static bool expect_obj_file(const char *dir)
  24. {
  25. char *olddir;
  26. struct manifest *dep_man;
  27. bool has_c_files;
  28. olddir = talloc_getcwd(dir);
  29. if (!olddir)
  30. err(1, "Getting current directory");
  31. /* We will fail below if this doesn't exist. */
  32. if (chdir(dir) != 0)
  33. return false;
  34. dep_man = get_manifest(dir);
  35. if (chdir(olddir) != 0)
  36. err(1, "Returning to original directory '%s'", olddir);
  37. talloc_free(olddir);
  38. /* If it has C files, we expect an object file built from them. */
  39. has_c_files = !list_empty(&dep_man->c_files);
  40. talloc_free(dep_man);
  41. return has_c_files;
  42. }
  43. static void *check_depends_built(struct manifest *m)
  44. {
  45. struct ccan_file *i;
  46. struct stat st;
  47. char *report = NULL;
  48. list_for_each(&m->dep_dirs, i, list) {
  49. char *objfile;
  50. if (!expect_obj_file(i->name))
  51. continue;
  52. objfile = talloc_asprintf(m, "%s.o", i->name);
  53. if (stat(objfile, &st) != 0) {
  54. report = talloc_asprintf_append(report,
  55. "object file %s\n",
  56. objfile);
  57. } else {
  58. struct ccan_file *f = new_ccan_file(m, objfile);
  59. list_add_tail(&m->dep_objs, &f->list);
  60. }
  61. }
  62. /* We may need libtap for testing, unless we're "tap" */
  63. if (!streq(m->basename, "tap")
  64. && (!list_empty(&m->run_tests) || !list_empty(&m->api_tests))) {
  65. if (stat("../tap.o", &st) != 0) {
  66. report = talloc_asprintf_append(report,
  67. "object file ../tap.o"
  68. " (for tests)\n");
  69. }
  70. }
  71. return talloc_steal(m, report);
  72. }
  73. static const char *describe_depends_built(struct manifest *m,
  74. void *check_result)
  75. {
  76. return talloc_asprintf(check_result,
  77. "The following dependencies are not built:\n"
  78. "%s", (char *)check_result);
  79. }
  80. struct ccanlint depends_built = {
  81. .name = "CCAN dependencies are built",
  82. .total_score = 1,
  83. .check = check_depends_built,
  84. .describe = describe_depends_built,
  85. .can_run = can_build,
  86. };
  87. REGISTER_TEST(depends_built, &depends_exist, NULL);