build: skip download for host build with AUTOREMOVE if already compiled
[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 $(if $(CONFIG_AUTOREMOVE),find_md5_reproducible,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 CXXFLAGS="$(HOST_CXXFLAGS)" \
55 LDFLAGS="$(HOST_LDFLAGS)" \
56 CONFIG_SHELL="$(SHELL)"
57
58 HOST_CONFIGURE_ARGS = \
59 --target=$(GNU_HOST_NAME) \
60 --host=$(GNU_HOST_NAME) \
61 --build=$(GNU_HOST_NAME) \
62 --program-prefix="" \
63 --program-suffix="" \
64 --prefix=$(HOST_BUILD_PREFIX) \
65 --exec-prefix=$(HOST_BUILD_PREFIX) \
66 --sysconfdir=$(HOST_BUILD_PREFIX)/etc \
67 --localstatedir=$(HOST_BUILD_PREFIX)/var \
68 --sbindir=$(HOST_BUILD_PREFIX)/bin
69
70 HOST_MAKE_VARS = \
71 CFLAGS="$(HOST_CFLAGS)" \
72 CPPFLAGS="$(HOST_CPPFLAGS)" \
73 CXXFLAGS="$(HOST_CXXFLAGS)" \
74 LDFLAGS="$(HOST_LDFLAGS)"
75
76 HOST_MAKE_FLAGS =
77
78 HOST_CONFIGURE_CMD = $(BASH) ./configure
79
80 ifeq ($(HOST_OS),Darwin)
81 HOST_CONFIG_SITE:=$(INCLUDE_DIR)/site/darwin
82 endif
83
84 define Host/Configure/Default
85 $(if $(HOST_CONFIGURE_PARALLEL),+)(cd $(HOST_BUILD_DIR)/$(3); \
86 if [ -x configure ]; then \
87 $(CP) $(SCRIPT_DIR)/config.{guess,sub} $(HOST_BUILD_DIR)/$(3)/ && \
88 $(HOST_CONFIGURE_VARS) \
89 $(2) \
90 $(HOST_CONFIGURE_CMD) \
91 $(HOST_CONFIGURE_ARGS) \
92 $(1); \
93 fi \
94 )
95 endef
96
97 define Host/Configure
98 $(call Host/Configure/Default)
99 endef
100
101 define Host/Compile/Default
102 +$(HOST_MAKE_VARS) \
103 $(MAKE) $(HOST_JOBS) -C $(HOST_BUILD_DIR) \
104 $(HOST_MAKE_FLAGS) \
105 $(1)
106 endef
107
108 define Host/Compile
109 $(call Host/Compile/Default)
110 endef
111
112 define Host/Install/Default
113 $(call Host/Compile/Default,install)
114 endef
115
116 define Host/Install
117 $(call Host/Install/Default,$(HOST_BUILD_PREFIX))
118 endef
119
120
121 ifneq ($(if $(HOST_QUILT),,$(CONFIG_AUTOREBUILD)),)
122 define HostHost/Autoclean
123 $(call rdep,${CURDIR} $(PKG_FILE_DEPENDS),$(HOST_STAMP_PREPARED))
124 $(if $(if $(Host/Compile),$(filter prepare,$(MAKECMDGOALS)),1),,$(call rdep,$(HOST_BUILD_DIR),$(HOST_STAMP_BUILT)))
125 endef
126 endif
127
128 define Host/Exports/Default
129 $(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))
130 $(1) : export STAGING_PREFIX=$$(HOST_BUILD_PREFIX)
131 $(1) : export PKG_CONFIG_PATH=$$(STAGING_DIR_HOST)/lib/pkgconfig:$$(HOST_BUILD_PREFIX)/lib/pkgconfig
132 $(1) : export PKG_CONFIG_LIBDIR=$$(HOST_BUILD_PREFIX)/lib/pkgconfig
133 $(if $(HOST_CONFIG_SITE),$(1) : export CONFIG_SITE:=$(HOST_CONFIG_SITE))
134 $(if $(IS_PACKAGE_BUILD),$(1) : export PATH=$$(TARGET_PATH_PKG))
135 endef
136 Host/Exports=$(Host/Exports/Default)
137
138 .NOTPARALLEL:
139
140 ifndef DUMP
141 define HostBuild/Core
142 $(if $(HOST_QUILT),$(Host/Quilt))
143 $(if $(DUMP),,$(call HostHost/Autoclean))
144
145 $(HOST_STAMP_PREPARED):
146 @-rm -rf $(HOST_BUILD_DIR)
147 @mkdir -p $(HOST_BUILD_DIR)
148 $(foreach hook,$(Hooks/HostPrepare/Pre),$(call $(hook))$(sep))
149 $(call Host/Prepare)
150 $(foreach hook,$(Hooks/HostPrepare/Post),$(call $(hook))$(sep))
151 touch $$@
152
153 $(call Host/Exports,$(HOST_STAMP_CONFIGURED))
154 $(HOST_STAMP_CONFIGURED): $(HOST_STAMP_PREPARED)
155 $(foreach hook,$(Hooks/HostConfigure/Pre),$(call $(hook))$(sep))
156 $(call Host/Configure)
157 $(foreach hook,$(Hooks/HostConfigure/Post),$(call $(hook))$(sep))
158 touch $$@
159
160 $(call Host/Exports,$(HOST_STAMP_BUILT))
161 $(HOST_STAMP_BUILT): $(HOST_STAMP_CONFIGURED)
162 $(foreach hook,$(Hooks/HostCompile/Pre),$(call $(hook))$(sep))
163 $(call Host/Compile)
164 $(foreach hook,$(Hooks/HostCompile/Post),$(call $(hook))$(sep))
165 touch $$@
166
167 $(call Host/Exports,$(HOST_STAMP_INSTALLED))
168 $(HOST_STAMP_INSTALLED): $(HOST_STAMP_BUILT) $(if $(FORCE_HOST_INSTALL),FORCE)
169 $(call Host/Install,$(HOST_BUILD_PREFIX))
170 $(foreach hook,$(Hooks/HostInstall/Post),$(call $(hook))$(sep))
171 mkdir -p $$(shell dirname $$@)
172 touch $(HOST_STAMP_BUILT)
173 touch $$@
174
175 $(call DefaultTargets,$(patsubst %,host-%,$(DEFAULT_SUBDIR_TARGETS)))
176 ifndef STAMP_BUILT
177 $(foreach t,$(DEFAULT_SUBDIR_TARGETS),
178 $(t): host-$(t)
179 .$(t): .host-$(t)
180 )
181 clean-build: host-clean-build
182 endif
183
184 $(call check_download_integrity)
185
186 $(_host_target)host-prepare: $(HOST_STAMP_PREPARED)
187 $(_host_target)host-configure: $(HOST_STAMP_CONFIGURED)
188 $(_host_target)host-compile: $(HOST_STAMP_BUILT) $(HOST_STAMP_INSTALLED)
189 host-install: host-compile
190
191 host-clean-build: FORCE
192 $(call Host/Uninstall)
193 rm -rf $(HOST_BUILD_DIR) $(HOST_STAMP_BUILT)
194
195 host-clean: host-clean-build
196 $(call Host/Clean)
197 rm -rf $(HOST_STAMP_INSTALLED)
198
199 ifneq ($(CONFIG_AUTOREMOVE),)
200 host-compile:
201 $(FIND) $(HOST_BUILD_DIR) -mindepth 1 -maxdepth 1 -not '(' -type f -and -name '.*' -and -size 0 ')' -print0 | \
202 $(XARGS) -0 rm -rf
203 endif
204 endef
205 endif
206
207 define HostBuild
208 $(HostBuild/Core)
209 $(if $(if $(PKG_HOST_ONLY),,$(if $(and $(filter host-%,$(MAKECMDGOALS)),$(PKG_SKIP_DOWNLOAD)),,$(STAMP_PREPARED))),,
210 $(if $(and $(CONFIG_AUTOREMOVE), $(wildcard $(HOST_STAMP_INSTALLED), $(wildcard $(HOST_STAMP_BUILT)))),,
211 $(if $(strip $(PKG_SOURCE_URL)),$(call Download,default))
212 )
213 )
214 endef