depends_build.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. const char *flags,
  30. enum compile_type ctype)
  31. {
  32. struct ccan_file *i;
  33. list_for_each(&m->c_files, i, list) {
  34. char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
  35. char *output;
  36. i->compiled[ctype] = temp_file(m, "", fullfile);
  37. if (!compile_object(m, fullfile, ccan_dir, compiler, flags,
  38. i->compiled[ctype], &output)) {
  39. talloc_free(i->compiled[ctype]);
  40. i->compiled[ctype] = NULL;
  41. return talloc_asprintf(m,
  42. "Dependency %s"
  43. " did not build:\n%s",
  44. m->basename, output);
  45. }
  46. }
  47. return NULL;
  48. }
  49. char *build_submodule(struct manifest *m, const char *flags,
  50. enum compile_type ctype)
  51. {
  52. char *errstr;
  53. if (m->compiled[ctype])
  54. return NULL;
  55. if (!expect_obj_file(m))
  56. return NULL;
  57. if (verbose >= 2)
  58. printf(" Building dependency %s\n", m->dir);
  59. errstr = build_subdir_objs(m, flags, ctype);
  60. if (errstr)
  61. return errstr;
  62. m->compiled[ctype] = build_module(m, ctype, &errstr);
  63. if (!m->compiled[ctype])
  64. return errstr;
  65. return NULL;
  66. }
  67. static void check_depends_built(struct manifest *m,
  68. unsigned int *timeleft, struct score *score)
  69. {
  70. struct manifest *i;
  71. list_for_each(&m->deps, i, list) {
  72. char *errstr = build_submodule(i, cflags, COMPILE_NORMAL);
  73. if (errstr) {
  74. score->error = talloc_asprintf(score,
  75. "Dependency %s"
  76. " did not build:\n%s",
  77. i->basename, errstr);
  78. return;
  79. }
  80. }
  81. score->pass = true;
  82. score->score = score->total;
  83. }
  84. struct ccanlint depends_build = {
  85. .key = "depends_build",
  86. .name = "Module's CCAN dependencies can be found or built",
  87. .check = check_depends_built,
  88. .can_run = can_build,
  89. .needs = "depends_exist"
  90. };
  91. REGISTER_TEST(depends_build);