build-coverage.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 = run_command(m, &timeleft, "gcov -h");
  21. if (output)
  22. return talloc_asprintf(m, "No gcov support: %s", output);
  23. return NULL;
  24. }
  25. static char *build_module_objs_with_coverage(struct manifest *m, bool keep,
  26. char **modobjs)
  27. {
  28. struct ccan_file *i;
  29. *modobjs = talloc_strdup(m, "");
  30. list_for_each(&m->c_files, i, list) {
  31. char *err;
  32. char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
  33. i->cov_compiled = maybe_temp_file(m, "", keep, fullfile);
  34. err = compile_object(m, fullfile, ccan_dir, "",
  35. i->cov_compiled);
  36. if (err) {
  37. talloc_free(i->cov_compiled);
  38. return err;
  39. }
  40. *modobjs = talloc_asprintf_append(*modobjs,
  41. " %s", i->cov_compiled);
  42. }
  43. return NULL;
  44. }
  45. static char *obj_list(const struct manifest *m, const char *modobjs)
  46. {
  47. char *list;
  48. struct ccan_file *i;
  49. /* We expect to be linked with tap, unless that's us. */
  50. if (!streq(m->basename, "tap"))
  51. list = talloc_asprintf(m, "%s/ccan/tap.o", ccan_dir);
  52. else
  53. list = talloc_strdup(m, "");
  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->dep_dirs, i, list) {
  61. if (i->compiled)
  62. list = talloc_asprintf_append(list, " %s", i->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, ".", &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 *errmsg;
  82. file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
  83. errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
  84. obj_list(m, modobjs),
  85. COVERAGE_CFLAGS,
  86. lib_list(m), file->cov_compiled);
  87. if (errmsg) {
  88. talloc_free(file->cov_compiled);
  89. return errmsg;
  90. }
  91. return NULL;
  92. }
  93. struct compile_tests_result {
  94. struct list_node list;
  95. const char *filename;
  96. const char *description;
  97. const char *output;
  98. };
  99. static void *do_compile_coverage_tests(struct manifest *m,
  100. bool keep,
  101. unsigned int *timeleft)
  102. {
  103. struct list_head *list = talloc(m, struct list_head);
  104. char *cmdout, *modobjs = NULL;
  105. struct ccan_file *i;
  106. struct compile_tests_result *res;
  107. list_head_init(list);
  108. if (!list_empty(&m->api_tests)) {
  109. cmdout = build_module_objs_with_coverage(m, keep, &modobjs);
  110. if (cmdout) {
  111. res = talloc(list, struct compile_tests_result);
  112. res->filename = "Module objects with coverage";
  113. res->description = "failed to compile";
  114. res->output = talloc_steal(res, cmdout);
  115. list_add_tail(list, &res->list);
  116. return list;
  117. }
  118. }
  119. list_for_each(&m->run_tests, i, list) {
  120. compile_tests.total_score++;
  121. cmdout = cov_compile(m, m, i, NULL, keep);
  122. if (cmdout) {
  123. res = talloc(list, struct compile_tests_result);
  124. res->filename = i->name;
  125. res->description = "failed to compile";
  126. res->output = talloc_steal(res, cmdout);
  127. list_add_tail(list, &res->list);
  128. }
  129. }
  130. list_for_each(&m->api_tests, i, list) {
  131. compile_tests.total_score++;
  132. cmdout = cov_compile(m, m, i, modobjs, keep);
  133. if (cmdout) {
  134. res = talloc(list, struct compile_tests_result);
  135. res->filename = i->name;
  136. res->description = "failed to compile";
  137. res->output = talloc_steal(res, cmdout);
  138. list_add_tail(list, &res->list);
  139. }
  140. }
  141. if (list_empty(list)) {
  142. talloc_free(list);
  143. list = NULL;
  144. }
  145. return list;
  146. }
  147. static const char *describe_compile_coverage_tests(struct manifest *m,
  148. void *check_result)
  149. {
  150. struct list_head *list = check_result;
  151. struct compile_tests_result *i;
  152. char *descrip;
  153. descrip = talloc_strdup(list,
  154. "Compilation of tests for coverage failed:\n");
  155. list_for_each(list, i, list)
  156. descrip = talloc_asprintf_append(descrip, "%s %s\n%s",
  157. i->filename, i->description,
  158. i->output);
  159. return descrip;
  160. }
  161. struct ccanlint compile_coverage_tests = {
  162. .key = "compile-coverage-tests",
  163. .name = "Module tests compile with profiling",
  164. .check = do_compile_coverage_tests,
  165. .total_score = 1,
  166. .describe = describe_compile_coverage_tests,
  167. .can_run = can_run_coverage,
  168. };
  169. REGISTER_TEST(compile_coverage_tests, &compile_tests, NULL);