manifest.c 6.3 KB

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