prereq.mk 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # SPDX-License-Identifier: GPL-2.0-only
  2. #
  3. # Copyright (C) 2006-2020 OpenWrt.org
  4. ifneq ($(__prereq_inc),1)
  5. __prereq_inc:=1
  6. prereq:
  7. if [ -f $(TMP_DIR)/.prereq-error ]; then \
  8. echo; \
  9. cat $(TMP_DIR)/.prereq-error; \
  10. rm -f $(TMP_DIR)/.prereq-error; \
  11. echo; \
  12. false; \
  13. fi
  14. .SILENT: prereq
  15. endif
  16. PREREQ_PREV=
  17. # 1: display name
  18. # 2: error message
  19. define Require
  20. export PREREQ_CHECK=1
  21. ifeq ($$(CHECK_$(1)),)
  22. prereq: prereq-$(1)
  23. prereq-$(1): $(if $(PREREQ_PREV),prereq-$(PREREQ_PREV)) FORCE
  24. printf "Checking '$(1)'... "
  25. if $(NO_TRACE_MAKE) -f $(firstword $(MAKEFILE_LIST)) check-$(1) PATH="$(ORIG_PATH)" >/dev/null 2>/dev/null; then \
  26. echo 'ok.'; \
  27. elif $(NO_TRACE_MAKE) -f $(firstword $(MAKEFILE_LIST)) check-$(1) PATH="$(ORIG_PATH)" >/dev/null 2>/dev/null; then \
  28. echo 'updated.'; \
  29. else \
  30. echo 'failed.'; \
  31. echo "$(PKG_NAME): $(strip $(2))" >> $(TMP_DIR)/.prereq-error; \
  32. fi
  33. check-$(1): FORCE
  34. $(call Require/$(1))
  35. CHECK_$(1):=1
  36. .SILENT: prereq-$(1) check-$(1)
  37. .NOTPARALLEL:
  38. endif
  39. PREREQ_PREV=$(1)
  40. endef
  41. define RequireCommand
  42. define Require/$(1)
  43. command -v $(1)
  44. endef
  45. $$(eval $$(call Require,$(1),$(2)))
  46. endef
  47. define RequireHeader
  48. define Require/$(1)
  49. [ -e "$(1)" ]
  50. endef
  51. $$(eval $$(call Require,$(1),$(2)))
  52. endef
  53. # 1: header to test
  54. # 2: failure message
  55. # 3: optional compile time test
  56. # 4: optional link library test (example -lncurses)
  57. define RequireCHeader
  58. define Require/$(1)
  59. echo 'int main(int argc, char **argv) { $(3); return 0; }' | gcc -include $(1) -x c -o $(TMP_DIR)/a.out - $(4)
  60. endef
  61. $$(eval $$(call Require,$(1),$(2)))
  62. endef
  63. define QuoteHostCommand
  64. '$(subst ','"'"',$(strip $(1)))'
  65. endef
  66. # 1: display name
  67. # 2: failure message
  68. # 3: test
  69. define TestHostCommand
  70. define Require/$(1)
  71. ($(3)) >/dev/null 2>/dev/null
  72. endef
  73. $$(eval $$(call Require,$(1),$(2)))
  74. endef
  75. # 1: canonical name
  76. # 2: failure message
  77. # 3+: candidates
  78. define SetupHostCommand
  79. define Require/$(1)
  80. mkdir -p "$(STAGING_DIR_HOST)/bin"; \
  81. for cmd in $(call QuoteHostCommand,$(3)) $(call QuoteHostCommand,$(4)) \
  82. $(call QuoteHostCommand,$(5)) $(call QuoteHostCommand,$(6)) \
  83. $(call QuoteHostCommand,$(7)) $(call QuoteHostCommand,$(8)) \
  84. $(call QuoteHostCommand,$(9)) $(call QuoteHostCommand,$(10)) \
  85. $(call QuoteHostCommand,$(11)) $(call QuoteHostCommand,$(12)); do \
  86. if [ -n "$$$$$$$$cmd" ]; then \
  87. bin="$$$$$$$$(command -v "$$$$$$$${cmd%% *}")"; \
  88. if [ -x "$$$$$$$$bin" ] && eval "$$$$$$$$cmd" >/dev/null 2>/dev/null; then \
  89. case "$$$$$$$$(ls -dl -- $(STAGING_DIR_HOST)/bin/$(strip $(1)))" in \
  90. "-"* | \
  91. *" -> $$$$$$$$bin"* | \
  92. *" -> "[!/]*) \
  93. [ -x "$(STAGING_DIR_HOST)/bin/$(strip $(1))" ] && exit 0 \
  94. ;; \
  95. esac; \
  96. ln -sf "$$$$$$$$bin" "$(STAGING_DIR_HOST)/bin/$(strip $(1))"; \
  97. exit 1; \
  98. fi; \
  99. fi; \
  100. done; \
  101. exit 1
  102. endef
  103. $$(eval $$(call Require,$(1),$(if $(2),$(2),Missing $(1) command)))
  104. endef