tests_compile.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #include "reduce_features.h"
  18. static const char *can_build(struct manifest *m)
  19. {
  20. if (safe_mode)
  21. return "Safe mode enabled";
  22. return NULL;
  23. }
  24. /* FIXME: Merge this into one place. */
  25. static char *obj_list(const struct manifest *m, bool link_with_module)
  26. {
  27. char *list = talloc_strdup(m, "");
  28. struct ccan_file *i;
  29. struct manifest *subm;
  30. /* Objects from any other C files. */
  31. list_for_each(&m->other_test_c_files, i, list)
  32. list = talloc_asprintf_append(list, " %s", i->compiled);
  33. /* Our own object files. */
  34. if (link_with_module)
  35. list_for_each(&m->c_files, i, list)
  36. list = talloc_asprintf_append(list, " %s", i->compiled);
  37. /* Other ccan modules. */
  38. list_for_each(&m->deps, subm, list) {
  39. if (subm->compiled)
  40. list = talloc_asprintf_append(list, " %s",
  41. subm->compiled);
  42. }
  43. return list;
  44. }
  45. static char *lib_list(const struct manifest *m)
  46. {
  47. unsigned int i, num;
  48. char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
  49. char *ret = talloc_strdup(m, "");
  50. for (i = 0; i < num; i++)
  51. ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
  52. return ret;
  53. }
  54. static bool compile(const void *ctx,
  55. struct manifest *m,
  56. struct ccan_file *file,
  57. const char *flags,
  58. bool fail,
  59. bool link_with_module,
  60. bool keep, char **output)
  61. {
  62. char *f = talloc_asprintf(ctx, "%s%s%s",
  63. flags, fail ? "-DFAIL " : "", cflags);
  64. file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
  65. if (!compile_and_link(ctx, file->fullname, ccan_dir,
  66. obj_list(m, link_with_module), compiler, f,
  67. lib_list(m), file->compiled, output)) {
  68. talloc_free(file->compiled);
  69. return false;
  70. }
  71. return true;
  72. }
  73. static void compile_tests(struct manifest *m, bool keep,
  74. struct score *score, const char *incl)
  75. {
  76. char *cmdout;
  77. struct ccan_file *i;
  78. struct list_head *list;
  79. bool errors = false, warnings = false;
  80. foreach_ptr(list, &m->compile_ok_tests, &m->run_tests, &m->api_tests) {
  81. list_for_each(list, i, list) {
  82. if (!compile(score, m, i, incl, false,
  83. list == &m->api_tests, keep, &cmdout)) {
  84. score_file_error(score, i, 0,
  85. "Compile failed:\n%s",
  86. cmdout);
  87. errors = true;
  88. } else if (!streq(cmdout, "")) {
  89. score_file_error(score, i, 0,
  90. "Compile gave warnings:\n%s",
  91. cmdout);
  92. warnings = true;
  93. }
  94. }
  95. }
  96. /* The compile fail tests are a bit weird, handle them separately */
  97. if (errors)
  98. return;
  99. /* For historical reasons, "fail" often means "gives warnings" */
  100. list_for_each(&m->compile_fail_tests, i, list) {
  101. if (!compile(score, m, i, incl, false, false, false, &cmdout)) {
  102. score_file_error(score, i, 0,
  103. "Compile without -DFAIL failed:\n%s",
  104. cmdout);
  105. return;
  106. }
  107. if (!streq(cmdout, "")) {
  108. score_file_error(score, i, 0,
  109. "Compile gave warnings"
  110. " without -DFAIL:\n%s",
  111. cmdout);
  112. return;
  113. }
  114. if (compile(score, m, i, incl, true, false, false, &cmdout)
  115. && streq(cmdout, "")) {
  116. score_file_error(score, i, 0,
  117. "Compiled successfully with -DFAIL?");
  118. return;
  119. }
  120. score->total++;
  121. }
  122. score->pass = true;
  123. score->score = score->total - warnings;
  124. }
  125. static void do_compile_tests(struct manifest *m,
  126. bool keep,
  127. unsigned int *timeleft, struct score *score)
  128. {
  129. return compile_tests(m, keep, score, "");
  130. }
  131. struct ccanlint tests_compile = {
  132. .key = "tests_compile",
  133. .name = "Module tests compile",
  134. .check = do_compile_tests,
  135. .can_run = can_build,
  136. .needs = "tests_helpers_compile objects_build"
  137. };
  138. REGISTER_TEST(tests_compile);
  139. static const char *features_reduced(struct manifest *m)
  140. {
  141. if (features_were_reduced)
  142. return NULL;
  143. return "No features to turn off";
  144. }
  145. static void do_compile_tests_without_features(struct manifest *m,
  146. bool keep,
  147. unsigned int *timeleft,
  148. struct score *score)
  149. {
  150. compile_tests(m, keep, score, "-I. ");
  151. }
  152. struct ccanlint tests_compile_without_features = {
  153. .key = "tests_compile_without_features",
  154. .name = "Module tests compile (without features)",
  155. .check = do_compile_tests_without_features,
  156. .can_run = features_reduced,
  157. .needs = "reduce_features"
  158. };
  159. REGISTER_TEST(tests_compile_without_features);