blob: 0829a58dd90f1df9e1038e8f3ad21d0456f9652d [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
Patrick Williams92b42cb2022-09-03 06:53:57 -050033RUSTFLAGS ??= ""
34BUILD_MODE = "${@['--release', ''][d.getVar('DEBUG_BUILD') == '1']}"
Patrick Williams2a254922023-08-11 09:48:11 -050035# --frozen flag will prevent network access (which is required since only
36# the do_fetch step is authorized to access network)
37# and will require an up to date Cargo.lock file.
38# This force the package being built to already ship a Cargo.lock, in the end
39# this is what we want, at least, for reproducibility of the build.
Patrick Williams169d7bc2024-01-05 11:33:25 -060040CARGO_BUILD_FLAGS = "-v --frozen --target ${RUST_HOST_SYS} ${BUILD_MODE} --manifest-path=${CARGO_MANIFEST_PATH}"
Patrick Williams92b42cb2022-09-03 06:53:57 -050041
42# This is based on the content of CARGO_BUILD_FLAGS and generally will need to
43# change if CARGO_BUILD_FLAGS changes.
44BUILD_DIR = "${@['release', 'debug'][d.getVar('DEBUG_BUILD') == '1']}"
45CARGO_TARGET_SUBDIR="${RUST_HOST_SYS}/${BUILD_DIR}"
46oe_cargo_build () {
47 export RUSTFLAGS="${RUSTFLAGS}"
48 bbnote "Using rust targets from ${RUST_TARGET_PATH}"
49 bbnote "cargo = $(which ${CARGO})"
Patrick Williams92b42cb2022-09-03 06:53:57 -050050 bbnote "${CARGO} build ${CARGO_BUILD_FLAGS} $@"
51 "${CARGO}" build ${CARGO_BUILD_FLAGS} "$@"
52}
53
54do_compile[progress] = "outof:\s+(\d+)/(\d+)"
55cargo_do_compile () {
Patrick Williams92b42cb2022-09-03 06:53:57 -050056 oe_cargo_build
57}
58
59cargo_do_install () {
60 local have_installed=false
61 for tgt in "${B}/target/${CARGO_TARGET_SUBDIR}/"*; do
62 case $tgt in
63 *.so|*.rlib)
64 install -d "${D}${rustlibdir}"
65 install -m755 "$tgt" "${D}${rustlibdir}"
66 have_installed=true
67 ;;
68 *examples)
69 if [ -d "$tgt" ]; then
70 for example in "$tgt/"*; do
71 if [ -f "$example" ] && [ -x "$example" ]; then
72 install -d "${D}${bindir}"
73 install -m755 "$example" "${D}${bindir}"
74 have_installed=true
75 fi
76 done
77 fi
78 ;;
79 *)
80 if [ -f "$tgt" ] && [ -x "$tgt" ]; then
81 install -d "${D}${bindir}"
82 install -m755 "$tgt" "${D}${bindir}"
83 have_installed=true
84 fi
85 ;;
86 esac
87 done
88 if ! $have_installed; then
89 die "Did not find anything to install"
90 fi
91}
92
93EXPORT_FUNCTIONS do_compile do_install