tests_compile_coverage.c 3.1 KB

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