compile_tests.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. list_for_each(&m->compile_ok_tests, i, list) {
  73. compile_tests.total_score++;
  74. cmdout = compile(list, m, i, false, false);
  75. if (cmdout) {
  76. res = talloc(list, struct compile_tests_result);
  77. res->filename = i->name;
  78. res->description = "failed to compile";
  79. res->output = talloc_steal(res, cmdout);
  80. list_add_tail(list, &res->list);
  81. }
  82. }
  83. list_for_each(&m->run_tests, i, list) {
  84. compile_tests.total_score++;
  85. cmdout = compile(m, m, i, false, false);
  86. if (cmdout) {
  87. res = talloc(list, struct compile_tests_result);
  88. res->filename = i->name;
  89. res->description = "failed to compile";
  90. res->output = talloc_steal(res, cmdout);
  91. list_add_tail(list, &res->list);
  92. }
  93. }
  94. list_for_each(&m->api_tests, i, list) {
  95. compile_tests.total_score++;
  96. cmdout = compile(m, m, i, false, true);
  97. if (cmdout) {
  98. res = talloc(list, struct compile_tests_result);
  99. res->filename = i->name;
  100. res->description = "failed to compile";
  101. res->output = talloc_steal(res, cmdout);
  102. list_add_tail(list, &res->list);
  103. }
  104. }
  105. list_for_each(&m->compile_fail_tests, i, list) {
  106. compile_tests.total_score++;
  107. cmdout = compile(list, m, i, true, false);
  108. if (cmdout) {
  109. res = talloc(list, struct compile_tests_result);
  110. res->filename = i->name;
  111. res->description = "failed to compile without -DFAIL";
  112. res->output = talloc_steal(res, cmdout);
  113. list_add_tail(list, &res->list);
  114. } else {
  115. cmdout = compile(list, m, i, false, false);
  116. if (!cmdout) {
  117. res = talloc(list, struct compile_tests_result);
  118. res->filename = i->name;
  119. res->description = "compiled successfully"
  120. " with -DFAIL";
  121. res->output = "";
  122. list_add_tail(list, &res->list);
  123. }
  124. }
  125. }
  126. if (list_empty(list)) {
  127. talloc_free(list);
  128. list = NULL;
  129. }
  130. return list;
  131. }
  132. static unsigned int score_compile_tests(struct manifest *m,
  133. void *check_result)
  134. {
  135. struct list_head *list = check_result;
  136. struct compile_tests_result *i;
  137. unsigned int score = compile_tests.total_score;
  138. list_for_each(list, i, list)
  139. score--;
  140. return score;
  141. }
  142. static const char *describe_compile_tests(struct manifest *m,
  143. void *check_result)
  144. {
  145. struct list_head *list = check_result;
  146. struct compile_tests_result *i;
  147. char *descrip = talloc_strdup(list, "Compilation tests failed:\n");
  148. list_for_each(list, i, list)
  149. descrip = talloc_asprintf_append(descrip, "%s %s\n%s",
  150. i->filename, i->description,
  151. i->output);
  152. return descrip;
  153. }
  154. struct ccanlint compile_tests = {
  155. .name = "Compile tests succeed",
  156. .score = score_compile_tests,
  157. .check = do_compile_tests,
  158. .describe = describe_compile_tests,
  159. .can_run = can_build,
  160. };
  161. REGISTER_TEST(compile_tests, &compile_test_helpers, NULL);