c72878ebb0eb0a0f0502393653b02594d4cf5bbb
[openwrt/openwrt.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) PATH="$(ORIG_PATH)" >/dev/null 2>/dev/null; then \
32 echo 'ok.'; \
33 elif $(NO_TRACE_MAKE) -f $(firstword $(MAKEFILE_LIST)) check-$(1) PATH="$(ORIG_PATH)" >/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 QuoteHostCommand
81 '$(subst ','"'"',$(strip $(1)))'
82 endef
83
84 # 1: display name
85 # 2: failure message
86 # 3: test
87 define TestHostCommand
88 define Require/$(1)
89 ($(3)) >/dev/null 2>/dev/null
90 endef
91
92 $$(eval $$(call Require,$(1),$(2)))
93 endef
94
95 # 1: canonical name
96 # 2: failure message
97 # 3+: candidates
98 define SetupHostCommand
99 define Require/$(1)
100 mkdir -p "$(STAGING_DIR_HOST)/bin"; \
101 for cmd in $(call QuoteHostCommand,$(3)) $(call QuoteHostCommand,$(4)) \
102 $(call QuoteHostCommand,$(5)) $(call QuoteHostCommand,$(6)) \
103 $(call QuoteHostCommand,$(7)) $(call QuoteHostCommand,$(8)) \
104 $(call QuoteHostCommand,$(9)) $(call QuoteHostCommand,$(10)) \
105 $(call QuoteHostCommand,$(11)) $(call QuoteHostCommand,$(12)); do \
106 if [ -n "$$$$$$$$cmd" ]; then \
107 bin="$$$$$$$$(command -v "$$$$$$$${cmd%% *}")"; \
108 if [ -x "$$$$$$$$bin" ] && eval "$$$$$$$$cmd" >/dev/null 2>/dev/null; then \
109 case "$$$$$$$$(ls -dl -- $(STAGING_DIR_HOST)/bin/$(strip $(1)))" in \
110 *" -> $$$$$$$$bin"*) \
111 [ -x "$(STAGING_DIR_HOST)/bin/$(strip $(1))" ] && exit 0 \
112 ;; \
113 esac; \
114 ln -sf "$$$$$$$$bin" "$(STAGING_DIR_HOST)/bin/$(strip $(1))"; \
115 exit 1; \
116 fi; \
117 fi; \
118 done; \
119 exit 1
120 endef
121
122 $$(eval $$(call Require,$(1),$(if $(2),$(2),Missing $(1) command)))
123 endef