Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

meta-balena-rust: update to v1.65.0 #3580

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions meta-balena-rust/classes/cargo.bbclass
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#

##
## Purpose:
## This class is used by any recipes that are built using
## Cargo.

inherit cargo_common
inherit rust-target-config

# the binary we will use
CARGO = "cargo"
Expand All @@ -12,8 +19,8 @@ CARGO = "cargo"
BASEDEPENDS:append = " cargo-native"

# Ensure we get the right rust variant
DEPENDS:append:class-target = " virtual/${TARGET_PREFIX}rust ${RUSTLIB_DEP}"
DEPENDS:append:class-nativesdk = " virtual/${TARGET_PREFIX}rust ${RUSTLIB_DEP}"
DEPENDS:append:class-target = " rust-native ${RUSTLIB_DEP}"
DEPENDS:append:class-nativesdk = " rust-native ${RUSTLIB_DEP}"
DEPENDS:append:class-native = " rust-native"

# Enable build separation
Expand All @@ -32,17 +39,16 @@ MANIFEST_PATH ??= "${S}/${CARGO_SRC_DIR}/Cargo.toml"

RUSTFLAGS ??= ""
BUILD_MODE = "${@['--release', ''][d.getVar('DEBUG_BUILD') == '1']}"
CARGO_BUILD_FLAGS = "-v --target ${HOST_SYS} ${BUILD_MODE} --manifest-path=${MANIFEST_PATH}"
CARGO_BUILD_FLAGS = "-v --target ${RUST_HOST_SYS} ${BUILD_MODE} --manifest-path=${MANIFEST_PATH}"

# This is based on the content of CARGO_BUILD_FLAGS and generally will need to
# change if CARGO_BUILD_FLAGS changes.
BUILD_DIR = "${@['release', 'debug'][d.getVar('DEBUG_BUILD') == '1']}"
CARGO_TARGET_SUBDIR="${HOST_SYS}/${BUILD_DIR}"
CARGO_TARGET_SUBDIR="${RUST_HOST_SYS}/${BUILD_DIR}"
oe_cargo_build () {
export RUSTFLAGS="${RUSTFLAGS}"
export RUST_TARGET_PATH="${RUST_TARGET_PATH}"
bbnote "Using rust targets from ${RUST_TARGET_PATH}"
bbnote "cargo = $(which ${CARGO})"
bbnote "rustc = $(which ${RUSTC})"
bbnote "${CARGO} build ${CARGO_BUILD_FLAGS} $@"
"${CARGO}" build ${CARGO_BUILD_FLAGS} "$@"
}
Expand Down
25 changes: 20 additions & 5 deletions meta-balena-rust/classes/cargo_common.bbclass
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#

