summaryrefslogtreecommitdiff
path: root/eclass/cargo.eclass
diff options
context:
space:
mode:
authorV3n3RiX <venerix@redcorelinux.org>2020-09-30 17:27:54 +0100
committerV3n3RiX <venerix@redcorelinux.org>2020-09-30 17:27:54 +0100
commitf70a1bfc721336d4fc7dfb711c2f518a6b18cf16 (patch)
treee907cb121b30e3c1df1710719c0ddf4029597a47 /eclass/cargo.eclass
parentdb063b515939ab15261136b24e4bc44386335c0c (diff)
gentoo resync : 30.09.2020
Diffstat (limited to 'eclass/cargo.eclass')
-rw-r--r--eclass/cargo.eclass177
1 files changed, 143 insertions, 34 deletions
diff --git a/eclass/cargo.eclass b/eclass/cargo.eclass
index 6d341601a112..e6fec844d274 100644
--- a/eclass/cargo.eclass
+++ b/eclass/cargo.eclass
@@ -46,6 +46,35 @@ ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
# }
# @CODE
+# @ECLASS-VARIABLE: ECARGO_REGISTRY_DIR
+# @USER_VARIABLE
+# @DEFAULT_UNSET
+# @DESCRIPTION:
+# Storage directory for cargo registry.
+# Used by cargo_live_src_unpack to cache downloads.
+# This is intended to be set by users.
+# Ebuilds must not set it.
+#
+# Defaults to "${DISTDIR}/cargo-registry" it not set.
+
+# @ECLASS-VARIABLE: ECARGO_OFFLINE
+# @USER_VARIABLE
+# @DEFAULT_UNSET
+# @DESCRIPTION:
+# If non-empty, this variable prevents online operations in
+# cargo_live_src_unpack.
+# Inherits value of EVCS_OFFLINE if not set explicitly.
+
+# @ECLASS-VARIABLE: EVCS_UMASK
+# @USER_VARIABLE
+# @DEFAULT_UNSET
+# @DESCRIPTION:
+# Set this variable to a custom umask. This is intended to be set by
+# users. By setting this to something like 002, it can make life easier
+# for people who use cargo in a home directory, but are in the portage
+# group, and then switch over to building with FEATURES=userpriv.
+# Or vice-versa.
+
# @FUNCTION: cargo_crate_uris
# @DESCRIPTION:
# Generates the URIs to put in SRC_URI to help fetch dependencies.
@@ -62,6 +91,44 @@ cargo_crate_uris() {
done
}
+# @FUNCTION: cargo_gen_config
+# @DESCRIPTION:
+# Generate the $CARGO_HOME/config necessary to use our local registry and settings.
+# Cargo can also be configured through environment variables in addition to the TOML syntax below.
+# For each configuration key below of the form foo.bar the environment variable CARGO_FOO_BAR
+# can also be used to define the value.
+# Environment variables will take precedent over TOML configuration,
+# and currently only integer, boolean, and string keys are supported.
+# For example the build.jobs key can also be defined by CARGO_BUILD_JOBS.
+# Or setting CARGO_TERM_VERBOSE=false in make.conf will make build quieter.
+cargo_gen_config() {
+ debug-print-function ${FUNCNAME} "$@"
+
+ mkdir -p "${ECARGO_HOME}" || die
+
+ cat > "${ECARGO_HOME}/config" <<- _EOF_ || die "Failed to create cargo config"
+ [source.gentoo]
+ directory = "${ECARGO_VENDOR}"
+
+ [source.crates-io]
+ replace-with = "gentoo"
+ local-registry = "/nonexistant"
+
+ [net]
+ offline = true
+
+ [build]
+ jobs = $(makeopts_jobs)
+
+ [term]
+ verbose = true
+ $([[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && echo "color = 'never'")
+ _EOF_
+
+ export CARGO_HOME="${ECARGO_HOME}"
+ _CARGO_GEN_CONFIG_HAS_RUN=1
+}
+
# @FUNCTION: cargo_src_unpack
# @DESCRIPTION:
# Unpacks the package and the cargo registry
@@ -122,50 +189,83 @@ cargo_live_src_unpack() {
[[ "${EBUILD_PHASE}" == unpack ]] || die "${FUNCNAME} only allowed in src_unpack"
mkdir -p "${S}" || die
+ mkdir -p "${ECARGO_VENDOR}" || die
+ mkdir -p "${ECARGO_HOME}" || die
+
+ local distdir=${PORTAGE_ACTUAL_DISTDIR:-${DISTDIR}}
+ : ${ECARGO_REGISTRY_DIR:=${distdir}/cargo-registry}
+
+ local offline="${ECARGO_OFFLINE:-${EVCS_OFFLINE}}"
+
+ if [[ ! -d ${ECARGO_REGISTRY_DIR} && ! ${offline} ]]; then
+ (
+ addwrite "${ECARGO_REGISTRY_DIR}"
+ mkdir -p "${ECARGO_REGISTRY_DIR}"
+ ) || die "Unable to create ${ECARGO_REGISTRY_DIR}"
+ fi
+
+ if [[ ${offline} ]]; then
+ local subdir
+ for subdir in cache index src; do
+ if [[ ! -d ${ECARGO_REGISTRY_DIR}/registry/${subdir} ]]; then
+ eerror "Networking activity has been disabled via ECARGO_OFFLINE or EVCS_OFFLINE"
+ eerror "However, no valid cargo registry available at ${ECARGO_REGISTRY_DIR}"
+ die "Unable to proceed with ECARGO_OFFLINE/EVCS_OFFLINE."
+ fi
+ done
+ fi
+
+ if [[ ${EVCS_UMASK} ]]; then
+ local saved_umask=$(umask)
+ umask "${EVCS_UMASK}" || die "Bad options to umask: ${EVCS_UMASK}"
+ fi
pushd "${S}" > /dev/null || die
- # need to specify CARGO_HOME before cargo_gen_config fired
- CARGO_HOME="${ECARGO_HOME}" cargo fetch || die
- CARGO_HOME="${ECARGO_HOME}" cargo vendor "${ECARGO_VENDOR}" || die
- popd > /dev/null || die
- cargo_gen_config
-}
+ # Respect user settings befire cargo_gen_config is called.
+ if [[ ! ${CARGO_TERM_COLOR} ]]; then
+ [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && export CARGO_TERM_COLOR=never
+ local unset_color=true
+ fi
+ if [[ ! ${CARGO_TERM_VERBOSE} ]]; then
+ export CARGO_TERM_VERBOSE=true
+ local unset_verbose=true
+ fi
-# @FUNCTION: cargo_gen_config
-# @DESCRIPTION:
-# Generate the $CARGO_HOME/config necessary to use our local registry and settings.
-# Cargo can also be configured through environment variables in addition to the TOML syntax below.
-# For each configuration key below of the form foo.bar the environment variable CARGO_FOO_BAR
-# can also be used to define the value.
-# Environment variables will take precedent over TOML configuration,
-# and currently only integer, boolean, and string keys are supported.
-# For example the build.jobs key can also be defined by CARGO_BUILD_JOBS.
-# Or setting CARGO_TERM_VERBOSE=false in make.conf will make build quieter.
-cargo_gen_config() {
- debug-print-function ${FUNCNAME} "$@"
+ # Let cargo fetch to system-wide location.
+ # It will keep directory organized by itself.
+ addwrite "${ECARGO_REGISTRY_DIR}"
+ export CARGO_HOME="${ECARGO_REGISTRY_DIR}"
- cat <<- EOF > "${ECARGO_HOME}/config"
- [source.gentoo]
- directory = "${ECARGO_VENDOR}"
+ # Absence of quotes around offline arg is intentional, as cargo bails out if it encounters ''
+ einfo "cargo fetch ${offline:+--offline}"
+ cargo fetch ${offline:+--offline} || die #nowarn
- [source.crates-io]
- replace-with = "gentoo"
- local-registry = "/nonexistant"
+ # Let cargo copy all required crates to "${WORKDIR}" for offline use in later phases.
+ einfo "cargo vendor ${offline:+--offline} ${ECARGO_VENDOR}"
+ cargo vendor ${offline:+--offline} "${ECARGO_VENDOR}" || die #nowarn
- [net]
- offline = true
+ # 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
+ ln -sv "${ECARGO_REGISTRY_DIR}/git" "${ECARGO_HOME}/git" || die
+ fi
- [build]
- jobs = $(makeopts_jobs)
+ popd > /dev/null || die
- [term]
- verbose = true
- EOF
- # honor NOCOLOR setting
- [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && echo "color = 'never'" >> "${ECARGO_HOME}/config"
+ # Restore settings if needed.
+ [[ ${unset_color} ]] && unset CARGO_TERM_COLOR
+ [[ ${unset_verbose} ]] && unset CARGO_TERM_VERBOSE
+ if [[ ${saved_umask} ]]; then
+ umask "${saved_umask}" || die
+ fi
- export CARGO_HOME="${ECARGO_HOME}"
+ # After following calls, cargo will no longer use ${ECARGO_REGISTRY_DIR} as CARGO_HOME
+ # It will be forced into offline mode to prevent network access.
+ # But since we already vendored crates and symlinked git, it has all it needs to build.
+ unset CARGO_HOME
+ cargo_gen_config
}
# @FUNCTION: cargo_src_configure
@@ -222,6 +322,9 @@ cargo_src_configure() {
cargo_src_compile() {
debug-print-function ${FUNCNAME} "$@"
+ [[ ${_CARGO_GEN_CONFIG_HAS_RUN} ]] || \
+ die "FATAL: please call cargo_gen_config before using ${FUNCNAME}"
+
tc-export AR CC CXX
set -- cargo build $(usex debug "" --release) ${ECARGO_ARGS[@]} "$@"
@@ -239,6 +342,9 @@ cargo_src_compile() {
cargo_src_install() {
debug-print-function ${FUNCNAME} "$@"
+ [[ ${_CARGO_GEN_CONFIG_HAS_RUN} ]] || \
+ die "FATAL: please call cargo_gen_config before using ${FUNCNAME}"
+
set -- cargo install $(has --path ${@} || echo --path ./) \
--root "${ED}/usr" \
$(usex debug --debug "") \
@@ -258,6 +364,9 @@ cargo_src_install() {
cargo_src_test() {
debug-print-function ${FUNCNAME} "$@"
+ [[ ${_CARGO_GEN_CONFIG_HAS_RUN} ]] || \
+ die "FATAL: please call cargo_gen_config before using ${FUNCNAME}"
+
set -- cargo test $(usex debug "" --release) ${ECARGO_ARGS[@]} "$@"
einfo "${@}"
"${@}" || die "cargo test failed"