xref: /OK3568_Linux_fs/buildroot/package/pkg-meson.mk (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1################################################################################
2# Meson package infrastructure
3#
4# This file implements an infrastructure that eases development of
5# package .mk files for Meson packages. It should be used for all
6# packages that use Meson as their build system.
7#
8# See the Buildroot documentation for details on the usage of this
9# infrastructure
10#
11# In terms of implementation, this Meson infrastructure requires
12# the .mk file to only specify metadata information about the
13# package: name, version, download URL, etc.
14#
15# We still allow the package .mk file to override what the different
16# steps are doing, if needed. For example, if <PKG>_BUILD_CMDS is
17# already defined, it is used as the list of commands to perform to
18# build the package, instead of the default Meson behaviour. The
19# package can also define some post operation hooks.
20#
21################################################################################
22
23#
24# Pass PYTHONNOUSERSITE environment variable when invoking Meson or Ninja, so
25# $(HOST_DIR)/bin/python3 will not look for Meson modules in
26# $HOME/.local/lib/python3.x/site-packages
27#
28MESON		= PYTHONNOUSERSITE=y $(HOST_DIR)/bin/meson
29NINJA		= PYTHONNOUSERSITE=y $(HOST_DIR)/bin/ninja
30NINJA_OPTS	= $(if $(VERBOSE),-v)
31
32# https://mesonbuild.com/Reference-tables.html#cpu-families
33ifeq ($(BR2_arcle)$(BR2_arceb),y)
34PKG_MESON_TARGET_CPU_FAMILY = arc
35else ifeq ($(BR2_arm)$(BR2_armeb),y)
36PKG_MESON_TARGET_CPU_FAMILY = arm
37else ifeq ($(BR2_aarch64)$(BR2_aarch64_be),y)
38PKG_MESON_TARGET_CPU_FAMILY = aarch64
39else ifeq ($(BR2_i386),y)
40PKG_MESON_TARGET_CPU_FAMILY = x86
41else ifeq ($(BR2_m68k),y)
42PKG_MESON_TARGET_CPU_FAMILY = m68k
43else ifeq ($(BR2_microblazeel)$(BR2_microblazebe),y)
44PKG_MESON_TARGET_CPU_FAMILY = microblaze
45else ifeq ($(BR2_mips)$(BR2_mipsel),y)
46PKG_MESON_TARGET_CPU_FAMILY = mips
47else ifeq ($(BR2_mips64)$(BR2_mips64el),y)
48PKG_MESON_TARGET_CPU_FAMILY = mips64
49else ifeq ($(BR2_powerpc),y)
50PKG_MESON_TARGET_CPU_FAMILY = ppc
51else ifeq ($(BR2_powerpc64)$(BR2_powerpc64le),y)
52PKG_MESON_TARGET_CPU_FAMILY = ppc64
53else ifeq ($(BR2_riscv)$(BR2_RISCV_32),yy)
54PKG_MESON_TARGET_CPU_FAMILY = riscv32
55else ifeq ($(BR2_riscv)$(BR2_RISCV_64),yy)
56PKG_MESON_TARGET_CPU_FAMILY = riscv64
57else ifeq ($(BR2_s390x),y)
58PKG_MESON_TARGET_CPU_FAMILY = s390x
59else ifeq ($(BR2_sh4)$(BR2_sh4eb)$(BR2_sh4a)$(BR2_sh4aeb),y)
60PKG_MESON_TARGET_CPU_FAMILY = sh4
61else ifeq ($(BR2_sparc),y)
62PKG_MESON_TARGET_CPU_FAMILY = sparc
63else ifeq ($(BR2_sparc64),y)
64PKG_MESON_TARGET_CPU_FAMILY = sparc64
65else ifeq ($(BR2_x86_64),y)
66PKG_MESON_TARGET_CPU_FAMILY = x86_64
67else
68PKG_MESON_TARGET_CPU_FAMILY = $(ARCH)
69endif
70
71# To avoid populating the cross-file with non existing compilers,
72# we tie them to /bin/false
73ifeq ($(BR2_INSTALL_LIBSTDCPP),y)
74PKG_MESON_TARGET_CXX = $(TARGET_CXX)
75else
76PKG_MESON_TARGET_CXX = /bin/false
77endif
78
79ifeq ($(BR2_TOOLCHAIN_HAS_FORTRAN),y)
80PKG_MESON_TARGET_FC = $(TARGET_FC)
81else
82PKG_MESON_TARGET_FC = /bin/false
83endif
84
85# Generates sed patterns for patching the cross-compilation.conf template,
86# since Flags might contain commas the arguments are passed indirectly by
87# variable name (stripped to deal with whitespaces).
88# Arguments are variable containing cflags, cxxflags, ldflags, fcflags
89define PKG_MESON_CROSSCONFIG_SED_COMMON
90        -e "s%@TARGET_FC@%$(PKG_MESON_TARGET_FC)%g" \
91        -e "s%@TARGET_ARCH@%$(PKG_MESON_TARGET_CPU_FAMILY)%g" \
92        -e "s%@TARGET_CPU@%$(GCC_TARGET_CPU)%g" \
93        -e "s%@TARGET_ENDIAN@%$(call qstrip,$(call LOWERCASE,$(BR2_ENDIAN)))%g" \
94        -e "s%@TARGET_FCFLAGS@%$(call make-sq-comma-list,$($(strip $(4))))%g" \
95        -e "s%@TARGET_CFLAGS@%$(call make-sq-comma-list,$($(strip $(1))))%g" \
96        -e "s%@TARGET_LDFLAGS@%$(call make-sq-comma-list,$($(strip $(3))))%g" \
97        -e "s%@TARGET_CXXFLAGS@%$(call make-sq-comma-list,$($(strip $(2))))%g" \
98        -e "s%@BR2_CMAKE@%$(BR2_CMAKE)%g" \
99        -e "s%@PKGCONF_HOST_BINARY@%$(HOST_DIR)/bin/pkgconf%g" \
100        -e "s%@HOST_DIR@%$(HOST_DIR)%g" \
101        -e "s%@STAGING_DIR@%$(STAGING_DIR)%g" \
102        -e "s%@STATIC@%$(if $(BR2_STATIC_LIBS),true,false)%g" \
103        $(TOPDIR)/support/misc/cross-compilation.conf.in
104endef
105
106define PKG_MESON_CROSSCONFIG_SED
107        -e "s%@TARGET_CC@%$(TARGET_CC)%g" \
108        -e "s%@TARGET_CXX@%$(PKG_MESON_TARGET_CXX)%g" \
109        -e "s%@TARGET_AR@%$(TARGET_AR)%g" \
110        -e "s%@TARGET_STRIP@%$(TARGET_STRIP)%g" \
111	$(call PKG_MESON_CROSSCONFIG_SED_COMMON,$(1),$(2),$(3),$(4))
112endef
113
114define PKG_MESON_CROSSCONFIG_SED_CUSTOM
115        -e "s%@TARGET_CC@%$($(1))%g" \
116        -e "s%@TARGET_CXX@%$($(2))%g" \
117        -e "s%@TARGET_AR@%$($(3))%g" \
118        -e "s%@TARGET_STRIP@%$($(4))%g" \
119	$(call PKG_MESON_CROSSCONFIG_SED_COMMON,$(5),$(6),$(7),$(8))
120endef
121
122################################################################################
123# inner-meson-package -- defines how the configuration, compilation and
124# installation of a Meson package should be done, implements a few hooks to
125# tune the build process and calls the generic package infrastructure to
126# generate the necessary make targets
127#
128#  argument 1 is the lowercase package name
129#  argument 2 is the uppercase package name, including a HOST_ prefix
130#             for host packages
131#  argument 3 is the uppercase package name, without the HOST_ prefix
132#             for host packages
133#  argument 4 is the type (target or host)
134################################################################################
135
136define inner-meson-package
137
138#
139# Configure step. Only define it if not already defined by the package
140# .mk file. And take care of the differences between host and target
141# packages.
142#
143ifndef $(2)_CONFIGURE_CMDS
144ifeq ($(4),target)
145
146$(2)_CFLAGS ?= $$(TARGET_CFLAGS)
147$(2)_LDFLAGS ?= $$(TARGET_LDFLAGS)
148$(2)_CXXFLAGS ?= $$(TARGET_CXXFLAGS)
149
150ifneq ($(BR2_TOOLCHAIN_PREFER_CLANG):$$($(2)_USE_CLANG),:)
151ifeq ($$($(2)_DISALLOW_CLANG),)
152$(2)_DEPENDENCIES += host-clang host-llvm
153
154$(2)_CC ?= $(HOST_DIR)/bin/clang
155$(2)_CXX ?= $(HOST_DIR)/bin/clang++
156$(2)_AR ?= $(HOST_DIR)/bin/llvm-ar
157
158ifeq ($(BR2_STRIP_strip),y)
159$(2)_STRIP ?= $(HOST_DIR)/bin/llvm-strip
160endif
161
162$(2)_CFLAGS += --sysroot=$(STAGING_DIR)
163$(2)_LDFLAGS += --sysroot=$(STAGING_DIR)
164$(2)_CXXFLAGS += --sysroot=$(STAGING_DIR)
165endif
166endif
167
168$(2)_CC ?= $$(TARGET_CC)
169$(2)_CXX ?= $$(TARGET_CXX)
170$(2)_AR ?= $$(TARGET_AR)
171$(2)_STRIP ?= $$(TARGET_STRIP)
172
173# Configure package for target
174#
175#
176define $(2)_CONFIGURE_CMDS
177	rm -rf $$($$(PKG)_SRCDIR)/build
178	mkdir -p $$($$(PKG)_SRCDIR)/build
179	sed -e "/^\[binaries\]$$$$/s:$$$$:$$(foreach x,$$($(2)_MESON_EXTRA_BINARIES),\n$$(x)):" \
180	    -e "/^\[properties\]$$$$/s:$$$$:$$(foreach x,$$($(2)_MESON_EXTRA_PROPERTIES),\n$$(x)):" \
181	    $$(call PKG_MESON_CROSSCONFIG_SED_CUSTOM,$(2)_CC,$(2)_CXX,$(2)_AR,$(2)_STRIP,$(2)_CFLAGS,$(2)_CXXFLAGS,$(2)_LDFLAGS,$(2)_FCFLAGS) \
182	    > $$($$(PKG)_SRCDIR)/build/cross-compilation.conf
183	PATH=$$(BR_PATH) \
184	CC_FOR_BUILD="$$(HOSTCC)" \
185	CXX_FOR_BUILD="$$(HOSTCXX)" \
186	$$($$(PKG)_CONF_ENV) \
187	$$(MESON) setup \
188		--prefix=/usr \
189		--libdir=lib \
190		--default-library=$(if $(BR2_STATIC_LIBS),static,$(if $(BR2_SHARED_STATIC_LIBS),both,shared)) \
191		--buildtype=$(if $(BR2_ENABLE_RUNTIME_DEBUG),debug,release) \
192		--cross-file=$$($$(PKG)_SRCDIR)/build/cross-compilation.conf \
193		-Db_pie=false \
194		-Dstrip=false \
195		-Dbuild.pkg_config_path=$$(HOST_DIR)/lib/pkgconfig \
196		-Dbuild.cmake_prefix_path=$$(HOST_DIR)/lib/cmake \
197		$$($$(PKG)_CONF_OPTS) \
198		$$($$(PKG)_SRCDIR) $$($$(PKG)_SRCDIR)/build
199endef
200else
201
202# Configure package for host
203define $(2)_CONFIGURE_CMDS
204	rm -rf $$($$(PKG)_SRCDIR)/build
205	mkdir -p $$($$(PKG)_SRCDIR)/build
206	$$(HOST_CONFIGURE_OPTS) \
207	$$($$(PKG)_CONF_ENV) $$(MESON) setup \
208		--prefix=$$(HOST_DIR) \
209		--libdir=lib \
210		--sysconfdir=$$(HOST_DIR)/etc \
211		--localstatedir=$$(HOST_DIR)/var \
212		--default-library=shared \
213		--buildtype=release \
214		--wrap-mode=nodownload \
215		-Dstrip=true \
216		$$($$(PKG)_CONF_OPTS) \
217		$$($$(PKG)_SRCDIR) $$($$(PKG)_SRCDIR)/build
218endef
219endif
220endif
221
222$(2)_DEPENDENCIES += host-meson
223
224#
225# Build step. Only define it if not already defined by the package .mk
226# file.
227#
228ifndef $(2)_BUILD_CMDS
229ifeq ($(4),target)
230define $(2)_BUILD_CMDS
231	$$(TARGET_MAKE_ENV) $$($$(PKG)_NINJA_ENV) \
232		$$(NINJA) $$(NINJA_OPTS) $$($$(PKG)_NINJA_OPTS) -C $$($$(PKG)_SRCDIR)/build
233endef
234else
235define $(2)_BUILD_CMDS
236	$$(HOST_MAKE_ENV) $$($$(PKG)_NINJA_ENV) \
237		$$(NINJA) $$(NINJA_OPTS) $$($$(PKG)_NINJA_OPTS) -C $$($$(PKG)_SRCDIR)/build
238endef
239endif
240endif
241
242#
243# Host installation step. Only define it if not already defined by the
244# package .mk file.
245#
246ifndef $(2)_INSTALL_CMDS
247define $(2)_INSTALL_CMDS
248	$$(HOST_MAKE_ENV) $$($$(PKG)_NINJA_ENV) \
249		$$(NINJA) $$(NINJA_OPTS) -C $$($$(PKG)_SRCDIR)/build install
250endef
251endif
252
253#
254# Staging installation step. Only define it if not already defined by
255# the package .mk file.
256#
257ifndef $(2)_INSTALL_STAGING_CMDS
258define $(2)_INSTALL_STAGING_CMDS
259	$$(TARGET_MAKE_ENV) $$($$(PKG)_NINJA_ENV) DESTDIR=$$(STAGING_DIR) \
260		$$(NINJA) $$(NINJA_OPTS) -C $$($$(PKG)_SRCDIR)/build install
261endef
262endif
263
264#
265# Target installation step. Only define it if not already defined by
266# the package .mk file.
267#
268ifndef $(2)_INSTALL_TARGET_CMDS
269define $(2)_INSTALL_TARGET_CMDS
270	$$(TARGET_MAKE_ENV) $$($$(PKG)_NINJA_ENV) DESTDIR=$$(TARGET_DIR) \
271		$$(NINJA) $$(NINJA_OPTS) -C $$($$(PKG)_SRCDIR)/build install
272endef
273endif
274
275# Call the generic package infrastructure to generate the necessary
276# make targets
277$(call inner-generic-package,$(1),$(2),$(3),$(4))
278
279endef
280
281################################################################################
282# meson-package -- the target generator macro for Meson packages
283################################################################################
284
285meson-package = $(call inner-meson-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
286host-meson-package = $(call inner-meson-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)
287
288################################################################################
289# Generation of the Meson compile flags and cross-compilation file
290################################################################################
291
292# Generate a Meson cross-compilation.conf suitable for use with the
293# SDK; also install the file as a template for users to add their
294# own flags if they need to.
295define PKG_MESON_INSTALL_CROSS_CONF
296	mkdir -p $(HOST_DIR)/etc/meson
297	sed -e "s%@TARGET_CFLAGS@%$(call make-sq-comma-list,$(TARGET_CFLAGS))@PKG_TARGET_CFLAGS@%g" \
298	    -e "s%@TARGET_LDFLAGS@%$(call make-sq-comma-list,$(TARGET_LDFLAGS))@PKG_TARGET_LDFLAGS@%g" \
299	    -e "s%@TARGET_CXXFLAGS@%$(call make-sq-comma-list,$(TARGET_CXXFLAGS))@PKG_TARGET_CXXFLAGS@%g" \
300	    -e "s%@TARGET_FCFLAGS@%$(call make-sq-comma-list,$(TARGET_FCFLAGS))@PKG_TARGET_FCFLAGS@%g" \
301	    $(call PKG_MESON_CROSSCONFIG_SED) \
302	    > $(HOST_DIR)/etc/meson/cross-compilation.conf.in
303	sed $(call PKG_MESON_CROSSCONFIG_SED,TARGET_CFLAGS,TARGET_CXXFLAGS,TARGET_LDFLAGS,TARGET_FCFLAGS) \
304	    > $(HOST_DIR)/etc/meson/cross-compilation.conf
305endef
306
307TOOLCHAIN_TARGET_FINALIZE_HOOKS += PKG_MESON_INSTALL_CROSS_CONF
308