diff options
Diffstat (limited to 'meta/classes-recipe/cargo.bbclass')
-rw-r--r-- | meta/classes-recipe/cargo.bbclass | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/meta/classes-recipe/cargo.bbclass b/meta/classes-recipe/cargo.bbclass new file mode 100644 index 0000000000..d1e83518b5 --- /dev/null +++ b/meta/classes-recipe/cargo.bbclass | |||
@@ -0,0 +1,97 @@ | |||
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 | |||
12 | inherit cargo_common | ||
13 | inherit rust-target-config | ||
14 | |||
15 | # the binary we will use | ||
16 | CARGO = "cargo" | ||
17 | |||
18 | # We need cargo to compile for the target | ||
19 | BASEDEPENDS:append = " cargo-native" | ||
20 | |||
21 | # Ensure we get the right rust variant | ||
22 | DEPENDS:append:class-target = " rust-native ${RUSTLIB_DEP}" | ||
23 | DEPENDS:append:class-nativesdk = " rust-native ${RUSTLIB_DEP}" | ||
24 | DEPENDS:append:class-native = " rust-native" | ||
25 | |||
26 | # Enable build separation | ||
27 | B = "${WORKDIR}/build" | ||
28 | |||
29 | # In case something fails in the build process, give a bit more feedback on | ||
30 | # where the issue occured | ||
31 | export 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 | ||
35 | CARGO_SRC_DIR ??= "" | ||
36 | |||
37 | # The actual path to the Cargo.toml | ||
38 | MANIFEST_PATH ??= "${S}/${CARGO_SRC_DIR}/Cargo.toml" | ||
39 | |||
40 | RUSTFLAGS ??= "" | ||
41 | BUILD_MODE = "${@['--release', ''][d.getVar('DEBUG_BUILD') == '1']}" | ||
42 | CARGO_BUILD_FLAGS = "-v --target ${RUST_HOST_SYS} ${BUILD_MODE} --manifest-path=${MANIFEST_PATH}" | ||
43 | |||
44 | # This is based on the content of CARGO_BUILD_FLAGS and generally will need to | ||
45 | # change if CARGO_BUILD_FLAGS changes. | ||
46 | BUILD_DIR = "${@['release', 'debug'][d.getVar('DEBUG_BUILD') == '1']}" | ||
47 | CARGO_TARGET_SUBDIR="${RUST_HOST_SYS}/${BUILD_DIR}" | ||
48 | oe_cargo_build () { | ||
49 | export RUSTFLAGS="${RUSTFLAGS}" | ||
50 | bbnote "Using rust targets from ${RUST_TARGET_PATH}" | ||
51 | bbnote "cargo = $(which ${CARGO})" | ||
52 | bbnote "rustc = $(which ${RUSTC})" | ||
53 | bbnote "${CARGO} build ${CARGO_BUILD_FLAGS} $@" | ||
54 | "${CARGO}" build ${CARGO_BUILD_FLAGS} "$@" | ||
55 | } | ||
56 | |||
57 | do_compile[progress] = "outof:\s+(\d+)/(\d+)" | ||
58 | cargo_do_compile () { | ||
59 | oe_cargo_fix_env | ||
60 | oe_cargo_build | ||
61 | } | ||
62 | |||
63 | cargo_do_install () { | ||
64 | local have_installed=false | ||
65 | for tgt in "${B}/target/${CARGO_TARGET_SUBDIR}/"*; do | ||
66 | case $tgt in | ||
67 | *.so|*.rlib) | ||
68 | install -d "${D}${rustlibdir}" | ||
69 | install -m755 "$tgt" "${D}${rustlibdir}" | ||
70 | have_installed=true | ||
71 | ;; | ||
72 | *examples) | ||
73 | if [ -d "$tgt" ]; then | ||
74 | for example in "$tgt/"*; do | ||
75 | if [ -f "$example" ] && [ -x "$example" ]; then | ||
76 | install -d "${D}${bindir}" | ||
77 | install -m755 "$example" "${D}${bindir}" | ||
78 | have_installed=true | ||
79 | fi | ||
80 | done | ||
81 | fi | ||
82 | ;; | ||
83 | *) | ||
84 | if [ -f "$tgt" ] && [ -x "$tgt" ]; then | ||
85 | install -d "${D}${bindir}" | ||
86 | install -m755 "$tgt" "${D}${bindir}" | ||
87 | have_installed=true | ||
88 | fi | ||
89 | ;; | ||
90 | esac | ||
91 | done | ||
92 | if ! $have_installed; then | ||
93 | die "Did not find anything to install" | ||
94 | fi | ||
95 | } | ||
96 | |||
97 | EXPORT_FUNCTIONS do_compile do_install | ||