compile_tests.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. static const char *can_build(struct manifest *m)
  18. {
  19. if (safe_mode)
  20. return "Safe mode enabled";
  21. return NULL;
  22. }
  23. static char *obj_list(const struct manifest *m, bool link_with_module)
  24. {
  25. char *list;
  26. struct ccan_file *i;
  27. /* We expect to be linked with tap, unless that's us. */
  28. if (!streq(m->basename, "tap"))
  29. list = talloc_asprintf(m, "%s/ccan/tap.o", ccan_dir);
  30. else
  31. list = talloc_strdup(m, "");
  32. /* Objects from any other C files. */
  33. list_for_each(&m->other_test_c_files, i, list)
  34. list = talloc_asprintf_append(list, " %s", i->compiled);
  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", i->compiled);
  39. /* Other ccan modules. */
  40. list_for_each(&m->dep_dirs, i, list) {
  41. if (i->compiled)
  42. list = talloc_asprintf_append(list, " %s", i->compiled);
  43. }
  44. return list;
  45. }
  46. static char *lib_list(const struct manifest *m)
  47. {
  48. unsigned int i, num;
  49. char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
  50. char *ret = talloc_strdup(m, "");
  51. for (i = 0; i < num; i++)
  52. ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
  53. return ret;
  54. }
  55. static bool compile(const void *ctx,
  56. struct manifest *m,
  57. struct ccan_file *file,
  58. bool fail,
  59. bool link_with_module,
  60. bool keep, char **output)
  61. {
  62. file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
  63. if (!compile_and_link(ctx, file->fullname, ccan_dir,
  64. obj_list(m, link_with_module),
  65. fail ? "-DFAIL" : "",
  66. lib_list(m), file->compiled, output)) {
  67. talloc_free(file->compiled);
  68. return false;
  69. }
  70. return true;
  71. }
  72. static void do_compile_tests(struct manifest *m,
  73. bool keep,
  74. unsigned int *timeleft, struct score *score)
  75. {
  76. char *cmdout;
  77. struct ccan_file *i;
  78. struct list_head *list;
  79. bool errors = false, warnings = false;
  80. foreach_ptr(list, &m->compile_ok_tests, &m->run_tests, &m->api_tests) {
  81. list_for_each(list, i, list) {
  82. if (!compile(score, m, i, false, list == &m->api_tests,
  83. keep, &cmdout)) {
  84. score->error = "Failed to compile tests";
  85. score_file_error(score, i, 0, cmdout);
  86. errors = true;
  87. } else if (!streq(cmdout, "")) {
  88. score->error = "Test compiled with warnings";
  89. score_file_error(score, i, 0, cmdout);
  90. warnings = true;
  91. }
  92. }
  93. }
  94. /* The compile fail tests are a bit weird, handle them separately */
  95. if (errors)
  96. return;
  97. /* For historical reasons, "fail" often means "gives warnings" */
  98. list_for_each(&m->compile_fail_tests, i, list) {
  99. if (!compile(score, m, i, false, false, false, &cmdout)) {
  100. score->error = "Failed to compile without -DFAIL";
  101. score_file_error(score, i, 0, cmdout);
  102. return;
  103. }
  104. if (!streq(cmdout, "")) {
  105. score->error = "Compile with warnigns without -DFAIL";
  106. score_file_error(score, i, 0, cmdout);
  107. return;
  108. }
  109. if (compile(score, m, i, true, false, false, &cmdout)
  110. && streq(cmdout, "")) {
  111. score->error = "Compiled successfully with -DFAIL?";
  112. score_file_error(score, i, 0, NULL);
  113. return;
  114. }
  115. }
  116. score->pass = true;
  117. score->total = 2;
  118. score->score = 1 + !warnings;
  119. }
  120. struct ccanlint compile_tests = {
  121. .key = "compile-tests",
  122. .name = "Module tests compile",
  123. .check = do_compile_tests,
  124. .can_run = can_build,
  125. };
  126. REGISTER_TEST(compile_tests, &compile_test_helpers, &build_objs, NULL);