depends_build.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "build.h"
  17. static const char *can_build(struct manifest *m)
  18. {
  19. if (safe_mode)
  20. return "Safe mode enabled";
  21. return NULL;
  22. }
  23. static bool expect_obj_file(struct manifest *m)
  24. {
  25. /* If it has C files, we expect an object file built from them. */
  26. return !list_empty(&m->c_files);
  27. }
  28. static char *build_subdir_objs(struct manifest *m)
  29. {
  30. struct ccan_file *i;
  31. list_for_each(&m->c_files, i, list) {
  32. char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
  33. char *output;
  34. i->compiled = maybe_temp_file(m, "", false, fullfile);
  35. if (!compile_object(m, fullfile, ccan_dir, compiler, cflags,
  36. i->compiled, &output)) {
  37. talloc_free(i->compiled);
  38. i->compiled = NULL;
  39. return talloc_asprintf(m,
  40. "Dependency %s"
  41. " did not build:\n%s",
  42. m->basename, output);
  43. }
  44. }
  45. return NULL;
  46. }
  47. char *build_submodule(struct manifest *m)
  48. {
  49. char *errstr;
  50. struct stat st;
  51. if (m->compiled)
  52. return NULL;
  53. if (!expect_obj_file(m))
  54. return NULL;
  55. m->compiled = talloc_asprintf(m, "%s.o", m->dir);
  56. if (stat(m->compiled, &st) == 0)
  57. return NULL;
  58. if (verbose >= 2)
  59. printf(" Building dependency %s\n", m->dir);
  60. errstr = build_subdir_objs(m);
  61. if (errstr)
  62. return errstr;
  63. m->compiled = build_module(m, false, &errstr);
  64. if (!m->compiled)
  65. return errstr;
  66. return NULL;
  67. }
  68. static void check_depends_built(struct manifest *m,
  69. bool keep,
  70. unsigned int *timeleft, struct score *score)
  71. {
  72. struct manifest *i;
  73. list_for_each(&m->deps, i, list) {
  74. char *errstr = build_submodule(i);
  75. if (errstr) {
  76. score->error = talloc_asprintf(score,
  77. "Dependency %s"
  78. " did not build:\n%s",
  79. i->basename, errstr);
  80. return;
  81. }
  82. }
  83. score->pass = true;
  84. score->score = score->total;
  85. }
  86. struct ccanlint depends_build = {
  87. .key = "depends_build",
  88. .name = "Module's CCAN dependencies can be found or built",
  89. .check = check_depends_built,
  90. .can_run = can_build,
  91. .needs = "depends_exist"
  92. };
  93. REGISTER_TEST(depends_build);