Skip to contentSkip to conversation

cmake-qemu

PATCH Superseded Adrian Freihofer 4 participants 12 messages Nov 20, 2023 – Nov 23, 2023 1 message not in the archive

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover cmake-qemu

Not sure if there is a better way to enable the cmake-qemu.bbclass for all recipes inheriting also the cmake.bbclass globaly.

Adrian Freihofer (2):
cmake-qemu.bbclass: support qemu for cmake
cmake.bbclass: optionally support qemu

 meta/classes-recipe/cmake-qemu.bbclass | 28 ++++++++++++++++++++++++++
 meta/classes-recipe/cmake.bbclass      |  4 ++++
 2 files changed, 32 insertions(+)
 create mode 100644 meta/classes-recipe/cmake-qemu.bbclass
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/2 cmake-qemu.bbclass: support qemu for cmake

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 when using an SDK.

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.

Further information can be found in the camke documentation in the CMAKE_CROSSCOMPILING_EMULATOR section.

Keep the code optional, as the core does not need this function and does not intend to use it in the future.

[YOCTO #15214]

---
 meta/classes-recipe/cmake-qemu.bbclass | 28 ++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
 create mode 100644 meta/classes-recipe/cmake-qemu.bbclass
meta/classes-recipe/cmake-qemu.bbclass new file +28 -0
new file mode 100644
index 0000000000..76b748f340
@@ -0,0 +1,28 @@
+#
+# Copyright OpenEmbedded Contributors
+#
+# SPDX-License-Identifier: MIT
+#
+# Not all platforms are supported by Qemu. Using qemu-user therefore
+# involves a certain risk, which is also the reason why this feature
+# is not activated by default.
+
+inherit qemu
+
+CMAKE_EXEWRAPPER_ENABLED:class-native = "False"
+CMAKE_EXEWRAPPER_ENABLED:class-nativesdk = "False"
+CMAKE_EXEWRAPPER_ENABLED ?= "${@bb.utils.contains('MACHINE_FEATURES', 'qemu-usermode', 'True', 'False', d)}"
+DEPENDS:append = "${@' qemu-native' if d.getVar('CMAKE_EXEWRAPPER_ENABLED') == 'True' else ''}"
+
+cmake_do_generate_toolchain_file:append:class-target() {
+    if [ "${CMAKE_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
+}
Signature · 1 line
2.41.0
#2 lore raw

Re: [OE-core] [PATCH 1/2] cmake-qemu.bbclass: support qemu for cmake

Quoted 29 lines · Adrian Freihofer

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 when using an SDK.

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.

Further information can be found in the camke documentation in the CMAKE_CROSSCOMPILING_EMULATOR section.

Keep the code optional, as the core does not need this function and does not intend to use it in the future.

[YOCTO #15214]

---
 meta/classes-recipe/cmake-qemu.bbclass | 28 ++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
 create mode 100644 meta/classes-recipe/cmake-qemu.bbclass
meta/classes-recipe/cmake-qemu.bbclass new file +0 -0
new file mode 100644
index 0000000000..76b748f340

meta/classes-recipe/cmake-qemu.bbclass line 8

Quoted 8 lines
meta/classes-recipe/cmake-qemu.bbclass new file +8 -0
@@ -0,0 +1,28 @@
+#
+# Copyright OpenEmbedded Contributors
+#
+# SPDX-License-Identifier: MIT
+#
+# Not all platforms are supported by Qemu. Using qemu-user therefore
+# involves a certain risk, which is also the reason why this feature
+# is not activated by default.

"not part of the main cmake class by default"?

meta/classes-recipe/cmake-qemu.bbclass line 15

Quoted 7 lines
meta/classes-recipe/cmake-qemu.bbclass new file +7 -0
+
+inherit qemu
+
+CMAKE_EXEWRAPPER_ENABLED:class-native = "False"
+CMAKE_EXEWRAPPER_ENABLED:class-nativesdk = "False"
+CMAKE_EXEWRAPPER_ENABLED ?= "${@bb.utils.contains('MACHINE_FEATURES', 'qemu-usermode', 'True', 'False', d)}"
+DEPENDS:append = "${@' qemu-native' if d.getVar('CMAKE_EXEWRAPPER_ENABLED') == 'True' else ''}"

I suspect making that a DEPENDS:append:class-target might help bitbake with parsing things a bit.

I'm also a bit torn on CMAKE_EXEWRAPPER_ENABLED and whether we need/want the variable or just switch on qemu-usermode. I'm guessing you worked the code this way so you could conditionally bypass it with different defaults.

meta/classes-recipe/cmake-qemu.bbclass line 27

Quoted 12 lines
meta/classes-recipe/cmake-qemu.bbclass new file +12 -0
+
+cmake_do_generate_toolchain_file:append:class-target() {
+    if [ "${CMAKE_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

Cheers,

Richard

#3 lore raw

PATCH 2/2 cmake.bbclass: optionally support qemu

In reply to Adrian Freihofer

Provide an easy way to enable cmake-qemu e.g. on distro level for all recipes inheriting the cmake.bbclass.

Therefore a new variable is introduced: OECMAKE_EXEWRAPPER_QEMU.

If set to "1" all recipes built with CMake can use qemu to execute cross-compiled binaries on the build machine.
One use case is the execution of cross-compiled unit tests or ptests, e.g. in an SDK context. Therefore, a variable that activates Qemu for all CMake recipes is much more useful than just providing the optional cmake-qemu.bbclass.
Further information can be found in the CMake documentation in the CMAKE_CROSSCOMPILING_EMULATOR section.

There are some use cases for qemu-user, but users should not be encouraged to use it as an alternative for clean cross-compilation. Since the core does not need it either it is optional.

---
 meta/classes-recipe/cmake.bbclass | 4 ++++
 1 file changed, 4 insertions(+)
meta/classes-recipe/cmake.bbclass +4 -0
index d978b88944..4d8e2a206f 100644
@@ -14,6 +14,10 @@ B = "${WORKDIR}/build"
 # The supported options are "Unix Makefiles" or "Ninja".
 OECMAKE_GENERATOR ?= "Ninja"
 
+# qemu-user can be used for special use cases, but not by default
+OECMAKE_EXEWRAPPER_QEMU ??= "0"
+inherit ${@'cmake-qemu' if d.getVar('OECMAKE_EXEWRAPPER_QEMU') else ''}
+
 python() {
     generator = d.getVar("OECMAKE_GENERATOR")
     if "Unix Makefiles" in generator:
Signature · 1 line
2.41.0
#4 lore raw

Re: [OE-core] [PATCH 2/2] cmake.bbclass: optionally support qemu

Hi Adrian,

Adrian Freihofer <[email protected]> escreveu no dia segunda, 20/11/2023 à(s) 14:46:

Quoted 14 lines

Provide an easy way to enable cmake-qemu e.g. on distro level for all recipes inheriting the cmake.bbclass.

Therefore a new variable is introduced: OECMAKE_EXEWRAPPER_QEMU.

If set to "1" all recipes built with CMake can use qemu to execute cross-compiled binaries on the build machine.
One use case is the execution of cross-compiled unit tests or ptests, e.g. in an SDK context. Therefore, a variable that activates Qemu for all CMake recipes is much more useful than just providing the optional cmake-qemu.bbclass.
Further information can be found in the CMake documentation in the CMAKE_CROSSCOMPILING_EMULATOR section.

Quoted 4 lines

There are some use cases for qemu-user, but users should not be encouraged to use it as an alternative for clean cross-compilation. Since the core does not need it either it is optional.

Are you sure this works in the majority of scenarios? Last time I worked on this qemu user mode failed to run multithreading applications.

Jose

Quoted 36 lines
---
 meta/classes-recipe/cmake.bbclass | 4 ++++
 1 file changed, 4 insertions(+)
meta/classes-recipe/cmake.bbclass +4 -1
index d978b88944..4d8e2a206f 100644
@@ -14,6 +14,10 @@ B = "${WORKDIR}/build"
 # The supported options are "Unix Makefiles" or "Ninja".
 OECMAKE_GENERATOR ?= "Ninja"
 
+# qemu-user can be used for special use cases, but not by default
+OECMAKE_EXEWRAPPER_QEMU ??= "0"
+inherit ${@'cmake-qemu' if d.getVar('OECMAKE_EXEWRAPPER_QEMU') else ''}
+
 python() {
     generator = d.getVar("OECMAKE_GENERATOR")
     if "Unix Makefiles" in generator:
--

2.41.0

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group. View/Reply Online (#190880):
https://lists.openembedded.org/g/openembedded-core/message/190880
Mute This Topic: https://lists.openembedded.org/mt/102708283/5052612
Group Owner: [email protected]
Unsubscribe: https://lists.openembedded.org/g/openembedded-core/unsub [ [email protected]]
-=-=-=-=-=-=-=-=-=-=-=-

Signature · 3 lines
Best regards,

José Quaresma

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw

Re: [OE-core] [PATCH 2/2] cmake.bbclass: optionally support qemu

Jose Quaresma via lists.openembedded.org <quaresma.jose= [email protected]> escreveu no dia segunda, 20/11/2023 à(s) 17:17:

Quoted 19 lines

Hi Adrian,

Adrian Freihofer <[email protected]> escreveu no dia segunda, 20/11/2023 à(s) 14:46:

Quoted 14 lines

Provide an easy way to enable cmake-qemu e.g. on distro level for all recipes inheriting the cmake.bbclass.

Therefore a new variable is introduced: OECMAKE_EXEWRAPPER_QEMU.

If set to "1" all recipes built with CMake can use qemu to execute cross-compiled binaries on the build machine.
One use case is the execution of cross-compiled unit tests or ptests, e.g. in an SDK context. Therefore, a variable that activates Qemu for all CMake recipes is much more useful than just providing the optional cmake-qemu.bbclass.
Further information can be found in the CMake documentation in the CMAKE_CROSSCOMPILING_EMULATOR section.

Quoted 9 lines
Quoted 4 lines

There are some use cases for qemu-user, but users should not be encouraged to use it as an alternative for clean cross-compilation. Since the core does not need it either it is optional.

Are you sure this works in the majority of scenarios? Last time I worked on this qemu user mode failed to run multithreading applications.

Sorry, I was wrong above.
The issue was not multithreading but with the fork system call.

Quoted 49 lines

Jose

Quoted 22 lines
---
 meta/classes-recipe/cmake.bbclass | 4 ++++
 1 file changed, 4 insertions(+)
meta/classes-recipe/cmake.bbclass +4 -1
index d978b88944..4d8e2a206f 100644
@@ -14,6 +14,10 @@ B = "${WORKDIR}/build"
 # The supported options are "Unix Makefiles" or "Ninja".
 OECMAKE_GENERATOR ?= "Ninja"
 
+# qemu-user can be used for special use cases, but not by default
+OECMAKE_EXEWRAPPER_QEMU ??= "0"
+inherit ${@'cmake-qemu' if d.getVar('OECMAKE_EXEWRAPPER_QEMU') else ''}
+
 python() {
     generator = d.getVar("OECMAKE_GENERATOR")
     if "Unix Makefiles" in generator:
--
Quoted 6 lines

2.41.0

--
Best regards,

José Quaresma

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group. View/Reply Online (#190925):
https://lists.openembedded.org/g/openembedded-core/message/190925
Mute This Topic: https://lists.openembedded.org/mt/102708283/5052612
Group Owner: [email protected]
Unsubscribe: https://lists.openembedded.org/g/openembedded-core/unsub [ [email protected]]
-=-=-=-=-=-=-=-=-=-=-=-

Signature · 3 lines
Best regards,

José Quaresma
#7 lore raw

Re: [OE-core] [PATCH 2/2] cmake.bbclass: optionally support qemu

Quoted 10 lines
Quoted 5 lines

Are you sure this works in the majority of scenarios? Last time I worked on this qemu user mode failed to run multithreading applications.

Sorry, I was wrong above.
The issue was not multithreading but with the fork system call.

Hi Jose

I was using it for running Unit Tests on the host before I deployed them to the target device as ptest. This worked well. It is well integrated with cmake and therefore also with IDEs. So I thought that there are certain use cases where it would be nice to have it opt-in.

meson offers qemu-user as well (because of gobject). So if qemu-user is not usable there will be other challenges as well.

There is also a MACHINE_FEATURE which allows to support it for ARCHs where it works well but not for ARCHs where qemu-user is known to be broken.

I think it would be a nice optional feature.

Regards,
Adrian

#8 lore raw

Re: [OE-core] [PATCH 2/2] cmake.bbclass: optionally support qemu

<[email protected]> escreveu no dia segunda, 20/11/2023 à(s) 18:41:

Quoted 18 lines
Quoted 10 lines
Quoted 5 lines

Are you sure this works in the majority of scenarios? Last time I worked on this qemu user mode failed to run multithreading applications.

Sorry, I was wrong above.
The issue was not multithreading but with the fork system call.

Hi Jose

I was using it for running Unit Tests on the host before I deployed them to the target device as ptest. This worked well. It is well integrated with cmake and therefore also with IDEs. So I thought that there are certain use cases where it would be nice to have it opt-in.

Quoted 9 lines

meson offers qemu-user as well (because of gobject). So if qemu-user is not usable there will be other challenges as well.

There is also a MACHINE_FEATURE which allows to support it for ARCHs where it works well but not for ARCHs where qemu-user is known to be broken.

I think it would be a nice optional feature.

I agree without any doubt on that part.

But I believe this doesn't work for applications that are using the fork system call.
So consider this will also break any ptest using fork, giving the wrong impression the test is failing.

Here is my attempt to add support in meson to run the ptest at build time.
https://lists.openembedded.org/g/openembedded-core/topic/89289917

Jose

Quoted 4 lines

Regards,
Adrian

Signature · 3 lines
Best regards,

José Quaresma
#9 lore raw

Re: [OE-core] [PATCH 2/2] cmake.bbclass: optionally support qemu

Quoted 12 lines · Jose Quaresma
Quoted 12 lines

I was using it for running Unit Tests on the host before I deployed them to the target device as ptest. This worked well. It is well integrated with cmake and therefore also with IDEs. So I thought that there are certain use cases where it would be nice to have it opt-in.

meson offers qemu-user as well (because of gobject). So if qemu-user is not usable there will be other challenges as well.

There is also a MACHINE_FEATURE which allows to support it for ARCHs where it works well but not for ARCHs where qemu-user is known to be broken.

Quoted 7 lines
Quoted 1 line

I think it would be a nice optional feature.

I agree without any doubt on that part.

But I believe this doesn't work for applications that are using the fork system call. So consider this will also break any ptest using fork, giving the wrong impression the test is failing.

I think this needs to be double checked. I find it odd that qemu usermode would fail on such a basic thing, and it would not show up anywhere in places where it's used in core (not just g-i - all the various postinst utilities for generating indexes etc.).

Alex

#10 lore raw

Re: [OE-core] [PATCH 2/2] cmake.bbclass: optionally support qemu

Alexander Kanavin <[email protected]> escreveu no dia terça, 21/11/2023 à(s) 11:21:

Quoted 22 lines
Quoted 12 lines · Jose Quaresma
Quoted 12 lines

I was using it for running Unit Tests on the host before I deployed them to the target device as ptest. This worked well. It is well integrated with cmake and therefore also with IDEs. So I thought that there are certain use cases where it would be nice to have it opt-in.

meson offers qemu-user as well (because of gobject). So if qemu-user is not usable there will be other challenges as well.

There is also a MACHINE_FEATURE which allows to support it for ARCHs where it works well but not for ARCHs where qemu-user is known to be broken.

Quoted 6 lines
Quoted 1 line

I think it would be a nice optional feature.

I agree without any doubt on that part.

But I believe this doesn't work for applications that are using the fork

system call.

Quoted 1 line

So consider this will also break any ptest using fork, giving the wrong

Quoted 9 lines

impression the test is failing.

I think this needs to be double checked. I find it odd that qemu usermode would fail on such a basic thing, and it would not show up anywhere in places where it's used in core (not just g-i - all the various postinst utilities for generating indexes etc.).

Alex

There is no recent information on the internet about the fork issue on qemu user mode
so this may no longer be a problem.
I can back again, not now, testing some of the oe-core packages who uses the meson build
system to get some results about failing/passing. The approach is the same used in this patchset.

It will be good also to have some numbers and example recipes in oe-core where we can use this patchset.
In conclusion, this is really a great future imo and improves a lot the integrations of new tests in the ptest infrastructure.

Jose

Signature · 3 lines
Best regards,

José Quaresma
#11 lore raw

Re: [OE-core] [PATCH 2/2] cmake.bbclass: optionally support qemu

In reply to Alexander Kanavin
Quoted 18 lines · Alexander Kanavin
Quoted 17 lines · Jose Quaresma
Quoted 5 lines

I was using it for running Unit Tests on the host before I deployed them to the target device as ptest. This worked well. It is well integrated with cmake and therefore also with IDEs. So I thought that there are certain use cases where it would be nice to have it opt-in.

Quoted 8 lines

meson offers qemu-user as well (because of gobject). So if qemu-user is not usable there will be other challenges as well.

There is also a MACHINE_FEATURE which allows to support it for ARCHs where it works well but not for ARCHs where qemu-user is known to be broken.

I think it would be a nice optional feature.

I agree without any doubt on that part.

Quoted 7 lines
Quoted 2 lines

But I believe this doesn't work for applications that are using the fork system call. So consider this will also break any ptest using fork, giving the wrong impression the test is failing.

I think this needs to be double checked. I find it odd that qemu usermode would fail on such a basic thing, and it would not show up anywhere in places where it's used in core (not just g-i - all the various postinst utilities for generating indexes etc.).

It is more that the exec() calls are problematic so it depends what the code is doing after the fork().

exec*() can be made to work under usermode emulation if you setup binfmt_misc and enable it properly. This can be setup and gets into the territory of what scratchbox tried to do for example but it does usually need more permissions that our build user would typically have.

We have no desire to need that level of system access.

We do also want to maintain the ability to bring up/build platforms that do not have qemu user mode support.

Cheers,

Richard

#12 lore raw

Re: [OE-core] [PATCH 2/2] cmake.bbclass: optionally support qemu

In reply to Adrian Freihofer
Quoted 28 lines · Adrian Freihofer

Provide an easy way to enable cmake-qemu e.g. on distro level for all recipes inheriting the cmake.bbclass.

Therefore a new variable is introduced: OECMAKE_EXEWRAPPER_QEMU.

If set to "1" all recipes built with CMake can use qemu to execute cross-compiled binaries on the build machine.
One use case is the execution of cross-compiled unit tests or ptests, e.g. in an SDK context. Therefore, a variable that activates Qemu for all CMake recipes is much more useful than just providing the optional cmake-qemu.bbclass.
Further information can be found in the CMake documentation in the CMAKE_CROSSCOMPILING_EMULATOR section.

There are some use cases for qemu-user, but users should not be encouraged to use it as an alternative for clean cross-compilation. Since the core does not need it either it is optional.

---
 meta/classes-recipe/cmake.bbclass | 4 ++++
 1 file changed, 4 insertions(+)
meta/classes-recipe/cmake.bbclass +0 -0
index d978b88944..4d8e2a206f 100644

meta/classes-recipe/cmake.bbclass B = "${WORKDIR}/build" · line 20

Quoted 7 lines
meta/classes-recipe/cmake.bbclass +4 -0
@@ -14,6 +14,10 @@ B = "${WORKDIR}/build"
 # The supported options are "Unix Makefiles" or "Ninja".
 OECMAKE_GENERATOR ?= "Ninja"
 
+# qemu-user can be used for special use cases, but not by default
+OECMAKE_EXEWRAPPER_QEMU ??= "0"
+inherit ${@'cmake-qemu' if d.getVar('OECMAKE_EXEWRAPPER_QEMU') else ''}
+

I don't really see why this is needed. If a recipe is using things which need this, wouldn't they just inherit cmake-qemu?

You could make cmake-qemu inherit cmake?

Cheers,

Richard