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. #ifdef __GNUC__
  23. unsigned int timeleft = default_timeout_ms;
  24. char *output;
  25. if (!run_command(m, &timeleft, &output, "gcov -h"))
  26. return talloc_asprintf(m, "No gcov support: %s", output);
  27. return NULL;
  28. #else
  29. return "No coverage support for this compiler";
  30. #endif
  31. }
  32. static void cov_compile(const void *ctx,
  33. unsigned int time_ms,
  34. struct manifest *m,
  35. struct ccan_file *file,
  36. bool link_with_module)
  37. {
  38. char *flags = talloc_asprintf(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
  39. file->compiled[COMPILE_COVERAGE] = temp_file(ctx, "", file->fullname);
  40. compile_and_link_async(file, time_ms, file->fullname, ccan_dir,
  41. test_obj_list(m, link_with_module,
  42. COMPILE_NORMAL,
  43. COMPILE_COVERAGE),
  44. compiler, flags,
  45. lib_list(m, COMPILE_NORMAL),
  46. file->compiled[COMPILE_COVERAGE]);
  47. }
  48. /* FIXME: Coverage from testable examples as well. */
  49. static void do_compile_coverage_tests(struct manifest *m,
  50. unsigned int *timeleft,
  51. struct score *score)
  52. {
  53. char *cmdout;
  54. struct ccan_file *i;
  55. struct list_head *h;
  56. bool ok;
  57. char *f = talloc_asprintf(score, "%s %s", cflags, COVERAGE_CFLAGS);
  58. /* For API tests, we need coverage version of module. */
  59. if (!list_empty(&m->api_tests)) {
  60. build_objects(m, score, f, COMPILE_COVERAGE);
  61. if (!score->pass) {
  62. score->error = talloc_strdup(score,
  63. "Failed to compile module objects with coverage");
  64. return;
  65. }
  66. }
  67. foreach_ptr(h, &m->run_tests, &m->api_tests) {
  68. list_for_each(h, i, list) {
  69. cov_compile(m, *timeleft, m, i, h == &m->api_tests);
  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);