tests_compile_coverage.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/str/str.h>
  4. #include <ccan/foreach/foreach.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <limits.h>
  10. #include <errno.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <err.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include "build.h"
  17. #include "tests_compile.h"
  18. /* Note: we already test safe_mode in run_tests.c */
  19. static const char *can_run_coverage(struct manifest *m)
  20. {
  21. #ifdef __GNUC__
  22. unsigned int timeleft = default_timeout_ms;
  23. char *output;
  24. if (!run_command(m, &timeleft, &output, "gcov -h"))
  25. return tal_fmt(m, "No gcov support: %s", output);
  26. return NULL;
  27. #else
  28. return "No coverage support for this compiler";
  29. #endif
  30. }
  31. static void cov_compile(const void *ctx,
  32. unsigned int time_ms,
  33. struct manifest *m,
  34. struct ccan_file *file,
  35. bool link_with_module)
  36. {
  37. char *flags = tal_fmt(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
  38. file->compiled[COMPILE_COVERAGE] = temp_file(ctx, "", file->fullname);
  39. compile_and_link_async(file, time_ms, file->fullname, ccan_dir,
  40. test_obj_list(m, link_with_module,
  41. COMPILE_NORMAL,
  42. COMPILE_COVERAGE),
  43. compiler, flags,
  44. test_lib_list(m, COMPILE_NORMAL),
  45. file->compiled[COMPILE_COVERAGE]);
  46. }
  47. /* FIXME: Coverage from testable examples as well. */
  48. static void do_compile_coverage_tests(struct manifest *m,
  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 = tal_fmt(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, score, f, COMPILE_COVERAGE);
  60. if (!score->pass) {
  61. score->error = tal_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. }
  70. }
  71. while ((i = collect_command(&ok, &cmdout)) != NULL) {
  72. if (!ok) {
  73. score_file_error(score, i, 0,
  74. "Failed to compile test with coverage:"
  75. " %s", cmdout);
  76. }
  77. }
  78. if (!score->error) {
  79. score->pass = true;
  80. score->score = score->total;
  81. }
  82. }
  83. struct ccanlint tests_compile_coverage = {
  84. .key = "tests_compile_coverage",
  85. .name = "Module tests compile with " COVERAGE_CFLAGS,
  86. .check = do_compile_coverage_tests,
  87. .can_run = can_run_coverage,
  88. .needs = "tests_compile"
  89. };
  90. REGISTER_TEST(tests_compile_coverage);