depends_build.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/foreach/foreach.h>
  5. #include <ccan/str/str.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <limits.h>
  11. #include <errno.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <err.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include "build.h"
  18. static const char *can_build(struct manifest *m)
  19. {
  20. if (safe_mode)
  21. return "Safe mode enabled";
  22. return NULL;
  23. }
  24. static bool expect_obj_file(struct manifest *m)
  25. {
  26. /* If it has C files, we expect an object file built from them. */
  27. return !list_empty(&m->c_files);
  28. }
  29. static char *build_subdir_objs(struct manifest *m,
  30. const char *flags,
  31. enum compile_type ctype)
  32. {
  33. struct ccan_file *i;
  34. list_for_each(&m->c_files, i, list) {
  35. char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
  36. char *output;
  37. i->compiled[ctype] = temp_file(m, "", fullfile);
  38. if (!compile_object(m, fullfile, ccan_dir, compiler, flags,
  39. i->compiled[ctype], &output)) {
  40. talloc_free(i->compiled[ctype]);
  41. i->compiled[ctype] = NULL;
  42. return talloc_asprintf(m,
  43. "Dependency %s"
  44. " did not build:\n%s",
  45. m->basename, output);
  46. }
  47. }
  48. return NULL;
  49. }
  50. char *build_submodule(struct manifest *m, const char *flags,
  51. enum compile_type ctype)
  52. {
  53. char *errstr;
  54. if (m->compiled[ctype])
  55. return NULL;
  56. if (!expect_obj_file(m))
  57. return NULL;
  58. if (verbose >= 2)
  59. printf(" Building dependency %s\n", m->dir);
  60. errstr = build_subdir_objs(m, flags, ctype);
  61. if (errstr)
  62. return errstr;
  63. m->compiled[ctype] = build_module(m, ctype, &errstr);
  64. if (!m->compiled[ctype])
  65. return errstr;
  66. return NULL;
  67. }
  68. static void check_depends_built(struct manifest *m,
  69. unsigned int *timeleft, struct score *score)
  70. {
  71. struct list_head *list;
  72. foreach_ptr(list, &m->deps, &m->test_deps) {
  73. struct manifest *i;
  74. list_for_each(list, i, list) {
  75. char *errstr;
  76. errstr = build_submodule(i, cflags, COMPILE_NORMAL);
  77. if (errstr) {
  78. score->error = talloc_asprintf(score,
  79. "Dependency %s"
  80. " did not"
  81. " build:\n%s",
  82. i->basename,
  83. errstr);
  84. return;
  85. }
  86. }
  87. }
  88. score->pass = true;
  89. score->score = score->total;
  90. }
  91. struct ccanlint depends_build = {
  92. .key = "depends_build",
  93. .name = "Module's CCAN dependencies can be found or built",
  94. .check = check_depends_built,
  95. .can_run = can_build,
  96. .needs = "depends_exist test_depends_exist"
  97. };
  98. REGISTER_TEST(depends_build);