compile_test_helpers.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 int cleanup_testfile(char *testfile)
  23. {
  24. unlink(testfile);
  25. return 0;
  26. }
  27. static char *objname(const void *ctx, const char *cfile)
  28. {
  29. return talloc_asprintf(ctx, "%.*s.o ", strlen(cfile) - 2, cfile);
  30. }
  31. static char *compile(struct manifest *m, const char *cfile)
  32. {
  33. char *obj;
  34. obj = objname(m, cfile);
  35. talloc_set_destructor(obj, cleanup_testfile);
  36. return run_command(m, "cc " CFLAGS " -c -o %s %s", obj, cfile);
  37. }
  38. static void *do_compile_test_helpers(struct manifest *m)
  39. {
  40. char *cmdout = NULL;
  41. struct ccan_file *i;
  42. list_for_each(&m->other_test_c_files, i, list) {
  43. compile_tests.total_score++;
  44. cmdout = compile(m, i->name);
  45. if (cmdout)
  46. return talloc_asprintf(m,
  47. "Failed to compile helper C"
  48. " code file %s:\n%s",
  49. i->name, cmdout);
  50. }
  51. return NULL;
  52. }
  53. static const char *describe_compile_test_helpers(struct manifest *m,
  54. void *check_result)
  55. {
  56. return check_result;
  57. }
  58. struct ccanlint compile_test_helpers = {
  59. .name = "Compiling test helper files",
  60. .total_score = 1,
  61. .check = do_compile_test_helpers,
  62. .describe = describe_compile_test_helpers,
  63. .can_run = can_build,
  64. };
  65. REGISTER_TEST(compile_test_helpers, &depends_built);