1#
2# Copyright (C) 2006-2015 OpenWrt.org
3#
4# This is free software, licensed under the GNU General Public License v2.
5# See /LICENSE for more information.
6#
7
8ifneq ($(__prereq_inc),1)
9__prereq_inc:=1
10
11prereq:
12	if [ -f $(TMP_DIR)/.prereq-error ]; then \
13		echo; \
14		cat $(TMP_DIR)/.prereq-error; \
15		rm -f $(TMP_DIR)/.prereq-error; \
16		echo; \
17		false; \
18	fi
19
20.SILENT: prereq
21endif
22
23PREREQ_PREV=
24
25# 1: display name
26# 2: error message
27define Require
28  export PREREQ_CHECK=1
29  ifeq ($$(CHECK_$(1)),)
30    prereq: prereq-$(1)
31
32    prereq-$(1): $(if $(PREREQ_PREV),prereq-$(PREREQ_PREV)) FORCE
33		printf "Checking '$(1)'... "
34		if $(NO_TRACE_MAKE) -f $(firstword $(MAKEFILE_LIST)) check-$(1) >/dev/null 2>/dev/null; then \
35			echo 'ok.'; \
36		else \
37			echo 'failed.'; \
38			echo "$(PKG_NAME): $(strip $(2))" >> $(TMP_DIR)/.prereq-error; \
39		fi
40
41    check-$(1): FORCE
42	  $(call Require/$(1))
43    CHECK_$(1):=1
44
45    .SILENT: prereq-$(1) check-$(1)
46    .NOTPARALLEL:
47  endif
48
49  PREREQ_PREV=$(1)
50endef
51
52
53define RequireCommand
54  define Require/$(1)
55    which $(1)
56  endef
57
58  $$(eval $$(call Require,$(1),$(2)))
59endef
60
61define RequireHeader
62  define Require/$(1)
63    [ -e "$(1)" ]
64  endef
65
66  $$(eval $$(call Require,$(1),$(2)))
67endef
68
69define QuoteHostCommand
70'$(subst ','"'"',$(strip $(1)))'
71endef
72
73# 1: display name
74# 2: failure message
75# 3: test
76define TestHostCommand
77  define Require/$(1)
78	($(3)) >/dev/null 2>/dev/null
79  endef
80
81  $$(eval $$(call Require,$(1),$(2)))
82endef
83
84# 1: canonical name
85# 2: failure message
86# 3+: candidates
87define SetupHostCommand
88  define Require/$(1)
89	[ -f "$(STAGING_DIR_HOST)/bin/$(strip $(1))" ] && exit 0; \
90	for cmd in $(call QuoteHostCommand,$(3)) $(call QuoteHostCommand,$(4)) \
91	           $(call QuoteHostCommand,$(5)) $(call QuoteHostCommand,$(6)) \
92	           $(call QuoteHostCommand,$(7)) $(call QuoteHostCommand,$(8)) \
93			   $(call QuoteHostCommand,$(9)); do \
94		if [ -n "$$$$$$$$cmd" ]; then \
95			bin="$$$$$$$$(PATH="$(subst $(space),:,$(filter-out $(STAGING_DIR_HOST)/%,$(subst :,$(space),$(PATH))))" \
96				which "$$$$$$$${cmd%% *}")"; \
97			if [ -x "$$$$$$$$bin" ] && eval "$$$$$$$$cmd" >/dev/null 2>/dev/null; then \
98				mkdir -p "$(STAGING_DIR_HOST)/bin"; \
99				ln -sf "$$$$$$$$bin" "$(STAGING_DIR_HOST)/bin/$(strip $(1))"; \
100				exit 0; \
101			fi; \
102		fi; \
103	done; \
104	exit 1
105  endef
106
107  $$(eval $$(call Require,$(1),$(if $(2),$(2),Missing $(1) command)))
108endef
109