manifest.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include "config.h"
  2. #include "manifest.h"
  3. #include "tools.h"
  4. #include <ccan/talloc/talloc.h>
  5. #include <ccan/str/str.h>
  6. #include <ccan/str_talloc/str_talloc.h>
  7. #include <ccan/talloc_link/talloc_link.h>
  8. #include <ccan/hash/hash.h>
  9. #include <ccan/htable/htable_type.h>
  10. #include <ccan/grab_file/grab_file.h>
  11. #include <ccan/noerr/noerr.h>
  12. #include <ccan/foreach/foreach.h>
  13. #include <ccan/asort/asort.h>
  14. #include <ccan/array_size/array_size.h>
  15. #include <unistd.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <fcntl.h>
  19. #include <err.h>
  20. #include <errno.h>
  21. #include <dirent.h>
  22. #include <ctype.h>
  23. #include <stdarg.h>
  24. #include <assert.h>
  25. const char *ccan_dir;
  26. static size_t dir_hash(const char *name)
  27. {
  28. return hash(name, strlen(name), 0);
  29. }
  30. static const char *manifest_name(const struct manifest *m)
  31. {
  32. return m->dir;
  33. }
  34. static bool dir_cmp(const struct manifest *m, const char *dir)
  35. {
  36. return strcmp(m->dir, dir) == 0;
  37. }
  38. HTABLE_DEFINE_TYPE(struct manifest, manifest_name, dir_hash, dir_cmp,
  39. htable_manifest);
  40. static struct htable_manifest *manifests;
  41. const char *get_ccan_file_contents(struct ccan_file *f)
  42. {
  43. if (!f->contents) {
  44. f->contents = grab_file(f, f->fullname, &f->contents_size);
  45. if (!f->contents)
  46. err(1, "Reading file %s", f->fullname);
  47. }
  48. return f->contents;
  49. }
  50. char **get_ccan_file_lines(struct ccan_file *f)
  51. {
  52. if (!f->lines)
  53. f->lines = strsplit(f, get_ccan_file_contents(f), "\n");
  54. /* FIXME: is f->num_lines necessary? */
  55. f->num_lines = talloc_array_length(f->lines) - 1;
  56. return f->lines;
  57. }
  58. struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name)
  59. {
  60. struct ccan_file *f;
  61. unsigned int i;
  62. assert(dir[0] == '/');
  63. f = talloc(ctx, struct ccan_file);
  64. f->lines = NULL;
  65. f->line_info = NULL;
  66. f->doc_sections = NULL;
  67. for (i = 0; i < ARRAY_SIZE(f->compiled); i++)
  68. f->compiled[i] = NULL;
  69. f->name = talloc_steal(f, name);
  70. f->fullname = talloc_asprintf(f, "%s/%s", dir, f->name);
  71. f->contents = NULL;
  72. f->simplified = NULL;
  73. return f;
  74. }
  75. static void add_files(struct manifest *m, const char *dir)
  76. {
  77. DIR *d;
  78. struct dirent *ent;
  79. char **subs = NULL;
  80. if (dir[0])
  81. d = opendir(dir);
  82. else
  83. d = opendir(".");
  84. if (!d)
  85. err(1, "Opening directory %s", dir[0] ? dir : ".");
  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. talloc_asprintf(m, "%s%s",
  95. dir, ent->d_name));
  96. if (lstat(f->name, &st) != 0)
  97. err(1, "lstat %s", f->name);
  98. if (S_ISDIR(st.st_mode)) {
  99. size_t len = talloc_array_length(subs);
  100. subs = talloc_realloc(m, subs, char *, len+1);
  101. subs[len] = talloc_append_string(f->name, "/");
  102. continue;
  103. }
  104. if (!S_ISREG(st.st_mode)) {
  105. talloc_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 (!dir[0]) {
  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 < talloc_array_length(subs); i++)
  147. add_files(m, subs[i]);
  148. }
  149. talloc_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 = NULL, *f;
  159. unsigned int i, num;
  160. num = 0;
  161. while ((f = list_top(list, struct ccan_file, list)) != NULL) {
  162. files = talloc_realloc(NULL, files, struct ccan_file *, num+1);
  163. files[num++] = f;
  164. list_del(&f->list);
  165. }
  166. asort(files, num, cmp_names, NULL);
  167. for (i = 0; i < num; i++)
  168. list_add_tail(list, &files[i]->list);
  169. talloc_free(files);
  170. }
  171. /* Walk up tp find /ccan/ => ccan directory. */
  172. static unsigned int ccan_dir_prefix(const char *fulldir)
  173. {
  174. unsigned int i;
  175. assert(fulldir[0] == '/');
  176. for (i = strlen(fulldir) - 1; i > 0; i--) {
  177. if (strncmp(fulldir+i, "/ccan", 5) != 0)
  178. continue;
  179. if (fulldir[i+5] != '\0' && fulldir[i+5] != '/')
  180. continue;
  181. return i + 1;
  182. }
  183. errx(1, "Could not find /ccan/ dir in %s", fulldir);
  184. }
  185. struct manifest *get_manifest(const void *ctx, const char *dir)
  186. {
  187. struct manifest *m;
  188. char *olddir, *canon_dir;
  189. unsigned int len;
  190. struct list_head *list;
  191. if (!manifests) {
  192. manifests = talloc(NULL, struct htable_manifest);
  193. htable_manifest_init(manifests);
  194. }
  195. olddir = talloc_getcwd(NULL);
  196. if (!olddir)
  197. err(1, "Getting current directory");
  198. if (chdir(dir) != 0)
  199. err(1, "Failed to chdir to %s", dir);
  200. canon_dir = talloc_getcwd(olddir);
  201. if (!canon_dir)
  202. err(1, "Getting current directory");
  203. m = htable_manifest_get(manifests, canon_dir);
  204. if (m)
  205. goto done;
  206. m = talloc_linked(ctx, talloc(NULL, struct manifest));
  207. m->info_file = NULL;
  208. m->compiled[COMPILE_NORMAL] = m->compiled[COMPILE_NOFEAT] = NULL;
  209. m->dir = talloc_steal(m, canon_dir);
  210. list_head_init(&m->c_files);
  211. list_head_init(&m->h_files);
  212. list_head_init(&m->api_tests);
  213. list_head_init(&m->run_tests);
  214. list_head_init(&m->compile_ok_tests);
  215. list_head_init(&m->compile_fail_tests);
  216. list_head_init(&m->other_test_c_files);
  217. list_head_init(&m->other_test_files);
  218. list_head_init(&m->other_files);
  219. list_head_init(&m->examples);
  220. list_head_init(&m->mangled_examples);
  221. list_head_init(&m->deps);
  222. len = strlen(m->dir);
  223. while (len && m->dir[len-1] == '/')
  224. m->dir[--len] = '\0';
  225. m->basename = strrchr(m->dir, '/');
  226. if (!m->basename)
  227. errx(1, "I don't expect to be run from the root directory");
  228. m->basename++;
  229. if (!ccan_dir) {
  230. unsigned int prefix = ccan_dir_prefix(m->dir);
  231. ccan_dir = talloc_strndup(NULL, m->dir, prefix);
  232. }
  233. add_files(m, "");
  234. /* Nicer to run tests in a predictable order. */
  235. foreach_ptr(list, &m->api_tests, &m->run_tests, &m->compile_ok_tests,
  236. &m->compile_fail_tests)
  237. sort_files(list);
  238. htable_manifest_add(manifests, m);
  239. done:
  240. if (chdir(olddir) != 0)
  241. err(1, "Returning to original directory '%s'", olddir);
  242. talloc_free(olddir);
  243. return m;
  244. }