tests_compile.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #include "tests_compile.h"
  19. static const char *can_build(struct manifest *m)
  20. {
  21. if (safe_mode)
  22. return "Safe mode enabled";
  23. return NULL;
  24. }
  25. char *test_obj_list(const struct manifest *m, bool link_with_module,
  26. enum compile_type ctype, enum compile_type own_ctype)
  27. {
  28. char *list = talloc_strdup(m, "");
  29. struct ccan_file *i;
  30. struct manifest *subm;
  31. /* Objects from any other C files. */
  32. list_for_each(&m->other_test_c_files, i, list)
  33. list = talloc_asprintf_append(list, " %s",
  34. i->compiled[ctype]);
  35. /* Our own object files. */
  36. if (link_with_module)
  37. list_for_each(&m->c_files, i, list)
  38. list = talloc_asprintf_append(list, " %s",
  39. i->compiled[own_ctype]);
  40. /* Other ccan modules (normal depends). */
  41. list_for_each(&m->deps, subm, list) {
  42. if (subm->compiled[ctype])
  43. list = talloc_asprintf_append(list, " %s",
  44. subm->compiled[ctype]);
  45. }
  46. /* Other ccan modules (test depends). */
  47. list_for_each(&m->test_deps, subm, list) {
  48. if (subm->compiled[ctype])
  49. list = talloc_asprintf_append(list, " %s",
  50. subm->compiled[ctype]);
  51. }
  52. return list;
  53. }
  54. char *test_lib_list(const struct manifest *m, enum compile_type ctype)
  55. {
  56. unsigned int i;
  57. char **libs;
  58. char *ret = talloc_strdup(m, "");
  59. libs = get_libs(m, m->dir, "testdepends", get_or_compile_info);
  60. for (i = 0; libs[i]; i++)
  61. ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
  62. return ret;
  63. }
  64. static bool compile(const void *ctx,
  65. struct manifest *m,
  66. struct ccan_file *file,
  67. bool fail,
  68. bool link_with_module,
  69. enum compile_type ctype,
  70. char **output)
  71. {
  72. char *fname, *flags;
  73. flags = talloc_asprintf(ctx, "%s%s%s",
  74. fail ? "-DFAIL " : "",
  75. cflags,
  76. ctype == COMPILE_NOFEAT
  77. ? " "REDUCE_FEATURES_FLAGS : "");
  78. fname = temp_file(ctx, "", file->fullname);
  79. if (!compile_and_link(ctx, file->fullname, ccan_dir,
  80. test_obj_list(m, link_with_module,
  81. ctype, ctype),
  82. compiler, flags, test_lib_list(m, ctype), fname,
  83. output)) {
  84. talloc_free(fname);
  85. return false;
  86. }
  87. file->compiled[ctype] = fname;
  88. return true;
  89. }
  90. static void compile_async(const void *ctx,
  91. struct manifest *m,
  92. struct ccan_file *file,
  93. bool link_with_module,
  94. enum compile_type ctype,
  95. unsigned int time_ms)
  96. {
  97. char *flags;
  98. file->compiled[ctype] = temp_file(ctx, "", file->fullname);
  99. flags = talloc_asprintf(ctx, "%s%s",
  100. cflags,
  101. ctype == COMPILE_NOFEAT
  102. ? " "REDUCE_FEATURES_FLAGS : "");
  103. compile_and_link_async(file, time_ms, file->fullname, ccan_dir,
  104. test_obj_list(m, link_with_module, ctype, ctype),
  105. compiler, flags, test_lib_list(m, ctype),
  106. file->compiled[ctype]);
  107. }
  108. static void compile_tests(struct manifest *m,
  109. struct score *score,
  110. enum compile_type ctype,
  111. unsigned int time_ms)
  112. {
  113. char *cmdout;
  114. struct ccan_file *i;
  115. struct list_head *list;
  116. bool errors = false, warnings = false, ok;
  117. foreach_ptr(list, &m->compile_ok_tests, &m->run_tests, &m->api_tests) {
  118. list_for_each(list, i, list) {
  119. compile_async(score, m, i,
  120. list == &m->api_tests,
  121. ctype, time_ms);
  122. }
  123. }
  124. while ((i = collect_command(&ok, &cmdout)) != NULL) {
  125. if (!ok) {
  126. score_file_error(score, i, 0,
  127. "Compile failed:\n%s",
  128. cmdout);
  129. errors = true;
  130. } else if (!streq(cmdout, "")) {
  131. score_file_error(score, i, 0,
  132. "Compile gave warnings:\n%s",
  133. cmdout);
  134. warnings = true;
  135. }
  136. }
  137. /* The compile fail tests are a bit weird, handle them separately */
  138. if (errors)
  139. return;
  140. /* For historical reasons, "fail" often means "gives warnings" */
  141. list_for_each(&m->compile_fail_tests, i, list) {
  142. if (!compile(score, m, i, false, false, ctype, &cmdout)) {
  143. score_file_error(score, i, 0,
  144. "Compile without -DFAIL failed:\n%s",
  145. cmdout);
  146. return;
  147. }
  148. if (!streq(cmdout, "")) {
  149. score_file_error(score, i, 0,
  150. "Compile gave warnings"
  151. " without -DFAIL:\n%s",
  152. cmdout);
  153. return;
  154. }
  155. if (compile(score, m, i, true, false, ctype, &cmdout)
  156. && streq(cmdout, "")) {
  157. score_file_error(score, i, 0,
  158. "Compiled successfully with -DFAIL?");
  159. return;
  160. }
  161. score->total++;
  162. }
  163. score->pass = true;
  164. score->score = score->total - warnings;
  165. }
  166. /* FIXME: If we time out, set *timeleft to 0 */
  167. static void do_compile_tests(struct manifest *m,
  168. unsigned int *timeleft, struct score *score)
  169. {
  170. compile_tests(m, score, COMPILE_NORMAL, *timeleft);
  171. }
  172. struct ccanlint tests_compile = {
  173. .key = "tests_compile",
  174. .name = "Module tests compile",
  175. .check = do_compile_tests,
  176. .can_run = can_build,
  177. .needs = "tests_helpers_compile objects_build"
  178. };
  179. REGISTER_TEST(tests_compile);
  180. static const char *features_reduced(struct manifest *m)
  181. {
  182. if (features_were_reduced)
  183. return NULL;
  184. return "No features to turn off";
  185. }
  186. static void do_compile_tests_without_features(struct manifest *m,
  187. unsigned int *timeleft,
  188. struct score *score)
  189. {
  190. compile_tests(m, score, COMPILE_NOFEAT, *timeleft);
  191. }
  192. struct ccanlint tests_compile_without_features = {
  193. .key = "tests_compile_without_features",
  194. .name = "Module tests compile (without features)",
  195. .check = do_compile_tests_without_features,
  196. .can_run = features_reduced,
  197. .needs = "module_builds tests_helpers_compile_without_features objects_build_without_features"
  198. };
  199. REGISTER_TEST(tests_compile_without_features);