check_depends.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 bool expect_obj_file(const char *dir)
  17. {
  18. char *olddir;
  19. struct manifest *dep_man;
  20. bool has_c_files;
  21. olddir = talloc_getcwd(dir);
  22. if (!olddir)
  23. err(1, "Getting current directory");
  24. /* We will fail below if this doesn't exist. */
  25. if (chdir(dir) != 0)
  26. return false;
  27. dep_man = get_manifest(dir);
  28. if (chdir(olddir) != 0)
  29. err(1, "Returning to original directory '%s'", olddir);
  30. talloc_free(olddir);
  31. /* If it has C files, we expect an object file built from them. */
  32. has_c_files = !list_empty(&dep_man->c_files);
  33. talloc_free(dep_man);
  34. return has_c_files;
  35. }
  36. /* FIXME: recursive ccanlint if they ask for it. */
  37. static char *add_dep(char *sofar, struct manifest *m, const char *dep)
  38. {
  39. char *file, *dir;
  40. struct stat st;
  41. bool need_obj;
  42. dir = talloc_asprintf(m, "../%s", dep);
  43. need_obj = expect_obj_file(dir);
  44. if (need_obj) {
  45. file = talloc_asprintf(m, "../%s.o", dep);
  46. if (stat(file, &st) == 0) {
  47. struct ccan_file *f = new_ccan_file(m, file);
  48. list_add_tail(&m->dep_obj_files, &f->list);
  49. return sofar;
  50. }
  51. }
  52. if (stat(dir, &st) == 0) {
  53. if (!need_obj)
  54. return sofar;
  55. return talloc_asprintf_append(sofar,
  56. "ccan/%s: isn't built (no %s)\n",
  57. dep, file);
  58. }
  59. return talloc_asprintf_append(sofar,
  60. "ccan/%s: could not find directory %s\n",
  61. dep, dir);
  62. }
  63. static void *check_depends(struct manifest *m)
  64. {
  65. unsigned int i;
  66. char *report = NULL;
  67. char **deps;
  68. if (safe_mode)
  69. deps = get_safe_ccan_deps(m, "..", m->basename, true);
  70. else
  71. deps = get_deps(m, "..", m->basename, true);
  72. for (i = 0; deps[i]; i++) {
  73. if (!strstarts(deps[i], "ccan/"))
  74. continue;
  75. report = add_dep(report, m, deps[i] + strlen("ccan/"));
  76. }
  77. return report;
  78. }
  79. static const char *describe_depends(struct manifest *m, void *check_result)
  80. {
  81. return talloc_asprintf(check_result,
  82. "The following dependencies are needed:\n"
  83. "%s\n", (char *)check_result);
  84. }
  85. struct ccanlint depends = {
  86. .name = "CCAN dependencies are built",
  87. .total_score = 1,
  88. .check = check_depends,
  89. .describe = describe_depends,
  90. };
  91. REGISTER_TEST(depends, NULL);