manifest.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 (stat(f->fullname, &st) != 0)
  99. err(1, "stat %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. /* Don't enter subdirs with _info: they're separate modules. */
  149. for (i = 0; i < tal_count(subs); i++) {
  150. struct stat st;
  151. char *subinfo = path_join(subs, base,
  152. path_join(subs, subs[i],
  153. "_info"));
  154. if (lstat(subinfo, &st) != 0)
  155. add_files(m, base, subs[i]);
  156. }
  157. }
  158. tal_free(subs);
  159. }
  160. static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
  161. void *unused)
  162. {
  163. return strcmp((*a)->name, (*b)->name);
  164. }
  165. static void sort_files(struct list_head *list)
  166. {
  167. struct ccan_file **files = tal_arr(NULL, struct ccan_file *, 0), *f;
  168. unsigned int i;
  169. while ((f = list_top(list, struct ccan_file, list)) != NULL) {
  170. tal_expand(&files, &f, 1);
  171. list_del(&f->list);
  172. }
  173. asort(files, tal_count(files), cmp_names, NULL);
  174. for (i = 0; i < tal_count(files); i++)
  175. list_add_tail(list, &files[i]->list);
  176. tal_free(files);
  177. }
  178. struct manifest *get_manifest(const void *ctx, const char *dir)
  179. {
  180. struct manifest *m;
  181. char *canon_dir;
  182. unsigned int len;
  183. struct list_head *list;
  184. if (!manifests) {
  185. manifests = tal(NULL, struct htable_manifest);
  186. htable_manifest_init(manifests);
  187. }
  188. canon_dir = path_canon(ctx, dir);
  189. if (!canon_dir)
  190. err(1, "Getting canonical version of directory %s", dir);
  191. m = htable_manifest_get(manifests, canon_dir);
  192. if (m)
  193. return m;
  194. m = tal_linkable(tal(NULL, struct manifest));
  195. m->info_file = NULL;
  196. m->compiled[COMPILE_NORMAL] = m->compiled[COMPILE_NOFEAT] = NULL;
  197. m->dir = tal_steal(m, canon_dir);
  198. list_head_init(&m->c_files);
  199. list_head_init(&m->h_files);
  200. list_head_init(&m->api_tests);
  201. list_head_init(&m->run_tests);
  202. list_head_init(&m->compile_ok_tests);
  203. list_head_init(&m->compile_fail_tests);
  204. list_head_init(&m->other_test_c_files);
  205. list_head_init(&m->other_test_files);
  206. list_head_init(&m->other_files);
  207. list_head_init(&m->examples);
  208. list_head_init(&m->mangled_examples);
  209. list_head_init(&m->deps);
  210. list_head_init(&m->test_deps);
  211. /* Trim trailing /. */
  212. len = strlen(m->dir);
  213. while (len && m->dir[len-1] == '/')
  214. m->dir[--len] = '\0';
  215. m->basename = strrchr(m->dir, '/');
  216. if (!m->basename)
  217. errx(1, "I don't expect to be run from the root directory");
  218. m->basename++;
  219. assert(strstarts(m->dir, find_ccan_dir(m->dir)));
  220. m->modname = m->dir + strlen(find_ccan_dir(m->dir)) + strlen("ccan/");
  221. add_files(m, canon_dir, NULL);
  222. /* Nicer to run tests in a predictable order. */
  223. foreach_ptr(list, &m->api_tests, &m->run_tests, &m->compile_ok_tests,
  224. &m->compile_fail_tests)
  225. sort_files(list);
  226. htable_manifest_add(manifests, tal_link(manifests, m));
  227. return m;
  228. }