tests_compile_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 <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", i->compiled);
  57. if (modobjs)
  58. list = talloc_append_string(list, modobjs);
  59. /* Other ccan modules (don't need coverage versions of those). */
  60. list_for_each(&m->deps, subm, list) {
  61. if (subm->compiled)
  62. list = talloc_asprintf_append(list, " %s",
  63. subm->compiled);
  64. }
  65. return list;
  66. }
  67. static char *lib_list(const struct manifest *m)
  68. {
  69. unsigned int i, num;
  70. char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
  71. char *ret = talloc_strdup(m, "");
  72. for (i = 0; i < num; i++)
  73. ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
  74. return ret;
  75. }
  76. static char *cov_compile(const void *ctx,
  77. struct manifest *m,
  78. struct ccan_file *file,
  79. const char *modobjs,
  80. bool keep)
  81. {
  82. char *output;
  83. char *f = talloc_asprintf(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
  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. compiler, f,
  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. struct list_head *h;
  105. if (!list_empty(&m->api_tests)
  106. && !build_module_objs_with_coverage(m, keep, score, &modobjs)) {
  107. score->error = talloc_strdup(score,
  108. "Failed to compile module objects with coverage");
  109. return;
  110. }
  111. foreach_ptr(h, &m->run_tests, &m->api_tests) {
  112. list_for_each(h, i, list) {
  113. cmdout = cov_compile(m, m, i, NULL, keep);
  114. if (cmdout) {
  115. score_file_error(score, i, 0,
  116. "Failed to compile test with coverage: %s",
  117. cmdout);
  118. }
  119. }
  120. }
  121. if (!score->error) {
  122. score->pass = true;
  123. score->score = score->total;
  124. }
  125. }
  126. struct ccanlint tests_compile_coverage = {
  127. .key = "tests_compile_coverage",
  128. .name = "Module tests compile with " COVERAGE_CFLAGS,
  129. .check = do_compile_coverage_tests,
  130. .can_run = can_run_coverage,
  131. .needs = "tests_compile"
  132. };
  133. REGISTER_TEST(tests_compile_coverage);