blob: 1039c5540c6a01a17d61851a7aa2f334d8eb1df4 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001# SPDX-License-Identifier: GPL-2.0-only
2#
3# Copyright (C) 2006-2020 OpenWrt.org
4
5ifneq ($(__prereq_inc),1)
6__prereq_inc:=1
7
8prereq:
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
18endif
19
20PREREQ_PREV=
21
22# 1: display name
23# 2: error message
24define 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)
49endef
50
51
52define RequireCommand
53 define Require/$(1)
54 command -v $(1)
55 endef
56
57 $$(eval $$(call Require,$(1),$(2)))
58endef
59
60define RequireHeader
61 define Require/$(1)
62 [ -e "$(1)" ]
63 endef
64
65 $$(eval $$(call Require,$(1),$(2)))
66endef
67
68# 1: header to test
69# 2: failure message
70# 3: optional compile time test
71# 4: optional link library test (example -lncurses)
72define 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)))
78endef
79
80define QuoteHostCommand
81'$(subst ','"'"',$(strip $(1)))'
82endef
83
84# 1: display name
85# 2: failure message
86# 3: test
87define TestHostCommand
88 define Require/$(1)
89 ($(3)) >/dev/null 2>/dev/null
90 endef
91
92 $$(eval $$(call Require,$(1),$(2)))
93endef
94
95# 1: canonical name
96# 2: failure message
97# 3+: candidates
98define 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 "-"* | \
111 *" -> $$$$$$$$bin"* | \
112 *" -> "[!/]*) \
113 [ -x "$(STAGING_DIR_HOST)/bin/$(strip $(1))" ] && exit 0 \
114 ;; \
115 esac; \
116 ln -sf "$$$$$$$$bin" "$(STAGING_DIR_HOST)/bin/$(strip $(1))"; \
117 exit 1; \
118 fi; \
119 fi; \
120 done; \
121 exit 1
122 endef
123
124 $$(eval $$(call Require,$(1),$(if $(2),$(2),Missing $(1) command)))
125endef