manifest.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. return f;
  72. }
  73. static void add_files(struct manifest *m, const char *base, const char *subdir)
  74. {
  75. DIR *d;
  76. struct dirent *ent;
  77. char **subs = tal_arr(m, char *, 0);
  78. const char *thisdir;
  79. if (!subdir)
  80. thisdir = base;
  81. else
  82. thisdir = path_join(subs, base, subdir);
  83. d = opendir(thisdir);
  84. if (!d)
  85. err(1, "Opening directory %s", thisdir);
  86. while ((ent = readdir(d)) != NULL) {
  87. struct stat st;
  88. struct ccan_file *f;
  89. struct list_head *dest;
  90. bool is_c_src;
  91. if (ent->d_name[0] == '.')
  92. continue;
  93. f = new_ccan_file(m, m->dir,
  94. subdir ? path_join(m, subdir, ent->d_name)
  95. : ent->d_name);
  96. if (lstat(f->fullname, &st) != 0)
  97. err(1, "lstat %s", f->fullname);
  98. if (S_ISDIR(st.st_mode)) {
  99. size_t len = tal_count(subs);
  100. tal_resize(&subs, len+1);
  101. subs[len] = tal_strcat(subs, f->name, "/");
  102. continue;
  103. }
  104. if (!S_ISREG(st.st_mode)) {
  105. tal_free(f);
  106. continue;
  107. }
  108. if (streq(f->name, "_info")) {
  109. m->info_file = f;
  110. continue;
  111. }
  112. is_c_src = strends(f->name, ".c");
  113. if (!is_c_src && !strends(f->name, ".h")) {
  114. dest = &m->other_files;
  115. } else if (!strchr(f->name, '/')) {
  116. if (is_c_src)
  117. dest = &m->c_files;
  118. else
  119. dest = &m->h_files;
  120. } else if (strstarts(f->name, "test/")) {
  121. if (is_c_src) {
  122. if (strstarts(f->name, "test/api"))
  123. dest = &m->api_tests;
  124. else if (strstarts(f->name, "test/run"))
  125. dest = &m->run_tests;
  126. else if (strstarts(f->name, "test/compile_ok"))
  127. dest = &m->compile_ok_tests;
  128. else if (strstarts(f->name, "test/compile_fail"))
  129. dest = &m->compile_fail_tests;
  130. else
  131. dest = &m->other_test_c_files;
  132. } else
  133. dest = &m->other_test_files;
  134. } else
  135. dest = &m->other_files;
  136. list_add(dest, &f->list);
  137. }
  138. closedir(d);
  139. /* Before we recurse, sanity check this is a ccan module. */
  140. if (!subdir) {
  141. size_t i;
  142. if (!m->info_file
  143. && list_empty(&m->c_files)
  144. && list_empty(&m->h_files))
  145. errx(1, "No _info, C or H files found here!");
  146. for (i = 0; i < tal_count(subs); i++)
  147. add_files(m, base, subs[i]);
  148. }
  149. tal_free(subs);
  150. }
  151. static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
  152. void *unused)
  153. {
  154. return strcmp((*a)->name, (*b)->name);
  155. }
  156. static void sort_files(struct list_head *list)
  157. {
  158. struct ccan_file **files = tal_arr(NULL, struct ccan_file *, 0), *f;
  159. unsigned int i;
  160. while ((f = list_top(list, struct ccan_file, list)) != NULL) {
  161. tal_expand(&files, &f, 1);
  162. list_del(&f->list);
  163. }
  164. asort(files, tal_count(files), cmp_names, NULL);
  165. for (i = 0; i < tal_count(files); i++)
  166. list_add_tail(list, &files[i]->list);
  167. tal_free(files);
  168. }
  169. struct manifest *get_manifest(const void *ctx, const char *dir)
  170. {
  171. struct manifest *m;
  172. char *canon_dir;
  173. unsigned int len;
  174. struct list_head *list;
  175. if (!manifests) {
  176. manifests = tal(NULL, struct htable_manifest);
  177. htable_manifest_init(manifests);
  178. }
  179. canon_dir = path_canon(ctx, dir);
  180. if (!canon_dir)
  181. err(1, "Getting canonical version of directory %s", dir);
  182. m = htable_manifest_get(manifests, canon_dir);
  183. if (m)
  184. return m;
  185. m = tal_linkable(tal(NULL, struct manifest));
  186. m->info_file = NULL;
  187. m->compiled[COMPILE_NORMAL] = m->compiled[COMPILE_NOFEAT] = NULL;
  188. m->dir = tal_steal(m, canon_dir);
  189. list_head_init(&m->c_files);
  190. list_head_init(&m->h_files);
  191. list_head_init(&m->api_tests);
  192. list_head_init(&m->run_tests);
  193. list_head_init(&m->compile_ok_tests);
  194. list_head_init(&m->compile_fail_tests);
  195. list_head_init(&m->other_test_c_files);
  196. list_head_init(&m->other_test_files);
  197. list_head_init(&m->other_files);
  198. list_head_init(&m->examples);
  199. list_head_init(&m->mangled_examples);
  200. list_head_init(&m->deps);
  201. list_head_init(&m->test_deps);
  202. /* Trim trailing /. */
  203. len = strlen(m->dir);
  204. while (len && m->dir[len-1] == '/')
  205. m->dir[--len] = '\0';
  206. m->basename = strrchr(m->dir, '/');
  207. if (!m->basename)
  208. errx(1, "I don't expect to be run from the root directory");
  209. m->basename++;
  210. assert(strstarts(m->dir, find_ccan_dir(m->dir)));
  211. m->modname = m->dir + strlen(find_ccan_dir(m->dir)) + strlen("ccan/");
  212. add_files(m, canon_dir, NULL);
  213. /* Nicer to run tests in a predictable order. */
  214. foreach_ptr(list, &m->api_tests, &m->run_tests, &m->compile_ok_tests,
  215. &m->compile_fail_tests)
  216. sort_files(list);
  217. htable_manifest_add(manifests, tal_link(manifests, m));
  218. return m;
  219. }