manifest.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include "config.h"
  2. #include "manifest.h"
  3. #include "tools.h"
  4. #include <ccan/str/str.h>
  5. #include <ccan/tal/link/link.h>
  6. #include <ccan/tal/path/path.h>
  7. #include <ccan/hash/hash.h>
  8. #include <ccan/htable/htable_type.h>
  9. #include <ccan/noerr/noerr.h>
  10. #include <ccan/foreach/foreach.h>
  11. #include <ccan/asort/asort.h>
  12. #include <ccan/array_size/array_size.h>
  13. #include <ccan/err/err.h>
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <fcntl.h>
  18. #include <errno.h>
  19. #include <dirent.h>
  20. #include <ctype.h>
  21. #include <stdarg.h>
  22. #include <assert.h>
  23. static size_t dir_hash(const char *name)
  24. {
  25. return hash(name, strlen(name), 0);
  26. }
  27. static const char *manifest_name(const struct manifest *m)
  28. {
  29. return m->dir;
  30. }
  31. static bool dir_cmp(const struct manifest *m, const char *dir)
  32. {
  33. return strcmp(m->dir, dir) == 0;
  34. }
  35. HTABLE_DEFINE_TYPE(struct manifest, manifest_name, dir_hash, dir_cmp,
  36. htable_manifest);
  37. static struct htable_manifest *manifests;
  38. const char *get_ccan_file_contents(struct ccan_file *f)
  39. {
  40. if (!f->contents) {
  41. f->contents = tal_grab_file(f, f->fullname,
  42. &f->contents_size);
  43. if (!f->contents)
  44. err(1, "Reading file %s", f->fullname);
  45. }
  46. return f->contents;
  47. }
  48. char **get_ccan_file_lines(struct ccan_file *f)
  49. {
  50. if (!f->lines)
  51. f->lines = tal_strsplit(f, get_ccan_file_contents(f), "\n",
  52. STR_EMPTY_OK);
  53. return f->lines;
  54. }
  55. struct ccan_file *new_ccan_file(const void *ctx, const char *dir,
  56. const char *name)
  57. {
  58. struct ccan_file *f;
  59. unsigned int i;
  60. assert(dir[0] == '/');
  61. f = tal(ctx, struct ccan_file);
  62. f->lines = NULL;
  63. f->line_info = NULL;
  64. f->doc_sections = NULL;
  65. for (i = 0; i < ARRAY_SIZE(f->compiled); i++)
  66. f->compiled[i] = NULL;
  67. f->name = tal_strdup(f, name);
  68. f->fullname = path_join(f, dir, f->name);
  69. f->contents = NULL;
  70. f->simplified = NULL;
  71. f->idempotent_cond = NULL;
  72. return f;
  73. }
  74. static void add_files(struct manifest *m, const char *base, const char *subdir)
  75. {
  76. DIR *d;
  77. struct dirent *ent;
  78. char **subs = tal_arr(m, char *, 0);
  79. const char *thisdir;
  80. if (!subdir)
  81. thisdir = base;
  82. else
  83. thisdir = path_join(subs, base, subdir);
  84. d = opendir(thisdir);
  85. if (!d)
  86. err(1, "Opening directory %s", thisdir);
  87. while ((ent = readdir(d)) != NULL) {
  88. struct stat st;
  89. struct ccan_file *f;
  90. struct list_head *dest;
  91. bool is_c_src;
  92. if (ent->d_name[0] == '.')
  93. continue;
  94. f = new_ccan_file(m, m->dir,
  95. subdir ? path_join(m, subdir, ent->d_name)
  96. : ent->d_name);
  97. if (lstat(f->fullname, &st) != 0)
  98. err(1, "lstat %s", f->fullname);
  99. if (S_ISDIR(st.st_mode)) {
  100. size_t len = tal_count(subs);
  101. tal_resize(&subs, len+1);
  102. subs[len] = tal_strcat(subs, f->name, "/");
  103. continue;
  104. }
  105. if (!S_ISREG(st.st_mode)) {
  106. tal_free(f);
  107. continue;
  108. }
  109. if (streq(f->name, "_info")) {
  110. m->info_file = f;
  111. continue;
  112. }
  113. is_c_src = strends(f->name, ".c");
  114. if (!is_c_src && !strends(f->name, ".h")) {
  115. dest = &m->other_files;
  116. } else if (!strchr(f->name, '/')) {
  117. if (is_c_src)
  118. dest = &m->c_files;
  119. else
  120. dest = &m->h_files;
  121. } else if (strstarts(f->name, "test/")) {
  122. if (is_c_src) {
  123. if (strstarts(f->name, "test/api"))
  124. dest = &m->api_tests;
  125. else if (strstarts(f->name, "test/run"))
  126. dest = &m->run_tests;
  127. else if (strstarts(f->name, "test/compile_ok"))
  128. dest = &m->compile_ok_tests;
  129. else if (strstarts(f->name, "test/compile_fail"))
  130. dest = &m->compile_fail_tests;
  131. else
  132. dest = &m->other_test_c_files;
  133. } else
  134. dest = &m->other_test_files;
  135. } else
  136. dest = &m->other_files;
  137. list_add(dest, &f->list);
  138. }
  139. closedir(d);
  140. /* Before we recurse, sanity check this is a ccan module. */
  141. if (!subdir) {
  142. size_t i;
  143. if (!m->info_file
  144. && list_empty(&m->c_files)
  145. && list_empty(&m->h_files))
  146. errx(1, "No _info, C or H files found here!");
  147. for (i = 0; i < tal_count(subs); i++)
  148. add_files(m, base, subs[i]);
  149. }
  150. tal_free(subs);
  151. }
  152. static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
  153. void *unused)
  154. {
  155. return strcmp((*a)->name, (*b)->name);
  156. }
  157. static void sort_files(struct list_head *list)
  158. {
  159. struct ccan_file **files = tal_arr(NULL, struct ccan_file *, 0), *f;
  160. unsigned int i;
  161. while ((f = list_top(list, struct ccan_file, list)) != NULL) {
  162. tal_expand(&files, &f, 1);
  163. list_del(&f->list);
  164. }
  165. asort(files, tal_count(files), cmp_names, NULL);
  166. for (i = 0; i < tal_count(files); i++)
  167. list_add_tail(list, &files[i]->list);
  168. tal_free(files);
  169. }
  170. struct manifest *get_manifest(const void *ctx, const char *dir)
  171. {
  172. struct manifest *m;
  173. char *canon_dir;
  174. unsigned int len;
  175. struct list_head *list;
  176. if (!manifests) {
  177. manifests = tal(NULL, struct htable_manifest);
  178. htable_manifest_init(manifests);
  179. }
  180. canon_dir = path_canon(ctx, dir);
  181. if (!canon_dir)
  182. err(1, "Getting canonical version of directory %s", dir);
  183. m = htable_manifest_get(manifests, canon_dir);
  184. if (m)
  185. return m;
  186. m = tal_linkable(tal(NULL, struct manifest));
  187. m->info_file = NULL;
  188. m->compiled[COMPILE_NORMAL] = m->compiled[COMPILE_NOFEAT] = NULL;
  189. m->dir = tal_steal(m, canon_dir);
  190. list_head_init(&m->c_files);
  191. list_head_init(&m->h_files);
  192. list_head_init(&m->api_tests);
  193. list_head_init(&m->run_tests);
  194. list_head_init(&m->compile_ok_tests);
  195. list_head_init(&m->compile_fail_tests);
  196. list_head_init(&m->other_test_c_files);
  197. list_head_init(&m->other_test_files);
  198. list_head_init(&m->other_files);
  199. list_head_init(&m->examples);
  200. list_head_init(&m->mangled_examples);
  201. list_head_init(&m->deps);
  202. list_head_init(&m->test_deps);
  203. /* Trim trailing /. */
  204. len = strlen(m->dir);
  205. while (len && m->dir[len-1] == '/')
  206. m->dir[--len] = '\0';
  207. m->basename = strrchr(m->dir, '/');
  208. if (!m->basename)
  209. errx(1, "I don't expect to be run from the root directory");
  210. m->basename++;
  211. assert(strstarts(m->dir, find_ccan_dir(m->dir)));
  212. m->modname = m->dir + strlen(find_ccan_dir(m->dir)) + strlen("ccan/");
  213. add_files(m, canon_dir, NULL);
  214. /* Nicer to run tests in a predictable order. */
  215. foreach_ptr(list, &m->api_tests, &m->run_tests, &m->compile_ok_tests,
  216. &m->compile_fail_tests)
  217. sort_files(list);
  218. htable_manifest_add(manifests, tal_link(manifests, m));
  219. return m;
  220. }