ccanlint.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * ccanlint: assorted checks and advice for a ccan package
  3. * Copyright (C) 2008 Rusty Russell, Idris Soule
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 2 of the License, or (at your option)
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include "ccanlint.h"
  20. #include "../tools.h"
  21. #include <unistd.h>
  22. #include <getopt.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <err.h>
  28. #include <ctype.h>
  29. #include <ccan/talloc/talloc.h>
  30. static unsigned int verbose = 0;
  31. static LIST_HEAD(compulsory_tests);
  32. static LIST_HEAD(normal_tests);
  33. static LIST_HEAD(finished_tests);
  34. bool safe_mode = false;
  35. static void usage(const char *name)
  36. {
  37. fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-d <dirname>]\n"
  38. " -v: verbose mode\n"
  39. " -s: simply give one line summary\n"
  40. " -d: use this directory instead of the current one\n"
  41. " -n: do not compile anything\n",
  42. name);
  43. exit(1);
  44. }
  45. #if 0
  46. static void indent_print(const char *string)
  47. {
  48. while (*string) {
  49. unsigned int line = strcspn(string, "\n");
  50. printf("\t%.*s", line, string);
  51. if (string[line] == '\n') {
  52. printf("\n");
  53. line++;
  54. }
  55. string += line;
  56. }
  57. }
  58. #endif
  59. bool ask(const char *question)
  60. {
  61. char reply[2];
  62. printf("%s ", question);
  63. fflush(stdout);
  64. return fgets(reply, sizeof(reply), stdin) != NULL
  65. && toupper(reply[0]) == 'Y';
  66. }
  67. static const char *should_skip(struct manifest *m, struct ccanlint *i)
  68. {
  69. if (i->skip_fail)
  70. return "dependency failed";
  71. if (i->skip)
  72. return "dependency was skipped";
  73. if (i->can_run)
  74. return i->can_run(m);
  75. return NULL;
  76. }
  77. static bool run_test(struct ccanlint *i,
  78. bool quiet,
  79. unsigned int *score,
  80. unsigned int *total_score,
  81. struct manifest *m)
  82. {
  83. void *result;
  84. unsigned int this_score;
  85. const struct dependent *d;
  86. const char *skip;
  87. //one less test to run through
  88. list_for_each(&i->dependencies, d, node)
  89. d->dependent->num_depends--;
  90. skip = should_skip(m, i);
  91. if (skip) {
  92. if (verbose)
  93. printf(" %s: skipped (%s)\n", i->name, skip);
  94. /* If we're skipping this because a prereq failed, we fail. */
  95. if (i->skip_fail)
  96. *total_score += i->total_score;
  97. list_del(&i->list);
  98. list_add_tail(&finished_tests, &i->list);
  99. list_for_each(&i->dependencies, d, node) {
  100. d->dependent->skip = true;
  101. d->dependent->skip_fail = i->skip_fail;
  102. }
  103. return true;
  104. }
  105. result = i->check(m);
  106. if (!result) {
  107. if (verbose)
  108. printf(" %s: OK\n", i->name);
  109. if (i->total_score) {
  110. *score += i->total_score;
  111. *total_score += i->total_score;
  112. }
  113. list_del(&i->list);
  114. list_add_tail(&finished_tests, &i->list);
  115. return true;
  116. }
  117. if (i->score)
  118. this_score = i->score(m, result);
  119. else
  120. this_score = 0;
  121. list_del(&i->list);
  122. list_add_tail(&finished_tests, &i->list);
  123. *total_score += i->total_score;
  124. *score += this_score;
  125. if (!quiet) {
  126. printf("%s\n", i->describe(m, result));
  127. if (i->handle)
  128. i->handle(m, result);
  129. }
  130. /* Skip any tests which depend on this one. */
  131. list_for_each(&i->dependencies, d, node) {
  132. d->dependent->skip = true;
  133. d->dependent->skip_fail = true;
  134. }
  135. return false;
  136. }
  137. static void register_test(struct list_head *h, struct ccanlint *test, ...)
  138. {
  139. va_list ap;
  140. struct ccanlint *depends;
  141. struct dependent *dchild;
  142. list_add(h, &test->list);
  143. va_start(ap, test);
  144. /* Careful: we might have been initialized by a dependent. */
  145. if (test->dependencies.n.next == NULL)
  146. list_head_init(&test->dependencies);
  147. //dependent(s) args (if any), last one is NULL
  148. while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
  149. dchild = malloc(sizeof(*dchild));
  150. dchild->dependent = test;
  151. /* The thing we depend on might not be initialized yet! */
  152. if (depends->dependencies.n.next == NULL)
  153. list_head_init(&depends->dependencies);
  154. list_add_tail(&depends->dependencies, &dchild->node);
  155. test->num_depends++;
  156. }
  157. va_end(ap);
  158. }
  159. /**
  160. * get_next_test - retrieves the next test to be processed
  161. **/
  162. static inline struct ccanlint *get_next_test(struct list_head *test)
  163. {
  164. struct ccanlint *i;
  165. if (list_empty(test))
  166. return NULL;
  167. list_for_each(test, i, list) {
  168. if (i->num_depends == 0)
  169. return i;
  170. }
  171. errx(1, "Can't make process; test dependency cycle");
  172. }
  173. static void init_tests(void)
  174. {
  175. const struct ccanlint *i;
  176. #undef REGISTER_TEST
  177. #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__)
  178. #include "generated-normal-tests"
  179. #undef REGISTER_TEST
  180. #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__)
  181. #include "generated-compulsory-tests"
  182. if (!verbose)
  183. return;
  184. printf("\nCompulsory Tests\n");
  185. list_for_each(&compulsory_tests, i, list) {
  186. printf("%s depends on %u others\n", i->name, i->num_depends);
  187. if (!list_empty(&i->dependencies)) {
  188. const struct dependent *d;
  189. printf("These depend on us:\n");
  190. list_for_each(&i->dependencies, d, node)
  191. printf("\t%s\n", d->dependent->name);
  192. }
  193. }
  194. printf("\nNormal Tests\n");
  195. list_for_each(&normal_tests, i, list) {
  196. printf("%s depends on %u others\n", i->name, i->num_depends);
  197. if (!list_empty(&i->dependencies)) {
  198. const struct dependent *d;
  199. printf("These depend on us:\n");
  200. list_for_each(&i->dependencies, d, node)
  201. printf("\t%s\n", d->dependent->name);
  202. }
  203. }
  204. }
  205. int main(int argc, char *argv[])
  206. {
  207. int c;
  208. bool summary = false;
  209. unsigned int score = 0, total_score = 0;
  210. struct manifest *m;
  211. struct ccanlint *i;
  212. const char *prefix = "", *dir = ".";
  213. /* I'd love to use long options, but that's not standard. */
  214. /* FIXME: getopt_long ccan package? */
  215. while ((c = getopt(argc, argv, "sd:vn")) != -1) {
  216. switch (c) {
  217. case 'd':
  218. dir = optarg;
  219. prefix = talloc_append_string(talloc_basename(NULL,
  220. optarg),
  221. ": ");
  222. break;
  223. case 's':
  224. summary = true;
  225. break;
  226. case 'v':
  227. verbose++;
  228. break;
  229. case 'n':
  230. safe_mode = true;
  231. break;
  232. default:
  233. usage(argv[0]);
  234. }
  235. }
  236. if (optind < argc)
  237. usage(argv[0]);
  238. m = get_manifest(talloc_autofree_context(), dir);
  239. init_tests();
  240. /* If you don't pass the compulsory tests, you don't even get a score */
  241. if (verbose)
  242. printf("Compulsory tests:\n");
  243. while ((i = get_next_test(&compulsory_tests)) != NULL) {
  244. if (!run_test(i, summary, &score, &total_score, m)) {
  245. errx(1, "%s%s failed", prefix, i->name);
  246. }
  247. }
  248. if (verbose)
  249. printf("\nNormal tests:\n");
  250. while ((i = get_next_test(&normal_tests)) != NULL)
  251. run_test(i, summary, &score, &total_score, m);
  252. printf("%sTotal score: %u/%u\n", prefix, score, total_score);
  253. return 0;
  254. }