1# 2# Copyright OpenEmbedded Contributors 3# 4# SPDX-License-Identifier: MIT 5# 6 7## 8## Purpose: 9## This class is used by any recipes that are built using 10## Cargo. 11 12inherit cargo_common 13inherit rust-target-config 14 15# the binary we will use 16CARGO = "cargo" 17 18# We need cargo to compile for the target 19BASEDEPENDS:append = " cargo-native" 20 21# Ensure we get the right rust variant 22DEPENDS:append:class-target = " rust-native ${RUSTLIB_DEP}" 23DEPENDS:append:class-nativesdk = " rust-native ${RUSTLIB_DEP}" 24DEPENDS:append:class-native = " rust-native" 25 26# Enable build separation 27B = "${WORKDIR}/build" 28 29# In case something fails in the build process, give a bit more feedback on 30# where the issue occured 31export RUST_BACKTRACE = "1" 32 33# The directory of the Cargo.toml relative to the root directory, per default 34# assume there's a Cargo.toml directly in the root directory 35CARGO_SRC_DIR ??= "" 36 37# The actual path to the Cargo.toml 38MANIFEST_PATH ??= "${S}/${CARGO_SRC_DIR}/Cargo.toml" 39 40RUSTFLAGS ??= "" 41BUILD_MODE = "${@['--release', ''][d.getVar('DEBUG_BUILD') == '1']}" 42# --frozen flag will prevent network access (which is required since only 43# the do_fetch step is authorized to access network) 44# and will require an up to date Cargo.lock file. 45# This force the package being built to already ship a Cargo.lock, in the end 46# this is what we want, at least, for reproducibility of the build. 47CARGO_BUILD_FLAGS = "-v --frozen --target ${RUST_HOST_SYS} ${BUILD_MODE} --manifest-path=${MANIFEST_PATH}" 48 49# This is based on the content of CARGO_BUILD_FLAGS and generally will need to 50# change if CARGO_BUILD_FLAGS changes. 51BUILD_DIR = "${@['release', 'debug'][d.getVar('DEBUG_BUILD') == '1']}" 52CARGO_TARGET_SUBDIR="${RUST_HOST_SYS}/${BUILD_DIR}" 53oe_cargo_build () { 54 export RUSTFLAGS="${RUSTFLAGS}" 55 bbnote "Using rust targets from ${RUST_TARGET_PATH}" 56 bbnote "cargo = $(which ${CARGO})" 57 bbnote "${CARGO} build ${CARGO_BUILD_FLAGS} $@" 58 "${CARGO}" build ${CARGO_BUILD_FLAGS} "$@" 59} 60 61do_compile[progress] = "outof:\s+(\d+)/(\d+)" 62cargo_do_compile () { 63 oe_cargo_build 64} 65 66cargo_do_install () { 67 local have_installed=false 68 for tgt in "${B}/target/${CARGO_TARGET_SUBDIR}/"*; do 69 case $tgt in 70 *.so|*.rlib) 71 install -d "${D}${rustlibdir}" 72 install -m755 "$tgt" "${D}${rustlibdir}" 73 have_installed=true 74 ;; 75 *examples) 76 if [ -d "$tgt" ]; then 77 for example in "$tgt/"*; do 78 if [ -f "$example" ] && [ -x "$example" ]; then 79 install -d "${D}${bindir}" 80 install -m755 "$example" "${D}${bindir}" 81 have_installed=true 82 fi 83 done 84 fi 85 ;; 86 *) 87 if [ -f "$tgt" ] && [ -x "$tgt" ]; then 88 install -d "${D}${bindir}" 89 install -m755 "$tgt" "${D}${bindir}" 90 have_installed=true 91 fi 92 ;; 93 esac 94 done 95 if ! $have_installed; then 96 die "Did not find anything to install" 97 fi 98} 99 100EXPORT_FUNCTIONS do_compile do_install 101