tests_helpers_compile.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "reduce_features.h"
  17. static const char *can_run(struct manifest *m)
  18. {
  19. if (safe_mode)
  20. return "Safe mode enabled";
  21. return NULL;
  22. }
  23. static bool compile(struct manifest *m,
  24. struct ccan_file *cfile,
  25. const char *flags,
  26. enum compile_type ctype,
  27. char **output)
  28. {
  29. cfile->compiled[ctype] = temp_file(m, ".o", cfile->fullname);
  30. return compile_object(m, cfile->fullname, ccan_dir, compiler, flags,
  31. cfile->compiled[ctype], output);
  32. }
  33. static void compile_test_helpers(struct manifest *m,
  34. unsigned int *timeleft,
  35. struct score *score,
  36. const char *flags,
  37. enum compile_type ctype)
  38. {
  39. struct ccan_file *i;
  40. bool errors = false, warnings = false;
  41. if (list_empty(&m->other_test_c_files))
  42. score->total = 0;
  43. else
  44. score->total = 2;
  45. list_for_each(&m->other_test_c_files, i, list) {
  46. char *cmdout;
  47. if (!compile(m, i, flags, ctype, &cmdout)) {
  48. errors = true;
  49. score_file_error(score, i, 0, "Compile failed:\n%s",
  50. cmdout);
  51. } else if (!streq(cmdout, "")) {
  52. warnings = true;
  53. score_file_error(score, i, 0,
  54. "Compile gave warnings:\n%s", cmdout);
  55. }
  56. }
  57. if (!errors) {
  58. score->pass = true;
  59. score->score = score->total - warnings;
  60. }
  61. }
  62. static void do_compile_test_helpers(struct manifest *m,
  63. unsigned int *timeleft,
  64. struct score *score)
  65. {
  66. compile_test_helpers(m, timeleft, score, cflags, COMPILE_NORMAL);
  67. }
  68. struct ccanlint tests_helpers_compile = {
  69. .key = "tests_helpers_compile",
  70. .name = "Module test helper objects compile",
  71. .check = do_compile_test_helpers,
  72. .can_run = can_run,
  73. .needs = "depends_build tests_exist"
  74. };
  75. REGISTER_TEST(tests_helpers_compile);
  76. static const char *features_reduced(struct manifest *m)
  77. {
  78. if (features_were_reduced)
  79. return NULL;
  80. return "No features to turn off";
  81. }
  82. static void do_compile_test_helpers_without_features(struct manifest *m,
  83. unsigned int *timeleft,
  84. struct score *score)
  85. {
  86. char *flags;
  87. flags = talloc_asprintf(score, "%s %s", cflags,
  88. REDUCE_FEATURES_FLAGS);
  89. compile_test_helpers(m, timeleft, score, flags, COMPILE_NOFEAT);
  90. }
  91. struct ccanlint tests_helpers_compile_without_features = {
  92. .key = "tests_helpers_compile_without_features",
  93. .name = "Module tests helpers compile (without features)",
  94. .check = do_compile_test_helpers_without_features,
  95. .can_run = features_reduced,
  96. .needs = "depends_build_without_features tests_exist"
  97. };
  98. REGISTER_TEST(tests_helpers_compile_without_features);