tests_compile.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_tests(struct manifest *m, bool keep,
  86. struct score *score,
  87. enum compile_type ctype)
  88. {
  89. char *cmdout;
  90. struct ccan_file *i;
  91. struct list_head *list;
  92. bool errors = false, warnings = false;
  93. foreach_ptr(list, &m->compile_ok_tests, &m->run_tests, &m->api_tests) {
  94. list_for_each(list, i, list) {
  95. if (!compile(score, m, i, false,
  96. list == &m->api_tests, keep,
  97. ctype, &cmdout)) {
  98. score_file_error(score, i, 0,
  99. "Compile failed:\n%s",
  100. cmdout);
  101. errors = true;
  102. } else if (!streq(cmdout, "")) {
  103. score_file_error(score, i, 0,
  104. "Compile gave warnings:\n%s",
  105. cmdout);
  106. warnings = true;
  107. }
  108. }
  109. }
  110. /* The compile fail tests are a bit weird, handle them separately */
  111. if (errors)
  112. return;
  113. /* For historical reasons, "fail" often means "gives warnings" */
  114. list_for_each(&m->compile_fail_tests, i, list) {
  115. if (!compile(score, m, i, false, false, false,
  116. ctype, &cmdout)) {
  117. score_file_error(score, i, 0,
  118. "Compile without -DFAIL failed:\n%s",
  119. cmdout);
  120. return;
  121. }
  122. if (!streq(cmdout, "")) {
  123. score_file_error(score, i, 0,
  124. "Compile gave warnings"
  125. " without -DFAIL:\n%s",
  126. cmdout);
  127. return;
  128. }
  129. if (compile(score, m, i, true, false, false,
  130. ctype, &cmdout)
  131. && streq(cmdout, "")) {
  132. score_file_error(score, i, 0,
  133. "Compiled successfully with -DFAIL?");
  134. return;
  135. }
  136. score->total++;
  137. }
  138. score->pass = true;
  139. score->score = score->total - warnings;
  140. }
  141. static void do_compile_tests(struct manifest *m,
  142. bool keep,
  143. unsigned int *timeleft, struct score *score)
  144. {
  145. compile_tests(m, keep, score, COMPILE_NORMAL);
  146. }
  147. struct ccanlint tests_compile = {
  148. .key = "tests_compile",
  149. .name = "Module tests compile",
  150. .check = do_compile_tests,
  151. .can_run = can_build,
  152. .needs = "tests_helpers_compile objects_build"
  153. };
  154. REGISTER_TEST(tests_compile);
  155. static const char *features_reduced(struct manifest *m)
  156. {
  157. if (features_were_reduced)
  158. return NULL;
  159. return "No features to turn off";
  160. }
  161. static void do_compile_tests_without_features(struct manifest *m,
  162. bool keep,
  163. unsigned int *timeleft,
  164. struct score *score)
  165. {
  166. compile_tests(m, keep, score, COMPILE_NOFEAT);
  167. }
  168. struct ccanlint tests_compile_without_features = {
  169. .key = "tests_compile_without_features",
  170. .name = "Module tests compile (without features)",
  171. .check = do_compile_tests_without_features,
  172. .can_run = features_reduced,
  173. .needs = "tests_helpers_compile_without_features objects_build_without_features"
  174. };
  175. REGISTER_TEST(tests_compile_without_features);