Hi everyone,

I promised Randy to post my experience with Yocto and Rust, which was a
first time attempt for me. Note, that when starting I had no knowledge about 
Rust whatsoever, so this is "clean start" observation.

On Tue, Dec 06, 2022 at 06:04:18PM -0500, Randy MacLeod wrote:
> On 2022-10-31 07:47, Alexander Kanavin wrote:
> >For better or worse, more and more rust components are appearing that do
> >not include their dependencies in tarballs (or git trees), and rely on cargo
> >to fetch them. On the other hand, bitbake does not use cargo (and quite 
> >possible
> >won't ever be able to), and relies on having each item explicitly listed in 
> >SRC_URI
> >with a crate:// prefix. This however creates a problem of both making such 
> >lists in
> >the first place and updating them when a recipe is updated to a newer 
> >version.
> >
> >So this class can be used to perform such updates by implementing a task 
> >that does it;
> >the next commit shows the outcome for python3-bcrypt (which has been tested 
> >to work
> >and produce a successful build).
> >
> >Note: the python script relies on tomllib library, which appears in Python 
> >3.11 and
> >does not exist in earlier versions - I've tested this by first updating 
> >python to 3.11-rc2
> >in oe-core.
> 
> 
> Thanks Alex, that's a nice approach.
> 
> I'm reply to the thread since Jin had some trouble getting started
> with this class. This pushed me to finally try it and I have some minor
> comments and suggestions.

my first issue was to actually find out how to approach this, it seems
most google hits point to meta-rust which has been around for a longer period
and I was lucky to get some hints in #oe on IRC, where Randy pointed me to 
cargo-update-recipe-crates.bbclass which is already in Poky.

The comment in the header about running bitbake -c update_crates recipe-name
allowed me to generate the .inc file, but it was not immediately clear to me
what I should do next.

The pointer to python3-bcrypt_*.bb was a bit confusing in the sense, that
from the rust perspective it only inherits cargo-update-recipe-crates,
but trying to build my package after having genrated the SRC_URI entries did
not produce any build output, I ended up with an empty -dev and an empty -dbg 
ipk.

By looking at the available classes I figured that I should probably also
inherit cargo, which indeed started to compile the application I needed to
package.

Next, I ran into some build issues for packages which I guess the application
was depending upon. I got an error:

error: failed to run custom build command for `libudev-sys v0.1.4`

which luckily had enough google hits to understand that it expects
a DEPENDS = "eudev"

It however still failed after that and it took me some time to realize
that I should also inherit pkgconfig, which finally did the trick.

I copied the working recipe back to Kirkstone and was happy to see, that it
builds there as well.

So overall, given that I had zero Rust knowledge, it wasn't a bad
experience, Yocto took care of most things for me.

> First, it works for me for adding ripgrep (1) and for Jin for
> something he was
> working on. It wasn't a perfect experience for me since at first, I
> had omitted:
> 
> S = "${WORKDIR}/git"
> 
> and
> $ bitbake -c update_crates  ripgrep
> 
> would fail silently with nothing useful in the log.do_update_crates file.
> 
> I'll likely send a patch to make it more verbose on error.
> 
> 
> It also wasn't clear how to get started but in hindsight, you need
> 'obviously'
> need a recipe that has SRC_URI and SRCREV and S and maybe a bit more
> to get started.
> 
> 
> Michael,
> 
> Do you think we should a document this workflow in addition to the
> cargo bitbake approach?

I think this should be documented more prominently, as missing information
on how to get going was the biggest obstacle, at least for me. The benefit of
this workflow is, that no additional layer is needed and that everything seems
to more or less work out of the box within the usual checkout.

The downside is a two step approach and I wonder if it would be possible to
append the SRC_URI crate entries on the fly, but in a way that still works
with sstate so that regeneration happens only when something has changed.

Anyway, it seems to be a good start, thanks to everyone who made this possible!

Kind regards,
Jin

 
 
> 1) https://github.com/BurntSushi/ripgrep
> 
> Recipe will go to meta-oe.
> 
> >
> >Signed-off-by: Alexander Kanavin <a...@linutronix.de>
> >---
> >  .../cargo-update-recipe-crates.bbclass        | 41 +++++++++++++++++++
> >  1 file changed, 41 insertions(+)
> >  create mode 100644 meta/classes-recipe/cargo-update-recipe-crates.bbclass
> >
> >diff --git a/meta/classes-recipe/cargo-update-recipe-crates.bbclass 
> >b/meta/classes-recipe/cargo-update-recipe-crates.bbclass
> >new file mode 100644
> >index 0000000000..f90938c734
> >--- /dev/null
> >+++ b/meta/classes-recipe/cargo-update-recipe-crates.bbclass
> >@@ -0,0 +1,41 @@
> >+#
> >+# Copyright OpenEmbedded Contributors
> >+#
> >+# SPDX-License-Identifier: MIT
> >+#
> >+
> >+##
> >+## Purpose:
> >+## This class is used to update the list of crates in SRC_URI
> >+## by reading Cargo.lock in the source tree.
> >+##
> >+## See meta/recipes-devtools/python/python3-bcrypt_*.bb for an example
> >+##
> >+## To perform the update: bitbake -c update_crates recipe-name
> >+
> >+addtask do_update_crates after do_patch
> >+do_update_crates[depends] = "python3-native:do_populate_sysroot"
> >+
> >+do_update_crates() {
> >+    nativepython3 - <<EOF
> >+
> >+def get_crates(f):
> >+    import tomllib
> >+    c_list = 'SRC_URI += " \\ \n'
> >+    crates = tomllib.load(open(f, 'rb'))
> >+    for c in crates['package']:
> >+        if 'source' in c and 'crates.io' in c['source']:
> >+            c_list += "        crate://crates.io/{}/{} \\ 
> >\n".format(c['name'], c['version'])
> >+    c_list += '"\n'
> >+    return c_list
> >+
> >+import os
> >+crates = "# Autogenerated with 'bitbake -c update_crates ${PN}'\n\n"
> >+for root, dirs, files in os.walk('${S}'):
> >+    for file in files:
> >+        if file == 'Cargo.lock':
> >+            crates += get_crates(os.path.join(root, file))
> >+open(os.path.join('${THISDIR}', '${PN}'+"-crates.inc"), 'w').write(crates)
> >+
> >+EOF
> >+}
> >
> >
> >
> 
> -- 
> # Randy MacLeod
> # Wind River Linux
-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#174338): 
https://lists.openembedded.org/g/openembedded-core/message/174338
Mute This Topic: https://lists.openembedded.org/mt/94683148/21656
Group Owner: openembedded-core+ow...@lists.openembedded.org
Unsubscribe: https://lists.openembedded.org/g/openembedded-core/unsub 
[arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-

Reply via email to