diff mbox series

[v8,2/8] cmake.bbclass: support qemu

Message ID 20231101110129.647878-3-adrian.freihofer@siemens.com
State New
Headers show
Series devtool ide plugin | expand

Commit Message

Adrian Freihofer Nov. 1, 2023, 11:01 a.m. UTC
Define the CMAKE_CROSSCOMPILING_EMULATOR variable similar to what the
meson bbclass does. This allows for example to execute cross compilied
unit tests on the build machine.

CMAKE_CROSSCOMPILING_EMULATOR is a semi colon separated list of
paramters which could directly handle the -L and the -E parameters.
Creating a wrapper script is not absolutely mandatory. But anyway lets
do it similar to what the meson.bbclass does and also disable pseudo.

Signed-off-by: Adrian Freihofer <adrian.freihofer@siemens.com>
---
 meta/classes-recipe/cmake.bbclass | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

Comments

Richard Purdie Nov. 6, 2023, 1:39 p.m. UTC | #1
On Wed, 2023-11-01 at 12:01 +0100, Adrian Freihofer wrote:
> Define the CMAKE_CROSSCOMPILING_EMULATOR variable similar to what the
> meson bbclass does. This allows for example to execute cross compilied
> unit tests on the build machine.
> 
> CMAKE_CROSSCOMPILING_EMULATOR is a semi colon separated list of
> paramters which could directly handle the -L and the -E parameters.
> Creating a wrapper script is not absolutely mandatory. But anyway lets
> do it similar to what the meson.bbclass does and also disable pseudo.
> 
> Signed-off-by: Adrian Freihofer <adrian.freihofer@siemens.com>
> ---
>  meta/classes-recipe/cmake.bbclass | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
> 
> diff --git a/meta/classes-recipe/cmake.bbclass b/meta/classes-recipe/cmake.bbclass
> index d978b889440..911c237a3fd 100644
> --- a/meta/classes-recipe/cmake.bbclass
> +++ b/meta/classes-recipe/cmake.bbclass
> @@ -4,6 +4,13 @@
>  # SPDX-License-Identifier: MIT
>  #
>  
> +inherit qemu
> +
> +EXEWRAPPER_ENABLED:class-native = "False"
> +EXEWRAPPER_ENABLED:class-nativesdk = "False"
> +EXEWRAPPER_ENABLED ?= "${@bb.utils.contains('MACHINE_FEATURES', 'qemu-usermode', 'True', 'False', d)}"
> +DEPENDS:append = "${@' qemu-native' if d.getVar('EXEWRAPPER_ENABLED') == 'True' else ''}"
> +
>  # Path to the CMake file to process.
>  OECMAKE_SOURCEPATH ??= "${S}"
>  
> @@ -156,6 +163,19 @@ EOF
>  
>  addtask generate_toolchain_file after do_patch before do_configure
>  
> +cmake_do_generate_toolchain_file:append:class-target() {
> +    if [ "${EXEWRAPPER_ENABLED}" = "True" ]; then
> +        # Write out a qemu wrapper that will be used as exe_wrapper so that camake
> +        # can run target helper binaries through that. This also allows to execute ctest.
> +        qemu_binary="${@qemu_wrapper_cmdline(d, '${STAGING_DIR_HOST}', ['${STAGING_DIR_HOST}/${libdir}','${STAGING_DIR_HOST}/${base_libdir}'])}"
> +        echo "#!/bin/sh" > "${WORKDIR}/cmake-qemuwrapper"
> +        echo "$qemu_binary \"\$@\"" >> "${WORKDIR}/cmake-qemuwrapper"
> +        chmod +x "${WORKDIR}/cmake-qemuwrapper"
> +        echo "set( CMAKE_CROSSCOMPILING_EMULATOR ${WORKDIR}/cmake-qemuwrapper)" \
> +          >> ${WORKDIR}/toolchain.cmake
> +    fi
> +}
> +
>  CONFIGURE_FILES = "CMakeLists.txt *.cmake"
>  
>  do_configure[cleandirs] = "${@d.getVar('B') if d.getVar('S') != d.getVar('B') else ''}"

We've tried very hard historically to avoid needing qemu usermode. It
has crept in, first because gobject introspection required it and more
recently, meson but that isn't a reason to encourage it.

I'm a bit reluctant to put this in the cmake class itself as it makes
it seem like it is part of the normal best practise and it really
isn't. I'd suggest a cmake-qemu class which people can use if they
really need this.

The reason is that some architectures/platorms don't work under qemu.
Even x86-64 didn't with certain more recent CPU optimisations until
surprisingly recently.

Cheers,

Richard
diff mbox series

Patch

diff --git a/meta/classes-recipe/cmake.bbclass b/meta/classes-recipe/cmake.bbclass
index d978b889440..911c237a3fd 100644
--- a/meta/classes-recipe/cmake.bbclass
+++ b/meta/classes-recipe/cmake.bbclass
@@ -4,6 +4,13 @@ 
 # SPDX-License-Identifier: MIT
 #
 
+inherit qemu
+
+EXEWRAPPER_ENABLED:class-native = "False"
+EXEWRAPPER_ENABLED:class-nativesdk = "False"
+EXEWRAPPER_ENABLED ?= "${@bb.utils.contains('MACHINE_FEATURES', 'qemu-usermode', 'True', 'False', d)}"
+DEPENDS:append = "${@' qemu-native' if d.getVar('EXEWRAPPER_ENABLED') == 'True' else ''}"
+
 # Path to the CMake file to process.
 OECMAKE_SOURCEPATH ??= "${S}"
 
@@ -156,6 +163,19 @@  EOF
 
 addtask generate_toolchain_file after do_patch before do_configure
 
+cmake_do_generate_toolchain_file:append:class-target() {
+    if [ "${EXEWRAPPER_ENABLED}" = "True" ]; then
+        # Write out a qemu wrapper that will be used as exe_wrapper so that camake
+        # can run target helper binaries through that. This also allows to execute ctest.
+        qemu_binary="${@qemu_wrapper_cmdline(d, '${STAGING_DIR_HOST}', ['${STAGING_DIR_HOST}/${libdir}','${STAGING_DIR_HOST}/${base_libdir}'])}"
+        echo "#!/bin/sh" > "${WORKDIR}/cmake-qemuwrapper"
+        echo "$qemu_binary \"\$@\"" >> "${WORKDIR}/cmake-qemuwrapper"
+        chmod +x "${WORKDIR}/cmake-qemuwrapper"
+        echo "set( CMAKE_CROSSCOMPILING_EMULATOR ${WORKDIR}/cmake-qemuwrapper)" \
+          >> ${WORKDIR}/toolchain.cmake
+    fi
+}
+
 CONFIGURE_FILES = "CMakeLists.txt *.cmake"
 
 do_configure[cleandirs] = "${@d.getVar('B') if d.getVar('S') != d.getVar('B') else ''}"