tests_compile.c 5.5 KB

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