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
90        -e "s%@TARGET_CC@%$(TARGET_CC)%g" \
91        -e "s%@TARGET_CXX@%$(PKG_MESON_TARGET_CXX)%g" \
92        -e "s%@TARGET_AR@%$(TARGET_AR)%g" \
93        -e "s%@TARGET_FC@%$(PKG_MESON_TARGET_FC)%g" \
94        -e "s%@TARGET_STRIP@%$(TARGET_STRIP)%g" \
95        -e "s%@TARGET_ARCH@%$(PKG_MESON_TARGET_CPU_FAMILY)%g" \
96        -e "s%@TARGET_CPU@%$(GCC_TARGET_CPU)%g" \
97        -e "s%@TARGET_ENDIAN@%$(call qstrip,$(call LOWERCASE,$(BR2_ENDIAN)))%g" \
98        -e "s%@TARGET_FCFLAGS@%$(call make-sq-comma-list,$($(strip $(4))))%g" \
99        -e "s%@TARGET_CFLAGS@%$(call make-sq-comma-list,$($(strip $(1))))%g" \
100        -e "s%@TARGET_LDFLAGS@%$(call make-sq-comma-list,$($(strip $(3))))%g" \
101        -e "s%@TARGET_CXXFLAGS@%$(call make-sq-comma-list,$($(strip $(2))))%g" \
102        -e "s%@BR2_CMAKE@%$(BR2_CMAKE)%g" \
103        -e "s%@PKGCONF_HOST_BINARY@%$(HOST_DIR)/bin/pkgconf%g" \
104        -e "s%@HOST_DIR@%$(HOST_DIR)%g" \
105        -e "s%@STAGING_DIR@%$(STAGING_DIR)%g" \
106        -e "s%@STATIC@%$(if $(BR2_STATIC_LIBS),true,false)%g" \
107        $(TOPDIR)/support/misc/cross-compilation.conf.in
108endef
109
110################################################################################
111# inner-meson-package -- defines how the configuration, compilation and
112# installation of a Meson package should be done, implements a few hooks to
113# tune the build process and calls the generic package infrastructure to
114# generate the necessary make targets
115#
116#  argument 1 is the lowercase package name
117#  argument 2 is the uppercase package name, including a HOST_ prefix
118#             for host packages
119#  argument 3 is the uppercase package name, without the HOST_ prefix
120#             for host packages
121#  argument 4 is the type (target or host)
122################################################################################
123
124define inner-meson-package
125
126#
127# Configure step. Only define it if not already defined by the package
128# .mk file. And take care of the differences between host and target
129# packages.
130#
131ifndef $(2)_CONFIGURE_CMDS
132ifeq ($(4),target)
133
134$(2)_CFLAGS ?= $$(TARGET_CFLAGS)
135$(2)_LDFLAGS ?= $$(TARGET_LDFLAGS)
136$(2)_CXXFLAGS ?= $$(TARGET_CXXFLAGS)
137
138# Configure package for target
139#
140#
141define $(2)_CONFIGURE_CMDS
142	rm -rf $$($$(PKG)_SRCDIR)/build
143	mkdir -p $$($$(PKG)_SRCDIR)/build
144	sed -e "/^\[binaries\]$$$$/s:$$$$:$$(foreach x,$$($(2)_MESON_EXTRA_BINARIES),\n$$(x)):" \
145	    -e "/^\[properties\]$$$$/s:$$$$:$$(foreach x,$$($(2)_MESON_EXTRA_PROPERTIES),\n$$(x)):" \
146	    $$(call PKG_MESON_CROSSCONFIG_SED,$(2)_CFLAGS,$(2)_CXXFLAGS,$(2)_LDFLAGS,$(2)_FCFLAGS) \
147	    > $$($$(PKG)_SRCDIR)/build/cross-compilation.conf
148	PATH=$$(BR_PATH) \
149	CC_FOR_BUILD="$$(HOSTCC)" \
150	CXX_FOR_BUILD="$$(HOSTCXX)" \
151	$$($$(PKG)_CONF_ENV) \
152	$$(MESON) setup \
153		--prefix=/usr \
154		--libdir=lib \
155		--default-library=$(if $(BR2_STATIC_LIBS),static,shared) \
156		--buildtype=$(if $(BR2_ENABLE_RUNTIME_DEBUG),debug,release) \
157		--cross-file=$$($$(PKG)_SRCDIR)/build/cross-compilation.conf \
158		-Db_pie=false \
159		-Db_staticpic=$(if $(BR2_m68k_cf),false,true) \
160		-Dstrip=false \
161		-Dbuild.pkg_config_path=$$(HOST_DIR)/lib/pkgconfig \
162		-Dbuild.cmake_prefix_path=$$(HOST_DIR)/lib/cmake \
163		$$($$(PKG)_CONF_OPTS) \
164		$$($$(PKG)_SRCDIR) $$($$(PKG)_SRCDIR)/build
165endef
166else
167
168# Configure package for host
169define $(2)_CONFIGURE_CMDS
170	rm -rf $$($$(PKG)_SRCDIR)/build
171	mkdir -p $$($$(PKG)_SRCDIR)/build
172	$$(HOST_CONFIGURE_OPTS) \
173	$$($$(PKG)_CONF_ENV) $$(MESON) setup \
174		--prefix=$$(HOST_DIR) \
175		--libdir=lib \
176		--sysconfdir=$$(HOST_DIR)/etc \
177		--localstatedir=$$(HOST_DIR)/var \
178		--default-library=shared \
179		--buildtype=release \
180		--wrap-mode=nodownload \
181		-Dstrip=true \
182		$$($$(PKG)_CONF_OPTS) \
183		$$($$(PKG)_SRCDIR) $$($$(PKG)_SRCDIR)/build
184endef
185endif
186endif
187
188$(2)_DEPENDENCIES += host-meson
189
190#
191# Build step. Only define it if not already defined by the package .mk
192# file.
193#
194ifndef $(2)_BUILD_CMDS
195ifeq ($(4),target)
196define $(2)_BUILD_CMDS
197	$$(TARGET_MAKE_ENV) $$($$(PKG)_NINJA_ENV) \
198		$$(NINJA) $$(NINJA_OPTS) $$($$(PKG)_NINJA_OPTS) -C $$($$(PKG)_SRCDIR)/build
199endef
200else
201define $(2)_BUILD_CMDS
202	$$(HOST_MAKE_ENV) $$($$(PKG)_NINJA_ENV) \
203		$$(NINJA) $$(NINJA_OPTS) $$($$(PKG)_NINJA_OPTS) -C $$($$(PKG)_SRCDIR)/build
204endef
205endif
206endif
207
208#
209# Host installation step. Only define it if not already defined by the
210# package .mk file.
211#
212ifndef $(2)_INSTALL_CMDS
213define $(2)_INSTALL_CMDS
214	$$(HOST_MAKE_ENV) $$($$(PKG)_NINJA_ENV) \
215		$$(NINJA) $$(NINJA_OPTS) -C $$($$(PKG)_SRCDIR)/build install
216endef
217endif
218
219#
220# Staging installation step. Only define it if not already defined by
221# the package .mk file.
222#
223ifndef $(2)_INSTALL_STAGING_CMDS
224define $(2)_INSTALL_STAGING_CMDS
225	$$(TARGET_MAKE_ENV) $$($$(PKG)_NINJA_ENV) DESTDIR=$$(STAGING_DIR) \
226		$$(NINJA) $$(NINJA_OPTS) -C $$($$(PKG)_SRCDIR)/build install
227endef
228endif
229
230#
231# Target installation step. Only define it if not already defined by
232# the package .mk file.
233#
234ifndef $(2)_INSTALL_TARGET_CMDS
235define $(2)_INSTALL_TARGET_CMDS
236	$$(TARGET_MAKE_ENV) $$($$(PKG)_NINJA_ENV) DESTDIR=$$(TARGET_DIR) \
237		$$(NINJA) $$(NINJA_OPTS) -C $$($$(PKG)_SRCDIR)/build install
238endef
239endif
240
241# Call the generic package infrastructure to generate the necessary
242# make targets
243$(call inner-generic-package,$(1),$(2),$(3),$(4))
244
245endef
246
247################################################################################
248# meson-package -- the target generator macro for Meson packages
249################################################################################
250
251meson-package = $(call inner-meson-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
252host-meson-package = $(call inner-meson-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)
253
254################################################################################
255# Generation of the Meson compile flags and cross-compilation file
256################################################################################
257
258# Generate a Meson cross-compilation.conf suitable for use with the
259# SDK; also install the file as a template for users to add their
260# own flags if they need to.
261define PKG_MESON_INSTALL_CROSS_CONF
262	mkdir -p $(HOST_DIR)/etc/meson
263	sed -e "s%@TARGET_CFLAGS@%$(call make-sq-comma-list,$(TARGET_CFLAGS))@PKG_TARGET_CFLAGS@%g" \
264	    -e "s%@TARGET_LDFLAGS@%$(call make-sq-comma-list,$(TARGET_LDFLAGS))@PKG_TARGET_LDFLAGS@%g" \
265	    -e "s%@TARGET_CXXFLAGS@%$(call make-sq-comma-list,$(TARGET_CXXFLAGS))@PKG_TARGET_CXXFLAGS@%g" \
266	    -e "s%@TARGET_FCFLAGS@%$(call make-sq-comma-list,$(TARGET_FCFLAGS))@PKG_TARGET_FCFLAGS@%g" \
267	    $(call PKG_MESON_CROSSCONFIG_SED) \
268	    > $(HOST_DIR)/etc/meson/cross-compilation.conf.in
269	sed $(call PKG_MESON_CROSSCONFIG_SED,TARGET_CFLAGS,TARGET_CXXFLAGS,TARGET_LDFLAGS,TARGET_FCFLAGS) \
270	    > $(HOST_DIR)/etc/meson/cross-compilation.conf
271endef
272
273TOOLCHAIN_TARGET_FINALIZE_HOOKS += PKG_MESON_INSTALL_CROSS_CONF
274