blob: 3ef0bbbb44e9670a03b205ae6b0cbb6a48363fb1 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
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']}"
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050042CARGO_BUILD_FLAGS = "-v --offline --target ${RUST_HOST_SYS} ${BUILD_MODE} --manifest-path=${MANIFEST_PATH}"
Patrick Williams92b42cb2022-09-03 06:53:57 -050043
44# This is based on the content of CARGO_BUILD_FLAGS and generally will need to
45# change if CARGO_BUILD_FLAGS changes.
46BUILD_DIR = "${@['release', 'debug'][d.getVar('DEBUG_BUILD') == '1']}"
47CARGO_TARGET_SUBDIR="${RUST_HOST_SYS}/${BUILD_DIR}"
48oe_cargo_build () {
49 export RUSTFLAGS="${RUSTFLAGS}"
50 bbnote "Using rust targets from ${RUST_TARGET_PATH}"
51 bbnote "cargo = $(which ${CARGO})"
Patrick Williams92b42cb2022-09-03 06:53:57 -050052 bbnote "${CARGO} build ${CARGO_BUILD_FLAGS} $@"
53 "${CARGO}" build ${CARGO_BUILD_FLAGS} "$@"
54}
55
56do_compile[progress] = "outof:\s+(\d+)/(\d+)"
57cargo_do_compile () {
Patrick Williams92b42cb2022-09-03 06:53:57 -050058 oe_cargo_build
59}
60
61cargo_do_install () {
62 local have_installed=false
63 for tgt in "${B}/target/${CARGO_TARGET_SUBDIR}/"*; do
64 case $tgt in
65 *.so|*.rlib)
66 install -d "${D}${rustlibdir}"
67 install -m755 "$tgt" "${D}${rustlibdir}"
68 have_installed=true
69 ;;
70 *examples)
71 if [ -d "$tgt" ]; then
72 for example in "$tgt/"*; do
73 if [ -f "$example" ] && [ -x "$example" ]; then
74 install -d "${D}${bindir}"
75 install -m755 "$example" "${D}${bindir}"
76 have_installed=true
77 fi
78 done
79 fi
80 ;;
81 *)
82 if [ -f "$tgt" ] && [ -x "$tgt" ]; then
83 install -d "${D}${bindir}"
84 install -m755 "$tgt" "${D}${bindir}"
85 have_installed=true
86 fi
87 ;;
88 esac
89 done
90 if ! $have_installed; then
91 die "Did not find anything to install"
92 fi
93}
94
95EXPORT_FUNCTIONS do_compile do_install