depends_build.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/foreach/foreach.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 = tal_fmt(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. tal_free(i->compiled[ctype]);
  40. i->compiled[ctype] = NULL;
  41. return tal_fmt(m,
  42. "Dependency %s did not build:\n%s",
  43. m->modname, output);
  44. }
  45. }
  46. return NULL;
  47. }
  48. char *build_submodule(struct manifest *m, const char *flags,
  49. enum compile_type ctype)
  50. {
  51. char *errstr;
  52. if (m->compiled[ctype])
  53. return NULL;
  54. if (!expect_obj_file(m))
  55. return NULL;
  56. if (verbose >= 2)
  57. printf(" Building dependency %s\n", m->dir);
  58. errstr = build_subdir_objs(m, flags, ctype);
  59. if (errstr)
  60. return errstr;
  61. m->compiled[ctype] = build_module(m, ctype, &errstr);
  62. if (!m->compiled[ctype])
  63. return errstr;
  64. return NULL;
  65. }
  66. static void check_depends_built(struct manifest *m,
  67. unsigned int *timeleft, struct score *score)
  68. {
  69. struct list_head *list;
  70. foreach_ptr(list, &m->deps, &m->test_deps) {
  71. struct manifest *i;
  72. list_for_each(list, i, list) {
  73. char *errstr;
  74. errstr = build_submodule(i, cflags, COMPILE_NORMAL);
  75. if (errstr) {
  76. score->error = tal_fmt(score,
  77. "Dependency %s"
  78. " did not"
  79. " build:\n%s",
  80. i->modname,
  81. errstr);
  82. return;
  83. }
  84. }
  85. }
  86. score->pass = true;
  87. score->score = score->total;
  88. }
  89. struct ccanlint depends_build = {
  90. .key = "depends_build",
  91. .name = "Module's CCAN dependencies can be found or built",
  92. .check = check_depends_built,
  93. .can_run = can_build,
  94. .needs = "depends_exist test_depends_exist"
  95. };
  96. REGISTER_TEST(depends_build);