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