tests_compile_coverage.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. bool keep)
  34. {
  35. char *flags = talloc_asprintf(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
  36. file->compiled[COMPILE_COVERAGE]
  37. = maybe_temp_file(ctx, "", keep, file->fullname);
  38. compile_and_link_async(file, time_ms, file->fullname, ccan_dir,
  39. test_obj_list(m, link_with_module,
  40. COMPILE_NORMAL,
  41. COMPILE_COVERAGE),
  42. compiler, flags,
  43. lib_list(m, COMPILE_NORMAL),
  44. file->compiled[COMPILE_COVERAGE]);
  45. }
  46. /* FIXME: Coverage from testable examples as well. */
  47. static void do_compile_coverage_tests(struct manifest *m,
  48. bool keep,
  49. unsigned int *timeleft,
  50. struct score *score)
  51. {
  52. char *cmdout;
  53. struct ccan_file *i;
  54. struct list_head *h;
  55. bool ok;
  56. char *f = talloc_asprintf(score, "%s %s", cflags, COVERAGE_CFLAGS);
  57. /* For API tests, we need coverage version of module. */
  58. if (!list_empty(&m->api_tests)) {
  59. build_objects(m, keep, score, f, COMPILE_COVERAGE);
  60. if (!score->pass) {
  61. score->error = talloc_strdup(score,
  62. "Failed to compile module objects with coverage");
  63. return;
  64. }
  65. }
  66. foreach_ptr(h, &m->run_tests, &m->api_tests) {
  67. list_for_each(h, i, list) {
  68. cov_compile(m, *timeleft, m, i, h == &m->api_tests,
  69. keep);
  70. }
  71. }
  72. while ((i = collect_command(&ok, &cmdout)) != NULL) {
  73. if (!ok) {
  74. score_file_error(score, i, 0,
  75. "Failed to compile test with coverage:"
  76. " %s", cmdout);
  77. }
  78. }
  79. if (!score->error) {
  80. score->pass = true;
  81. score->score = score->total;
  82. }
  83. }
  84. struct ccanlint tests_compile_coverage = {
  85. .key = "tests_compile_coverage",
  86. .name = "Module tests compile with " COVERAGE_CFLAGS,
  87. .check = do_compile_coverage_tests,
  88. .can_run = can_run_coverage,
  89. .needs = "tests_compile"
  90. };
  91. REGISTER_TEST(tests_compile_coverage);