tests_compile_coverage.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. /* Note: we already test safe_mode in run_tests.c */
  18. static const char *can_run_coverage(struct manifest *m)
  19. {
  20. unsigned int timeleft = default_timeout_ms;
  21. char *output;
  22. if (!run_command(m, &timeleft, &output, "gcov -h"))
  23. return talloc_asprintf(m, "No gcov support: %s", output);
  24. return NULL;
  25. }
  26. static bool build_module_objs_with_coverage(struct manifest *m, bool keep,
  27. struct score *score,
  28. char **modobjs)
  29. {
  30. struct ccan_file *i;
  31. *modobjs = talloc_strdup(m, "");
  32. list_for_each(&m->c_files, i, list) {
  33. char *err;
  34. char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
  35. i->cov_compiled = maybe_temp_file(m, "", keep, fullfile);
  36. if (!compile_object(m, fullfile, ccan_dir, compiler, cflags,
  37. i->cov_compiled, &err)) {
  38. score_file_error(score, i, 0, "%s", err);
  39. talloc_free(i->cov_compiled);
  40. i->cov_compiled = NULL;
  41. return false;
  42. }
  43. *modobjs = talloc_asprintf_append(*modobjs,
  44. " %s", i->cov_compiled);
  45. }
  46. return true;
  47. }
  48. /* FIXME: Merge this into one place. */
  49. static char *obj_list(const struct manifest *m, const char *modobjs)
  50. {
  51. char *list = talloc_strdup(m, "");
  52. struct ccan_file *i;
  53. struct manifest *subm;
  54. /* Objects from any other C files. */
  55. list_for_each(&m->other_test_c_files, i, list)
  56. list = talloc_asprintf_append(list, " %s",
  57. i->compiled[COMPILE_NORMAL]);
  58. if (modobjs)
  59. list = talloc_append_string(list, modobjs);
  60. /* Other ccan modules (don't need coverage versions of those). */
  61. list_for_each(&m->deps, subm, list) {
  62. if (subm->compiled[COMPILE_NORMAL])
  63. list = talloc_asprintf_append(list, " %s",
  64. subm->compiled
  65. [COMPILE_NORMAL]);
  66. }
  67. return list;
  68. }
  69. static char *lib_list(const struct manifest *m)
  70. {
  71. unsigned int i, num;
  72. char **libs = get_libs(m, m->dir, &num,
  73. &m->info_file->compiled[COMPILE_NORMAL]);
  74. char *ret = talloc_strdup(m, "");
  75. for (i = 0; i < num; i++)
  76. ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
  77. return ret;
  78. }
  79. static char *cov_compile(const void *ctx,
  80. struct manifest *m,
  81. struct ccan_file *file,
  82. const char *modobjs,
  83. bool keep)
  84. {
  85. char *output;
  86. char *f = talloc_asprintf(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
  87. file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
  88. if (!compile_and_link(ctx, file->fullname, ccan_dir,
  89. obj_list(m, modobjs),
  90. compiler, f,
  91. lib_list(m), file->cov_compiled, &output)) {
  92. talloc_free(file->cov_compiled);
  93. file->cov_compiled = NULL;
  94. return output;
  95. }
  96. talloc_free(output);
  97. return NULL;
  98. }
  99. /* FIXME: Coverage from testable examples as well. */
  100. static void do_compile_coverage_tests(struct manifest *m,
  101. bool keep,
  102. unsigned int *timeleft,
  103. struct score *score)
  104. {
  105. char *cmdout, *modobjs = NULL;
  106. struct ccan_file *i;
  107. struct list_head *h;
  108. if (!list_empty(&m->api_tests)
  109. && !build_module_objs_with_coverage(m, keep, score, &modobjs)) {
  110. score->error = talloc_strdup(score,
  111. "Failed to compile module objects with coverage");
  112. return;
  113. }
  114. foreach_ptr(h, &m->run_tests, &m->api_tests) {
  115. list_for_each(h, i, list) {
  116. cmdout = cov_compile(m, m, i,
  117. h == &m->api_tests
  118. ? modobjs : NULL,
  119. keep);
  120. if (cmdout) {
  121. score_file_error(score, i, 0,
  122. "Failed to compile test with coverage: %s",
  123. cmdout);
  124. }
  125. }
  126. }
  127. if (!score->error) {
  128. score->pass = true;
  129. score->score = score->total;
  130. }
  131. }
  132. struct ccanlint tests_compile_coverage = {
  133. .key = "tests_compile_coverage",
  134. .name = "Module tests compile with " COVERAGE_CFLAGS,
  135. .check = do_compile_coverage_tests,
  136. .can_run = can_run_coverage,
  137. .needs = "tests_compile"
  138. };
  139. REGISTER_TEST(tests_compile_coverage);