##
## Purpose:
## This class is to support building with cargo. It
Expand Down Expand Up @@ -57,7 +63,7 @@ cargo_common_do_configure () {

[source.crates-io]
replace-with = "bitbake"
local-registry = "/nonexistant"
local-registry = "/nonexistent"
EOF
fi

Expand All @@ -76,26 +82,35 @@ cargo_common_do_configure () {
cat <<- EOF >> ${CARGO_HOME}/config

# HOST_SYS
[target.${HOST_SYS}]
[target.${RUST_HOST_SYS}]
linker = "${CARGO_RUST_TARGET_CCLD}"
EOF

if [ "${HOST_SYS}" != "${BUILD_SYS}" ]; then
if [ "${RUST_HOST_SYS}" != "${RUST_BUILD_SYS}" ]; then
cat <<- EOF >> ${CARGO_HOME}/config

# BUILD_SYS
[target.${BUILD_SYS}]
[target.${RUST_BUILD_SYS}]
linker = "${RUST_BUILD_CCLD}"
EOF
fi

if [ "${RUST_TARGET_SYS}" != "${RUST_BUILD_SYS}" -a "${RUST_TARGET_SYS}" != "${RUST_HOST_SYS}" ]; then
cat <<- EOF >> ${CARGO_HOME}/config

# TARGET_SYS
[target.${RUST_TARGET_SYS}]
linker = "${RUST_TARGET_CCLD}"
EOF
fi

# Put build output in build directory preferred by bitbake instead of
# inside source directory unless they are the same
if [ "${B}" != "${S}" ]; then
cat <<- EOF >> ${CARGO_HOME}/config

[build]
# Use out of tree build destination to avoid poluting the source tree
# Use out of tree build destination to avoid polluting the source tree
target-dir = "${B}/target"
EOF
fi
Expand Down
7 changes: 6 additions & 1 deletion meta-balena-rust/classes/rust-bin.bbclass
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#

inherit rust

RDEPENDS:${PN}:append:class-target = " ${RUSTLIB_DEP}"
Expand Down Expand Up @@ -93,7 +99,6 @@ do_configure () {
}

oe_runrustc () {
export RUST_TARGET_PATH="${RUST_TARGET_PATH}"
bbnote ${RUSTC} ${RUSTC_ARCHFLAGS} ${RUSTC_FLAGS} "$@"
"${RUSTC}" ${RUSTC_ARCHFLAGS} ${RUSTC_FLAGS} "$@"
}
Expand Down
96 changes: 40 additions & 56 deletions meta-balena-rust/classes/rust-common.bbclass
Original file line number Diff line number Diff line change
@@ -1,46 +1,31 @@
#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#

inherit python3native
inherit rust-target-config

# Common variables used by all Rust builds
export rustlibdir = "${libdir}/rust"
export rustlibdir = "${libdir}/rustlib/${RUST_HOST_SYS}/lib"
FILES:${PN} += "${rustlibdir}/*.so"
FILES:${PN}-dev += "${rustlibdir}/*.rlib ${rustlibdir}/*.rmeta"
FILES:${PN}-dbg += "${rustlibdir}/.debug"

RUSTLIB = "-L ${STAGING_LIBDIR}/rust"
RUSTLIB = "-L ${STAGING_DIR_HOST}${rustlibdir}"
RUST_DEBUG_REMAP = "--remap-path-prefix=${WORKDIR}=/usr/src/debug/${PN}/${EXTENDPE}${PV}-${PR}"
RUSTFLAGS += "${RUSTLIB} ${RUST_DEBUG_REMAP}"
RUSTLIB_DEP ?= "libstd-rs"
export RUST_TARGET_PATH = "${STAGING_LIBDIR_NATIVE}/rustlib"
RUST_PANIC_STRATEGY ?= "unwind"

# Native builds are not effected by TCLIBC. Without this, rust-native
# thinks it's "target" (i.e. x86_64-linux) is a musl target.
RUST_LIBC = "${TCLIBC}"
RUST_LIBC:class-crosssdk = "glibc"
RUST_LIBC:class-native = "glibc"

def set_rust_arch(arch):
try:
import oe.rust
return oe.rust.arch_to_rust_arch(arch)
except ImportError or ModuleNotFoundError:
return arch

def determine_libc(d, thing):
'''Determine which libc something should target'''

# BUILD is never musl, TARGET may be musl or glibc,
# HOST could be musl, but only if a compiler is built to be run on
# target in which case HOST_SYS != BUILD_SYS.
if thing == 'TARGET':
libc = d.getVar('RUST_LIBC')
elif thing == 'BUILD' and (d.getVar('HOST_SYS') != d.getVar('BUILD_SYS')):
libc = d.getVar('RUST_LIBC')
else:
libc = d.getVar('RUST_LIBC:class-native')

return libc

def target_is_armv7(d):
'''Determine if target is armv7'''
# TUNE_FEATURES may include arm* even if the target is not arm
Expand Down Expand Up @@ -70,33 +55,26 @@ def rust_base_triple(d, thing):
'''

# The llvm-target for armv7 is armv7-unknown-linux-gnueabihf
if thing == "TARGET" and target_is_armv7(d):
if d.getVar('{}_ARCH'.format(thing)) == d.getVar('TARGET_ARCH') and target_is_armv7(d):
arch = "armv7"
else:
arch = set_rust_arch(d.getVar('{}_ARCH'.format(thing)))

# All the Yocto targets are Linux and are 'unknown'
vendor = "-unknown"
os = d.getVar('{}_OS'.format(thing))
libc = determine_libc(d, thing)

# Prefix with a dash and convert glibc -> gnu
if libc == "glibc":
libc = "-gnu"
elif libc == "musl":
libc = "-musl"

# Don't double up musl (only appears to be the case on aarch64)
if os == "linux-musl":
if libc != "-musl":
bb.fatal("{}_OS was '{}' but TCLIBC was not 'musl'".format(thing, os))
os = "linux"
# Substituting "unknown" when vendor is empty will match rust's standard
# targets when building native recipes (including rust-native itself)
vendor = d.getVar('{}_VENDOR'.format(thing)) or "-unknown"

# Default to glibc
libc = "-gnu"
os = d.getVar('{}_OS'.format(thing))
# This catches ARM targets and appends the necessary hard float bits
if os == "linux-gnueabi" or os == "linux-musleabi":
libc = bb.utils.contains('TUNE_FEATURES', 'callconvention-hard', 'hf', '', d)
return arch + vendor + '-' + os + libc
elif "musl" in os:
libc = "-musl"
os = "linux"

return arch + vendor + '-' + os + libc


# In some cases uname and the toolchain differ on their idea of the arch name
Expand Down Expand Up @@ -146,26 +124,32 @@ RUST_TARGET_CXX = "${WRAPPER_DIR}/target-rust-cxx"
RUST_TARGET_CCLD = "${WRAPPER_DIR}/target-rust-ccld"
RUST_TARGET_AR = "${WRAPPER_DIR}/target-rust-ar"

create_wrapper () {
create_wrapper_rust () {
file="$1"
shift
extras="$1"
shift

cat <<- EOF > "${file}"
#!/usr/bin/env python3
import os, sys
orig_binary = "$@"
extras = "${extras}"
binary = orig_binary.split()[0]
args = orig_binary.split() + sys.argv[1:]
if extras:
args.append(extras)
os.execvp(binary, args)
EOF
chmod +x "${file}"
}

export WRAPPER_TARGET_CC = "${CC}"
export WRAPPER_TARGET_CXX = "${CXX}"
export WRAPPER_TARGET_CCLD = "${CCLD}"
export WRAPPER_TARGET_LDFLAGS = "${LDFLAGS}"
export WRAPPER_TARGET_AR = "${AR}"
WRAPPER_TARGET_CC = "${CC}"
WRAPPER_TARGET_CXX = "${CXX}"
WRAPPER_TARGET_CCLD = "${CCLD}"
WRAPPER_TARGET_LDFLAGS = "${LDFLAGS}"
WRAPPER_TARGET_EXTRALD = ""
WRAPPER_TARGET_AR = "${AR}"

# compiler is used by gcc-rs
# linker is used by rustc/cargo
Expand All @@ -174,22 +158,22 @@ do_rust_create_wrappers () {
mkdir -p "${WRAPPER_DIR}"

# Yocto Build / Rust Host C compiler
create_wrapper "${RUST_BUILD_CC}" "${BUILD_CC}"
create_wrapper_rust "${RUST_BUILD_CC}" "" "${BUILD_CC}"
# Yocto Build / Rust Host C++ compiler
create_wrapper "${RUST_BUILD_CXX}" "${BUILD_CXX}"
create_wrapper_rust "${RUST_BUILD_CXX}" "" "${BUILD_CXX}"
# Yocto Build / Rust Host linker
create_wrapper "${RUST_BUILD_CCLD}" "${BUILD_CCLD}" "${BUILD_LDFLAGS}"
create_wrapper_rust "${RUST_BUILD_CCLD}" "" "${BUILD_CCLD}" "${BUILD_LDFLAGS}"
# Yocto Build / Rust Host archiver
create_wrapper "${RUST_BUILD_AR}" "${BUILD_AR}"
create_wrapper_rust "${RUST_BUILD_AR}" "" "${BUILD_AR}"

# Yocto Target / Rust Target C compiler
create_wrapper "${RUST_TARGET_CC}" "${WRAPPER_TARGET_CC}" "${WRAPPER_TARGET_LDFLAGS}"
create_wrapper_rust "${RUST_TARGET_CC}" "${WRAPPER_TARGET_EXTRALD}" "${WRAPPER_TARGET_CC}" "${WRAPPER_TARGET_LDFLAGS}"
# Yocto Target / Rust Target C++ compiler
create_wrapper "${RUST_TARGET_CXX}" "${WRAPPER_TARGET_CXX}"
create_wrapper_rust "${RUST_TARGET_CXX}" "${WRAPPER_TARGET_EXTRALD}" "${WRAPPER_TARGET_CXX}" "${CXXFLAGS}"
# Yocto Target / Rust Target linker
create_wrapper "${RUST_TARGET_CCLD}" "${WRAPPER_TARGET_CCLD}" "${WRAPPER_TARGET_LDFLAGS}"
create_wrapper_rust "${RUST_TARGET_CCLD}" "${WRAPPER_TARGET_EXTRALD}" "${WRAPPER_TARGET_CCLD}" "${WRAPPER_TARGET_LDFLAGS}"
# Yocto Target / Rust Target archiver
create_wrapper "${RUST_TARGET_AR}" "${WRAPPER_TARGET_AR}"
create_wrapper_rust "${RUST_TARGET_AR}" "" "${WRAPPER_TARGET_AR}"

}

Expand Down
Loading
Loading