ccanlint.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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 <unistd.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <err.h>
  27. #include <ctype.h>
  28. #include <ccan/str/str.h>
  29. #include <ccan/str_talloc/str_talloc.h>
  30. #include <ccan/talloc/talloc.h>
  31. #include <ccan/opt/opt.h>
  32. #include <ccan/foreach/foreach.h>
  33. #include <ccan/grab_file/grab_file.h>
  34. #include <ccan/cast/cast.h>
  35. #include <ccan/tlist/tlist.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. static bool targeting = false;
  44. static unsigned int timeout;
  45. /* These are overridden at runtime if we can find config.h */
  46. const char *compiler = NULL;
  47. const char *cflags = NULL;
  48. const char *config_header;
  49. #if 0
  50. static void indent_print(const char *string)
  51. {
  52. while (*string) {
  53. unsigned int line = strcspn(string, "\n");
  54. printf("\t%.*s", line, string);
  55. if (string[line] == '\n') {
  56. printf("\n");
  57. line++;
  58. }
  59. string += line;
  60. }
  61. }
  62. #endif
  63. bool ask(const char *question)
  64. {
  65. char reply[80];
  66. printf("%s ", question);
  67. fflush(stdout);
  68. return fgets(reply, sizeof(reply), stdin) != NULL
  69. && toupper(reply[0]) == 'Y';
  70. }
  71. /* Skip, but don't remove. */
  72. static bool skip_test(struct dgraph_node *node, const char *why)
  73. {
  74. struct ccanlint *c = container_of(node, struct ccanlint, node);
  75. c->skip = why;
  76. return true;
  77. }
  78. static const char *dep_failed(struct manifest *m)
  79. {
  80. return "dependency couldn't run";
  81. }
  82. static bool cannot_run(struct dgraph_node *node, void *unused)
  83. {
  84. struct ccanlint *c = container_of(node, struct ccanlint, node);
  85. c->can_run = dep_failed;
  86. return true;
  87. }
  88. struct run_info {
  89. bool quiet;
  90. unsigned int score, total;
  91. struct manifest *m;
  92. const char *prefix;
  93. bool pass;
  94. };
  95. static bool run_test(struct dgraph_node *n, struct run_info *run)
  96. {
  97. struct ccanlint *i = container_of(n, struct ccanlint, node);
  98. unsigned int timeleft;
  99. struct score *score;
  100. if (i->done)
  101. return true;
  102. score = talloc(run->m, struct score);
  103. list_head_init(&score->per_file_errors);
  104. score->error = NULL;
  105. score->pass = false;
  106. score->score = 0;
  107. score->total = 1;
  108. /* We can see skipped things in two cases:
  109. * (1) _info excluded them (presumably because they fail).
  110. * (2) A prerequisite failed.
  111. */
  112. if (i->skip) {
  113. if (verbose)
  114. printf("%s%s: skipped (%s)\n",
  115. run->prefix, i->name, i->skip);
  116. /* Pass us up to the test which failed, not us. */
  117. score->pass = true;
  118. goto out;
  119. }
  120. if (i->can_run) {
  121. i->skip = i->can_run(run->m);
  122. if (i->skip) {
  123. /* Test doesn't apply, or can't run? That's OK. */
  124. if (verbose > 1)
  125. printf("%s%s: skipped (%s)\n",
  126. run->prefix, i->name, i->skip);
  127. /* Mark our dependencies to skip. */
  128. dgraph_traverse_from(&i->node, cannot_run, NULL);
  129. score->pass = true;
  130. score->total = 0;
  131. goto out;
  132. }
  133. }
  134. timeleft = timeout ? timeout : default_timeout_ms;
  135. i->check(run->m, i->keep_results, &timeleft, score);
  136. if (timeout && timeleft == 0) {
  137. i->skip = "timeout";
  138. if (verbose)
  139. printf("%s%s: skipped (%s)\n",
  140. run->prefix, i->name, i->skip);
  141. /* Mark our dependencies to skip. */
  142. dgraph_traverse_from(&i->node, skip_test,
  143. "dependency timed out");
  144. score->pass = true;
  145. score->total = 0;
  146. goto out;
  147. }
  148. assert(score->score <= score->total);
  149. if ((!score->pass && !run->quiet)
  150. || (score->score < score->total && verbose)
  151. || verbose > 1) {
  152. printf("%s%s (%s): %s",
  153. run->prefix, i->name, i->key,
  154. score->pass ? "PASS" : "FAIL");
  155. if (score->total > 1)
  156. printf(" (+%u/%u)", score->score, score->total);
  157. printf("\n");
  158. }
  159. if ((!run->quiet && !score->pass) || verbose) {
  160. if (score->error) {
  161. printf("%s%s", score->error,
  162. strends(score->error, "\n") ? "" : "\n");
  163. }
  164. }
  165. if (!run->quiet && score->score < score->total && i->handle)
  166. i->handle(run->m, score);
  167. if (!score->pass) {
  168. /* Skip any tests which depend on this one. */
  169. dgraph_traverse_from(&i->node, skip_test, "dependency failed");
  170. }
  171. out:
  172. run->score += score->score;
  173. run->total += score->total;
  174. /* FIXME: Free score. */
  175. run->pass &= score->pass;
  176. i->done = true;
  177. if (!score->pass && i->compulsory) {
  178. warnx("%s%s failed", run->prefix, i->name);
  179. run->score = 0;
  180. return false;
  181. }
  182. return true;
  183. }
  184. static void register_test(struct ccanlint *test)
  185. {
  186. if (!strmap_add(&tests, test->key, test))
  187. err(1, "Adding test %s", test->key);
  188. test->options = talloc_array(NULL, char *, 1);
  189. test->options[0] = NULL;
  190. dgraph_init_node(&test->node);
  191. }
  192. static bool get_test(const char *member, struct ccanlint *i,
  193. struct ccanlint **ret)
  194. {
  195. if (tlist_empty(&i->node.edge[DGRAPH_TO])) {
  196. *ret = i;
  197. return false;
  198. }
  199. return true;
  200. }
  201. /**
  202. * get_next_test - retrieves the next test to be processed
  203. **/
  204. static inline struct ccanlint *get_next_test(void)
  205. {
  206. struct ccanlint *i = NULL;
  207. strmap_iterate(&tests, get_test, &i);
  208. if (i)
  209. return i;
  210. if (strmap_empty(&tests))
  211. return NULL;
  212. errx(1, "Can't make process; test dependency cycle");
  213. }
  214. static struct ccanlint *find_test(const char *key)
  215. {
  216. return strmap_get(&tests, key);
  217. }
  218. bool is_excluded(const char *name)
  219. {
  220. return find_test(name)->skip != NULL;
  221. }
  222. static bool init_deps(const char *member, struct ccanlint *c, void *unused)
  223. {
  224. char **deps = strsplit(NULL, c->needs, " ");
  225. unsigned int i;
  226. for (i = 0; deps[i]; i++) {
  227. struct ccanlint *dep;
  228. dep = find_test(deps[i]);
  229. if (!dep)
  230. errx(1, "BUG: unknown dep '%s' for %s",
  231. deps[i], c->key);
  232. dgraph_add_edge(&dep->node, &c->node);
  233. }
  234. talloc_free(deps);
  235. return true;
  236. }
  237. static bool check_names(const char *member, struct ccanlint *c,
  238. struct ccanlint_map *names)
  239. {
  240. if (!strmap_add(names, c->name, c))
  241. err(1, "Duplicate name %s", c->name);
  242. return true;
  243. }
  244. #undef REGISTER_TEST
  245. #define REGISTER_TEST(name, ...) extern struct ccanlint name
  246. #include "generated-testlist"
  247. static void init_tests(void)
  248. {
  249. struct ccanlint_map names;
  250. strmap_init(&tests);
  251. #undef REGISTER_TEST
  252. #define REGISTER_TEST(name) register_test(&name)
  253. #include "generated-testlist"
  254. strmap_iterate(&tests, init_deps, NULL);
  255. /* Check for duplicate names. */
  256. strmap_init(&names);
  257. strmap_iterate(&tests, check_names, &names);
  258. strmap_clear(&names);
  259. }
  260. static bool reset_test(struct dgraph_node *node, void *unused)
  261. {
  262. struct ccanlint *c = container_of(node, struct ccanlint, node);
  263. c->skip = NULL;
  264. c->done = false;
  265. return true;
  266. }
  267. static void reset_tests(struct dgraph_node *all)
  268. {
  269. dgraph_traverse_to(all, reset_test, NULL);
  270. }
  271. static bool print_deps(const char *member, struct ccanlint *c, void *unused)
  272. {
  273. if (!tlist_empty(&c->node.edge[DGRAPH_FROM])) {
  274. struct dgraph_edge *e;
  275. printf("These depend on %s:\n", c->key);
  276. dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
  277. struct ccanlint *to = container_of(e->n[DGRAPH_TO],
  278. struct ccanlint,
  279. node);
  280. printf("\t%s\n", to->key);
  281. }
  282. }
  283. return true;
  284. }
  285. static void print_test_depends(void)
  286. {
  287. printf("Tests:\n");
  288. strmap_iterate(&tests, print_deps, NULL);
  289. }
  290. static int show_tmpdir(const char *dir)
  291. {
  292. printf("You can find ccanlint working files in '%s'\n", dir);
  293. return 0;
  294. }
  295. static bool keep_one_test(const char *member, struct ccanlint *c, void *unused)
  296. {
  297. c->keep_results = true;
  298. return true;
  299. }
  300. static char *keep_test(const char *testname, void *unused)
  301. {
  302. if (streq(testname, "all")) {
  303. strmap_iterate(&tests, keep_one_test, NULL);
  304. } else {
  305. struct ccanlint *i = find_test(testname);
  306. if (!i)
  307. errx(1, "No test %s to --keep", testname);
  308. keep_one_test(testname, i, NULL);
  309. }
  310. /* Don't automatically destroy temporary dir. */
  311. talloc_set_destructor(temp_dir(NULL), show_tmpdir);
  312. return NULL;
  313. }
  314. static bool remove_test(struct dgraph_node *node, const char *why)
  315. {
  316. struct ccanlint *c = container_of(node, struct ccanlint, node);
  317. c->skip = why;
  318. dgraph_clear_node(node);
  319. return true;
  320. }
  321. static char *exclude_test(const char *testname, void *unused)
  322. {
  323. struct ccanlint *i = find_test(testname);
  324. if (!i)
  325. return talloc_asprintf(NULL, "No test %s to --exclude",
  326. testname);
  327. /* Remove this, and everything which depends on it. */
  328. dgraph_traverse_from(&i->node, remove_test, "excluded on command line");
  329. remove_test(&i->node, "excluded on command line");
  330. return NULL;
  331. }
  332. static void skip_test_and_deps(struct ccanlint *c, const char *why)
  333. {
  334. /* Skip this, and everything which depends on us. */
  335. dgraph_traverse_from(&c->node, skip_test, why);
  336. skip_test(&c->node, why);
  337. }
  338. static char *list_tests(void *arg)
  339. {
  340. struct ccanlint *i;
  341. printf("Tests:\n");
  342. /* This makes them print in topological order. */
  343. while ((i = get_next_test()) != NULL) {
  344. printf(" %-25s %s\n", i->key, i->name);
  345. dgraph_clear_node(&i->node);
  346. strmap_del(&tests, i->key, NULL);
  347. }
  348. exit(0);
  349. }
  350. static bool draw_test(const char *member, struct ccanlint *c, const char *style)
  351. {
  352. /*
  353. * todo: escape labels in case ccanlint test keys have
  354. * characters interpreted as GraphViz syntax.
  355. */
  356. printf("\t\"%p\" [label=\"%s\"%s]\n", c, c->key, style);
  357. return true;
  358. }
  359. static void test_dgraph_vertices(const char *style)
  360. {
  361. strmap_iterate(&tests, draw_test, style);
  362. }
  363. static bool draw_edges(const char *member, struct ccanlint *c, void *unused)
  364. {
  365. struct dgraph_edge *e;
  366. dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
  367. struct ccanlint *to = container_of(e->n[DGRAPH_TO],
  368. struct ccanlint,
  369. node);
  370. printf("\t\"%p\" -> \"%p\"\n", c->name, to->name);
  371. }
  372. return true;
  373. }
  374. static void test_dgraph_edges(void)
  375. {
  376. strmap_iterate(&tests, draw_edges, NULL);
  377. }
  378. static char *test_dependency_graph(void *arg)
  379. {
  380. puts("digraph G {");
  381. test_dgraph_vertices("");
  382. test_dgraph_edges();
  383. puts("}");
  384. exit(0);
  385. }
  386. /* Remove empty lines. */
  387. static char **collapse(char **lines, unsigned int *nump)
  388. {
  389. unsigned int i, j;
  390. for (i = j = 0; lines[i]; i++) {
  391. if (lines[i][0])
  392. lines[j++] = lines[i];
  393. }
  394. lines[j] = NULL;
  395. if (nump)
  396. *nump = j;
  397. return lines;
  398. }
  399. static void add_options(struct ccanlint *test, char **options,
  400. unsigned int num_options)
  401. {
  402. unsigned int num;
  403. if (!test->options)
  404. num = 0;
  405. else
  406. /* -1, because last one is NULL. */
  407. num = talloc_array_length(test->options) - 1;
  408. test->options = talloc_realloc(NULL, test->options,
  409. char *,
  410. num + num_options + 1);
  411. memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
  412. }
  413. void add_info_options(struct ccan_file *info)
  414. {
  415. struct doc_section *d;
  416. unsigned int i;
  417. struct ccanlint *test;
  418. list_for_each(get_ccan_file_docs(info), d, list) {
  419. if (!streq(d->type, "ccanlint"))
  420. continue;
  421. for (i = 0; i < d->num_lines; i++) {
  422. unsigned int num_words;
  423. char **words = collapse(strsplit(d, d->lines[i], " \t"),
  424. &num_words);
  425. if (num_words == 0)
  426. continue;
  427. if (strncmp(words[0], "//", 2) == 0)
  428. continue;
  429. test = find_test(words[0]);
  430. if (!test) {
  431. warnx("%s: unknown ccanlint test '%s'",
  432. info->fullname, words[0]);
  433. continue;
  434. }
  435. if (!words[1]) {
  436. warnx("%s: no argument to test '%s'",
  437. info->fullname, words[0]);
  438. continue;
  439. }
  440. /* Known failure? */
  441. if (strcasecmp(words[1], "FAIL") == 0) {
  442. if (!targeting)
  443. skip_test_and_deps(test,
  444. "excluded in _info"
  445. " file");
  446. } else {
  447. if (!test->takes_options)
  448. warnx("%s: %s doesn't take options",
  449. info->fullname, words[0]);
  450. add_options(test, words+1, num_words-1);
  451. }
  452. }
  453. }
  454. }
  455. /* If options are of form "filename:<option>" they only apply to that file */
  456. char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
  457. {
  458. char **ret;
  459. unsigned int i, j = 0;
  460. /* Fast path. */
  461. if (!test->options[0])
  462. return test->options;
  463. ret = talloc_array(f, char *, talloc_array_length(test->options));
  464. for (i = 0; test->options[i]; i++) {
  465. char *optname;
  466. if (!test->options[i] || !strchr(test->options[i], ':')) {
  467. optname = test->options[i];
  468. } else if (strstarts(test->options[i], f->name)
  469. && test->options[i][strlen(f->name)] == ':') {
  470. optname = test->options[i] + strlen(f->name) + 1;
  471. } else
  472. continue;
  473. /* FAIL overrides anything else. */
  474. if (streq(optname, "FAIL")) {
  475. ret = talloc_array(f, char *, 2);
  476. ret[0] = (char *)"FAIL";
  477. ret[1] = NULL;
  478. return ret;
  479. }
  480. ret[j++] = optname;
  481. }
  482. ret[j] = NULL;
  483. /* Shrink it to size so talloc_array_length() works as expected. */
  484. return talloc_realloc(NULL, ret, char *, j + 1);
  485. }
  486. static char *demangle_string(char *string)
  487. {
  488. unsigned int i;
  489. const char mapfrom[] = "abfnrtv";
  490. const char mapto[] = "\a\b\f\n\r\t\v";
  491. if (!strchr(string, '"'))
  492. return NULL;
  493. string = strchr(string, '"') + 1;
  494. if (!strrchr(string, '"'))
  495. return NULL;
  496. *strrchr(string, '"') = '\0';
  497. for (i = 0; i < strlen(string); i++) {
  498. if (string[i] == '\\') {
  499. char repl;
  500. unsigned len = 0;
  501. const char *p = strchr(mapfrom, string[i+1]);
  502. if (p) {
  503. repl = mapto[p - mapfrom];
  504. len = 1;
  505. } else if (strlen(string+i+1) >= 3) {
  506. if (string[i+1] == 'x') {
  507. repl = (string[i+2]-'0')*16
  508. + string[i+3]-'0';
  509. len = 3;
  510. } else if (cisdigit(string[i+1])) {
  511. repl = (string[i+2]-'0')*8*8
  512. + (string[i+3]-'0')*8
  513. + (string[i+4]-'0');
  514. len = 3;
  515. }
  516. }
  517. if (len == 0) {
  518. repl = string[i+1];
  519. len = 1;
  520. }
  521. string[i] = repl;
  522. memmove(string + i + 1, string + i + len + 1,
  523. strlen(string + i + len + 1) + 1);
  524. }
  525. }
  526. return string;
  527. }
  528. static void read_config_header(void)
  529. {
  530. char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir);
  531. char **lines;
  532. unsigned int i;
  533. config_header = grab_file(NULL, fname, NULL);
  534. if (!config_header) {
  535. talloc_free(fname);
  536. return;
  537. }
  538. lines = strsplit(config_header, config_header, "\n");
  539. for (i = 0; i < talloc_array_length(lines) - 1; i++) {
  540. char *sym;
  541. const char **line = (const char **)&lines[i];
  542. if (!get_token(line, "#"))
  543. continue;
  544. if (!get_token(line, "define"))
  545. continue;
  546. sym = get_symbol_token(lines, line);
  547. if (streq(sym, "CCAN_COMPILER") && !compiler) {
  548. compiler = demangle_string(lines[i]);
  549. if (!compiler)
  550. errx(1, "%s:%u:could not parse CCAN_COMPILER",
  551. fname, i+1);
  552. if (verbose > 1)
  553. printf("%s: compiler set to '%s'\n",
  554. fname, compiler);
  555. } else if (streq(sym, "CCAN_CFLAGS") && !cflags) {
  556. cflags = demangle_string(lines[i]);
  557. if (!cflags)
  558. errx(1, "%s:%u:could not parse CCAN_CFLAGS",
  559. fname, i+1);
  560. if (verbose > 1)
  561. printf("%s: compiler flags set to '%s'\n",
  562. fname, cflags);
  563. }
  564. }
  565. if (!compiler)
  566. compiler = CCAN_COMPILER;
  567. if (!cflags)
  568. compiler = CCAN_CFLAGS;
  569. }
  570. static char *opt_set_const_charp(const char *arg, const char **p)
  571. {
  572. return opt_set_charp(arg, cast_const2(char **, p));
  573. }
  574. static char *opt_set_target(const char *arg, struct dgraph_node *all)
  575. {
  576. struct ccanlint *t = find_test(arg);
  577. if (!t)
  578. return talloc_asprintf(NULL, "unknown --target %s", arg);
  579. targeting = true;
  580. dgraph_add_edge(&t->node, all);
  581. return NULL;
  582. }
  583. static bool run_tests(struct dgraph_node *all,
  584. bool summary,
  585. struct manifest *m,
  586. const char *prefix)
  587. {
  588. struct run_info run;
  589. run.quiet = summary;
  590. run.m = m;
  591. run.prefix = prefix;
  592. run.score = run.total = 0;
  593. run.pass = true;
  594. dgraph_traverse_to(all, run_test, &run);
  595. printf("%sTotal score: %u/%u\n", prefix, run.score, run.total);
  596. return run.pass;
  597. }
  598. static bool add_to_all(const char *member, struct ccanlint *c,
  599. struct dgraph_node *all)
  600. {
  601. dgraph_add_edge(&c->node, all);
  602. return true;
  603. }
  604. int main(int argc, char *argv[])
  605. {
  606. bool summary = false, pass = true;
  607. unsigned int i;
  608. struct manifest *m;
  609. const char *prefix = "";
  610. char *dir = talloc_getcwd(NULL), *base_dir = dir, *testlink;
  611. struct dgraph_node all;
  612. /* Empty graph node to which we attach everything else. */
  613. dgraph_init_node(&all);
  614. opt_register_early_noarg("--verbose|-v", opt_inc_intval, &verbose,
  615. "verbose mode (up to -vvvv)");
  616. opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
  617. "do not compile anything");
  618. opt_register_noarg("-l|--list-tests", list_tests, NULL,
  619. "list tests ccanlint performs (and exit)");
  620. opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
  621. "print dependency graph of tests in Graphviz .dot format");
  622. opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
  623. "keep results of <testname>"
  624. " (can be used multiple times, or 'all')");
  625. opt_register_noarg("--summary|-s", opt_set_bool, &summary,
  626. "simply give one line summary");
  627. opt_register_arg("-x|--exclude <testname>", exclude_test, NULL, NULL,
  628. "exclude <testname> (can be used multiple times)");
  629. opt_register_arg("--timeout <milleseconds>", opt_set_uintval,
  630. NULL, &timeout,
  631. "ignore (terminate) tests that are slower than this");
  632. opt_register_arg("-t|--target <testname>", opt_set_target, NULL, &all,
  633. "only run one test (and its prerequisites)");
  634. opt_register_arg("--compiler <compiler>", opt_set_const_charp,
  635. NULL, &compiler, "set the compiler");
  636. opt_register_arg("--cflags <flags>", opt_set_const_charp,
  637. NULL, &cflags, "set the compiler flags");
  638. opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
  639. "\nA program for checking and guiding development"
  640. " of CCAN modules.",
  641. "This usage message");
  642. /* Do verbose before anything else... */
  643. opt_early_parse(argc, argv, opt_log_stderr_exit);
  644. /* We move into temporary directory, so gcov dumps its files there. */
  645. if (chdir(temp_dir(talloc_autofree_context())) != 0)
  646. err(1, "Error changing to %s temporary dir", temp_dir(NULL));
  647. init_tests();
  648. if (verbose >= 3) {
  649. compile_verbose = true;
  650. print_test_depends();
  651. }
  652. if (verbose >= 4)
  653. tools_verbose = true;
  654. opt_parse(&argc, argv, opt_log_stderr_exit);
  655. if (!targeting)
  656. strmap_iterate(&tests, add_to_all, &all);
  657. /* This links back to the module's test dir. */
  658. testlink = talloc_asprintf(NULL, "%s/test", temp_dir(NULL));
  659. /* Defaults to pwd. */
  660. if (argc == 1) {
  661. i = 1;
  662. goto got_dir;
  663. }
  664. for (i = 1; i < argc; i++) {
  665. unsigned int score, total_score;
  666. dir = argv[i];
  667. if (dir[0] != '/')
  668. dir = talloc_asprintf_append(NULL, "%s/%s",
  669. base_dir, dir);
  670. while (strends(dir, "/"))
  671. dir[strlen(dir)-1] = '\0';
  672. got_dir:
  673. if (dir != base_dir)
  674. prefix = talloc_append_string(talloc_basename(NULL,dir),
  675. ": ");
  676. m = get_manifest(talloc_autofree_context(), dir);
  677. /* FIXME: This has to come after we've got manifest. */
  678. if (i == 1)
  679. read_config_header();
  680. /* Create a symlink from temp dir back to src dir's
  681. * test directory. */
  682. unlink(testlink);
  683. if (symlink(talloc_asprintf(m, "%s/test", dir), testlink) != 0)
  684. err(1, "Creating test symlink in %s", temp_dir(NULL));
  685. score = total_score = 0;
  686. if (!run_tests(&all, summary, m, prefix))
  687. pass = false;
  688. reset_tests(&all);
  689. }
  690. return pass ? 0 : 1;
  691. }