xref: /openbmc/openbmc/poky/meta/classes-recipe/cmake-qemu.bbclass (revision edff49234e31f23dc79f823473c9e286a21596c1)
1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6# Not all platforms are supported by Qemu. Using qemu-user therefore
7# involves a certain risk, which is also the reason why this feature
8# is not part of the main cmake class by default.
9#
10# One use case is the execution of cross-compiled unit tests with CTest
11# on the build machine. If CMAKE_EXEWRAPPER_ENABLED is configured,
12#   cmake --build --target test
13# works transparently with qemu-user. If the cmake project is developed
14# with this use case in mind this works very nicely also out of an IDE
15# configured to use cmake-native for cross compiling.
16
17inherit qemu cmake
18
19DEPENDS:append:class-target = "${@' qemu-native' if bb.utils.contains('MACHINE_FEATURES', 'qemu-usermode', True, False, d) else ''}"
20
21cmake_do_generate_toolchain_file:append:class-target() {
22    if ${@bb.utils.contains('MACHINE_FEATURES', 'qemu-usermode', 'true', 'false', d)}; then
23        # Write out a qemu wrapper that will be used as exe_wrapper so that cmake
24        # can run target helper binaries through that. This also allows to execute ctest.
25        qemu_binary="${@qemu_wrapper_cmdline(d, '${STAGING_DIR_HOST}', ['${STAGING_DIR_HOST}/${libdir}','${STAGING_DIR_HOST}/${base_libdir}'])}"
26        echo "#!/bin/sh" > "${WORKDIR}/cmake-qemuwrapper"
27        echo "$qemu_binary \"\$@\"" >> "${WORKDIR}/cmake-qemuwrapper"
28        chmod +x "${WORKDIR}/cmake-qemuwrapper"
29        echo "set( CMAKE_CROSSCOMPILING_EMULATOR ${WORKDIR}/cmake-qemuwrapper)" \
30          >> ${WORKDIR}/toolchain.cmake
31    fi
32}
33