tests_compile_coverage.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/str/str.h>
  5. #include <ccan/foreach/foreach.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <limits.h>
  11. #include <errno.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <err.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include "build.h"
  18. #include "tests_compile.h"
  19. /* Note: we already test safe_mode in run_tests.c */
  20. static const char *can_run_coverage(struct manifest *m)
  21. {
  22. unsigned int timeleft = default_timeout_ms;
  23. char *output;
  24. if (!run_command(m, &timeleft, &output, "gcov -h"))
  25. return talloc_asprintf(m, "No gcov support: %s", output);
  26. return NULL;
  27. }
  28. static void cov_compile(const void *ctx,
  29. unsigned int time_ms,
  30. struct manifest *m,
  31. struct ccan_file *file,
  32. bool link_with_module)
  33. {
  34. char *flags = talloc_asprintf(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
  35. file->compiled[COMPILE_COVERAGE] = temp_file(ctx, "", file->fullname);
  36. compile_and_link_async(file, time_ms, file->fullname, ccan_dir,
  37. test_obj_list(m, link_with_module,
  38. COMPILE_NORMAL,
  39. COMPILE_COVERAGE),
  40. compiler, flags,
  41. lib_list(m, COMPILE_NORMAL),
  42. file->compiled[COMPILE_COVERAGE]);
  43. }
  44. /* FIXME: Coverage from testable examples as well. */
  45. static void do_compile_coverage_tests(struct manifest *m,
  46. unsigned int *timeleft,
  47. struct score *score)
  48. {
  49. char *cmdout;
  50. struct ccan_file *i;
  51. struct list_head *h;
  52. bool ok;
  53. char *f = talloc_asprintf(score, "%s %s", cflags, COVERAGE_CFLAGS);
  54. /* For API tests, we need coverage version of module. */
  55. if (!list_empty(&m->api_tests)) {
  56. build_objects(m, score, f, COMPILE_COVERAGE);
  57. if (!score->pass) {
  58. score->error = talloc_strdup(score,
  59. "Failed to compile module objects with coverage");
  60. return;
  61. }
  62. }
  63. foreach_ptr(h, &m->run_tests, &m->api_tests) {
  64. list_for_each(h, i, list) {
  65. cov_compile(m, *timeleft, m, i, h == &m->api_tests);
  66. }
  67. }
  68. while ((i = collect_command(&ok, &cmdout)) != NULL) {
  69. if (!ok) {
  70. score_file_error(score, i, 0,
  71. "Failed to compile test with coverage:"
  72. " %s", cmdout);
  73. }
  74. }
  75. if (!score->error) {
  76. score->pass = true;
  77. score->score = score->total;
  78. }
  79. }
  80. struct ccanlint tests_compile_coverage = {
  81. .key = "tests_compile_coverage",
  82. .name = "Module tests compile with " COVERAGE_CFLAGS,
  83. .check = do_compile_coverage_tests,
  84. .can_run = can_run_coverage,
  85. .needs = "tests_compile"
  86. };
  87. REGISTER_TEST(tests_compile_coverage);