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