Introduction of cargo-c class and recipe
v4 cover Introduction of cargo-c class and recipe
From: Frederic Martinsons <[email protected]>
This series introduces a cargo-c.bbclass and cargo-c-native package that help building C-ABI compatible header and library around rust code.
The third patch add examples and test to demonstrate the usage and the good working of this use case.
The fourth patch correct previous rust test by requiring scp package.
Tests have been passed with the following in local.conf:
CORE_IMAGE_EXTRA_INSTALL:append = " openssh-sshd openssh-scp cargo rust rust-c-lib-example-bin" IMAGE_CLASSES += "testimage"TEST_SUITES = "ping ssh rust"
# To use slirp option in testimage.bbclassTEST_RUNQEMUPARAMS = "slirp"TEST_SERVER_IP = "127.0.0.1"QEMU_USE_SLIRP = "1"
and the following commands:
bitbake core-image-minimal -c testimage
and
bitbake core-image-sato -c testimage
The following changes since commit ed1789638d025447db8f28d71d119de85a696b09:
qemu: Upgrade 8.0.3 -> 8.0.4 (2023-08-15 08:14:32 +0100)
are available in the Git repository at:
https://gitlab.com/fmartinsons/openembedded-core cargo-c-introduction
Frederic Martinsons (4):
rust: add cargo-c native recipe
classes-recipe: add cargo_c.bbclass
rust: provide examples for C library generation in rust oeqa/runtime/rust: correct rust test
.../rust/rust-c-lib-example-bin_git.bb | 16 + .../rust/rust-c-lib-example-crates.inc | 79 + .../rust/rust-c-lib-example_git.bb | 15 + meta/classes-recipe/cargo_c.bbclass | 41 + meta/conf/distro/include/maintainers.inc | 3 + meta/lib/oeqa/runtime/cases/rust.py | 18 +- meta/recipes-devtools/rust/cargo-c-crates.inc | 453 ++++ .../rust/cargo-c-native_0.9.18.bb | 23 + .../rust/files/0001-Add-Cargo.lock-file.patch | 2180 +++++++++++++++++ 9 files changed, 2826 insertions(+), 2 deletions(-) create mode 100644 meta-selftest/recipes-devtools/rust/rust-c-lib-example-bin_git.bb create mode 100644 meta-selftest/recipes-devtools/rust/rust-c-lib-example-crates.inc create mode 100644 meta-selftest/recipes-devtools/rust/rust-c-lib-example_git.bb create mode 100644 meta/classes-recipe/cargo_c.bbclass create mode 100644 meta/recipes-devtools/rust/cargo-c-crates.inc create mode 100644 meta/recipes-devtools/rust/cargo-c-native_0.9.18.bb create mode 100644 meta/recipes-devtools/rust/files/0001-Add-Cargo.lock-file.patch
Signature · 1 line
2.34.1
PATCH v4 1/4 rust: add cargo-c native recipe
From: Frederic Martinsons <[email protected]>
This package make it easier to generate C/C++ ABI compatible header, library and also generate package config file.
It is built around cbindgen (https://github.com/mozilla/cbindgen)
for the header generation, it compiles the library (static or dynamic) through cargo and finally handle the generation of a custom package config file.
--- meta/conf/distro/include/maintainers.inc | 1 + meta/recipes-devtools/rust/cargo-c-crates.inc | 453 ++++ .../rust/cargo-c-native_0.9.18.bb | 23 + .../rust/files/0001-Add-Cargo.lock-file.patch | 2180 +++++++++++++++++ 4 files changed, 2657 insertions(+) create mode 100644 meta/recipes-devtools/rust/cargo-c-crates.inc create mode 100644 meta/recipes-devtools/rust/cargo-c-native_0.9.18.bb create mode 100644 meta/recipes-devtools/rust/files/0001-Add-Cargo.lock-file.patch
meta/conf/distro/include/maintainers.inc +1 -0
@@ -88,6 +88,7 @@ RECIPE_MAINTAINER:pn-bzip2 = "Denys Dmytriyenko <[email protected]>" RECIPE_MAINTAINER:pn-ca-certificates = "Alexander Kanavin <[email protected]>" RECIPE_MAINTAINER:pn-cairo = "Anuj Mittal <[email protected]>" RECIPE_MAINTAINER:pn-cargo = "Randy MacLeod <[email protected]>" +RECIPE_MAINTAINER:pn-cargo-c-native = "Frederic Martinsons <[email protected]>" RECIPE_MAINTAINER:pn-cantarell-fonts = "Alexander Kanavin <[email protected]>" RECIPE_MAINTAINER:pn-ccache = "Robert Yang <[email protected]>" RECIPE_MAINTAINER:pn-cdrtools-native = "Yi Zhao <[email protected]>"
meta/recipes-devtools/rust/cargo-c-crates.inc new file +453 -0
453 lines not on this page · raw message
meta/recipes-devtools/rust/cargo-c-native_0.9.18.bb new file +23 -0
@@ -0,0 +1,23 @@ +SUMMARY = "cargo applet to build and install C-ABI compatible dynamic and static libraries." +HOMEPAGE = "https://crates.io/crates/cargo-c" +LICENSE = "MIT" +LIC_FILES_CHKSUM = " \ + file://LICENSE;md5=384ed0e2e0b2dac094e51fbf93fdcbe0 \ +" + +PR = "r1" + +SRC_URI = " \ + git://github.com/lu-zero/cargo-c.git;branch=master;protocol=https \ + file://0001-Add-Cargo.lock-file.patch \ +" + +SRCREV = "4eaf39ebbbc9ab8f092adf487d5b53435511d619" +S = "${WORKDIR}/git" + +inherit cargo cargo-update-recipe-crates pkgconfig native + +DEPENDS = "openssl curl" + +require ${BPN}-crates.inc +
meta/recipes-devtools/rust/files/0001-Add-Cargo.lock-file.patch new file +2180 -0
2,180 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [OE-core] [PATHCH V4 1/4] rust: add cargo-c native recipe
Quoted 11 lines · Frederic Martinsons
From: Frederic Martinsons <[email protected]>
This package make it easier to generate C/C++ ABI compatible header, library and also generate package config file.
It is built around cbindgen (https://github.com/mozilla/cbindgen)
for the header generation, it compiles the library (static or dynamic) through cargo and finally handle the generation of a custom package config file.Signed-off-by Frederic Martinsons <[email protected]>
Quoted 9 lines
--- meta/conf/distro/include/maintainers.inc | 1 + meta/recipes-devtools/rust/cargo-c-crates.inc | 453 ++++ .../rust/cargo-c-native_0.9.18.bb | 23 + .../rust/files/0001-Add-Cargo.lock-file.patch | 2180 +++++++++++++++++ 4 files changed, 2657 insertions(+) create mode 100644 meta/recipes-devtools/rust/cargo-c-crates.inc create mode 100644 meta/recipes-devtools/rust/cargo-c-native_0.9.18.bb create mode 100644 meta/recipes-devtools/rust/files/0001-Add-Cargo.lock-file.patch
Should 0001-Add-Cargo.lock-file.patch be here?
Cheers,
Richard
Re: [OE-core] [PATHCH V4 1/4] rust: add cargo-c native recipe
Quoted 21 lines · Richard Purdie
Quoted 11 lines · Frederic Martinsons
From: Frederic Martinsons <[email protected]>
This package make it easier to generate C/C++ ABI compatible header, library and also generate package config file.
It is built around cbindgen (https://github.com/mozilla/cbindgen)
for the header generation, it compiles the library (static or dynamic) through cargo and finally handle the generation of a custom package config file.Signed-off-by Frederic Martinsons <[email protected]>Quoted 9 lines
--- meta/conf/distro/include/maintainers.inc | 1 + meta/recipes-devtools/rust/cargo-c-crates.inc | 453 ++++ .../rust/cargo-c-native_0.9.18.bb | 23 + .../rust/files/0001-Add-Cargo.lock-file.patch | 2180 +++++++++++++++++ 4 files changed, 2657 insertions(+) create mode 100644 meta/recipes-devtools/rust/cargo-c-crates.inc create mode 100644 meta/recipes-devtools/rust/cargo-c-native_0.9.18.bb create mode 100644
Quoted 4 lines
meta/recipes-devtools/rust/files/0001-Add-Cargo.lock-file.patchShould 0001-Add-Cargo.lock-file.patch be here?
Hello Richard,
What do you mean? The file 0001-Add-Cargo.lock-file.patch is present. By re reading it though, there is a wrong signed off , will correct that in a v5
Maybe I missed your point though, sorry if I misunderstood your question, can you tell me more?
Quoted 4 lines
Cheers,
Richard
Re: [OE-core] [PATHCH V4 1/4] rust: add cargo-c native recipe
Quoted 36 lines · Frédéric Martinsons
Le mer. 16 août 2023, 11:23, Richard Purdie
<[email protected]> a écrit :Quoted 28 lines · Frederic Martinsons
Quoted 19 lines
From: Frederic Martinsons <[email protected]>
This package make it easier to generate C/C++ ABI compatible header, library and also generate package config file.
It is built around cbindgen (https://github.com/mozilla/cbindgen)
for the header generation, it compiles the library (static or dynamic)
through cargo and finally handle the generation of a custom package config file.Signed-off-by: Frederic Martinsons <[email protected]>
---
meta/conf/distro/include/maintainers.inc | 1 + meta/recipes-devtools/rust/cargo-c-crates.inc | 453 ++++ .../rust/cargo-c-native_0.9.18.bb | 23 +.../rust/files/0001-Add-Cargo.lock-file.patch| 2180
+++++++++++++++++Quoted 7 lines
4 files changed, 2657 insertions(+)create mode 100644 meta/recipes-devtools/rust/cargo-c- crates.inc
create mode 100644 meta/recipes-devtools/rust/cargo-c- native_0.9.18.bb
create mode 100644 meta/recipes-devtools/rust/files/0001-Add- Cargo.lock-file.patchQuoted 1 line
Should 0001-Add-Cargo.lock-file.patch be here?
Hello Richard,
Quoted 7 lines
What do you mean? The file 0001-Add-Cargo.lock-file.patch is present.
By re reading it though, there is a wrong signed off , will correct that in a v5Maybe I missed your point though, sorry if I misunderstood your question, can you tell me more?
Sorry, I'm getting a little confused as these are all in the rust directory and in general you'd assume these patches were for rust itself. I appreciate now that in this case it isn't.
What you've done will work but is confusing, can you move it to a cargo-c or cargo-c-native directory please?
Thanks,
Richard
Re: [OE-core] [PATHCH V4 1/4] rust: add cargo-c native recipe
Quoted 45 lines · Richard Purdie
Quoted 36 lines · Frédéric Martinsons
Quoted 28 lines · Richard Purdie
Quoted 20 lines · Frederic Martinsons
From: Frederic Martinsons <[email protected]>
This package make it easier to generate C/C++ ABI compatible header, library and also generate package config file.
It is built around cbindgen (https://github.com/mozilla/cbindgen)
for the header generation, it compiles the library (static or dynamic)
through cargo and finally handle the generation of a custom package config file.Signed-off-by: Frederic Martinsons
<[email protected]>--- meta/conf/distro/include/maintainers.inc | 1 + meta/recipes-devtools/rust/cargo-c-crates.inc | 453 ++++ .../rust/cargo-c-native_0.9.18.bb | 23 + .../rust/files/0001-Add-Cargo.lock-file.patch | 2180 +++++++++++++++++ 4 files changed, 2657 insertions(+)Quoted 6 lines
create mode 100644 meta/recipes-devtools/rust/cargo-c-
crates.inc
create mode 100644 meta/recipes-devtools/rust/cargo-c-
native_0.9.18.bb
create mode 100644 meta/recipes-devtools/rust/files/0001-Add-
Cargo.lock-file.patchQuoted 1 line
Should 0001-Add-Cargo.lock-file.patch be here?
Hello Richard,
Quoted 7 lines
What do you mean? The file 0001-Add-Cargo.lock-file.patch is present.
By re reading it though, there is a wrong signed off , will correct that in a v5Maybe I missed your point though, sorry if I misunderstood your question, can you tell me more?
Quoted 7 lines
Sorry, I'm getting a little confused as these are all in the rust directory and in general you'd assume these patches were for rust itself. I appreciate now that in this case it isn't.
What you've done will work but is confusing, can you move it to a cargo-c or cargo-c-native directory please?
Yes of course, will do right away.
Thanks,
Quoted 5 lines
Richard
PATCH v4 2/4 classes-recipe: add cargo_c.bbclass
In reply to [email protected]From: Frederic Martinsons <[email protected]>
This class can be used inside rust recipe to generate a rust library that can be called by C/C++ code.
The rust recipe which uses this class has to only replace "inherit cargo" by "inherit cargo_c".
--- meta/classes-recipe/cargo_c.bbclass | 41 +++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 meta/classes-recipe/cargo_c.bbclass
meta/classes-recipe/cargo_c.bbclass new file +41 -0
@@ -0,0 +1,41 @@ +# +# Copyright OpenEmbedded Contributors +# +# SPDX-License-Identifier: MIT +# + +## +## Purpose: +## This class is used by any recipes that want to compile a C ABI compatible +## library with header and pkg config file + +inherit cargo pkgconfig + +# the binaries we will use +CARGO_C_BUILD = "cargo-cbuild" +CARGO_C_INSTALL = "cargo-cinstall" + +# We need cargo-c to compile for the target +BASEDEPENDS:append = " cargo-c-native" + +do_compile[progress] = "outof:\s+(\d+)/(\d+)" +cargo_c_do_compile() { + oe_cargo_fix_env + export RUSTFLAGS="${RUSTFLAGS}" + bbnote "Using rust targets from ${RUST_TARGET_PATH}" + bbnote "cargo-cbuild = $(which ${CARGO_C_BUILD})" + bbnote "${CARGO_C_BUILD} cbuild ${CARGO_BUILD_FLAGS}" + "${CARGO_C_BUILD}" cbuild ${CARGO_BUILD_FLAGS} +} + +cargo_c_do_install() { + oe_cargo_fix_env + export RUSTFLAGS="${RUSTFLAGS}" + bbnote "cargo-cinstall = $(which ${CARGO_C_INSTALL})" + "${CARGO_C_INSTALL}" cinstall ${CARGO_BUILD_FLAGS} \ + --destdir ${D} \ + --prefix ${prefix} \ + --library-type cdylib +} + +EXPORT_FUNCTIONS do_compile do_install
Signature · 1 line
2.34.1
PATCH v4 3/4 rust: provide examples for C library generation in rust
In reply to [email protected]From: Frederic Martinsons <[email protected]>
rust-c-lib-example is a little rust code which provide a single function to print a formatted date (via the chrono crate) from an input timestamp in millisecond. It has the necessary FFI annotation and inherit cargo_c class for the C ABI compatible library generation.
rust-c-lib-example is meson project for the C code which will call the print_date function from rust-c-lib-example if no argument is provided, if any argument is provided it will print "Hello world in rust from C!"
add a runtime test case to check if all went well.
--- .../rust/rust-c-lib-example-bin_git.bb | 16 ++++ .../rust/rust-c-lib-example-crates.inc | 79 +++++++++++++++++++ .../rust/rust-c-lib-example_git.bb | 15 ++++ meta/conf/distro/include/maintainers.inc | 2 + meta/lib/oeqa/runtime/cases/rust.py | 12 +++ 5 files changed, 124 insertions(+) create mode 100644 meta-selftest/recipes-devtools/rust/rust-c-lib-example-bin_git.bb create mode 100644 meta-selftest/recipes-devtools/rust/rust-c-lib-example-crates.inc create mode 100644 meta-selftest/recipes-devtools/rust/rust-c-lib-example_git.bb
meta-selftest/recipes-devtools/rust/rust-c-lib-example-bin_git.bb new file +16 -0
@@ -0,0 +1,16 @@ +DESCRIPTION = "A simple example for C wrapper around a rust library" +HOMEPAGE = "https://gitlab.com/fmartinsonsHome/rust-c-lib-example" +LICENSE = "MIT" +LIC_FILES_CHKSUM = "file://LICENSE;md5=cb9c441273ed8a029701a086befbfc63" + +SRC_URI = " \ + git://gitlab.com/fmartinsonsHome/rust-c-lib-example.git;branch=main;protocol=https \ +" + +SRCREV = "fc53c457f69aa5221ec1f8619a007e8150db5e60" +S = "${WORKDIR}/git" + +DEPENDS = "rust-c-lib-example" + +inherit meson pkgconfig +
meta-selftest/recipes-devtools/rust/rust-c-lib-example-crates.inc new file +79 -0
79 lines not on this page · raw message
meta-selftest/recipes-devtools/rust/rust-c-lib-example_git.bb new file +15 -0
@@ -0,0 +1,15 @@ +DESCRIPTION = "A simple example for rust C library" +HOMEPAGE = "https://gitlab.com/fmartinsonsHome/rust-c-lib-example" +LICENSE = "MIT" +LIC_FILES_CHKSUM = "file://LICENSE;md5=cb9c441273ed8a029701a086befbfc63" + +SRC_URI = " \ + git://gitlab.com/fmartinsonsHome/rust-c-lib-example.git;branch=main;protocol=https \ +" + +SRCREV = "fc53c457f69aa5221ec1f8619a007e8150db5e60" +S = "${WORKDIR}/git" + +inherit cargo_c + +require ${BPN}-crates.inc
meta/conf/distro/include/maintainers.inc +2 -0
@@ -735,6 +735,8 @@ RECIPE_MAINTAINER:pn-ruby = "Ross Burton <[email protected]>" RECIPE_MAINTAINER:pn-run-postinsts = "Ross Burton <[email protected]>" RECIPE_MAINTAINER:pn-rust = "Randy MacLeod <[email protected]>" RECIPE_MAINTAINER:pn-rust-cross-canadian-${TRANSLATED_TARGET_ARCH} = "Randy MacLeod <[email protected]>" +RECIPE_MAINTAINER:pn-rust-c-lib-example = "Frederic Martinsons <[email protected]>" +RECIPE_MAINTAINER:pn-rust-c-lib-example-bin = "Frederic Martinsons <[email protected]>" RECIPE_MAINTAINER:pn-rust-llvm = "Randy MacLeod <[email protected]>" RECIPE_MAINTAINER:pn-rxvt-unicode = "Unassigned <[email protected]>" RECIPE_MAINTAINER:pn-sato-screenshot = "Ross Burton <[email protected]>"
meta/lib/oeqa/runtime/cases/rust.py +12 -0
@@ -48,3 +48,15 @@ class RustCompileTest(OERuntimeTestCase): status, output = self.target.run('cargo run --manifest-path=/tmp/hello/Cargo.toml') msg = 'running compiled file failed, output: %s' % output self.assertEqual(status, 0, msg=msg) + +class RustCLibExampleTest(OERuntimeTestCase): + @OETestDepends(['ssh.SSHTest.test_ssh']) + @OEHasPackage('rust-c-lib-example-bin') + def test_rust_c_lib_example(self): + cmd = "rust-c-lib-example-bin test" + status, output = self.target.run(cmd) + msg = 'Exit status was not 0. Output: %s' % output + self.assertEqual(status, 0, msg=msg) + + msg = 'Incorrect output: %s' % output + self.assertEqual(output, "Hello world in rust from C!", msg=msg)
Signature · 1 line
2.34.1
PATCH v4 4/4 oeqa/runtime/rust: correct rust test
In reply to [email protected]From: Frederic Martinsons <[email protected]>
Since setUp of RustCompileTest use cls.tc.copyTo, those tests needs the scp command:
NOTE: ======================================================================
NOTE: FAIL: test_cargo_compile (rust.RustCompileTest)
NOTE: ----------------------------------------------------------------------
NOTE: Traceback (most recent call last):
File "/home/jenkins/yocto-poky-master/poky/meta/lib/oeqa/core/case.py", line 53, in _oeSetUp
self.setUpMethod()
File "/home/jenkins/yocto-poky-master/poky/meta/lib/oeqa/runtime/cases/rust.py", line 17, in setUp
cls.tc.target.copyTo(src, dst)
File "/home/jenkins/yocto-poky-master/poky/meta/lib/oeqa/core/target/ssh.py", line 132, in copyTo
return self._run(scpCmd, ignore_status=False)
File "/home/jenkins/yocto-poky-master/poky/meta/lib/oeqa/core/target/ssh.py", line 81, in _run
raise AssertionError("Command '%s' returned non-zero exit "
AssertionError: Command '['scp', '-o', 'ServerAliveCountMax=2', '-o', 'ServerAliveInterval=30', '-o', 'UserKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=no', '-o', 'LogLevel=ERROR', '-r', '-P', '2222', '/home/jenkins/yocto-poky-master/poky/meta/lib/oeqa/files/test.rs', '[email protected]:/tmp/']' returned non-zero exit status 1:
sh: scp: not found
lost connection
--- meta/lib/oeqa/runtime/cases/rust.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
meta/lib/oeqa/runtime/cases/rust.py +4 -2
@@ -24,7 +24,8 @@ class RustCompileTest(OERuntimeTestCase): cls.tc.target.run('rm -r %s' % dirs) @OETestDepends(['ssh.SSHTest.test_ssh']) - @OEHasPackage(['rust']) + @OEHasPackage('rust') + @OEHasPackage('openssh-scp') def test_rust_compile(self): status, output = self.target.run('rustc /tmp/test.rs -o /tmp/test') msg = 'rust compile failed, output: %s' % output
@@ -35,7 +36,8 @@ class RustCompileTest(OERuntimeTestCase): self.assertEqual(status, 0, msg=msg) @OETestDepends(['ssh.SSHTest.test_ssh']) - @OEHasPackage(['cargo']) + @OEHasPackage('cargo') + @OEHasPackage('openssh-scp') def test_cargo_compile(self): status, output = self.target.run('cargo new /tmp/hello') msg = 'cargo new failed, output: %s' % output
Signature · 1 line
2.34.1