manifest.c 6.1 KB

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