xref: /openbmc/openbmc/poky/meta/classes-recipe/meson.bbclass (revision c9537f57ab488bf5d90132917b0184e2527970a5)
1#
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"
12EXEWRAPPER_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
22# The target to build in do_compile. If unset the default targets are built.
23MESON_TARGET ?= ""
24
25# Since 0.60.0 you can specify custom tags to install
26MESON_INSTALL_TAGS ?= ""
27
28def 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
65def bindgen_args(d):
66    args = '${HOST_CC_ARCH}${TOOLCHAIN_OPTIONS} --target=${TARGET_SYS}'
67    # For SDK packages TOOLCHAIN_OPTIONS don't contain full sysroot path
68    if bb.data.inherits_class("nativesdk", d):
69        args += ' --sysroot=${STAGING_DIR_HOST}${SDKPATHNATIVE}${prefix_nativesdk}'
70    items = d.expand(args).split()
71    return repr(items[0] if len(items) == 1 else items)
72
73addtask write_config before do_configure
74do_write_config[vardeps] += "CC CXX AR NM STRIP READELF OBJCOPY CFLAGS CXXFLAGS LDFLAGS RUSTC RUSTFLAGS EXEWRAPPER_ENABLED"
75do_write_config() {
76    # This needs to be Py to split the args into single-element lists
77    cat >${WORKDIR}/meson.cross <<EOF
78[binaries]
79c = ${@meson_array('CC', d)}
80cpp = ${@meson_array('CXX', d)}
81cython = 'cython3'
82ar = ${@meson_array('AR', d)}
83nm = ${@meson_array('NM', d)}
84strip = ${@meson_array('STRIP', d)}
85readelf = ${@meson_array('READELF', d)}
86objcopy = ${@meson_array('OBJCOPY', d)}
87pkg-config = 'pkg-config'
88llvm-config = 'llvm-config'
89cups-config = 'cups-config'
90g-ir-scanner = '${STAGING_BINDIR}/g-ir-scanner-wrapper'
91g-ir-compiler = '${STAGING_BINDIR}/g-ir-compiler-wrapper'
92${@rust_tool(d, "RUST_HOST_SYS")}
93${@"exe_wrapper = '${WORKDIR}/meson-qemuwrapper'" if d.getVar('EXEWRAPPER_ENABLED') == 'True' else ""}
94
95[built-in options]
96c_args = ${@meson_array('CFLAGS', d)}
97c_link_args = ${@meson_array('LDFLAGS', d)}
98cpp_args = ${@meson_array('CXXFLAGS', d)}
99cpp_link_args = ${@meson_array('LDFLAGS', d)}
100
101[properties]
102needs_exe_wrapper = true
103sys_root = '${STAGING_DIR_HOST}'
104bindgen_clang_arguments = ${@bindgen_args(d)}
105
106[host_machine]
107system = '${@meson_operating_system('HOST_OS', d)}'
108cpu_family = '${@meson_cpu_family('HOST_ARCH', d)}'
109cpu = '${HOST_ARCH}'
110endian = '${@meson_endian('HOST', d)}'
111
112[target_machine]
113system = '${@meson_operating_system('TARGET_OS', d)}'
114cpu_family = '${@meson_cpu_family('TARGET_ARCH', d)}'
115cpu = '${TARGET_ARCH}'
116endian = '${@meson_endian('TARGET', d)}'
117EOF
118
119    cat >${WORKDIR}/meson.native <<EOF
120[binaries]
121c = ${@meson_array('BUILD_CC', d)}
122cpp = ${@meson_array('BUILD_CXX', d)}
123cython = 'cython3'
124ar = ${@meson_array('BUILD_AR', d)}
125nm = ${@meson_array('BUILD_NM', d)}
126strip = ${@meson_array('BUILD_STRIP', d)}
127readelf = ${@meson_array('BUILD_READELF', d)}
128objcopy = ${@meson_array('BUILD_OBJCOPY', d)}
129llvm-config = '${STAGING_BINDIR_NATIVE}/llvm-config'
130pkg-config = 'pkg-config-native'
131${@rust_tool(d, "RUST_BUILD_SYS")}
132
133[built-in options]
134c_args = ${@meson_array('BUILD_CFLAGS', d)}
135c_link_args = ${@meson_array('BUILD_LDFLAGS', d)}
136cpp_args = ${@meson_array('BUILD_CXXFLAGS', d)}
137cpp_link_args = ${@meson_array('BUILD_LDFLAGS', d)}
138EOF
139}
140
141write_qemuwrapper() {
142    # Write out a qemu wrapper that will be used as exe_wrapper so that meson
143    # can run target helper binaries through that.
144    qemu_binary="${@qemu_wrapper_cmdline(d, '$STAGING_DIR_HOST', ['$STAGING_DIR_HOST/${libdir}','$STAGING_DIR_HOST/${base_libdir}'])}"
145    cat > ${WORKDIR}/meson-qemuwrapper << EOF
146#!/bin/sh
147# Use a modules directory which doesn't exist so we don't load random things
148# which may then get deleted (or their dependencies) and potentially segfault
149export GIO_MODULE_DIR=${STAGING_LIBDIR}/gio/modules-dummy
150
151# meson sets this wrongly (only to libs in build-dir), qemu_wrapper_cmdline() and GIR_EXTRA_LIBS_PATH take care of it properly
152unset LD_LIBRARY_PATH
153
154$qemu_binary "\$@"
155EOF
156    chmod +x ${WORKDIR}/meson-qemuwrapper
157}
158
159do_write_config:append:class-target() {
160    write_qemuwrapper
161}
162
163do_write_config:append:class-nativesdk() {
164    write_qemuwrapper
165}
166
167# Tell externalsrc that changes to this file require a reconfigure
168CONFIGURE_FILES = "meson.build"
169
170meson_do_configure() {
171    # Meson requires this to be 'bfd, 'lld' or 'gold' from 0.53 onwards
172    # https://github.com/mesonbuild/meson/commit/ef9aeb188ea2bc7353e59916c18901cde90fa2b3
173    unset LD
174
175    bbnote Executing meson ${EXTRA_OEMESON}...
176    if ! meson setup ${MESONOPTS} "${MESON_SOURCEPATH}" "${B}" ${MESON_CROSS_FILE} ${EXTRA_OEMESON}; then
177        bbfatal_log meson failed
178    fi
179}
180
181python meson_do_qa_configure() {
182    import re
183    warn_re = re.compile(r"^WARNING: Cross property (.+) is using default value (.+)$", re.MULTILINE)
184    with open(d.expand("${B}/meson-logs/meson-log.txt")) as logfile:
185        log = logfile.read()
186    for (prop, value) in warn_re.findall(log):
187        bb.warn("Meson cross property %s used without explicit assignment, defaulting to %s" % (prop, value))
188}
189do_configure[postfuncs] += "meson_do_qa_configure"
190
191do_compile[progress] = "outof:^\[(\d+)/(\d+)\]\s+"
192meson_do_compile() {
193    meson compile -v ${PARALLEL_MAKE} ${MESON_TARGET}
194}
195
196meson_do_install() {
197    if [ "x${MESON_INSTALL_TAGS}" != "x" ] ; then
198        meson_install_tags="--tags ${MESON_INSTALL_TAGS}"
199    fi
200    meson install --destdir ${D} --no-rebuild $meson_install_tags
201}
202
203EXPORT_FUNCTIONS do_configure do_compile do_install
204