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