compile_tests.c 3.5 KB

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