Introduction of cargo-c class and recipe
v6 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 | 1 + 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 + .../cargo-c/0001-Add-Cargo.lock-file.patch | 2180 +++++++++++++++++ 9 files changed, 2824 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/cargo-c/0001-Add-Cargo.lock-file.patch
Signature · 1 line
2.34.1
PATCH v6 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 + .../cargo-c/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/cargo-c/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/cargo-c/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
PATCH v6 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 v6 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/lib/oeqa/runtime/cases/rust.py | 12 +++ 4 files changed, 122 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/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 v6 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