From 3a2a348ca79c2203ec4faa4334ed584c5ddb901c Mon Sep 17 00:00:00 2001 From: banditopazzo Date: Sat, 19 Oct 2024 14:26:20 +0200 Subject: [PATCH] feat: dependencies in a separate task --- README.md | 19 ++++++++++--------- classes/cargo_bin.bbclass | 22 +++++++++++++++------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index f01cb1d..40db2e1 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ An OpenEmebdded/Yocto layer providing pre-built toolchains for the - [meta-rust-bin](#meta-rust-bin) - [Basic Example](#basic-example) - [Features](#features) - - [Use with Yocto Release 4.0 (kirkstone) and Above](#use-with-yocto-release-40-kirkstone-and-above) - - [Use with Yocto Release 3.4 (honister) and Above](#use-with-yocto-release-34-honister-and-above) + - [Allow `build.rs` to access the network in Yocto Release 4.0 (kirkstone) and Above](#allow-buildrs-to-access-the-network-in-yocto-release-40-kirkstone-and-above) + - [Updating from an old `meta-rust-bin`](#updating-from-an-old-meta-rust-bin) - [Advanced Features](#advanced-features) - [Specifying Cargo Features](#specifying-cargo-features) - [Using Components Individually](#using-components-individually) @@ -34,9 +34,6 @@ LICENSE = "MIT" inherit cargo_bin -# Enable network for the compile task allowing cargo to download dependencies -do_compile[network] = "1" - SRC_URI = "git://github.com/rust-embedded/gpio-utils.git;protocol=https;branch=master" SRCREV="02b0658cd7e13e46f6b1a5de3fd9655711749759" S = "${WORKDIR}/git" @@ -76,18 +73,22 @@ Future: (provides space savings). * [ ] Total static linking using MUSL. -### Use with Yocto Release 4.0 (kirkstone) and Above +### Allow `build.rs` to access the network in Yocto Release 4.0 (kirkstone) and Above From Yocto version 4.0 network access from tasks is disabled by default on kernels which support this feature (on most recent distros such as CentOS 8 and -Debian 11 onwards). The task `do_compile` need to access the network because it -downloads dependencies, so add the following line to the recipe: +Debian 11 onwards). If the `build.rs` needs access to the network, you have to +enable the network for the `do_compile` task adding the following line to the +recipe: ```bitbake +# After the line `inherit cargo_bin` + +# Enable network access for `build.rs` in compile task do_compile[network] = "1" ``` -### Updating from an old `meta-rust-bin` +## Updating from an old `meta-rust-bin` To avoid conflicts with the offical Rust layer of Yocto, the class `cargo` of `meta-rust-bin` was renamed to `cargo_bin`. diff --git a/classes/cargo_bin.bbclass b/classes/cargo_bin.bbclass index 2c0416d..4805504 100644 --- a/classes/cargo_bin.bbclass +++ b/classes/cargo_bin.bbclass @@ -22,14 +22,17 @@ RUST_BUILD = "${@rust_target(d, 'BUILD')}" # Additional flags passed directly to the "cargo build" invocation EXTRA_CARGO_FLAGS ??= "" -EXTRA_RUSTFLAGS ??= "" -RUSTFLAGS += "${EXTRA_RUSTFLAGS}" + +# Optional RUSTFLAGS +RUSTFLAGS ??= "" # Space-separated list of features to enable CARGO_FEATURES ??= "" # Control the Cargo build type (debug or release) -CARGO_BUILD_PROFILE ?= "release" +# This is based on the content of CARGO_BUILD_FLAGS and generally will need to +# change if CARGO_BUILD_FLAGS changes. +CARGO_BUILD_PROFILE ?= "${@oe.utils.conditional('DEBUG_BUILD', '1', 'debug', 'release', d)}" CARGO_INSTALL_DIR ?= "${D}${bindir}" @@ -49,9 +52,8 @@ WRAPPER_DIR = "${WORKDIR}/wrappers" # Set the Cargo manifest path to the typical location CARGO_MANIFEST_PATH ?= "${S}/Cargo.toml" -FILES:${PN}-dev += "${libdir}/*.rlib" - CARGO_BUILD_FLAGS = "\ + --offline \ --verbose \ --manifest-path ${CARGO_MANIFEST_PATH} \ --target=${RUST_TARGET} \ @@ -93,6 +95,13 @@ cargo_bin_do_configure() { chmod +x "${WRAPPER_DIR}/linker-native-wrapper.sh" } +addtask do_cargo_fetch after do_configure before do_compile +do_cargo_fetch[network] = "1" +do_cargo_fetch[dirs]= "${B}" +cargo_bin_do_cargo_fetch() { + cargo fetch --manifest-path ${CARGO_MANIFEST_PATH} +} + cargo_bin_do_compile() { export TARGET_CC="${WRAPPER_DIR}/cc-wrapper.sh" export TARGET_CXX="${WRAPPER_DIR}/cxx-wrapper.sh" @@ -114,7 +123,6 @@ cargo_bin_do_compile() { export CARGO_TARGET_APPLIES_TO_HOST="false" export CARGO_TARGET_${@rust_target(d, 'TARGET').replace('-','_').upper()}_LINKER="${WRAPPER_DIR}/linker-wrapper.sh" export CARGO_HOST_LINKER="${WRAPPER_DIR}/linker-native-wrapper.sh" - export CARGO_BUILD_FLAGS="-C rpath" export CARGO_PROFILE_RELEASE_DEBUG="true" # The CC crate defaults to using CFLAGS when compiling everything. We can @@ -171,4 +179,4 @@ cargo_bin_do_install() { fi } -EXPORT_FUNCTIONS do_configure do_compile do_install +EXPORT_FUNCTIONS do_configure do_cargo_fetch do_compile do_install