tests_compile_coverage.c 2.8 KB

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