tests_compile.c 5.6 KB

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