89660d323d9b82858ffda8498255522ed84b7aaa
[openwrt/staging/ldir.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 elif $(NO_TRACE_MAKE) -f $(firstword $(MAKEFILE_LIST)) check-$(1) >/dev/null 2>/dev/null; then \
34 echo 'updated.'; \
35 else \
36 echo 'failed.'; \
37 echo "$(PKG_NAME): $(strip $(2))" >> $(TMP_DIR)/.prereq-error; \
38 fi
39
40 check-$(1): FORCE
41 $(call Require/$(1))
42 CHECK_$(1):=1
43
44 .SILENT: prereq-$(1) check-$(1)
45 .NOTPARALLEL:
46 endif
47
48 PREREQ_PREV=$(1)
49 endef
50
51
52 define RequireCommand
53 define Require/$(1)
54 command -v $(1)
55 endef
56
57 $$(eval $$(call Require,$(1),$(2)))
58 endef
59
60 define RequireHeader
61 define Require/$(1)
62 [ -e "$(1)" ]
63 endef
64
65 $$(eval $$(call Require,$(1),$(2)))
66 endef
67
68 # 1: header to test
69 # 2: failure message
70 # 3: optional compile time test
71 # 4: optional link library test (example -lncurses)
72 define RequireCHeader
73 define Require/$(1)
74 echo 'int main(int argc, char **argv) { $(3); return 0; }' | gcc -include $(1) -x c -o $(TMP_DIR)/a.out - $(4)
75 endef
76
77 $$(eval $$(call Require,$(1),$(2)))
78 endef
79
80 define CleanupPython2
81 define Require/python2-cleanup
82 if [ -f "$(STAGING_DIR_HOST)/bin/python" ] && \
83 $(STAGING_DIR_HOST)/bin/python -V 2>&1 | \
84 grep -q 'Python 2'; then \
85 rm $(STAGING_DIR_HOST)/bin/python; \
86 fi
87 endef
88
89 $$(eval $$(call Require,python2-cleanup))
90 endef
91
92 define QuoteHostCommand
93 '$(subst ','"'"',$(strip $(1)))'
94 endef
95
96 # 1: display name
97 # 2: failure message
98 # 3: test
99 define TestHostCommand
100 define Require/$(1)
101 ($(3)) >/dev/null 2>/dev/null
102 endef
103
104 $$(eval $$(call Require,$(1),$(2)))
105 endef
106
107 # 1: canonical name
108 # 2: failure message
109 # 3+: candidates
110 define SetupHostCommand
111 define Require/$(1)
112 mkdir -p "$(STAGING_DIR_HOST)/bin"; \
113 for cmd in $(call QuoteHostCommand,$(3)) $(call QuoteHostCommand,$(4)) \
114 $(call QuoteHostCommand,$(5)) $(call QuoteHostCommand,$(6)) \
115 $(call QuoteHostCommand,$(7)) $(call QuoteHostCommand,$(8)) \
116 $(call QuoteHostCommand,$(9)) $(call QuoteHostCommand,$(10)) \
117 $(call QuoteHostCommand,$(11)) $(call QuoteHostCommand,$(12)); do \
118 if [ -n "$$$$$$$$cmd" ]; then \
119 bin="$$$$$$$$(PATH="$(subst $(space),:,$(filter-out $(STAGING_DIR_HOST)/%,$(subst :,$(space),$(PATH))))" \
120 command -v "$$$$$$$${cmd%% *}")"; \
121 if [ -x "$$$$$$$$bin" ] && eval "$$$$$$$$cmd" >/dev/null 2>/dev/null; then \
122 case "$$$$$$$$(ls -dl -- $(STAGING_DIR_HOST)/bin/$(strip $(1)))" in \
123 *" -> $$$$$$$$bin"*) \
124 [ -x "$(STAGING_DIR_HOST)/bin/$(strip $(1))" ] && exit 0 \
125 ;; \
126 esac; \
127 ln -sf "$$$$$$$$bin" "$(STAGING_DIR_HOST)/bin/$(strip $(1))"; \
128 exit 1; \
129 fi; \
130 fi; \
131 done; \
132 exit 1
133 endef
134
135 $$(eval $$(call Require,$(1),$(if $(2),$(2),Missing $(1) command)))
136 endef