tests_helpers_compile.c 2.8 KB

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