ccanlint.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. * ccanlint: assorted checks and advice for a ccan package
  3. * Copyright (C) 2008 Rusty Russell, Idris Soule
  4. * Copyright (C) 2010 Rusty Russell, Idris Soule
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc., 51
  18. * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "ccanlint.h"
  21. #include "../tools.h"
  22. #include "../read_config_header.h"
  23. #include <unistd.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <err.h>
  28. #include <ctype.h>
  29. #include <ccan/str/str.h>
  30. #include <ccan/take/take.h>
  31. #include <ccan/opt/opt.h>
  32. #include <ccan/foreach/foreach.h>
  33. #include <ccan/cast/cast.h>
  34. #include <ccan/tlist/tlist.h>
  35. #include <ccan/tal/path/path.h>
  36. #include <ccan/strmap/strmap.h>
  37. struct ccanlint_map {
  38. STRMAP_MEMBERS(struct ccanlint *);
  39. };
  40. int verbose = 0;
  41. static struct ccanlint_map tests;
  42. bool safe_mode = false;
  43. bool keep_results = false;
  44. bool non_ccan_deps = false;
  45. bool build_failed = false;
  46. static bool targeting = false;
  47. static unsigned int timeout;
  48. const char *config_header;
  49. const char *ccan_dir;
  50. #if 0
  51. static void indent_print(const char *string)
  52. {
  53. while (*string) {
  54. unsigned int line = strcspn(string, "\n");
  55. printf("\t%.*s", line, string);
  56. if (string[line] == '\n') {
  57. printf("\n");
  58. line++;
  59. }
  60. string += line;
  61. }
  62. }
  63. #endif
  64. bool ask(const char *question)
  65. {
  66. char reply[80];
  67. printf("%s ", question);
  68. fflush(stdout);
  69. return fgets(reply, sizeof(reply), stdin) != NULL
  70. && toupper(reply[0]) == 'Y';
  71. }
  72. /* Skip, but don't remove. */
  73. static bool skip_test(struct dgraph_node *node, const char *why)
  74. {
  75. struct ccanlint *c = container_of(node, struct ccanlint, node);
  76. c->skip = why;
  77. return true;
  78. }
  79. static const char *dep_failed(struct manifest *m)
  80. {
  81. return "dependency couldn't run";
  82. }
  83. static bool cannot_run(struct dgraph_node *node, void *all)
  84. {
  85. struct ccanlint *c = container_of(node, struct ccanlint, node);
  86. c->can_run = dep_failed;
  87. return true;
  88. }
  89. struct run_info {
  90. bool quiet;
  91. unsigned int score, total;
  92. struct manifest *m;
  93. const char *prefix;
  94. bool pass;
  95. };
  96. static bool run_test(struct dgraph_node *n, struct run_info *run)
  97. {
  98. struct ccanlint *i = container_of(n, struct ccanlint, node);
  99. unsigned int timeleft;
  100. struct score *score;
  101. if (i->done)
  102. return true;
  103. score = tal(run->m, struct score);
  104. list_head_init(&score->per_file_errors);
  105. score->error = NULL;
  106. score->pass = false;
  107. score->score = 0;
  108. score->total = 1;
  109. /* We can see skipped things in two cases:
  110. * (1) _info excluded them (presumably because they fail).
  111. * (2) A prerequisite failed.
  112. */
  113. if (i->skip) {
  114. if (verbose)
  115. printf("%s%s: skipped (%s)\n",
  116. run->prefix, i->name, i->skip);
  117. /* Pass us up to the test which failed, not us. */
  118. score->pass = true;
  119. goto out;
  120. }
  121. if (i->can_run) {
  122. i->skip = i->can_run(run->m);
  123. if (i->skip) {
  124. /* Test doesn't apply, or can't run? That's OK. */
  125. if (verbose > 1)
  126. printf("%s%s: skipped (%s)\n",
  127. run->prefix, i->name, i->skip);
  128. /* Mark our dependencies to skip. */
  129. dgraph_traverse_from(&i->node, cannot_run, NULL);
  130. score->pass = true;
  131. score->total = 0;
  132. goto out;
  133. }
  134. }
  135. timeleft = timeout ? timeout : default_timeout_ms;
  136. i->check(run->m, &timeleft, score);
  137. if (timeout && timeleft == 0) {
  138. i->skip = "timeout";
  139. if (verbose)
  140. printf("%s%s: skipped (%s)\n",
  141. run->prefix, i->name, i->skip);
  142. /* Mark our dependencies to skip. */
  143. dgraph_traverse_from(&i->node, skip_test,
  144. "dependency timed out");
  145. score->pass = true;
  146. score->total = 0;
  147. goto out;
  148. }
  149. assert(score->score <= score->total);
  150. if ((!score->pass && !run->quiet)
  151. || (score->score < score->total && verbose)
  152. || verbose > 1) {
  153. printf("%s%s (%s): %s",
  154. run->prefix, i->name, i->key,
  155. score->pass ? "PASS" : "FAIL");
  156. if (score->total > 1)
  157. printf(" (+%u/%u)", score->score, score->total);
  158. printf("\n");
  159. }
  160. if ((!run->quiet && !score->pass) || verbose) {
  161. if (score->error) {
  162. printf("%s%s", score->error,
  163. strends(score->error, "\n") ? "" : "\n");
  164. }
  165. }
  166. if (!run->quiet && score->score < score->total && i->handle)
  167. i->handle(run->m, score);
  168. if (!score->pass) {
  169. /* Skip any tests which depend on this one. */
  170. dgraph_traverse_from(&i->node, skip_test, "dependency failed");
  171. }
  172. out:
  173. run->score += score->score;
  174. run->total += score->total;
  175. /* FIXME: Free score. */
  176. run->pass &= score->pass;
  177. i->done = true;
  178. if (!score->pass && i->compulsory) {
  179. warnx("%s%s failed", run->prefix, i->name);
  180. run->score = 0;
  181. return false;
  182. }
  183. return true;
  184. }
  185. static void register_test(struct ccanlint *test)
  186. {
  187. if (!strmap_add(&tests, test->key, test))
  188. err(1, "Adding test %s", test->key);
  189. test->options = tal_arr(NULL, char *, 1);
  190. test->options[0] = NULL;
  191. dgraph_init_node(&test->node);
  192. }
  193. static bool get_test(const char *member, struct ccanlint *i,
  194. struct ccanlint **ret)
  195. {
  196. if (tlist_empty(&i->node.edge[DGRAPH_TO])) {
  197. *ret = i;
  198. return false;
  199. }
  200. return true;
  201. }
  202. /**
  203. * get_next_test - retrieves the next test to be processed
  204. **/
  205. static inline struct ccanlint *get_next_test(void)
  206. {
  207. struct ccanlint *i = NULL;
  208. strmap_iterate(&tests, get_test, &i);
  209. if (i)
  210. return i;
  211. if (strmap_empty(&tests))
  212. return NULL;
  213. errx(1, "Can't make process; test dependency cycle");
  214. }
  215. static struct ccanlint *find_test(const char *key)
  216. {
  217. return strmap_get(&tests, key);
  218. }
  219. bool is_excluded(const char *name)
  220. {
  221. return find_test(name)->skip != NULL;
  222. }
  223. static bool init_deps(const char *member, struct ccanlint *c, void *unused)
  224. {
  225. char **deps = tal_strsplit(NULL, c->needs, " ", STR_EMPTY_OK);
  226. unsigned int i;
  227. for (i = 0; deps[i]; i++) {
  228. struct ccanlint *dep;
  229. dep = find_test(deps[i]);
  230. if (!dep)
  231. errx(1, "BUG: unknown dep '%s' for %s",
  232. deps[i], c->key);
  233. dgraph_add_edge(&dep->node, &c->node);
  234. }
  235. tal_free(deps);
  236. return true;
  237. }
  238. static bool check_names(const char *member, struct ccanlint *c,
  239. struct ccanlint_map *names)
  240. {
  241. if (!strmap_add(names, c->name, c))
  242. err(1, "Duplicate name %s", c->name);
  243. return true;
  244. }
  245. static void init_tests(void)
  246. {
  247. struct ccanlint_map names;
  248. struct ccanlint **table;
  249. size_t i, num;
  250. strmap_init(&tests);
  251. table = autodata_get(ccanlint_tests, &num);
  252. for (i = 0; i < num; i++)
  253. register_test(table[i]);
  254. autodata_free(table);
  255. strmap_iterate(&tests, init_deps, NULL);
  256. /* Check for duplicate names. */
  257. strmap_init(&names);
  258. strmap_iterate(&tests, check_names, &names);
  259. strmap_clear(&names);
  260. }
  261. static bool reset_test(struct dgraph_node *node, void *unused)
  262. {
  263. struct ccanlint *c = container_of(node, struct ccanlint, node);
  264. c->skip = NULL;
  265. c->done = false;
  266. return true;
  267. }
  268. static void reset_tests(struct dgraph_node *all)
  269. {
  270. dgraph_traverse_to(all, reset_test, NULL);
  271. }
  272. static bool print_deps(const char *member, struct ccanlint *c, void *unused)
  273. {
  274. if (!tlist_empty(&c->node.edge[DGRAPH_FROM])) {
  275. struct dgraph_edge *e;
  276. printf("These depend on %s:\n", c->key);
  277. dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
  278. struct ccanlint *to = container_of(e->n[DGRAPH_TO],
  279. struct ccanlint,
  280. node);
  281. printf("\t%s\n", to->key);
  282. }
  283. }
  284. return true;
  285. }
  286. static void print_test_depends(void)
  287. {
  288. printf("Tests:\n");
  289. strmap_iterate(&tests, print_deps, NULL);
  290. }
  291. static void show_tmpdir(const char *dir)
  292. {
  293. printf("You can find ccanlint working files in '%s'\n", dir);
  294. }
  295. static char *keep_tests(void *unused)
  296. {
  297. keep_results = true;
  298. /* Don't automatically destroy temporary dir. */
  299. keep_temp_dir();
  300. tal_add_destructor(temp_dir(), show_tmpdir);
  301. return NULL;
  302. }
  303. static bool remove_test(struct dgraph_node *node, const char *why)
  304. {
  305. struct ccanlint *c = container_of(node, struct ccanlint, node);
  306. c->skip = why;
  307. dgraph_clear_node(node);
  308. return true;
  309. }
  310. static char *exclude_test(const char *testname, void *unused)
  311. {
  312. struct ccanlint *i = find_test(testname);
  313. if (!i)
  314. return tal_fmt(NULL, "No test %s to --exclude", testname);
  315. /* Remove this, and everything which depends on it. */
  316. dgraph_traverse_from(&i->node, remove_test, "excluded on command line");
  317. remove_test(&i->node, "excluded on command line");
  318. return NULL;
  319. }
  320. static void skip_test_and_deps(struct ccanlint *c, const char *why)
  321. {
  322. /* Skip this, and everything which depends on us. */
  323. dgraph_traverse_from(&c->node, skip_test, why);
  324. skip_test(&c->node, why);
  325. }
  326. static char *list_tests(void *arg)
  327. {
  328. struct ccanlint *i;
  329. printf("Tests:\n");
  330. /* This makes them print in topological order. */
  331. while ((i = get_next_test()) != NULL) {
  332. printf(" %-25s %s\n", i->key, i->name);
  333. dgraph_clear_node(&i->node);
  334. strmap_del(&tests, i->key, NULL);
  335. }
  336. exit(0);
  337. }
  338. static bool draw_test(const char *member, struct ccanlint *c, const char *style)
  339. {
  340. /*
  341. * todo: escape labels in case ccanlint test keys have
  342. * characters interpreted as GraphViz syntax.
  343. */
  344. printf("\t\"%p\" [label=\"%s\"%s]\n", c, c->key, style);
  345. return true;
  346. }
  347. static void test_dgraph_vertices(const char *style)
  348. {
  349. strmap_iterate(&tests, draw_test, style);
  350. }
  351. static bool draw_edges(const char *member, struct ccanlint *c, void *unused)
  352. {
  353. struct dgraph_edge *e;
  354. dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
  355. struct ccanlint *to = container_of(e->n[DGRAPH_TO],
  356. struct ccanlint,
  357. node);
  358. printf("\t\"%p\" -> \"%p\"\n", c->name, to->name);
  359. }
  360. return true;
  361. }
  362. static void test_dgraph_edges(void)
  363. {
  364. strmap_iterate(&tests, draw_edges, NULL);
  365. }
  366. static char *test_dependency_graph(void *arg)
  367. {
  368. puts("digraph G {");
  369. test_dgraph_vertices("");
  370. test_dgraph_edges();
  371. puts("}");
  372. exit(0);
  373. }
  374. static void add_options(struct ccanlint *test, char **options,
  375. unsigned int num_options)
  376. {
  377. unsigned int num;
  378. if (!test->options)
  379. num = 0;
  380. else
  381. /* -1, because last one is NULL. */
  382. num = tal_count(test->options) - 1;
  383. tal_resize(&test->options, num + num_options + 1);
  384. memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
  385. }
  386. void add_info_options(struct ccan_file *info)
  387. {
  388. struct doc_section *d;
  389. unsigned int i;
  390. struct ccanlint *test;
  391. list_for_each(get_ccan_file_docs(info), d, list) {
  392. if (!streq(d->type, "ccanlint"))
  393. continue;
  394. for (i = 0; i < d->num_lines; i++) {
  395. char **words = tal_strsplit(d, d->lines[i], " \t",
  396. STR_NO_EMPTY);
  397. if (!words[0])
  398. continue;
  399. if (strncmp(words[0], "//", 2) == 0)
  400. continue;
  401. test = find_test(words[0]);
  402. if (!test) {
  403. warnx("%s: unknown ccanlint test '%s'",
  404. info->fullname, words[0]);
  405. continue;
  406. }
  407. if (!words[1]) {
  408. warnx("%s: no argument to test '%s'",
  409. info->fullname, words[0]);
  410. continue;
  411. }
  412. /* Known failure? */
  413. if (strcasecmp(words[1], "FAIL") == 0) {
  414. if (!targeting)
  415. skip_test_and_deps(test,
  416. "excluded in _info"
  417. " file");
  418. } else {
  419. if (!test->takes_options)
  420. warnx("%s: %s doesn't take options",
  421. info->fullname, words[0]);
  422. add_options(test, words+1, tal_count(words)-1);
  423. }
  424. }
  425. }
  426. }
  427. /* If options are of form "filename:<option>" they only apply to that file */
  428. char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
  429. {
  430. char **ret;
  431. unsigned int i, j = 0;
  432. /* Fast path. */
  433. if (!test->options[0])
  434. return test->options;
  435. ret = tal_arr(f, char *, tal_count(test->options));
  436. for (i = 0; test->options[i]; i++) {
  437. char *optname;
  438. if (!test->options[i] || !strchr(test->options[i], ':')) {
  439. optname = test->options[i];
  440. } else if (strstarts(test->options[i], f->name)
  441. && test->options[i][strlen(f->name)] == ':') {
  442. optname = test->options[i] + strlen(f->name) + 1;
  443. } else
  444. continue;
  445. /* FAIL overrides anything else. */
  446. if (streq(optname, "FAIL")) {
  447. ret = tal_arr(f, char *, 2);
  448. ret[0] = (char *)"FAIL";
  449. ret[1] = NULL;
  450. return ret;
  451. }
  452. ret[j++] = optname;
  453. }
  454. ret[j] = NULL;
  455. /* Shrink it to size so tal_array_length() works as expected. */
  456. tal_resize(&ret, j + 1);
  457. return ret;
  458. }
  459. static char *opt_set_const_charp(const char *arg, const char **p)
  460. {
  461. return opt_set_charp(arg, cast_const2(char **, p));
  462. }
  463. static char *opt_set_target(const char *arg, struct dgraph_node *all)
  464. {
  465. struct ccanlint *t = find_test(arg);
  466. if (!t)
  467. return tal_fmt(NULL, "unknown --target %s", arg);
  468. targeting = true;
  469. dgraph_add_edge(&t->node, all);
  470. return NULL;
  471. }
  472. static bool run_tests(struct dgraph_node *all,
  473. bool summary,
  474. bool deps_fail_ignore,
  475. struct manifest *m,
  476. const char *prefix)
  477. {
  478. struct run_info run;
  479. const char *comment = "";
  480. run.quiet = summary;
  481. run.m = m;
  482. run.prefix = prefix;
  483. run.score = run.total = 0;
  484. run.pass = true;
  485. non_ccan_deps = build_failed = false;
  486. dgraph_traverse_to(all, run_test, &run);
  487. /* We can completely fail if we're missing external stuff: ignore */
  488. if (deps_fail_ignore && non_ccan_deps && build_failed) {
  489. comment = " (missing non-ccan dependencies?)";
  490. run.pass = true;
  491. }
  492. printf("%sTotal score: %u/%u%s\n",
  493. prefix, run.score, run.total, comment);
  494. return run.pass;
  495. }
  496. static bool add_to_all(const char *member, struct ccanlint *c,
  497. struct dgraph_node *all)
  498. {
  499. /* If we're excluded on cmdline, don't add. */
  500. if (!c->skip)
  501. dgraph_add_edge(&c->node, all);
  502. return true;
  503. }
  504. static bool test_module(struct dgraph_node *all,
  505. const char *dir, const char *prefix, bool summary,
  506. bool deps_fail_ignore)
  507. {
  508. struct manifest *m = get_manifest(autofree(), dir);
  509. char *testlink = path_join(NULL, temp_dir(), "test");
  510. /* Create a symlink from temp dir back to src dir's
  511. * test directory. */
  512. unlink(testlink);
  513. if (symlink(path_join(m, dir, "test"), testlink) != 0)
  514. err(1, "Creating test symlink in %s", temp_dir());
  515. return run_tests(all, summary, deps_fail_ignore, m, prefix);
  516. }
  517. int main(int argc, char *argv[])
  518. {
  519. bool summary = false, pass = true, deps_fail_ignore = false;
  520. unsigned int i;
  521. const char *prefix = "";
  522. char *cwd = path_cwd(NULL), *dir;
  523. struct ccanlint top; /* cannot_run may try to set ->can_run */
  524. const char *override_compiler = NULL, *override_cflags = NULL;
  525. /* Empty graph node to which we attach everything else. */
  526. dgraph_init_node(&top.node);
  527. opt_register_early_noarg("--verbose|-v", opt_inc_intval, &verbose,
  528. "verbose mode (up to -vvvv)");
  529. opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
  530. "do not compile anything");
  531. opt_register_noarg("-l|--list-tests", list_tests, NULL,
  532. "list tests ccanlint performs (and exit)");
  533. opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
  534. "print dependency graph of tests in Graphviz .dot format");
  535. opt_register_noarg("-k|--keep", keep_tests, NULL,
  536. "do not delete ccanlint working files");
  537. opt_register_noarg("--summary|-s", opt_set_bool, &summary,
  538. "simply give one line summary");
  539. opt_register_arg("-x|--exclude <testname>", exclude_test, NULL, NULL,
  540. "exclude <testname> (can be used multiple times)");
  541. opt_register_arg("--timeout <milleseconds>", opt_set_uintval,
  542. NULL, &timeout,
  543. "ignore (terminate) tests that are slower than this");
  544. opt_register_arg("-t|--target <testname>", opt_set_target, NULL,
  545. &top.node,
  546. "only run one test (and its prerequisites)");
  547. opt_register_arg("--compiler <compiler>", opt_set_const_charp,
  548. NULL, &override_compiler, "set the compiler");
  549. opt_register_arg("--cflags <flags>", opt_set_const_charp,
  550. NULL, &override_cflags, "set the compiler flags");
  551. opt_register_noarg("--deps-fail-ignore", opt_set_bool,
  552. &deps_fail_ignore,
  553. "don't fail if external dependencies are missing");
  554. opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
  555. "\nA program for checking and guiding development"
  556. " of CCAN modules.",
  557. "This usage message");
  558. /* Do verbose before anything else... */
  559. opt_early_parse(argc, argv, opt_log_stderr_exit);
  560. /* We move into temporary directory, so gcov dumps its files there. */
  561. if (chdir(temp_dir()) != 0)
  562. err(1, "Error changing to %s temporary dir", temp_dir());
  563. init_tests();
  564. if (verbose >= 3) {
  565. compile_verbose = true;
  566. print_test_depends();
  567. }
  568. if (verbose >= 4)
  569. tools_verbose = true;
  570. opt_parse(&argc, argv, opt_log_stderr_exit);
  571. if (!targeting)
  572. strmap_iterate(&tests, add_to_all, &top.node);
  573. if (argc == 1)
  574. dir = cwd;
  575. else
  576. dir = path_simplify(NULL, take(path_join(NULL, cwd, argv[1])));
  577. ccan_dir = find_ccan_dir(dir);
  578. if (!ccan_dir)
  579. errx(1, "Cannot find ccan/ base directory in %s", dir);
  580. config_header = read_config_header(ccan_dir, verbose > 1);
  581. /* We do this after read_config_header has set compiler & cflags */
  582. if (override_cflags)
  583. cflags = override_cflags;
  584. if (override_compiler)
  585. compiler = override_compiler;
  586. if (argc == 1)
  587. pass = test_module(&top.node, cwd, "",
  588. summary, deps_fail_ignore);
  589. else {
  590. for (i = 1; i < argc; i++) {
  591. dir = path_canon(NULL,
  592. take(path_join(NULL, cwd, argv[i])));
  593. if (!dir)
  594. err(1, "Cannot get canonical name of '%s'",
  595. argv[i]);
  596. prefix = path_join(NULL, ccan_dir, "ccan");
  597. prefix = path_rel(NULL, take(prefix), dir);
  598. prefix = tal_strcat(NULL, take(prefix), ": ");
  599. pass &= test_module(&top.node, dir, prefix, summary,
  600. deps_fail_ignore);
  601. reset_tests(&top.node);
  602. }
  603. }
  604. return pass ? 0 : 1;
  605. }