prereq.mk 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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) >/dev/null 2>/dev/null; then \
  26. echo 'ok.'; \
  27. else \
  28. echo 'failed.'; \
  29. echo "$(PKG_NAME): $(strip $(2))" >> $(TMP_DIR)/.prereq-error; \
  30. fi
  31. check-$(1): FORCE
  32. $(call Require/$(1))
  33. CHECK_$(1):=1
  34. .SILENT: prereq-$(1) check-$(1)
  35. .NOTPARALLEL:
  36. endif
  37. PREREQ_PREV=$(1)
  38. endef
  39. define RequireCommand
  40. define Require/$(1)
  41. command -v $(1)
  42. endef
  43. $$(eval $$(call Require,$(1),$(2)))
  44. endef
  45. define RequireHeader
  46. define Require/$(1)
  47. [ -e "$(1)" ]
  48. endef
  49. $$(eval $$(call Require,$(1),$(2)))
  50. endef
  51. # 1: header to test
  52. # 2: failure message
  53. # 3: optional compile time test
  54. # 4: optional link library test (example -lncurses)
  55. define RequireCHeader
  56. define Require/$(1)
  57. echo 'int main(int argc, char **argv) { $(3); return 0; }' | gcc -include $(1) -x c -o $(TMP_DIR)/a.out - $(4)
  58. endef
  59. $$(eval $$(call Require,$(1),$(2)))
  60. endef
  61. define CleanupPython2
  62. define Require/python2-cleanup
  63. if [ -f "$(STAGING_DIR_HOST)/bin/python" ] && \
  64. $(STAGING_DIR_HOST)/bin/python -V 2>&1 | \
  65. grep -q 'Python 2'; then \
  66. rm $(STAGING_DIR_HOST)/bin/python; \
  67. fi
  68. endef
  69. $$(eval $$(call Require,python2-cleanup))
  70. endef
  71. define QuoteHostCommand
  72. '$(subst ','"'"',$(strip $(1)))'
  73. endef
  74. # 1: display name
  75. # 2: failure message
  76. # 3: test
  77. define TestHostCommand
  78. define Require/$(1)
  79. ($(3)) >/dev/null 2>/dev/null
  80. endef
  81. $$(eval $$(call Require,$(1),$(2)))
  82. endef
  83. # 1: canonical name
  84. # 2: failure message
  85. # 3+: candidates
  86. define SetupHostCommand
  87. define Require/$(1)
  88. [ -f "$(STAGING_DIR_HOST)/bin/$(strip $(1))" ] && exit 0; \
  89. for cmd in $(call QuoteHostCommand,$(3)) $(call QuoteHostCommand,$(4)) \
  90. $(call QuoteHostCommand,$(5)) $(call QuoteHostCommand,$(6)) \
  91. $(call QuoteHostCommand,$(7)) $(call QuoteHostCommand,$(8)) \
  92. $(call QuoteHostCommand,$(9)) $(call QuoteHostCommand,$(10)) \
  93. $(call QuoteHostCommand,$(11)) $(call QuoteHostCommand,$(12)); do \
  94. if [ -n "$$$$$$$$cmd" ]; then \
  95. bin="$$$$$$$$(PATH="$(subst $(space),:,$(filter-out $(STAGING_DIR_HOST)/%,$(subst :,$(space),$(PATH))))" \
  96. command -v "$$$$$$$${cmd%% *}")"; \
  97. if [ -x "$$$$$$$$bin" ] && eval "$$$$$$$$cmd" >/dev/null 2>/dev/null; then \
  98. mkdir -p "$(STAGING_DIR_HOST)/bin"; \
  99. ln -sf "$$$$$$$$bin" "$(STAGING_DIR_HOST)/bin/$(strip $(1))"; \
  100. exit 0; \
  101. fi; \
  102. fi; \
  103. done; \
  104. exit 1
  105. endef
  106. $$(eval $$(call Require,$(1),$(if $(2),$(2),Missing $(1) command)))
  107. endef