blob: 31675cf42d196a3a3a85da9032f1380f6741aa97 [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"
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
Andrew Geissler5082cc72023-09-11 08:41:39 -040023# The target to build in do_compile. If unset the default targets are built.
24MESON_TARGET ?= ""
25
Patrick Williams92b42cb2022-09-03 06:53:57 -050026def 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
Andrew Geissler5082cc72023-09-11 08:41:39 -040064do_write_config[vardeps] += "CC CXX AR NM STRIP READELF OBJCOPY CFLAGS CXXFLAGS LDFLAGS RUSTC RUSTFLAGS EXEWRAPPER_ENABLED"
Patrick Williams92b42cb2022-09-03 06:53:57 -050065do_write_config() {
66 # This needs to be Py to split the args into single-element lists
Patrick Williams73bd93f2024-02-20 08:07:48 -060067 # The generated compile_commands.json file can be used by external IDEs
68 # which do not know the $PATH set-up by bitbake. They need the absolute
69 # compiler paths.
Patrick Williams92b42cb2022-09-03 06:53:57 -050070 cat >${WORKDIR}/meson.cross <<EOF
71[binaries]
Patrick Williams73bd93f2024-02-20 08:07:48 -060072c = ${@meson_array_abspath('CC', d)}
73cpp = ${@meson_array_abspath('CXX', d)}
Patrick Williams92b42cb2022-09-03 06:53:57 -050074cython = 'cython3'
75ar = ${@meson_array('AR', d)}
76nm = ${@meson_array('NM', d)}
77strip = ${@meson_array('STRIP', d)}
78readelf = ${@meson_array('READELF', d)}
79objcopy = ${@meson_array('OBJCOPY', d)}
Patrick Williams705982a2024-01-12 09:51:57 -060080pkg-config = 'pkg-config'
Andrew Geissler517393d2023-01-13 08:55:19 -060081llvm-config = 'llvm-config'
Patrick Williams92b42cb2022-09-03 06:53:57 -050082cups-config = 'cups-config'
83g-ir-scanner = '${STAGING_BINDIR}/g-ir-scanner-wrapper'
84g-ir-compiler = '${STAGING_BINDIR}/g-ir-compiler-wrapper'
Patrick Williamsac13d5f2023-11-24 18:59:46 -060085${@rust_tool(d, "RUST_HOST_SYS")}
Patrick Williams92b42cb2022-09-03 06:53:57 -050086${@"exe_wrapper = '${WORKDIR}/meson-qemuwrapper'" if d.getVar('EXEWRAPPER_ENABLED') == 'True' else ""}
87
88[built-in options]
89c_args = ${@meson_array('CFLAGS', d)}
90c_link_args = ${@meson_array('LDFLAGS', d)}
91cpp_args = ${@meson_array('CXXFLAGS', d)}
92cpp_link_args = ${@meson_array('LDFLAGS', d)}
93
94[properties]
95needs_exe_wrapper = true
Patrick Williams73bd93f2024-02-20 08:07:48 -060096sys_root = '${STAGING_DIR_HOST}'
Patrick Williams92b42cb2022-09-03 06:53:57 -050097
98[host_machine]
99system = '${@meson_operating_system('HOST_OS', d)}'
100cpu_family = '${@meson_cpu_family('HOST_ARCH', d)}'
101cpu = '${HOST_ARCH}'
102endian = '${@meson_endian('HOST', d)}'
103
104[target_machine]
105system = '${@meson_operating_system('TARGET_OS', d)}'
106cpu_family = '${@meson_cpu_family('TARGET_ARCH', d)}'
107cpu = '${TARGET_ARCH}'
108endian = '${@meson_endian('TARGET', d)}'
109EOF
110
111 cat >${WORKDIR}/meson.native <<EOF
112[binaries]
113c = ${@meson_array('BUILD_CC', d)}
114cpp = ${@meson_array('BUILD_CXX', d)}
115cython = 'cython3'
116ar = ${@meson_array('BUILD_AR', d)}
117nm = ${@meson_array('BUILD_NM', d)}
118strip = ${@meson_array('BUILD_STRIP', d)}
119readelf = ${@meson_array('BUILD_READELF', d)}
120objcopy = ${@meson_array('BUILD_OBJCOPY', d)}
Andrew Geissler8f840682023-07-21 09:09:43 -0500121llvm-config = '${STAGING_BINDIR_NATIVE}/llvm-config'
Patrick Williams705982a2024-01-12 09:51:57 -0600122pkg-config = 'pkg-config-native'
Patrick Williamsac13d5f2023-11-24 18:59:46 -0600123${@rust_tool(d, "RUST_BUILD_SYS")}
Patrick Williams92b42cb2022-09-03 06:53:57 -0500124
125[built-in options]
126c_args = ${@meson_array('BUILD_CFLAGS', d)}
127c_link_args = ${@meson_array('BUILD_LDFLAGS', d)}
128cpp_args = ${@meson_array('BUILD_CXXFLAGS', d)}
129cpp_link_args = ${@meson_array('BUILD_LDFLAGS', d)}
130EOF
131}
132
133do_write_config:append:class-target() {
134 # Write out a qemu wrapper that will be used as exe_wrapper so that meson
135 # can run target helper binaries through that.
136 qemu_binary="${@qemu_wrapper_cmdline(d, '$STAGING_DIR_HOST', ['$STAGING_DIR_HOST/${libdir}','$STAGING_DIR_HOST/${base_libdir}'])}"
137 cat > ${WORKDIR}/meson-qemuwrapper << EOF
138#!/bin/sh
139# Use a modules directory which doesn't exist so we don't load random things
140# which may then get deleted (or their dependencies) and potentially segfault
141export GIO_MODULE_DIR=${STAGING_LIBDIR}/gio/modules-dummy
142
143# meson sets this wrongly (only to libs in build-dir), qemu_wrapper_cmdline() and GIR_EXTRA_LIBS_PATH take care of it properly
144unset LD_LIBRARY_PATH
145
146$qemu_binary "\$@"
147EOF
148 chmod +x ${WORKDIR}/meson-qemuwrapper
149}
150
151# Tell externalsrc that changes to this file require a reconfigure
152CONFIGURE_FILES = "meson.build"
153
154meson_do_configure() {
155 # Meson requires this to be 'bfd, 'lld' or 'gold' from 0.53 onwards
156 # https://github.com/mesonbuild/meson/commit/ef9aeb188ea2bc7353e59916c18901cde90fa2b3
157 unset LD
158
Patrick Williams92b42cb2022-09-03 06:53:57 -0500159 bbnote Executing meson ${EXTRA_OEMESON}...
Andrew Geissler517393d2023-01-13 08:55:19 -0600160 if ! meson setup ${MESONOPTS} "${MESON_SOURCEPATH}" "${B}" ${MESON_CROSS_FILE} ${EXTRA_OEMESON}; then
Patrick Williams92b42cb2022-09-03 06:53:57 -0500161 bbfatal_log meson failed
162 fi
163}
164
165python meson_do_qa_configure() {
166 import re
167 warn_re = re.compile(r"^WARNING: Cross property (.+) is using default value (.+)$", re.MULTILINE)
168 with open(d.expand("${B}/meson-logs/meson-log.txt")) as logfile:
169 log = logfile.read()
170 for (prop, value) in warn_re.findall(log):
171 bb.warn("Meson cross property %s used without explicit assignment, defaulting to %s" % (prop, value))
172}
173do_configure[postfuncs] += "meson_do_qa_configure"
174
175do_compile[progress] = "outof:^\[(\d+)/(\d+)\]\s+"
176meson_do_compile() {
Andrew Geissler5082cc72023-09-11 08:41:39 -0400177 meson compile -v ${PARALLEL_MAKE} ${MESON_TARGET}
Patrick Williams92b42cb2022-09-03 06:53:57 -0500178}
179
180meson_do_install() {
Andrew Geissler517393d2023-01-13 08:55:19 -0600181 meson install --destdir ${D} --no-rebuild
Patrick Williams92b42cb2022-09-03 06:53:57 -0500182}
183
184EXPORT_FUNCTIONS do_configure do_compile do_install