build: introduce $(MKHASH)
[openwrt/openwrt.git] / include / host-build.mk
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # Copyright (C) 2006-2020 OpenWrt.org
4
5 include $(INCLUDE_DIR)/download.mk
6
7 HOST_BUILD_DIR ?= $(BUILD_DIR_HOST)/$(PKG_NAME)$(if $(PKG_VERSION),-$(PKG_VERSION))
8 HOST_INSTALL_DIR ?= $(HOST_BUILD_DIR)/host-install
9 HOST_BUILD_PARALLEL ?=
10
11 HOST_MAKE_J:=$(if $(MAKE_JOBSERVER),$(MAKE_JOBSERVER) $(if $(filter 3.% 4.0 4.1,$(MAKE_VERSION)),-j))
12
13 ifeq ($(strip $(HOST_BUILD_PARALLEL)),0)
14 HOST_JOBS?=-j1
15 else
16 HOST_JOBS?=$(if $(HOST_BUILD_PARALLEL),$(HOST_MAKE_J),-j1)
17 endif
18
19 include $(INCLUDE_DIR)/unpack.mk
20 include $(INCLUDE_DIR)/depends.mk
21 include $(INCLUDE_DIR)/quilt.mk
22
23 BUILD_TYPES += host
24 HOST_STAMP_PREPARED=$(HOST_BUILD_DIR)/.prepared$(if $(HOST_QUILT)$(DUMP),,$(shell $(call find_md5,${CURDIR} $(PKG_FILE_DEPENDS),))_$(call confvar,CONFIG_AUTOREMOVE $(HOST_PREPARED_DEPENDS)))
25 HOST_STAMP_CONFIGURED:=$(HOST_BUILD_DIR)/.configured
26 HOST_STAMP_BUILT:=$(HOST_BUILD_DIR)/.built
27 HOST_BUILD_PREFIX?=$(if $(IS_PACKAGE_BUILD),$(STAGING_DIR_HOSTPKG),$(STAGING_DIR_HOST))
28 HOST_STAMP_INSTALLED:=$(HOST_BUILD_PREFIX)/stamp/.$(PKG_NAME)_installed
29
30 override MAKEFLAGS=
31
32 include $(INCLUDE_DIR)/autotools.mk
33
34 _host_target:=$(if $(HOST_QUILT),,.)
35
36 Host/Patch:=$(Host/Patch/Default)
37 ifneq ($(strip $(HOST_UNPACK)),)
38 define Host/Prepare/Default
39 $(HOST_UNPACK)
40 [ ! -d ./src/ ] || $(CP) ./src/* $(HOST_BUILD_DIR)
41 $(Host/Patch)
42 endef
43 endif
44
45 define Host/Prepare
46 $(call Host/Prepare/Default)
47 endef
48
49 HOST_CONFIGURE_VARS = \
50 CC="$(HOSTCC)" \
51 CFLAGS="$(HOST_CFLAGS)" \
52 CXX="$(HOSTCXX)" \
53 CPPFLAGS="$(HOST_CPPFLAGS)" \
54 LDFLAGS="$(HOST_LDFLAGS)" \
55 CONFIG_SHELL="$(SHELL)"
56
57 HOST_CONFIGURE_ARGS = \
58 --target=$(GNU_HOST_NAME) \
59 --host=$(GNU_HOST_NAME) \
60 --build=$(GNU_HOST_NAME) \
61 --program-prefix="" \
62 --program-suffix="" \
63 --prefix=$(HOST_BUILD_PREFIX) \
64 --exec-prefix=$(HOST_BUILD_PREFIX) \
65 --sysconfdir=$(HOST_BUILD_PREFIX)/etc \
66 --localstatedir=$(HOST_BUILD_PREFIX)/var \
67 --sbindir=$(HOST_BUILD_PREFIX)/bin
68
69 HOST_MAKE_VARS = \
70 CFLAGS="$(HOST_CFLAGS)" \
71 CPPFLAGS="$(HOST_CPPFLAGS)" \
72 CXXFLAGS="$(HOST_CXXFLAGS)" \
73 LDFLAGS="$(HOST_LDFLAGS)"
74
75 HOST_MAKE_FLAGS =
76
77 HOST_CONFIGURE_CMD = $(BASH) ./configure
78
79 ifeq ($(HOST_OS),Darwin)
80 HOST_CONFIG_SITE:=$(INCLUDE_DIR)/site/darwin
81 endif
82
83 define Host/Configure/Default
84 $(if $(HOST_CONFIGURE_PARALLEL),+)(cd $(HOST_BUILD_DIR)/$(3); \
85 if [ -x configure ]; then \
86 $(CP) $(SCRIPT_DIR)/config.{guess,sub} $(HOST_BUILD_DIR)/$(3)/ && \
87 $(HOST_CONFIGURE_VARS) \
88 $(2) \
89 $(HOST_CONFIGURE_CMD) \
90 $(HOST_CONFIGURE_ARGS) \
91 $(1); \
92 fi \
93 )
94 endef
95
96 define Host/Configure
97 $(call Host/Configure/Default)
98 endef
99
100 define Host/Compile/Default
101 +$(HOST_MAKE_VARS) \
102 $(MAKE) $(HOST_JOBS) -C $(HOST_BUILD_DIR) \
103 $(HOST_MAKE_FLAGS) \
104 $(1)
105 endef
106
107 define Host/Compile
108 $(call Host/Compile/Default)
109 endef
110
111 define Host/Install/Default
112 $(call Host/Compile/Default,install)
113 endef
114
115 define Host/Install
116 $(call Host/Install/Default,$(HOST_BUILD_PREFIX))
117 endef
118
119
120 ifneq ($(if $(HOST_QUILT),,$(CONFIG_AUTOREBUILD)),)
121 define HostHost/Autoclean
122 $(call rdep,${CURDIR} $(PKG_FILE_DEPENDS),$(HOST_STAMP_PREPARED))
123 $(if $(if $(Host/Compile),$(filter prepare,$(MAKECMDGOALS)),1),,$(call rdep,$(HOST_BUILD_DIR),$(HOST_STAMP_BUILT)))
124 endef
125 endif
126
127 define Host/Exports/Default
128 $(1) : export ACLOCAL_INCLUDE=$$(foreach p,$$(wildcard $$(STAGING_DIR_HOST)/share/aclocal $$(STAGING_DIR_HOST)/share/aclocal-* $(if $(IS_PACKAGE_BUILD),$$(STAGING_DIR)/host/share/aclocal $$(STAGING_DIR_HOSTPKG)/share/aclocal $$(STAGING_DIR)/host/share/aclocal-*)),-I $$(p))
129 $(1) : export STAGING_PREFIX=$$(HOST_BUILD_PREFIX)
130 $(1) : export PKG_CONFIG_PATH=$$(STAGING_DIR_HOST)/lib/pkgconfig:$$(HOST_BUILD_PREFIX)/lib/pkgconfig
131 $(1) : export PKG_CONFIG_LIBDIR=$$(HOST_BUILD_PREFIX)/lib/pkgconfig
132 $(if $(HOST_CONFIG_SITE),$(1) : export CONFIG_SITE:=$(HOST_CONFIG_SITE))
133 $(if $(IS_PACKAGE_BUILD),$(1) : export PATH=$$(TARGET_PATH_PKG))
134 endef
135 Host/Exports=$(Host/Exports/Default)
136
137 .NOTPARALLEL:
138
139 ifndef DUMP
140 define HostBuild/Core
141 $(if $(HOST_QUILT),$(Host/Quilt))
142 $(if $(DUMP),,$(call HostHost/Autoclean))
143
144 $(HOST_STAMP_PREPARED):
145 @-rm -rf $(HOST_BUILD_DIR)
146 @mkdir -p $(HOST_BUILD_DIR)
147 $(foreach hook,$(Hooks/HostPrepare/Pre),$(call $(hook))$(sep))
148 $(call Host/Prepare)
149 $(foreach hook,$(Hooks/HostPrepare/Post),$(call $(hook))$(sep))
150 touch $$@
151
152 $(call Host/Exports,$(HOST_STAMP_CONFIGURED))
153 $(HOST_STAMP_CONFIGURED): $(HOST_STAMP_PREPARED)
154 $(foreach hook,$(Hooks/HostConfigure/Pre),$(call $(hook))$(sep))
155 $(call Host/Configure)
156 $(foreach hook,$(Hooks/HostConfigure/Post),$(call $(hook))$(sep))
157 touch $$@
158
159 $(call Host/Exports,$(HOST_STAMP_BUILT))
160 $(HOST_STAMP_BUILT): $(HOST_STAMP_CONFIGURED)
161 $(foreach hook,$(Hooks/HostCompile/Pre),$(call $(hook))$(sep))
162 $(call Host/Compile)
163 $(foreach hook,$(Hooks/HostCompile/Post),$(call $(hook))$(sep))
164 touch $$@
165
166 $(call Host/Exports,$(HOST_STAMP_INSTALLED))
167 $(HOST_STAMP_INSTALLED): $(HOST_STAMP_BUILT) $(if $(FORCE_HOST_INSTALL),FORCE)
168 $(call Host/Install,$(HOST_BUILD_PREFIX))
169 $(foreach hook,$(Hooks/HostInstall/Post),$(call $(hook))$(sep))
170 mkdir -p $$(shell dirname $$@)
171 touch $(HOST_STAMP_BUILT)
172 touch $$@
173
174 $(call DefaultTargets,$(patsubst %,host-%,$(DEFAULT_SUBDIR_TARGETS)))
175 ifndef STAMP_BUILT
176 $(foreach t,$(DEFAULT_SUBDIR_TARGETS),
177 $(t): host-$(t)
178 .$(t): .host-$(t)
179 )
180 clean-build: host-clean-build
181 endif
182
183 $(DL_DIR)/$(FILE): FORCE
184
185 $(_host_target)host-prepare: $(HOST_STAMP_PREPARED)
186 $(_host_target)host-configure: $(HOST_STAMP_CONFIGURED)
187 $(_host_target)host-compile: $(HOST_STAMP_BUILT) $(HOST_STAMP_INSTALLED)
188 host-install: host-compile
189
190 host-clean-build: FORCE
191 $(call Host/Uninstall)
192 rm -rf $(HOST_BUILD_DIR) $(HOST_STAMP_BUILT)
193
194 host-clean: host-clean-build
195 $(call Host/Clean)
196 rm -rf $(HOST_STAMP_INSTALLED)
197
198 ifneq ($(CONFIG_AUTOREMOVE),)
199 host-compile:
200 $(FIND) $(HOST_BUILD_DIR) -mindepth 1 -maxdepth 1 -not '(' -type f -and -name '.*' -and -size 0 ')' | \
201 $(XARGS) rm -rf
202 endif
203 endef
204 endif
205
206 define HostBuild
207 $(HostBuild/Core)
208 $(if $(if $(PKG_HOST_ONLY),,$(STAMP_PREPARED)),,$(if $(strip $(PKG_SOURCE_URL)),$(call Download,default)))
209 endef