tests_compile.c 5.4 KB

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