mirror of
https://github.com/flatcar/scripts.git
synced 2025-11-27 05:21:34 +01:00
eclass/cargo: Sync with Gentoo
It's from Gentoo commit dc51935f7aae5f89d1ffecabef322680979952b8.
This commit is contained in:
parent
86ad817042
commit
b880096ab5
@ -1,4 +1,4 @@
|
||||
# Copyright 1999-2022 Gentoo Authors
|
||||
# Copyright 1999-2023 Gentoo Authors
|
||||
# Distributed under the terms of the GNU General Public License v2
|
||||
|
||||
# @ECLASS: cargo.eclass
|
||||
@ -10,6 +10,11 @@
|
||||
# @SUPPORTED_EAPIS: 7 8
|
||||
# @BLURB: common functions and variables for cargo builds
|
||||
|
||||
case ${EAPI} in
|
||||
7|8) ;;
|
||||
*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
|
||||
esac
|
||||
|
||||
if [[ -z ${_CARGO_ECLASS} ]]; then
|
||||
_CARGO_ECLASS=1
|
||||
|
||||
@ -17,15 +22,11 @@ _CARGO_ECLASS=1
|
||||
# https://github.com/rust-lang/cargo/blob/master/CHANGELOG.md
|
||||
RUST_DEPEND="virtual/rust"
|
||||
|
||||
case "${EAPI:-0}" in
|
||||
0|1|2|3|4|5|6)
|
||||
die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
|
||||
;;
|
||||
case ${EAPI} in
|
||||
7)
|
||||
# 1.37 added 'cargo vendor' subcommand and net.offline config knob
|
||||
RUST_DEPEND=">=virtual/rust-1.37.0"
|
||||
;;
|
||||
|
||||
8)
|
||||
# 1.39 added --workspace
|
||||
# 1.46 added --target dir
|
||||
@ -40,61 +41,93 @@ case "${EAPI:-0}" in
|
||||
die "CRATES variable not defined"
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
|
||||
;;
|
||||
esac
|
||||
|
||||
inherit multiprocessing toolchain-funcs
|
||||
inherit flag-o-matic multiprocessing toolchain-funcs
|
||||
|
||||
if [[ ! ${CARGO_OPTIONAL} ]]; then
|
||||
BDEPEND="${RUST_DEPEND}"
|
||||
EXPORT_FUNCTIONS src_unpack src_configure src_compile src_install src_test
|
||||
fi
|
||||
[[ ! ${CARGO_OPTIONAL} ]] && BDEPEND="${RUST_DEPEND}"
|
||||
|
||||
IUSE="${IUSE} debug"
|
||||
|
||||
ECARGO_HOME="${WORKDIR}/cargo_home"
|
||||
ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
|
||||
|
||||
# @ECLASS-VARIABLE: CRATES
|
||||
# @ECLASS_VARIABLE: CRATES
|
||||
# @DEFAULT_UNSET
|
||||
# @PRE_INHERIT
|
||||
# @DESCRIPTION:
|
||||
# bash string containing all crates package wants to download
|
||||
# used by cargo_crate_uris()
|
||||
# Bash string containing all crates that are to be downloaded.
|
||||
# It is used by cargo_crate_uris.
|
||||
#
|
||||
# Ideally, crate names and versions should be separated by a `@`
|
||||
# character. A legacy syntax using hyphen is also supported but it is
|
||||
# much slower.
|
||||
#
|
||||
# Example:
|
||||
# @CODE
|
||||
# CRATES="
|
||||
# metal-1.2.3
|
||||
# bar-4.5.6
|
||||
# iron_oxide-0.0.1
|
||||
# metal@1.2.3
|
||||
# bar@4.5.6
|
||||
# iron_oxide@0.0.1
|
||||
# "
|
||||
# inherit cargo
|
||||
# ...
|
||||
# SRC_URI="$(cargo_crate_uris)"
|
||||
# SRC_URI="${CARGO_CRATE_URIS}"
|
||||
# @CODE
|
||||
|
||||
# @ECLASS-VARIABLE: CARGO_OPTIONAL
|
||||
# @ECLASS_VARIABLE: GIT_CRATES
|
||||
# @DEFAULT_UNSET
|
||||
# @PRE_INHERIT
|
||||
# @DESCRIPTION:
|
||||
# If set to a non-null value, before inherit cargo part of the ebuild will
|
||||
# Bash associative array containing all of the crates that are to be
|
||||
# fetched via git. It is used by cargo_crate_uris.
|
||||
# If this is defined, then cargo_src_install will add --frozen to "cargo install".
|
||||
# The key is a crate name, the value is a semicolon-separated list of:
|
||||
#
|
||||
# - the URI to fetch the crate from.
|
||||
# - This intelligently handles GitHub and GitLab URIs so that
|
||||
# just the repository path is needed.
|
||||
# - The string "%commit%" gets replaced with the commit's checksum.
|
||||
# - the checksum of the commit to use.
|
||||
# - optionally: the path to look for Cargo.toml in.
|
||||
# - This will also replace the string "%commit%" with the commit's checksum.
|
||||
# - Defaults to: "${crate}-%commit%"
|
||||
#
|
||||
# Example of a simple definition with no path to Cargo.toml:
|
||||
# @CODE
|
||||
# declare -A GIT_CRATES=(
|
||||
# [home]="https://github.com/rbtcollins/home;a243ee2fbee6022c57d56f5aa79aefe194eabe53"
|
||||
# )
|
||||
# @CODE
|
||||
#
|
||||
# Example with paths defined:
|
||||
# @CODE
|
||||
# declare -A GIT_CRATES=(
|
||||
# [rustpython-common]="https://github.com/RustPython/RustPython;4f38cb68e4a97aeea9eb19673803a0bd5f655383;RustPython-%commit%/common"
|
||||
# [rustpython-parser]="https://github.com/RustPython/RustPython;4f38cb68e4a97aeea9eb19673803a0bd5f655383;RustPython-%commit%/compiler/parser"
|
||||
# )
|
||||
# @CODE
|
||||
|
||||
# @ECLASS_VARIABLE: CARGO_OPTIONAL
|
||||
# @DEFAULT_UNSET
|
||||
# @PRE_INHERIT
|
||||
# @DESCRIPTION:
|
||||
# If set to a non-null value, the part of the ebuild before "inherit cargo" will
|
||||
# be considered optional. No dependencies will be added and no phase
|
||||
# functions will be exported.
|
||||
#
|
||||
# If you enable CARGO_OPTIONAL, you have to set BDEPEND on virtual/rust
|
||||
# for your package and call at least cargo_gen_config manually before using
|
||||
# other src_ functions of this eclass.
|
||||
# note that cargo_gen_config is automatically called by cargo_src_unpack.
|
||||
# other src_functions of this eclass.
|
||||
# Note that cargo_gen_config is automatically called by cargo_src_unpack.
|
||||
|
||||
# @ECLASS_VARIABLE: myfeatures
|
||||
# @DEFAULT_UNSET
|
||||
# @DESCRIPTION:
|
||||
# Optional cargo features defined as bash array.
|
||||
# Should be defined before calling cargo_src_configure().
|
||||
# Should be defined before calling cargo_src_configure.
|
||||
#
|
||||
# Example package that has x11 and wayland as features, and disables default.
|
||||
# Example of a package that has x11 and wayland features and disables default features.
|
||||
# @CODE
|
||||
# src_configure() {
|
||||
# local myfeatures=(
|
||||
@ -105,7 +138,7 @@ ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
|
||||
# }
|
||||
# @CODE
|
||||
|
||||
# @ECLASS-VARIABLE: ECARGO_REGISTRY_DIR
|
||||
# @ECLASS_VARIABLE: ECARGO_REGISTRY_DIR
|
||||
# @USER_VARIABLE
|
||||
# @DEFAULT_UNSET
|
||||
# @DESCRIPTION:
|
||||
@ -114,9 +147,9 @@ ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
|
||||
# This is intended to be set by users.
|
||||
# Ebuilds must not set it.
|
||||
#
|
||||
# Defaults to "${DISTDIR}/cargo-registry" it not set.
|
||||
# Defaults to "${DISTDIR}/cargo-registry" if not set.
|
||||
|
||||
# @ECLASS-VARIABLE: ECARGO_OFFLINE
|
||||
# @ECLASS_VARIABLE: ECARGO_OFFLINE
|
||||
# @USER_VARIABLE
|
||||
# @DEFAULT_UNSET
|
||||
# @DESCRIPTION:
|
||||
@ -124,7 +157,7 @@ ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
|
||||
# cargo_live_src_unpack.
|
||||
# Inherits value of EVCS_OFFLINE if not set explicitly.
|
||||
|
||||
# @ECLASS-VARIABLE: EVCS_UMASK
|
||||
# @ECLASS_VARIABLE: EVCS_UMASK
|
||||
# @USER_VARIABLE
|
||||
# @DEFAULT_UNSET
|
||||
# @DESCRIPTION:
|
||||
@ -134,32 +167,87 @@ ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
|
||||
# group, and then switch over to building with FEATURES=userpriv.
|
||||
# Or vice-versa.
|
||||
|
||||
# @FUNCTION: cargo_crate_uris
|
||||
# @ECLASS_VARIABLE: CARGO_CRATE_URIS
|
||||
# @OUTPUT_VARIABLE
|
||||
# @DESCRIPTION:
|
||||
# List of URIs to put in SRC_URI created from CRATES variable.
|
||||
|
||||
# @FUNCTION: _cargo_set_crate_uris
|
||||
# @USAGE: <crates>
|
||||
# @DESCRIPTION:
|
||||
# Generates the URIs to put in SRC_URI to help fetch dependencies.
|
||||
# Uses first argument as crate list.
|
||||
# If no argument provided, uses CRATES variable.
|
||||
cargo_crate_uris() {
|
||||
# Constructs a list of crates from its arguments.
|
||||
# If no arguments are provided, it uses the CRATES variable.
|
||||
# The value is set as CARGO_CRATE_URIS.
|
||||
_cargo_set_crate_uris() {
|
||||
local -r regex='^([a-zA-Z0-9_\-]+)-([0-9]+\.[0-9]+\.[0-9]+.*)$'
|
||||
local crate crates
|
||||
local crates=${1}
|
||||
local crate
|
||||
|
||||
if [[ -n ${@} ]]; then
|
||||
crates="$@"
|
||||
elif [[ -n ${CRATES} ]]; then
|
||||
crates="${CRATES}"
|
||||
else
|
||||
CARGO_CRATE_URIS=
|
||||
for crate in ${crates}; do
|
||||
local name version url
|
||||
if [[ ${crate} == *@* ]]; then
|
||||
name=${crate%@*}
|
||||
version=${crate##*@}
|
||||
else
|
||||
[[ ${crate} =~ ${regex} ]] ||
|
||||
die "Could not parse name and version from crate: ${crate}"
|
||||
name="${BASH_REMATCH[1]}"
|
||||
version="${BASH_REMATCH[2]}"
|
||||
fi
|
||||
url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${name}-${version}.crate"
|
||||
CARGO_CRATE_URIS+="${url} "
|
||||
done
|
||||
|
||||
if declare -p GIT_CRATES &>/dev/null; then
|
||||
if [[ $(declare -p GIT_CRATES) == "declare -A"* ]]; then
|
||||
local crate commit crate_uri crate_dir repo_ext feat_expr
|
||||
|
||||
for crate in "${!GIT_CRATES[@]}"; do
|
||||
IFS=';' read -r crate_uri commit crate_dir <<< "${GIT_CRATES[${crate}]}"
|
||||
|
||||
case "${crate_uri}" in
|
||||
https://github.com/*)
|
||||
repo_ext=".gh"
|
||||
repo_name="${crate_uri##*/}"
|
||||
crate_uri="${crate_uri%/}/archive/%commit%.tar.gz"
|
||||
;;
|
||||
https://gitlab.com/*)
|
||||
repo_ext=".gl"
|
||||
repo_name="${crate_uri##*/}"
|
||||
crate_uri="${crate_uri%/}/-/archive/%commit%/${repo_name}-%commit%.tar.gz"
|
||||
;;
|
||||
*)
|
||||
repo_ext=
|
||||
repo_name="${crate}"
|
||||
;;
|
||||
esac
|
||||
|
||||
CARGO_CRATE_URIS+="${crate_uri//%commit%/${commit}} -> ${repo_name}-${commit}${repo_ext}.tar.gz "
|
||||
done
|
||||
else
|
||||
die "GIT_CRATE must be declared as an associative array"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
_cargo_set_crate_uris "${CRATES}"
|
||||
|
||||
# @FUNCTION: cargo_crate_uris
|
||||
# @USAGE: [<crates>...]
|
||||
# @DESCRIPTION:
|
||||
# Generates the URIs to put in SRC_URI to help fetch dependencies.
|
||||
# Constructs a list of crates from its arguments.
|
||||
# If no arguments are provided, it uses the CRATES variable.
|
||||
cargo_crate_uris() {
|
||||
local crates=${*-${CRATES}}
|
||||
if [[ -z ${crates} ]]; then
|
||||
eerror "CRATES variable is not defined and nothing passed as argument"
|
||||
die "Can't generate SRC_URI from empty input"
|
||||
fi
|
||||
|
||||
for crate in ${crates}; do
|
||||
local name version url
|
||||
[[ $crate =~ $regex ]] || die "Could not parse name and version from crate: $crate"
|
||||
name="${BASH_REMATCH[1]}"
|
||||
version="${BASH_REMATCH[2]}"
|
||||
url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
|
||||
echo "${url}"
|
||||
done
|
||||
_cargo_set_crate_uris "${crates}"
|
||||
echo "${CARGO_CRATE_URIS}"
|
||||
}
|
||||
|
||||
# @FUNCTION: cargo_gen_config
|
||||
@ -183,7 +271,7 @@ cargo_gen_config() {
|
||||
|
||||
[source.crates-io]
|
||||
replace-with = "gentoo"
|
||||
local-registry = "/nonexistant"
|
||||
local-registry = "/nonexistent"
|
||||
|
||||
[net]
|
||||
offline = true
|
||||
@ -195,15 +283,49 @@ cargo_gen_config() {
|
||||
[term]
|
||||
verbose = true
|
||||
$([[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && echo "color = 'never'")
|
||||
$(_cargo_gen_git_config)
|
||||
_EOF_
|
||||
|
||||
export CARGO_HOME="${ECARGO_HOME}"
|
||||
_CARGO_GEN_CONFIG_HAS_RUN=1
|
||||
}
|
||||
|
||||
# @FUNCTION: _cargo_gen_git_config
|
||||
# @USAGE:
|
||||
# @INTERNAL
|
||||
# @DESCRIPTION:
|
||||
# Generate the cargo config for git crates, this will output the
|
||||
# configuration for cargo to override the cargo config so the local git crates
|
||||
# specified in GIT_CRATES will be used rather than attempting to fetch
|
||||
# from git.
|
||||
#
|
||||
# Called by cargo_gen_config when generating the config.
|
||||
_cargo_gen_git_config() {
|
||||
local git_crates_type
|
||||
git_crates_type="$(declare -p GIT_CRATES 2>&-)"
|
||||
|
||||
if [[ ${git_crates_type} == "declare -A "* ]]; then
|
||||
local crate commit crate_uri crate_dir
|
||||
local -A crate_patches
|
||||
|
||||
for crate in "${!GIT_CRATES[@]}"; do
|
||||
IFS=';' read -r crate_uri commit crate_dir <<< "${GIT_CRATES[${crate}]}"
|
||||
: "${crate_dir:=${crate}-%commit%}"
|
||||
crate_patches["${crate_uri}"]+="${crate} = { path = \"${WORKDIR}/${crate_dir//%commit%/${commit}}\" };;"
|
||||
done
|
||||
|
||||
for crate_uri in "${!crate_patches[@]}"; do
|
||||
printf -- "[patch.'%s']\\n%s\n" "${crate_uri}" "${crate_patches["${crate_uri}"]//;;/$'\n'}"
|
||||
done
|
||||
|
||||
elif [[ -n ${git_crates_type} ]]; then
|
||||
die "GIT_CRATE must be declared as an associative array"
|
||||
fi
|
||||
}
|
||||
|
||||
# @FUNCTION: cargo_src_unpack
|
||||
# @DESCRIPTION:
|
||||
# Unpacks the package and the cargo registry
|
||||
# Unpacks the package and the cargo registry.
|
||||
cargo_src_unpack() {
|
||||
debug-print-function ${FUNCNAME} "$@"
|
||||
|
||||
@ -243,7 +365,8 @@ cargo_src_unpack() {
|
||||
|
||||
# @FUNCTION: cargo_live_src_unpack
|
||||
# @DESCRIPTION:
|
||||
# Runs 'cargo fetch' and vendors downloaded crates for offline use, used in live ebuilds
|
||||
# Runs 'cargo fetch' and vendors downloaded crates for offline use, used in live ebuilds.
|
||||
# NOTE: might require passing --frozen to cargo_src_configure if git dependencies are used.
|
||||
cargo_live_src_unpack() {
|
||||
debug-print-function ${FUNCNAME} "$@"
|
||||
|
||||
@ -255,7 +378,7 @@ cargo_live_src_unpack() {
|
||||
mkdir -p "${ECARGO_HOME}" || die
|
||||
|
||||
local distdir=${PORTAGE_ACTUAL_DISTDIR:-${DISTDIR}}
|
||||
: ${ECARGO_REGISTRY_DIR:=${distdir}/cargo-registry}
|
||||
: "${ECARGO_REGISTRY_DIR:=${distdir}/cargo-registry}"
|
||||
|
||||
local offline="${ECARGO_OFFLINE:-${EVCS_OFFLINE}}"
|
||||
|
||||
@ -284,7 +407,7 @@ cargo_live_src_unpack() {
|
||||
|
||||
pushd "${S}" > /dev/null || die
|
||||
|
||||
# Respect user settings befire cargo_gen_config is called.
|
||||
# Respect user settings before cargo_gen_config is called.
|
||||
if [[ ! ${CARGO_TERM_COLOR} ]]; then
|
||||
[[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && export CARGO_TERM_COLOR=never
|
||||
local unset_color=true
|
||||
@ -309,8 +432,8 @@ cargo_live_src_unpack() {
|
||||
|
||||
# Users may have git checkouts made by cargo.
|
||||
# While cargo vendors the sources, it still needs git checkout to be present.
|
||||
# Copying full dir is an overkill, so just symlink it.
|
||||
if [[ -d ${ECARGO_REGISTRY_DIR}/git ]]; then
|
||||
# Copying full dir is overkill, so just symlink it (guard w/ -L to keep idempotent).
|
||||
if [[ -d ${ECARGO_REGISTRY_DIR}/git && ! -L "${ECARGO_HOME}/git" ]]; then
|
||||
ln -sv "${ECARGO_REGISTRY_DIR}/git" "${ECARGO_HOME}/git" || die
|
||||
fi
|
||||
|
||||
@ -337,10 +460,10 @@ cargo_live_src_unpack() {
|
||||
# will be passed to cargo in all phases.
|
||||
# Make sure all cargo subcommands support flags passed here.
|
||||
#
|
||||
# Example for package that explicitly builds only 'baz' binary and
|
||||
# Example of a package that explicitly builds only 'baz' binary and
|
||||
# enables 'barfeature' and optional 'foo' feature.
|
||||
# will pass '--features barfeature --features foo --bin baz'
|
||||
# in src_{compile,test,install}
|
||||
# It will pass '--features barfeature --features foo --bin baz'
|
||||
# in src_{compile,test,install}.
|
||||
#
|
||||
# @CODE
|
||||
# src_configure() {
|
||||
@ -352,9 +475,13 @@ cargo_live_src_unpack() {
|
||||
# }
|
||||
# @CODE
|
||||
#
|
||||
# In some cases crates may need '--no-default-features' option,
|
||||
# as there is no way to disable single feature, except disabling all.
|
||||
# It can be passed directly to cargo_src_configure().
|
||||
# In some cases crates may need the '--no-default-features' option,
|
||||
# as there is no way to disable a single default feature, except disabling all.
|
||||
# It can be passed directly to cargo_src_configure.
|
||||
#
|
||||
# Some live/9999 ebuild may need the '--frozen' option, if git crates
|
||||
# are used.
|
||||
# Otherwise src_install phase may query network again and fail.
|
||||
cargo_src_configure() {
|
||||
debug-print-function ${FUNCNAME} "$@"
|
||||
|
||||
@ -379,13 +506,14 @@ cargo_src_configure() {
|
||||
|
||||
# @FUNCTION: cargo_src_compile
|
||||
# @DESCRIPTION:
|
||||
# Build the package using cargo build
|
||||
# Build the package using cargo build.
|
||||
cargo_src_compile() {
|
||||
debug-print-function ${FUNCNAME} "$@"
|
||||
|
||||
[[ ${_CARGO_GEN_CONFIG_HAS_RUN} ]] || \
|
||||
die "FATAL: please call cargo_gen_config before using ${FUNCNAME}"
|
||||
|
||||
filter-lto
|
||||
tc-export AR CC CXX PKG_CONFIG
|
||||
|
||||
set -- cargo build $(usex debug "" --release) ${ECARGO_ARGS[@]} "$@"
|
||||
@ -395,10 +523,10 @@ cargo_src_compile() {
|
||||
|
||||
# @FUNCTION: cargo_src_install
|
||||
# @DESCRIPTION:
|
||||
# Installs the binaries generated by cargo
|
||||
# In come case workspaces need alternative --path parameter
|
||||
# default is '--path ./' if nothing specified.
|
||||
# '--path ./somedir' can be passed directly to cargo_src_install()
|
||||
# Installs the binaries generated by cargo.
|
||||
# In come cases workspaces need an alternative --path parameter.
|
||||
# Defaults to '--path ./' if no path is specified.
|
||||
# '--path ./somedir' can be passed directly to cargo_src_install.
|
||||
cargo_src_install() {
|
||||
debug-print-function ${FUNCNAME} "$@"
|
||||
|
||||
@ -407,6 +535,7 @@ cargo_src_install() {
|
||||
|
||||
set -- cargo install $(has --path ${@} || echo --path ./) \
|
||||
--root "${ED}/usr" \
|
||||
${GIT_CRATES[@]:+--frozen} \
|
||||
$(usex debug --debug "") \
|
||||
${ECARGO_ARGS[@]} "$@"
|
||||
einfo "${@}"
|
||||
@ -429,7 +558,7 @@ cargo_src_install() {
|
||||
|
||||
# @FUNCTION: cargo_src_test
|
||||
# @DESCRIPTION:
|
||||
# Test the package using cargo test
|
||||
# Test the package using cargo test.
|
||||
cargo_src_test() {
|
||||
debug-print-function ${FUNCNAME} "$@"
|
||||
|
||||
@ -442,3 +571,7 @@ cargo_src_test() {
|
||||
}
|
||||
|
||||
fi
|
||||
|
||||
if [[ ! ${CARGO_OPTIONAL} ]]; then
|
||||
EXPORT_FUNCTIONS src_unpack src_configure src_compile src_install src_test
|
||||
fi
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user