tests_compile.c 4.8 KB

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