ccanlint.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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", i->name);
  109. if (i->total_score)
  110. printf(" (+%u/%u)",
  111. i->total_score, i->total_score);
  112. printf("\n");
  113. }
  114. if (i->total_score) {
  115. *score += i->total_score;
  116. *total_score += i->total_score;
  117. }
  118. list_del(&i->list);
  119. list_add_tail(&finished_tests, &i->list);
  120. return true;
  121. }
  122. if (i->score)
  123. this_score = i->score(m, result);
  124. else
  125. this_score = 0;
  126. list_del(&i->list);
  127. list_add_tail(&finished_tests, &i->list);
  128. *total_score += i->total_score;
  129. *score += this_score;
  130. if (verbose) {
  131. printf(" %s: FAIL (+%u/%u)\n",
  132. i->name, this_score, i->total_score);
  133. }
  134. if (!quiet) {
  135. printf("%s\n", i->describe(m, result));
  136. if (i->handle)
  137. i->handle(m, result);
  138. }
  139. /* Skip any tests which depend on this one. */
  140. list_for_each(&i->dependencies, d, node) {
  141. d->dependent->skip = true;
  142. d->dependent->skip_fail = true;
  143. }
  144. return false;
  145. }
  146. static void register_test(struct list_head *h, struct ccanlint *test, ...)
  147. {
  148. va_list ap;
  149. struct ccanlint *depends;
  150. struct dependent *dchild;
  151. list_add(h, &test->list);
  152. va_start(ap, test);
  153. /* Careful: we might have been initialized by a dependent. */
  154. if (test->dependencies.n.next == NULL)
  155. list_head_init(&test->dependencies);
  156. //dependent(s) args (if any), last one is NULL
  157. while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
  158. dchild = malloc(sizeof(*dchild));
  159. dchild->dependent = test;
  160. /* The thing we depend on might not be initialized yet! */
  161. if (depends->dependencies.n.next == NULL)
  162. list_head_init(&depends->dependencies);
  163. list_add_tail(&depends->dependencies, &dchild->node);
  164. test->num_depends++;
  165. }
  166. va_end(ap);
  167. }
  168. /**
  169. * get_next_test - retrieves the next test to be processed
  170. **/
  171. static inline struct ccanlint *get_next_test(struct list_head *test)
  172. {
  173. struct ccanlint *i;
  174. if (list_empty(test))
  175. return NULL;
  176. list_for_each(test, i, list) {
  177. if (i->num_depends == 0)
  178. return i;
  179. }
  180. errx(1, "Can't make process; test dependency cycle");
  181. }
  182. static void init_tests(void)
  183. {
  184. const struct ccanlint *i;
  185. #undef REGISTER_TEST
  186. #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__)
  187. #include "generated-normal-tests"
  188. #undef REGISTER_TEST
  189. #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__)
  190. #include "generated-compulsory-tests"
  191. if (!verbose)
  192. return;
  193. printf("\nCompulsory Tests\n");
  194. list_for_each(&compulsory_tests, i, list) {
  195. printf("%s depends on %u others\n", i->name, i->num_depends);
  196. if (!list_empty(&i->dependencies)) {
  197. const struct dependent *d;
  198. printf("These depend on us:\n");
  199. list_for_each(&i->dependencies, d, node)
  200. printf("\t%s\n", d->dependent->name);
  201. }
  202. }
  203. printf("\nNormal Tests\n");
  204. list_for_each(&normal_tests, i, list) {
  205. printf("%s depends on %u others\n", i->name, i->num_depends);
  206. if (!list_empty(&i->dependencies)) {
  207. const struct dependent *d;
  208. printf("These depend on us:\n");
  209. list_for_each(&i->dependencies, d, node)
  210. printf("\t%s\n", d->dependent->name);
  211. }
  212. }
  213. }
  214. int main(int argc, char *argv[])
  215. {
  216. int c;
  217. bool summary = false;
  218. unsigned int score = 0, total_score = 0;
  219. struct manifest *m;
  220. struct ccanlint *i;
  221. const char *prefix = "", *dir = ".";
  222. /* I'd love to use long options, but that's not standard. */
  223. /* FIXME: getopt_long ccan package? */
  224. while ((c = getopt(argc, argv, "sd:vn")) != -1) {
  225. switch (c) {
  226. case 'd':
  227. dir = optarg;
  228. prefix = talloc_append_string(talloc_basename(NULL,
  229. optarg),
  230. ": ");
  231. break;
  232. case 's':
  233. summary = true;
  234. break;
  235. case 'v':
  236. verbose++;
  237. break;
  238. case 'n':
  239. safe_mode = true;
  240. break;
  241. default:
  242. usage(argv[0]);
  243. }
  244. }
  245. if (optind < argc)
  246. usage(argv[0]);
  247. m = get_manifest(talloc_autofree_context(), dir);
  248. init_tests();
  249. /* If you don't pass the compulsory tests, you don't even get a score */
  250. if (verbose)
  251. printf("Compulsory tests:\n");
  252. while ((i = get_next_test(&compulsory_tests)) != NULL) {
  253. if (!run_test(i, summary, &score, &total_score, m)) {
  254. errx(1, "%s%s failed", prefix, i->name);
  255. }
  256. }
  257. if (verbose)
  258. printf("\nNormal tests:\n");
  259. while ((i = get_next_test(&normal_tests)) != NULL)
  260. run_test(i, summary, &score, &total_score, m);
  261. printf("%sTotal score: %u/%u\n", prefix, score, total_score);
  262. return 0;
  263. }