2
0

prereq.mk 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. which $(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. define CleanupPython2
  52. define Require/python2-cleanup
  53. if [ -f "$(STAGING_DIR_HOST)/bin/python" ] && \
  54. $(STAGING_DIR_HOST)/bin/python -V 2>&1 | \
  55. grep -q 'Python 2'; then \
  56. rm $(STAGING_DIR_HOST)/bin/python; \
  57. fi
  58. endef
  59. $$(eval $$(call Require,python2-cleanup))
  60. endef
  61. define QuoteHostCommand
  62. '$(subst ','"'"',$(strip $(1)))'
  63. endef
  64. # 1: display name
  65. # 2: failure message
  66. # 3: test
  67. define TestHostCommand
  68. define Require/$(1)
  69. ($(3)) >/dev/null 2>/dev/null
  70. endef
  71. $$(eval $$(call Require,$(1),$(2)))
  72. endef
  73. # 1: canonical name
  74. # 2: failure message
  75. # 3+: candidates
  76. define SetupHostCommand
  77. define Require/$(1)
  78. [ -f "$(STAGING_DIR_HOST)/bin/$(strip $(1))" ] && exit 0; \
  79. for cmd in $(call QuoteHostCommand,$(3)) $(call QuoteHostCommand,$(4)) \
  80. $(call QuoteHostCommand,$(5)) $(call QuoteHostCommand,$(6)) \
  81. $(call QuoteHostCommand,$(7)) $(call QuoteHostCommand,$(8)) \
  82. $(call QuoteHostCommand,$(9)) $(call QuoteHostCommand,$(10)) \
  83. $(call QuoteHostCommand,$(11)) $(call QuoteHostCommand,$(12)); do \
  84. if [ -n "$$$$$$$$cmd" ]; then \
  85. bin="$$$$$$$$(PATH="$(subst $(space),:,$(filter-out $(STAGING_DIR_HOST)/%,$(subst :,$(space),$(PATH))))" \
  86. which "$$$$$$$${cmd%% *}")"; \
  87. if [ -x "$$$$$$$$bin" ] && eval "$$$$$$$$cmd" >/dev/null 2>/dev/null; then \
  88. mkdir -p "$(STAGING_DIR_HOST)/bin"; \
  89. ln -sf "$$$$$$$$bin" "$(STAGING_DIR_HOST)/bin/$(strip $(1))"; \
  90. exit 0; \
  91. fi; \
  92. fi; \
  93. done; \
  94. exit 1
  95. endef
  96. $$(eval $$(call Require,$(1),$(if $(2),$(2),Missing $(1) command)))
  97. endef