build-coverage.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. static char *obj_list(const struct manifest *m, const char *modobjs)
  48. {
  49. char *list;
  50. struct ccan_file *i;
  51. /* We expect to be linked with tap, unless that's us. */
  52. if (!streq(m->basename, "tap"))
  53. list = talloc_asprintf(m, "%s/ccan/tap.o", ccan_dir);
  54. else
  55. list = talloc_strdup(m, "");
  56. /* Objects from any other C files. */
  57. list_for_each(&m->other_test_c_files, i, list)
  58. list = talloc_asprintf_append(list, " %s", i->compiled);
  59. if (modobjs)
  60. list = talloc_append_string(list, modobjs);
  61. /* Other ccan modules (don't need coverage versions of those). */
  62. list_for_each(&m->dep_dirs, i, list) {
  63. if (i->compiled)
  64. list = talloc_asprintf_append(list, " %s", i->compiled);
  65. }
  66. return list;
  67. }
  68. static char *lib_list(const struct manifest *m)
  69. {
  70. unsigned int i, num;
  71. char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
  72. char *ret = talloc_strdup(m, "");
  73. for (i = 0; i < num; i++)
  74. ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
  75. return ret;
  76. }
  77. static char *cov_compile(const void *ctx,
  78. struct manifest *m,
  79. struct ccan_file *file,
  80. const char *modobjs,
  81. bool keep)
  82. {
  83. char *output;
  84. file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
  85. if (!compile_and_link(ctx, file->fullname, ccan_dir,
  86. obj_list(m, modobjs),
  87. COVERAGE_CFLAGS,
  88. lib_list(m), file->cov_compiled, &output)) {
  89. talloc_free(file->cov_compiled);
  90. file->cov_compiled = NULL;
  91. return output;
  92. }
  93. talloc_free(output);
  94. return NULL;
  95. }
  96. /* FIXME: Coverage from testable examples as well. */
  97. static void do_compile_coverage_tests(struct manifest *m,
  98. bool keep,
  99. unsigned int *timeleft,
  100. struct score *score)
  101. {
  102. char *cmdout, *modobjs = NULL;
  103. struct ccan_file *i;
  104. if (!list_empty(&m->api_tests)
  105. && !build_module_objs_with_coverage(m, keep, score, &modobjs)) {
  106. score->error = "Failed to compile module objects with coverage";
  107. return;
  108. }
  109. list_for_each(&m->run_tests, i, list) {
  110. cmdout = cov_compile(m, m, i, NULL, keep);
  111. if (cmdout) {
  112. score->error = "Failed to compile test with coverage";
  113. score_file_error(score, i, 0, cmdout);
  114. }
  115. }
  116. list_for_each(&m->api_tests, i, list) {
  117. cmdout = cov_compile(m, m, i, modobjs, keep);
  118. if (cmdout) {
  119. score->error = "Failed to compile test with coverage";
  120. score_file_error(score, i, 0, cmdout);
  121. }
  122. }
  123. if (!score->error) {
  124. score->pass = true;
  125. score->score = score->total;
  126. }
  127. }
  128. struct ccanlint compile_coverage_tests = {
  129. .key = "compile-coverage-tests",
  130. .name = "Module tests compile with profiling",
  131. .check = do_compile_coverage_tests,
  132. .can_run = can_run_coverage,
  133. };
  134. REGISTER_TEST(compile_coverage_tests, &compile_tests, NULL);