tests_compile.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 UNNEEDED)
  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 UNNEEDED)
  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 char *cflags_list(const struct manifest *m, const char *iflags)
  60. {
  61. unsigned int i;
  62. char *ret = tal_strdup(m, iflags);
  63. char **flags = get_cflags(m, m->dir, get_or_compile_info);
  64. for (i = 0; flags[i]; i++)
  65. tal_append_fmt(&ret, " %s", flags[i]);
  66. return ret;
  67. }
  68. static bool compile(const void *ctx,
  69. struct manifest *m,
  70. struct ccan_file *file,
  71. bool fail,
  72. bool link_with_module,
  73. enum compile_type ctype,
  74. char **output)
  75. {
  76. char *fname, *flags;
  77. flags = tal_fmt(ctx, "%s%s%s",
  78. fail ? "-DFAIL " : "",
  79. cflags,
  80. ctype == COMPILE_NOFEAT
  81. ? " "REDUCE_FEATURES_FLAGS : "");
  82. flags = cflags_list(m, flags);
  83. fname = temp_file(ctx, "", file->fullname);
  84. if (!compile_and_link(ctx, file->fullname, ccan_dir,
  85. test_obj_list(m, link_with_module,
  86. ctype, ctype),
  87. compiler, flags, test_lib_list(m, ctype), fname,
  88. output)) {
  89. tal_free(fname);
  90. return false;
  91. }
  92. file->compiled[ctype] = fname;
  93. return true;
  94. }
  95. static void compile_async(const void *ctx,
  96. struct manifest *m,
  97. struct ccan_file *file,
  98. bool link_with_module,
  99. enum compile_type ctype,
  100. unsigned int time_ms)
  101. {
  102. char *flags;
  103. file->compiled[ctype] = temp_file(ctx, "", file->fullname);
  104. flags = tal_fmt(ctx, "%s%s",
  105. cflags,
  106. ctype == COMPILE_NOFEAT
  107. ? " "REDUCE_FEATURES_FLAGS : "");
  108. flags = cflags_list(m, flags);
  109. compile_and_link_async(file, time_ms, file->fullname, ccan_dir,
  110. test_obj_list(m, link_with_module, ctype, ctype),
  111. compiler, flags, test_lib_list(m, ctype),
  112. file->compiled[ctype]);
  113. }
  114. static void compile_tests(struct manifest *m,
  115. struct score *score,
  116. enum compile_type ctype,
  117. unsigned int time_ms)
  118. {
  119. char *cmdout;
  120. struct ccan_file *i;
  121. struct list_head *list;
  122. bool errors = false, warnings = false, ok;
  123. foreach_ptr(list, &m->compile_ok_tests, &m->run_tests, &m->api_tests) {
  124. list_for_each(list, i, list) {
  125. compile_async(score, m, i,
  126. list == &m->api_tests,
  127. ctype, time_ms);
  128. }
  129. }
  130. while ((i = collect_command(&ok, &cmdout)) != NULL) {
  131. if (!ok) {
  132. score_file_error(score, i, 0,
  133. "Compile failed:\n%s",
  134. cmdout);
  135. errors = true;
  136. } else if (!streq(cmdout, "")) {
  137. score_file_error(score, i, 0,
  138. "Compile gave warnings:\n%s",
  139. cmdout);
  140. warnings = true;
  141. }
  142. }
  143. /* The compile fail tests are a bit weird, handle them separately */
  144. if (errors)
  145. return;
  146. /* For historical reasons, "fail" often means "gives warnings" */
  147. list_for_each(&m->compile_fail_tests, i, list) {
  148. if (!compile(score, m, i, false, false, ctype, &cmdout)) {
  149. score_file_error(score, i, 0,
  150. "Compile without -DFAIL failed:\n%s",
  151. cmdout);
  152. return;
  153. }
  154. if (!streq(cmdout, "")) {
  155. score_file_error(score, i, 0,
  156. "Compile gave warnings"
  157. " without -DFAIL:\n%s",
  158. cmdout);
  159. return;
  160. }
  161. if (compile(score, m, i, true, false, ctype, &cmdout)
  162. && streq(cmdout, "")) {
  163. score_file_error(score, i, 0,
  164. "Compiled successfully with -DFAIL?");
  165. return;
  166. }
  167. score->total++;
  168. }
  169. score->pass = true;
  170. score->score = score->total - warnings;
  171. }
  172. /* FIXME: If we time out, set *timeleft to 0 */
  173. static void do_compile_tests(struct manifest *m,
  174. unsigned int *timeleft, struct score *score)
  175. {
  176. compile_tests(m, score, COMPILE_NORMAL, *timeleft);
  177. }
  178. struct ccanlint tests_compile = {
  179. .key = "tests_compile",
  180. .name = "Module tests compile",
  181. .check = do_compile_tests,
  182. .can_run = can_build,
  183. .needs = "tests_helpers_compile objects_build"
  184. };
  185. REGISTER_TEST(tests_compile);
  186. static const char *features_reduced(struct manifest *m UNNEEDED)
  187. {
  188. if (features_were_reduced)
  189. return NULL;
  190. return "No features to turn off";
  191. }
  192. static void do_compile_tests_without_features(struct manifest *m,
  193. unsigned int *timeleft,
  194. struct score *score)
  195. {
  196. compile_tests(m, score, COMPILE_NOFEAT, *timeleft);
  197. }
  198. struct ccanlint tests_compile_without_features = {
  199. .key = "tests_compile_without_features",
  200. .name = "Module tests compile (without features)",
  201. .check = do_compile_tests_without_features,
  202. .can_run = features_reduced,
  203. .needs = "module_builds tests_helpers_compile_without_features objects_build_without_features"
  204. };
  205. REGISTER_TEST(tests_compile_without_features);