build-coverage.c 3.8 KB

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