compile_tests.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <limits.h>
  10. #include <errno.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <err.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. static const char *can_build(struct manifest *m)
  17. {
  18. if (safe_mode)
  19. return "Safe mode enabled";
  20. return NULL;
  21. }
  22. static char *obj_list(const struct manifest *m, bool link_with_module)
  23. {
  24. char *list;
  25. struct ccan_file *i;
  26. /* We expect to be linked with tap, unless that's us. */
  27. if (!streq(m->basename, "tap"))
  28. list = talloc_asprintf(m, "%s/ccan/tap.o", ccan_dir);
  29. else
  30. list = talloc_strdup(m, "");
  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", i->compiled);
  34. /* Our own object files. */
  35. if (link_with_module)
  36. list_for_each(&m->c_files, i, list)
  37. list = talloc_asprintf_append(list, " %s", i->compiled);
  38. /* Other ccan modules. */
  39. list_for_each(&m->dep_dirs, i, list) {
  40. if (i->compiled)
  41. list = talloc_asprintf_append(list, " %s", i->compiled);
  42. }
  43. return list;
  44. }
  45. static char *lib_list(const struct manifest *m)
  46. {
  47. unsigned int i, num;
  48. char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
  49. char *ret = talloc_strdup(m, "");
  50. for (i = 0; i < num; i++)
  51. ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
  52. return ret;
  53. }
  54. static char *compile(const void *ctx,
  55. struct manifest *m,
  56. struct ccan_file *file,
  57. bool fail,
  58. bool link_with_module,
  59. bool keep)
  60. {
  61. char *errmsg;
  62. file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
  63. errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
  64. obj_list(m, link_with_module),
  65. fail ? "-DFAIL" : "",
  66. lib_list(m), file->compiled);
  67. if (errmsg) {
  68. talloc_free(file->compiled);
  69. return errmsg;
  70. }
  71. return NULL;
  72. }
  73. static void do_compile_tests(struct manifest *m,
  74. bool keep,
  75. unsigned int *timeleft, struct score *score)
  76. {
  77. char *cmdout;
  78. struct ccan_file *i;
  79. list_for_each(&m->compile_ok_tests, i, list) {
  80. cmdout = compile(score, m, i, false, false, keep);
  81. if (cmdout) {
  82. score->error = "Failed to compile tests";
  83. score_file_error(score, i, 0, cmdout);
  84. }
  85. }
  86. list_for_each(&m->run_tests, i, list) {
  87. cmdout = compile(score, m, i, false, false, keep);
  88. if (cmdout) {
  89. score->error = "Failed to compile tests";
  90. score_file_error(score, i, 0, cmdout);
  91. }
  92. }
  93. list_for_each(&m->api_tests, i, list) {
  94. cmdout = compile(score, m, i, false, true, keep);
  95. if (cmdout) {
  96. score->error = "Failed to compile tests";
  97. score_file_error(score, i, 0, cmdout);
  98. }
  99. }
  100. /* The compile fail tests are a bit weird, handle them separately */
  101. if (score->error)
  102. return;
  103. list_for_each(&m->compile_fail_tests, i, list) {
  104. cmdout = compile(score, m, i, false, false, false);
  105. if (cmdout) {
  106. score->error = "Failed to compile without -DFAIL";
  107. score_file_error(score, i, 0, cmdout);
  108. return;
  109. }
  110. cmdout = compile(score, m, i, true, false, false);
  111. if (!cmdout) {
  112. score->error = "Compiled successfully with -DFAIL?";
  113. score_file_error(score, i, 0, NULL);
  114. return;
  115. }
  116. }
  117. score->pass = true;
  118. score->score = score->total;
  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);