ccanlint.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. bool keep_results = false;
  44. static bool targeting = false;
  45. static unsigned int timeout;
  46. /* These are overridden at runtime if we can find config.h */
  47. const char *compiler = NULL;
  48. const char *cflags = NULL;
  49. const char *config_header;
  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 *unused)
  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 = talloc(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 = talloc_array(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 = strsplit(NULL, c->needs, " ");
  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. talloc_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 int show_tmpdir(const char *dir)
  292. {
  293. printf("You can find ccanlint working files in '%s'\n", dir);
  294. return 0;
  295. }
  296. static char *keep_tests(void *unused)
  297. {
  298. keep_results = true;
  299. /* Don't automatically destroy temporary dir. */
  300. talloc_set_destructor(temp_dir(NULL), 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 talloc_asprintf(NULL, "No test %s to --exclude",
  315. testname);
  316. /* Remove this, and everything which depends on it. */
  317. dgraph_traverse_from(&i->node, remove_test, "excluded on command line");
  318. remove_test(&i->node, "excluded on command line");
  319. return NULL;
  320. }
  321. static void skip_test_and_deps(struct ccanlint *c, const char *why)
  322. {
  323. /* Skip this, and everything which depends on us. */
  324. dgraph_traverse_from(&c->node, skip_test, why);
  325. skip_test(&c->node, why);
  326. }
  327. static char *list_tests(void *arg)
  328. {
  329. struct ccanlint *i;
  330. printf("Tests:\n");
  331. /* This makes them print in topological order. */
  332. while ((i = get_next_test()) != NULL) {
  333. printf(" %-25s %s\n", i->key, i->name);
  334. dgraph_clear_node(&i->node);
  335. strmap_del(&tests, i->key, NULL);
  336. }
  337. exit(0);
  338. }
  339. static bool draw_test(const char *member, struct ccanlint *c, const char *style)
  340. {
  341. /*
  342. * todo: escape labels in case ccanlint test keys have
  343. * characters interpreted as GraphViz syntax.
  344. */
  345. printf("\t\"%p\" [label=\"%s\"%s]\n", c, c->key, style);
  346. return true;
  347. }
  348. static void test_dgraph_vertices(const char *style)
  349. {
  350. strmap_iterate(&tests, draw_test, style);
  351. }
  352. static bool draw_edges(const char *member, struct ccanlint *c, void *unused)
  353. {
  354. struct dgraph_edge *e;
  355. dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
  356. struct ccanlint *to = container_of(e->n[DGRAPH_TO],
  357. struct ccanlint,
  358. node);
  359. printf("\t\"%p\" -> \"%p\"\n", c->name, to->name);
  360. }
  361. return true;
  362. }
  363. static void test_dgraph_edges(void)
  364. {
  365. strmap_iterate(&tests, draw_edges, NULL);
  366. }
  367. static char *test_dependency_graph(void *arg)
  368. {
  369. puts("digraph G {");
  370. test_dgraph_vertices("");
  371. test_dgraph_edges();
  372. puts("}");
  373. exit(0);
  374. }
  375. /* Remove empty lines. */
  376. static char **collapse(char **lines, unsigned int *nump)
  377. {
  378. unsigned int i, j;
  379. for (i = j = 0; lines[i]; i++) {
  380. if (lines[i][0])
  381. lines[j++] = lines[i];
  382. }
  383. lines[j] = NULL;
  384. if (nump)
  385. *nump = j;
  386. return lines;
  387. }
  388. static void add_options(struct ccanlint *test, char **options,
  389. unsigned int num_options)
  390. {
  391. unsigned int num;
  392. if (!test->options)
  393. num = 0;
  394. else
  395. /* -1, because last one is NULL. */
  396. num = talloc_array_length(test->options) - 1;
  397. test->options = talloc_realloc(NULL, test->options,
  398. char *,
  399. num + num_options + 1);
  400. memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
  401. }
  402. void add_info_options(struct ccan_file *info)
  403. {
  404. struct doc_section *d;
  405. unsigned int i;
  406. struct ccanlint *test;
  407. list_for_each(get_ccan_file_docs(info), d, list) {
  408. if (!streq(d->type, "ccanlint"))
  409. continue;
  410. for (i = 0; i < d->num_lines; i++) {
  411. unsigned int num_words;
  412. char **words = collapse(strsplit(d, d->lines[i], " \t"),
  413. &num_words);
  414. if (num_words == 0)
  415. continue;
  416. if (strncmp(words[0], "//", 2) == 0)
  417. continue;
  418. test = find_test(words[0]);
  419. if (!test) {
  420. warnx("%s: unknown ccanlint test '%s'",
  421. info->fullname, words[0]);
  422. continue;
  423. }
  424. if (!words[1]) {
  425. warnx("%s: no argument to test '%s'",
  426. info->fullname, words[0]);
  427. continue;
  428. }
  429. /* Known failure? */
  430. if (strcasecmp(words[1], "FAIL") == 0) {
  431. if (!targeting)
  432. skip_test_and_deps(test,
  433. "excluded in _info"
  434. " file");
  435. } else {
  436. if (!test->takes_options)
  437. warnx("%s: %s doesn't take options",
  438. info->fullname, words[0]);
  439. add_options(test, words+1, num_words-1);
  440. }
  441. }
  442. }
  443. }
  444. /* If options are of form "filename:<option>" they only apply to that file */
  445. char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
  446. {
  447. char **ret;
  448. unsigned int i, j = 0;
  449. /* Fast path. */
  450. if (!test->options[0])
  451. return test->options;
  452. ret = talloc_array(f, char *, talloc_array_length(test->options));
  453. for (i = 0; test->options[i]; i++) {
  454. char *optname;
  455. if (!test->options[i] || !strchr(test->options[i], ':')) {
  456. optname = test->options[i];
  457. } else if (strstarts(test->options[i], f->name)
  458. && test->options[i][strlen(f->name)] == ':') {
  459. optname = test->options[i] + strlen(f->name) + 1;
  460. } else
  461. continue;
  462. /* FAIL overrides anything else. */
  463. if (streq(optname, "FAIL")) {
  464. ret = talloc_array(f, char *, 2);
  465. ret[0] = (char *)"FAIL";
  466. ret[1] = NULL;
  467. return ret;
  468. }
  469. ret[j++] = optname;
  470. }
  471. ret[j] = NULL;
  472. /* Shrink it to size so talloc_array_length() works as expected. */
  473. return talloc_realloc(NULL, ret, char *, j + 1);
  474. }
  475. static char *demangle_string(char *string)
  476. {
  477. unsigned int i;
  478. const char mapfrom[] = "abfnrtv";
  479. const char mapto[] = "\a\b\f\n\r\t\v";
  480. if (!strchr(string, '"'))
  481. return NULL;
  482. string = strchr(string, '"') + 1;
  483. if (!strrchr(string, '"'))
  484. return NULL;
  485. *strrchr(string, '"') = '\0';
  486. for (i = 0; i < strlen(string); i++) {
  487. if (string[i] == '\\') {
  488. char repl;
  489. unsigned len = 0;
  490. const char *p = strchr(mapfrom, string[i+1]);
  491. if (p) {
  492. repl = mapto[p - mapfrom];
  493. len = 1;
  494. } else if (strlen(string+i+1) >= 3) {
  495. if (string[i+1] == 'x') {
  496. repl = (string[i+2]-'0')*16
  497. + string[i+3]-'0';
  498. len = 3;
  499. } else if (cisdigit(string[i+1])) {
  500. repl = (string[i+2]-'0')*8*8
  501. + (string[i+3]-'0')*8
  502. + (string[i+4]-'0');
  503. len = 3;
  504. }
  505. }
  506. if (len == 0) {
  507. repl = string[i+1];
  508. len = 1;
  509. }
  510. string[i] = repl;
  511. memmove(string + i + 1, string + i + len + 1,
  512. strlen(string + i + len + 1) + 1);
  513. }
  514. }
  515. return string;
  516. }
  517. static void read_config_header(void)
  518. {
  519. char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir);
  520. char **lines;
  521. unsigned int i;
  522. config_header = grab_file(NULL, fname, NULL);
  523. if (!config_header) {
  524. talloc_free(fname);
  525. return;
  526. }
  527. lines = strsplit(config_header, config_header, "\n");
  528. for (i = 0; i < talloc_array_length(lines) - 1; i++) {
  529. char *sym;
  530. const char **line = (const char **)&lines[i];
  531. if (!get_token(line, "#"))
  532. continue;
  533. if (!get_token(line, "define"))
  534. continue;
  535. sym = get_symbol_token(lines, line);
  536. if (streq(sym, "CCAN_COMPILER") && !compiler) {
  537. compiler = demangle_string(lines[i]);
  538. if (!compiler)
  539. errx(1, "%s:%u:could not parse CCAN_COMPILER",
  540. fname, i+1);
  541. if (verbose > 1)
  542. printf("%s: compiler set to '%s'\n",
  543. fname, compiler);
  544. } else if (streq(sym, "CCAN_CFLAGS") && !cflags) {
  545. cflags = demangle_string(lines[i]);
  546. if (!cflags)
  547. errx(1, "%s:%u:could not parse CCAN_CFLAGS",
  548. fname, i+1);
  549. if (verbose > 1)
  550. printf("%s: compiler flags set to '%s'\n",
  551. fname, cflags);
  552. }
  553. }
  554. if (!compiler)
  555. compiler = CCAN_COMPILER;
  556. if (!cflags)
  557. compiler = CCAN_CFLAGS;
  558. }
  559. static char *opt_set_const_charp(const char *arg, const char **p)
  560. {
  561. return opt_set_charp(arg, cast_const2(char **, p));
  562. }
  563. static char *opt_set_target(const char *arg, struct dgraph_node *all)
  564. {
  565. struct ccanlint *t = find_test(arg);
  566. if (!t)
  567. return talloc_asprintf(NULL, "unknown --target %s", arg);
  568. targeting = true;
  569. dgraph_add_edge(&t->node, all);
  570. return NULL;
  571. }
  572. static bool run_tests(struct dgraph_node *all,
  573. bool summary,
  574. struct manifest *m,
  575. const char *prefix)
  576. {
  577. struct run_info run;
  578. run.quiet = summary;
  579. run.m = m;
  580. run.prefix = prefix;
  581. run.score = run.total = 0;
  582. run.pass = true;
  583. dgraph_traverse_to(all, run_test, &run);
  584. printf("%sTotal score: %u/%u\n", prefix, run.score, run.total);
  585. return run.pass;
  586. }
  587. static bool add_to_all(const char *member, struct ccanlint *c,
  588. struct dgraph_node *all)
  589. {
  590. dgraph_add_edge(&c->node, all);
  591. return true;
  592. }
  593. int main(int argc, char *argv[])
  594. {
  595. bool summary = false, pass = true;
  596. unsigned int i;
  597. struct manifest *m;
  598. const char *prefix = "";
  599. char *dir = talloc_getcwd(NULL), *base_dir = dir, *testlink;
  600. struct dgraph_node all;
  601. /* Empty graph node to which we attach everything else. */
  602. dgraph_init_node(&all);
  603. opt_register_early_noarg("--verbose|-v", opt_inc_intval, &verbose,
  604. "verbose mode (up to -vvvv)");
  605. opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
  606. "do not compile anything");
  607. opt_register_noarg("-l|--list-tests", list_tests, NULL,
  608. "list tests ccanlint performs (and exit)");
  609. opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
  610. "print dependency graph of tests in Graphviz .dot format");
  611. opt_register_noarg("-k|--keep", keep_tests, NULL,
  612. "do not delete ccanlint working files");
  613. opt_register_noarg("--summary|-s", opt_set_bool, &summary,
  614. "simply give one line summary");
  615. opt_register_arg("-x|--exclude <testname>", exclude_test, NULL, NULL,
  616. "exclude <testname> (can be used multiple times)");
  617. opt_register_arg("--timeout <milleseconds>", opt_set_uintval,
  618. NULL, &timeout,
  619. "ignore (terminate) tests that are slower than this");
  620. opt_register_arg("-t|--target <testname>", opt_set_target, NULL, &all,
  621. "only run one test (and its prerequisites)");
  622. opt_register_arg("--compiler <compiler>", opt_set_const_charp,
  623. NULL, &compiler, "set the compiler");
  624. opt_register_arg("--cflags <flags>", opt_set_const_charp,
  625. NULL, &cflags, "set the compiler flags");
  626. opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
  627. "\nA program for checking and guiding development"
  628. " of CCAN modules.",
  629. "This usage message");
  630. /* Do verbose before anything else... */
  631. opt_early_parse(argc, argv, opt_log_stderr_exit);
  632. /* We move into temporary directory, so gcov dumps its files there. */
  633. if (chdir(temp_dir(talloc_autofree_context())) != 0)
  634. err(1, "Error changing to %s temporary dir", temp_dir(NULL));
  635. init_tests();
  636. if (verbose >= 3) {
  637. compile_verbose = true;
  638. print_test_depends();
  639. }
  640. if (verbose >= 4)
  641. tools_verbose = true;
  642. opt_parse(&argc, argv, opt_log_stderr_exit);
  643. if (!targeting)
  644. strmap_iterate(&tests, add_to_all, &all);
  645. /* This links back to the module's test dir. */
  646. testlink = talloc_asprintf(NULL, "%s/test", temp_dir(NULL));
  647. /* Defaults to pwd. */
  648. if (argc == 1) {
  649. i = 1;
  650. goto got_dir;
  651. }
  652. for (i = 1; i < argc; i++) {
  653. unsigned int score, total_score;
  654. dir = argv[i];
  655. if (dir[0] != '/')
  656. dir = talloc_asprintf_append(NULL, "%s/%s",
  657. base_dir, dir);
  658. while (strends(dir, "/"))
  659. dir[strlen(dir)-1] = '\0';
  660. got_dir:
  661. if (dir != base_dir)
  662. prefix = talloc_append_string(talloc_basename(NULL,dir),
  663. ": ");
  664. m = get_manifest(talloc_autofree_context(), dir);
  665. /* FIXME: This has to come after we've got manifest. */
  666. if (i == 1)
  667. read_config_header();
  668. /* Create a symlink from temp dir back to src dir's
  669. * test directory. */
  670. unlink(testlink);
  671. if (symlink(talloc_asprintf(m, "%s/test", dir), testlink) != 0)
  672. err(1, "Creating test symlink in %s", temp_dir(NULL));
  673. score = total_score = 0;
  674. if (!run_tests(&all, summary, m, prefix))
  675. pass = false;
  676. reset_tests(&all);
  677. }
  678. return pass ? 0 : 1;
  679. }