manifest.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/hash/hash.h>
  7. #include <ccan/htable/htable_type.h>
  8. #include <ccan/noerr/noerr.h>
  9. #include <ccan/foreach/foreach.h>
  10. #include <ccan/asort/asort.h>
  11. #include <ccan/array_size/array_size.h>
  12. #include <ccan/err/err.h>
  13. #include <unistd.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17. #include <errno.h>
  18. #include <dirent.h>
  19. #include <ctype.h>
  20. #include <stdarg.h>
  21. #include <assert.h>
  22. static size_t dir_hash(const char *name)
  23. {
  24. return hash(name, strlen(name), 0);
  25. }
  26. static const char *manifest_name(const struct manifest *m)
  27. {
  28. return m->dir;
  29. }
  30. static bool dir_cmp(const struct manifest *m, const char *dir)
  31. {
  32. return strcmp(m->dir, dir) == 0;
  33. }
  34. HTABLE_DEFINE_TYPE(struct manifest, manifest_name, dir_hash, dir_cmp,
  35. htable_manifest);
  36. static struct htable_manifest *manifests;
  37. const char *get_ccan_file_contents(struct ccan_file *f)
  38. {
  39. if (!f->contents) {
  40. f->contents = tal_grab_file(f, f->fullname,
  41. &f->contents_size);
  42. if (!f->contents)
  43. err(1, "Reading file %s", f->fullname);
  44. }
  45. return f->contents;
  46. }
  47. char **get_ccan_file_lines(struct ccan_file *f)
  48. {
  49. if (!f->lines)
  50. f->lines = tal_strsplit(f, get_ccan_file_contents(f), "\n",
  51. STR_EMPTY_OK);
  52. /* FIXME: is f->num_lines necessary? */
  53. f->num_lines = tal_count(f->lines) - 1;
  54. return f->lines;
  55. }
  56. struct ccan_file *new_ccan_file(const void *ctx, const char *dir, 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_steal(f, name);
  68. f->fullname = tal_fmt(f, "%s/%s", 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 *dir)
  74. {
  75. DIR *d;
  76. struct dirent *ent;
  77. char **subs = tal_arr(m, char *, 0);
  78. if (dir[0])
  79. d = opendir(dir);
  80. else
  81. d = opendir(".");
  82. if (!d)
  83. err(1, "Opening directory %s", dir[0] ? dir : ".");
  84. while ((ent = readdir(d)) != NULL) {
  85. struct stat st;
  86. struct ccan_file *f;
  87. struct list_head *dest;
  88. bool is_c_src;
  89. if (ent->d_name[0] == '.')
  90. continue;
  91. f = new_ccan_file(m, m->dir,
  92. tal_fmt(m, "%s%s", dir, ent->d_name));
  93. if (lstat(f->name, &st) != 0)
  94. err(1, "lstat %s", f->name);
  95. if (S_ISDIR(st.st_mode)) {
  96. size_t len = tal_count(subs);
  97. tal_resize(&subs, len+1);
  98. subs[len] = tal_strcat(subs, f->name, "/");
  99. continue;
  100. }
  101. if (!S_ISREG(st.st_mode)) {
  102. tal_free(f);
  103. continue;
  104. }
  105. if (streq(f->name, "_info")) {
  106. m->info_file = f;
  107. continue;
  108. }
  109. is_c_src = strends(f->name, ".c");
  110. if (!is_c_src && !strends(f->name, ".h")) {
  111. dest = &m->other_files;
  112. } else if (!strchr(f->name, '/')) {
  113. if (is_c_src)
  114. dest = &m->c_files;
  115. else
  116. dest = &m->h_files;
  117. } else if (strstarts(f->name, "test/")) {
  118. if (is_c_src) {
  119. if (strstarts(f->name, "test/api"))
  120. dest = &m->api_tests;
  121. else if (strstarts(f->name, "test/run"))
  122. dest = &m->run_tests;
  123. else if (strstarts(f->name, "test/compile_ok"))
  124. dest = &m->compile_ok_tests;
  125. else if (strstarts(f->name, "test/compile_fail"))
  126. dest = &m->compile_fail_tests;
  127. else
  128. dest = &m->other_test_c_files;
  129. } else
  130. dest = &m->other_test_files;
  131. } else
  132. dest = &m->other_files;
  133. list_add(dest, &f->list);
  134. }
  135. closedir(d);
  136. /* Before we recurse, sanity check this is a ccan module. */
  137. if (!dir[0]) {
  138. size_t i;
  139. if (!m->info_file
  140. && list_empty(&m->c_files)
  141. && list_empty(&m->h_files))
  142. errx(1, "No _info, C or H files found here!");
  143. for (i = 0; i < tal_count(subs); i++)
  144. add_files(m, subs[i]);
  145. }
  146. tal_free(subs);
  147. }
  148. static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
  149. void *unused)
  150. {
  151. return strcmp((*a)->name, (*b)->name);
  152. }
  153. static void sort_files(struct list_head *list)
  154. {
  155. struct ccan_file **files = tal_arr(NULL, struct ccan_file *, 0), *f;
  156. unsigned int i;
  157. while ((f = list_top(list, struct ccan_file, list)) != NULL) {
  158. tal_expand(&files, &f, 1);
  159. list_del(&f->list);
  160. }
  161. asort(files, tal_count(files), cmp_names, NULL);
  162. for (i = 0; i < tal_count(files); i++)
  163. list_add_tail(list, &files[i]->list);
  164. tal_free(files);
  165. }
  166. struct manifest *get_manifest(const void *ctx, const char *dir)
  167. {
  168. struct manifest *m;
  169. char *olddir, *canon_dir;
  170. unsigned int len;
  171. struct list_head *list;
  172. if (!manifests) {
  173. manifests = tal(NULL, struct htable_manifest);
  174. htable_manifest_init(manifests);
  175. }
  176. olddir = tal_getcwd(NULL);
  177. if (!olddir)
  178. err(1, "Getting current directory");
  179. if (chdir(dir) != 0)
  180. err(1, "Failed to chdir to %s", dir);
  181. canon_dir = tal_getcwd(olddir);
  182. if (!canon_dir)
  183. err(1, "Getting current directory");
  184. m = htable_manifest_get(manifests, canon_dir);
  185. if (m)
  186. goto done;
  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, "");
  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. done:
  221. if (chdir(olddir) != 0)
  222. err(1, "Returning to original directory '%s'", olddir);
  223. tal_free(olddir);
  224. return m;
  225. }