blob: cbfc45b94bbd076735847134a0976b6b30784d50 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7inherit python3native meson-routines qemu
8
9DEPENDS:append = " meson-native ninja-native"
10
11EXEWRAPPER_ENABLED:class-native = "False"
Patrick Williams92b42cb2022-09-03 06:53:57 -050012EXEWRAPPER_ENABLED ?= "${@bb.utils.contains('MACHINE_FEATURES', 'qemu-usermode', 'True', 'False', d)}"
13DEPENDS:append = "${@' qemu-native' if d.getVar('EXEWRAPPER_ENABLED') == 'True' else ''}"
14
15# As Meson enforces out-of-tree builds we can just use cleandirs
16B = "${WORKDIR}/build"
17do_configure[cleandirs] = "${B}"
18
19# Where the meson.build build configuration is
20MESON_SOURCEPATH = "${S}"
21
Andrew Geissler5082cc72023-09-11 08:41:39 -040022# The target to build in do_compile. If unset the default targets are built.
23MESON_TARGET ?= ""
24
Patrick Williams43a6b7c2025-02-13 15:13:32 -050025# Since 0.60.0 you can specify custom tags to install
26MESON_INSTALL_TAGS ?= ""
27
Patrick Williams92b42cb2022-09-03 06:53:57 -050028def noprefix(var, d):
29 return d.getVar(var).replace(d.getVar('prefix') + '/', '', 1)
30
31MESON_BUILDTYPE ?= "${@oe.utils.vartrue('DEBUG_BUILD', 'debug', 'plain', d)}"
32MESON_BUILDTYPE[vardeps] += "DEBUG_BUILD"
33MESONOPTS = " --prefix ${prefix} \
34 --buildtype ${MESON_BUILDTYPE} \
35 --bindir ${@noprefix('bindir', d)} \
36 --sbindir ${@noprefix('sbindir', d)} \
37 --datadir ${@noprefix('datadir', d)} \
38 --libdir ${@noprefix('libdir', d)} \
39 --libexecdir ${@noprefix('libexecdir', d)} \
40 --includedir ${@noprefix('includedir', d)} \
41 --mandir ${@noprefix('mandir', d)} \
42 --infodir ${@noprefix('infodir', d)} \
43 --sysconfdir ${sysconfdir} \
44 --localstatedir ${localstatedir} \
45 --sharedstatedir ${sharedstatedir} \
46 --wrap-mode nodownload \
47 --native-file ${WORKDIR}/meson.native"
48
49EXTRA_OEMESON:append = " ${PACKAGECONFIG_CONFARGS}"
50
51MESON_CROSS_FILE = ""
52MESON_CROSS_FILE:class-target = "--cross-file ${WORKDIR}/meson.cross"
53MESON_CROSS_FILE:class-nativesdk = "--cross-file ${WORKDIR}/meson.cross"
54
55# Needed to set up qemu wrapper below
56export STAGING_DIR_HOST
57
58def rust_tool(d, target_var):
59 rustc = d.getVar('RUSTC')
60 if not rustc:
61 return ""
62 cmd = [rustc, "--target", d.getVar(target_var)] + d.getVar("RUSTFLAGS").split()
63 return "rust = %s" % repr(cmd)
64
65addtask write_config before do_configure
Andrew Geissler5082cc72023-09-11 08:41:39 -040066do_write_config[vardeps] += "CC CXX AR NM STRIP READELF OBJCOPY CFLAGS CXXFLAGS LDFLAGS RUSTC RUSTFLAGS EXEWRAPPER_ENABLED"
Patrick Williams92b42cb2022-09-03 06:53:57 -050067do_write_config() {
68 # This needs to be Py to split the args into single-element lists
69 cat >${WORKDIR}/meson.cross <<EOF
70[binaries]
Patrick Williams39653562024-03-01 08:54:02 -060071c = ${@meson_array('CC', d)}
72cpp = ${@meson_array('CXX', d)}
Patrick Williams92b42cb2022-09-03 06:53:57 -050073cython = 'cython3'
74ar = ${@meson_array('AR', d)}
75nm = ${@meson_array('NM', d)}
76strip = ${@meson_array('STRIP', d)}
77readelf = ${@meson_array('READELF', d)}
78objcopy = ${@meson_array('OBJCOPY', d)}
Patrick Williams705982a2024-01-12 09:51:57 -060079pkg-config = 'pkg-config'
Andrew Geissler517393d2023-01-13 08:55:19 -060080llvm-config = 'llvm-config'
Patrick Williams92b42cb2022-09-03 06:53:57 -050081cups-config = 'cups-config'
82g-ir-scanner = '${STAGING_BINDIR}/g-ir-scanner-wrapper'
83g-ir-compiler = '${STAGING_BINDIR}/g-ir-compiler-wrapper'
Patrick Williamsac13d5f2023-11-24 18:59:46 -060084${@rust_tool(d, "RUST_HOST_SYS")}
Patrick Williams92b42cb2022-09-03 06:53:57 -050085${@"exe_wrapper = '${WORKDIR}/meson-qemuwrapper'" if d.getVar('EXEWRAPPER_ENABLED') == 'True' else ""}
86
87[built-in options]
88c_args = ${@meson_array('CFLAGS', d)}
89c_link_args = ${@meson_array('LDFLAGS', d)}
90cpp_args = ${@meson_array('CXXFLAGS', d)}
91cpp_link_args = ${@meson_array('LDFLAGS', d)}
92
93[properties]
94needs_exe_wrapper = true
Patrick Williams73bd93f2024-02-20 08:07:48 -060095sys_root = '${STAGING_DIR_HOST}'
Patrick Williams92b42cb2022-09-03 06:53:57 -050096
97[host_machine]
98system = '${@meson_operating_system('HOST_OS', d)}'
99cpu_family = '${@meson_cpu_family('HOST_ARCH', d)}'
100cpu = '${HOST_ARCH}'
101endian = '${@meson_endian('HOST', d)}'
102
103[target_machine]
104system = '${@meson_operating_system('TARGET_OS', d)}'
105cpu_family = '${@meson_cpu_family('TARGET_ARCH', d)}'
106cpu = '${TARGET_ARCH}'
107endian = '${@meson_endian('TARGET', d)}'
108EOF
109
110 cat >${WORKDIR}/meson.native <<EOF
111[binaries]
112c = ${@meson_array('BUILD_CC', d)}
113cpp = ${@meson_array('BUILD_CXX', d)}
114cython = 'cython3'
115ar = ${@meson_array('BUILD_AR', d)}
116nm = ${@meson_array('BUILD_NM', d)}
117strip = ${@meson_array('BUILD_STRIP', d)}
118readelf = ${@meson_array('BUILD_READELF', d)}
119objcopy = ${@meson_array('BUILD_OBJCOPY', d)}
Andrew Geissler8f840682023-07-21 09:09:43 -0500120llvm-config = '${STAGING_BINDIR_NATIVE}/llvm-config'
Patrick Williams705982a2024-01-12 09:51:57 -0600121pkg-config = 'pkg-config-native'
Patrick Williamsac13d5f2023-11-24 18:59:46 -0600122${@rust_tool(d, "RUST_BUILD_SYS")}
Patrick Williams92b42cb2022-09-03 06:53:57 -0500123
124[built-in options]
125c_args = ${@meson_array('BUILD_CFLAGS', d)}
126c_link_args = ${@meson_array('BUILD_LDFLAGS', d)}
127cpp_args = ${@meson_array('BUILD_CXXFLAGS', d)}
128cpp_link_args = ${@meson_array('BUILD_LDFLAGS', d)}
129EOF
130}
131
Patrick Williams84603582024-12-14 08:00:57 -0500132write_qemuwrapper() {
Patrick Williams92b42cb2022-09-03 06:53:57 -0500133 # Write out a qemu wrapper that will be used as exe_wrapper so that meson
134 # can run target helper binaries through that.
135 qemu_binary="${@qemu_wrapper_cmdline(d, '$STAGING_DIR_HOST', ['$STAGING_DIR_HOST/${libdir}','$STAGING_DIR_HOST/${base_libdir}'])}"
136 cat > ${WORKDIR}/meson-qemuwrapper << EOF
137#!/bin/sh
138# Use a modules directory which doesn't exist so we don't load random things
139# which may then get deleted (or their dependencies) and potentially segfault
140export GIO_MODULE_DIR=${STAGING_LIBDIR}/gio/modules-dummy
141
142# meson sets this wrongly (only to libs in build-dir), qemu_wrapper_cmdline() and GIR_EXTRA_LIBS_PATH take care of it properly
143unset LD_LIBRARY_PATH
144
145$qemu_binary "\$@"
146EOF
147 chmod +x ${WORKDIR}/meson-qemuwrapper
148}
149
Patrick Williams84603582024-12-14 08:00:57 -0500150do_write_config:append:class-target() {
151 write_qemuwrapper
152}
153
154do_write_config:append:class-nativesdk() {
155 write_qemuwrapper
156}
157
Patrick Williams92b42cb2022-09-03 06:53:57 -0500158# Tell externalsrc that changes to this file require a reconfigure
159CONFIGURE_FILES = "meson.build"
160
161meson_do_configure() {
162 # Meson requires this to be 'bfd, 'lld' or 'gold' from 0.53 onwards
163 # https://github.com/mesonbuild/meson/commit/ef9aeb188ea2bc7353e59916c18901cde90fa2b3
164 unset LD
165
Patrick Williams92b42cb2022-09-03 06:53:57 -0500166 bbnote Executing meson ${EXTRA_OEMESON}...
Andrew Geissler517393d2023-01-13 08:55:19 -0600167 if ! meson setup ${MESONOPTS} "${MESON_SOURCEPATH}" "${B}" ${MESON_CROSS_FILE} ${EXTRA_OEMESON}; then
Patrick Williams92b42cb2022-09-03 06:53:57 -0500168 bbfatal_log meson failed
169 fi
170}
171
172python meson_do_qa_configure() {
173 import re
174 warn_re = re.compile(r"^WARNING: Cross property (.+) is using default value (.+)$", re.MULTILINE)
175 with open(d.expand("${B}/meson-logs/meson-log.txt")) as logfile:
176 log = logfile.read()
177 for (prop, value) in warn_re.findall(log):
178 bb.warn("Meson cross property %s used without explicit assignment, defaulting to %s" % (prop, value))
179}
180do_configure[postfuncs] += "meson_do_qa_configure"
181
182do_compile[progress] = "outof:^\[(\d+)/(\d+)\]\s+"
183meson_do_compile() {
Andrew Geissler5082cc72023-09-11 08:41:39 -0400184 meson compile -v ${PARALLEL_MAKE} ${MESON_TARGET}
Patrick Williams92b42cb2022-09-03 06:53:57 -0500185}
186
187meson_do_install() {
Patrick Williams43a6b7c2025-02-13 15:13:32 -0500188 if [ "x${MESON_INSTALL_TAGS}" != "x" ] ; then
189 meson_install_tags="--tags ${MESON_INSTALL_TAGS}"
190 fi
191 meson install --destdir ${D} --no-rebuild $meson_install_tags
Patrick Williams92b42cb2022-09-03 06:53:57 -0500192}
193
194EXPORT_FUNCTIONS do_configure do_compile do_install