compile_tests.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 = talloc_strdup(m, "../tap.o");
  25. struct ccan_file *i;
  26. /* Objects from any other C files. */
  27. list_for_each(&m->other_test_c_files, i, list)
  28. list = talloc_asprintf_append(list, " %.*s.o",
  29. strlen(i->name) - 2, i->name);
  30. if (link_with_module)
  31. list = talloc_asprintf_append(list, " ../%s.o", m->basename);
  32. return list;
  33. }
  34. static char *lib_list(const struct manifest *m)
  35. {
  36. unsigned int i, num;
  37. char **libs = get_libs(m, ".", ".", &num);
  38. char *ret = talloc_strdup(m, "");
  39. for (i = 0; i < num; i++)
  40. ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
  41. return ret;
  42. }
  43. static int cleanup_testfile(const char *testfile)
  44. {
  45. unlink(testfile);
  46. return 0;
  47. }
  48. static char *compile(const void *ctx,
  49. struct manifest *m, struct ccan_file *file, bool fail,
  50. bool link_with_module)
  51. {
  52. file->compiled = talloc_strdup(ctx, tempnam("/tmp", "ccanlint"));
  53. talloc_set_destructor(file->compiled, cleanup_testfile);
  54. return run_command(m, "cc " CFLAGS " %s -o %s %s %s %s",
  55. fail ? "-DFAIL" : "",
  56. file->compiled, file->name,
  57. obj_list(m, link_with_module), lib_list(m));
  58. }
  59. struct compile_tests_result {
  60. struct list_node list;
  61. const char *filename;
  62. const char *description;
  63. const char *output;
  64. };
  65. static void *do_compile_tests(struct manifest *m)
  66. {
  67. struct list_head *list = talloc(m, struct list_head);
  68. char *cmdout;
  69. struct ccan_file *i;
  70. struct compile_tests_result *res;
  71. list_head_init(list);
  72. compile_tests.total_score = 0;
  73. list_for_each(&m->compile_ok_tests, i, list) {
  74. compile_tests.total_score++;
  75. cmdout = compile(list, m, i, false, false);
  76. if (cmdout) {
  77. res = talloc(list, struct compile_tests_result);
  78. res->filename = i->name;
  79. res->description = "failed to compile";
  80. res->output = talloc_steal(res, cmdout);
  81. list_add_tail(list, &res->list);
  82. }
  83. }
  84. list_for_each(&m->run_tests, i, list) {
  85. compile_tests.total_score++;
  86. cmdout = compile(m, m, i, false, false);
  87. if (cmdout) {
  88. res = talloc(list, struct compile_tests_result);
  89. res->filename = i->name;
  90. res->description = "failed to compile";
  91. res->output = talloc_steal(res, cmdout);
  92. list_add_tail(list, &res->list);
  93. }
  94. }
  95. list_for_each(&m->api_tests, i, list) {
  96. compile_tests.total_score++;
  97. cmdout = compile(m, m, i, false, true);
  98. if (cmdout) {
  99. res = talloc(list, struct compile_tests_result);
  100. res->filename = i->name;
  101. res->description = "failed to compile";
  102. res->output = talloc_steal(res, cmdout);
  103. list_add_tail(list, &res->list);
  104. }
  105. }
  106. list_for_each(&m->compile_fail_tests, i, list) {
  107. compile_tests.total_score++;
  108. cmdout = compile(list, m, i, true, false);
  109. if (cmdout) {
  110. res = talloc(list, struct compile_tests_result);
  111. res->filename = i->name;
  112. res->description = "failed to compile without -DFAIL";
  113. res->output = talloc_steal(res, cmdout);
  114. list_add_tail(list, &res->list);
  115. } else {
  116. cmdout = compile(list, m, i, false, false);
  117. if (!cmdout) {
  118. res = talloc(list, struct compile_tests_result);
  119. res->filename = i->name;
  120. res->description = "compiled successfully"
  121. " with -DFAIL";
  122. res->output = "";
  123. list_add_tail(list, &res->list);
  124. }
  125. }
  126. }
  127. if (list_empty(list)) {
  128. talloc_free(list);
  129. list = NULL;
  130. }
  131. return list;
  132. }
  133. static unsigned int score_compile_tests(struct manifest *m,
  134. void *check_result)
  135. {
  136. struct list_head *list = check_result;
  137. struct compile_tests_result *i;
  138. unsigned int score = compile_tests.total_score;
  139. list_for_each(list, i, list)
  140. score--;
  141. return score;
  142. }
  143. static const char *describe_compile_tests(struct manifest *m,
  144. void *check_result)
  145. {
  146. struct list_head *list = check_result;
  147. struct compile_tests_result *i;
  148. char *descrip = talloc_strdup(list, "Compilation tests failed:\n");
  149. list_for_each(list, i, list)
  150. descrip = talloc_asprintf_append(descrip, "%s %s\n%s",
  151. i->filename, i->description,
  152. i->output);
  153. return descrip;
  154. }
  155. struct ccanlint compile_tests = {
  156. .name = "Compile tests succeed",
  157. .total_score = 1,
  158. .score = score_compile_tests,
  159. .check = do_compile_tests,
  160. .describe = describe_compile_tests,
  161. .can_run = can_build,
  162. };
  163. REGISTER_TEST(compile_tests, &compile_test_helpers, NULL);