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