diff --git a/README.md b/README.md index c2d1cc6..a93bae4 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,19 @@ This openembedded layer provides the rust compiler, tools for building packages - rust (built for target) +## Building a rust package + +When building a rust package in bitbake, it's usually easiest to build with +cargo using cargo.bbclass. If the package already has a Cargo.toml file (most +rust packages do), then it's especially easy. Otherwise you should probably +get the code building in cargo first. + +Once your package builds in cargo, you can use +[cargo-bitbake](https://github.com/cardoe/cargo-bitbake) to generate a bitbake +recipe for it. This allows bitbake to fetch all the necessary dependent +crates, as well as a pegged version of the crates.io index, to ensure maximum +reproducibility. + ## Common issues when packaging things using cargo You may run into errors similar to: diff --git a/classes/cargo.bbclass b/classes/cargo.bbclass index 3d1605c..ded9894 100644 --- a/classes/cargo.bbclass +++ b/classes/cargo.bbclass @@ -1,20 +1,23 @@ -inherit rust +inherit rust-vars +# add crate fetch support +inherit crate-fetch -CARGO ?= "cargo" +# the binary we will use +CARGO = "cargo" + +# Where we download our registry and dependencies to export CARGO_HOME = "${WORKDIR}/cargo_home" -def cargo_base_dep(d): - deps = "" - if not d.getVar('INHIBIT_DEFAULT_DEPS', True) and not d.getVar('INHIBIT_CARGO_DEP', True): - deps += " cargo-native" - return deps +# We need cargo to compile for the target +BASEDEPENDS_append = " cargo-native" -BASEDEPENDS_append = " ${@cargo_base_dep(d)}" +# Ensure we get the right rust variant +DEPENDS_append_class-target = " virtual/${TARGET_PREFIX}rust ${RUSTLIB_DEP}" +DEPENDS_append_class-native = " rust-native" # Cargo only supports in-tree builds at the moment B = "${S}" - # In case something fails in the build process, give a bit more feedback on # where the issue occured export RUST_BACKTRACE = "1" @@ -24,22 +27,26 @@ export RUST_BACKTRACE = "1" # for cross compilation, so tell it we know better than it. export PKG_CONFIG_ALLOW_CROSS = "1" -EXTRA_OECARGO_PATHS ??= "" - cargo_do_configure () { - # FIXME: we currently make a mess in the directory above us - # (${WORKDIR}), which may not be ideal. Look into whether this is - # allowed - mkdir -p ../.cargo + mkdir -p ${CARGO_HOME} # NOTE: we cannot pass more flags via this interface, the 'linker' is # assumed to be a path to a binary. If flags are needed, a wrapper must # be used. - echo "paths = [" >../.cargo/config + echo "paths = [" > ${CARGO_HOME}/config for p in ${EXTRA_OECARGO_PATHS}; do printf "\"%s\"\n" "$p" - done | sed -e 's/$/,/' >>../.cargo/config - echo "]" >>../.cargo/config + done | sed -e 's/$/,/' >> ${CARGO_HOME}/config + echo "]" >> ${CARGO_HOME}/config + + # Point cargo at our local mirror of the registry + cat >> ${CARGO_HOME}/config < +Date: Fri, 2 Sep 2016 14:02:03 -0400 +Subject: [PATCH] Never update the registry index + +Bitbake will fetch the index for us so that we needn't do network IO +during a build +--- + src/cargo/ops/cargo_run.rs | 2 +- + src/cargo/sources/registry.rs | 34 +--------------------------------- + 2 files changed, 2 insertions(+), 34 deletions(-) + +diff --git a/src/cargo/ops/cargo_run.rs b/src/cargo/ops/cargo_run.rs +index 6764118..379b98b 100644 +--- a/src/cargo/ops/cargo_run.rs ++++ b/src/cargo/ops/cargo_run.rs +@@ -1,7 +1,7 @@ + use std::path::Path; + + use ops::{self, CompileFilter}; +-use util::{self, CargoResult, process, ProcessError}; ++use util::{self, CargoResult, ProcessError}; + use core::Package; + + pub fn run(manifest_path: &Path, +diff --git a/src/cargo/sources/registry.rs b/src/cargo/sources/registry.rs +index 614d654..a0c7fe6 100644 +--- a/src/cargo/sources/registry.rs ++++ b/src/cargo/sources/registry.rs +@@ -166,7 +166,6 @@ use std::path::{PathBuf, Path}; + + use curl::http; + use flate2::read::GzDecoder; +-use git2; + use rustc_serialize::hex::ToHex; + use rustc_serialize::json; + use tar::Archive; +@@ -174,7 +173,7 @@ use url::Url; + + use core::{Source, SourceId, PackageId, Package, Summary, Registry}; + use core::dependency::{Dependency, DependencyInner, Kind}; +-use sources::{PathSource, git}; ++use sources::PathSource; + use util::{CargoResult, Config, internal, ChainError, ToUrl, human}; + use util::{hex, Sha256, paths, Filesystem, FileLock}; + use ops; +@@ -464,38 +463,7 @@ impl<'cfg> RegistrySource<'cfg> { + + /// Actually perform network operations to update the registry + fn do_update(&mut self) -> CargoResult<()> { +- if self.updated { +- return Ok(()) +- } +- try!(self.checkout_path.create_dir()); +- let lock = try!(self.checkout_path.open_rw(Path::new(INDEX_LOCK), +- self.config, +- "the registry index")); +- let path = lock.path().parent().unwrap(); +- +- try!(self.config.shell().status("Updating", +- format!("registry `{}`", self.source_id.url()))); +- let repo = try!(git2::Repository::open(path).or_else(|_| { +- let _ = lock.remove_siblings(); +- git2::Repository::init(path) +- })); +- +- // git fetch origin +- let url = self.source_id.url().to_string(); +- let refspec = "refs/heads/*:refs/remotes/origin/*"; +- +- try!(git::fetch(&repo, &url, refspec, &self.config).chain_error(|| { +- internal(format!("failed to fetch `{}`", url)) +- })); +- +- // git reset --hard origin/master +- let reference = "refs/remotes/origin/master"; +- let oid = try!(repo.refname_to_id(reference)); +- trace!("[{}] updating to rev {}", self.source_id, oid); +- let object = try!(repo.find_object(oid, None)); +- try!(repo.reset(&object, git2::ResetType::Hard, None)); + self.updated = true; +- self.cache.clear(); + Ok(()) + } + } +-- +2.7.4 + diff --git a/recipes-devtools/rust/libstd-rs.bb b/recipes-devtools/rust/libstd-rs.bb index d8e72a5..45229e2 100644 --- a/recipes-devtools/rust/libstd-rs.bb +++ b/recipes-devtools/rust/libstd-rs.bb @@ -6,22 +6,27 @@ LICENSE = "MIT | Apache-2.0" LIC_FILES_CHKSUM ="file://COPYRIGHT;md5=43e1f1fb9c0ee3af66693d8c4fecafa8" require rust-shared-source.inc +CARGO_INDEX_COMMIT = "6127fc24b0b6fe73fe4d339817fbf000b9a798a2" + +SRC_URI += "\ + crate://crates.io/gcc/0.3.26 \ + crate-index://crates.io/${CARGO_INDEX_COMMIT} \ +" + DEPENDS += "compiler-rt" RUSTLIB_DEP = "" -inherit cargo_util +inherit cargo # Needed so cargo can find libbacktrace RUSTFLAGS += "-L ${STAGING_LIBDIR}" B = "${WORKDIR}/build" -do_compile () { +do_compile_prepend () { cd ${S}/src/rustc/std_shim export CARGO_TARGET_DIR="${B}" export RUSTC_BOOTSTRAP_KEY="e8edd0fd" - oe_cargo_fix_env - oe_cargo_build } do_install () { diff --git a/recipes-example/rust-hello-world/rust-hello-world_git.bb b/recipes-example/rust-hello-world/rust-hello-world_git.bb index 7b5e53e..ba88548 100644 --- a/recipes-example/rust-hello-world/rust-hello-world_git.bb +++ b/recipes-example/rust-hello-world/rust-hello-world_git.bb @@ -1,4 +1,4 @@ -inherit cargo_util +inherit cargo SRC_URI = "git://github.com/jmesmon/rust-hello-world.git;protocol=https" SRCREV="e0fa23f1a3cb1eb1407165bd2fc36d2f6e6ad728" diff --git a/recipes-example/rustfmt/rustfmt_0.4.0.bb b/recipes-example/rustfmt/rustfmt_0.4.0.bb index 27bb92d..af487ef 100644 --- a/recipes-example/rustfmt/rustfmt_0.4.0.bb +++ b/recipes-example/rustfmt/rustfmt_0.4.0.bb @@ -1,4 +1,4 @@ -inherit cargo_util +inherit cargo CARGO_INDEX_COMMIT = "3b3994e099281c394a6a66604d1af6c0920e4c31"