commit:     be8bafc57aff94f0f708726805bccd44ddcd30a8
Author:     Georgy Yakovlev <gyakovlev <AT> gentoo <DOT> org>
AuthorDate: Sat Jun 13 00:56:33 2020 +0000
Commit:     Georgy Yakovlev <gyakovlev <AT> gentoo <DOT> org>
CommitDate: Wed Sep 23 05:58:47 2020 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=be8bafc5

eclass/cargo.eclass: add cargo_src_configure (revised)

simple src_configure implementation inspired by cmake.eclass
plus some nice improvements to eclass.

Closes: https://bugs.gentoo.org/721936
Signed-off-by: Georgy Yakovlev <gyakovlev <AT> gentoo.org>

 eclass/cargo.eclass | 103 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 87 insertions(+), 16 deletions(-)

diff --git a/eclass/cargo.eclass b/eclass/cargo.eclass
index ccbf87aa9a6..6d341601a11 100644
--- a/eclass/cargo.eclass
+++ b/eclass/cargo.eclass
@@ -22,17 +22,29 @@ esac
 
 inherit multiprocessing toolchain-funcs
 
-EXPORT_FUNCTIONS src_unpack src_compile src_install src_test
+EXPORT_FUNCTIONS src_unpack src_configure src_compile src_install src_test
 
 IUSE="${IUSE} debug"
 
 ECARGO_HOME="${WORKDIR}/cargo_home"
 ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
 
-# @ECLASS-VARIABLE: CARGO_INSTALL_PATH
+# @VARIABLE: myfeatures
+# @DEFAULT_UNSET
 # @DESCRIPTION:
-# Allows overriding the default cwd to run cargo install from
-: ${CARGO_INSTALL_PATH:=.}
+# Optional cargo features defined as bash array.
+# Should be defined before calling cargo_src_configure().
+#
+# Example package that has x11 and wayland as features, and disables default.
+# @CODE
+# src_configure() {
+#      local myfeatures=(
+#              $(usex X x11 '')
+#              $(usev wayland)
+#      )
+#      cargo_src_configure --no-default-features
+# }
+# @CODE
 
 # @FUNCTION: cargo_crate_uris
 # @DESCRIPTION:
@@ -112,6 +124,7 @@ cargo_live_src_unpack() {
        mkdir -p "${S}" || die
 
        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
@@ -151,6 +164,56 @@ cargo_gen_config() {
        EOF
        # honor NOCOLOR setting
        [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && echo "color = 
'never'" >> "${ECARGO_HOME}/config"
+
+       export CARGO_HOME="${ECARGO_HOME}"
+}
+
+# @FUNCTION: cargo_src_configure
+# @DESCRIPTION:
+# Configure cargo package features and arguments.
+# Extra positional arguments supplied to this function
+# 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
+# enables 'barfeature' and optional 'foo' feature.
+# will pass '--features barfeature --features foo --bin baz'
+# in src_{compile,test,install}
+#
+# @CODE
+# src_configure() {
+#      local myfeatures=(
+#              barfeature
+#              $(usev foo)
+#      )
+#      cargo_src_configure --bin baz
+# }
+# @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().
+
+cargo_src_configure() {
+       debug-print-function ${FUNCNAME} "$@"
+
+       [[ -z ${myfeatures} ]] && declare -a myfeatures=()
+       local myfeaturestype=$(declare -p myfeatures 2>&-)
+       if [[ "${myfeaturestype}" != "declare -a myfeatures="* ]]; then
+               die "myfeatures must be declared as array"
+       fi
+
+       # transform array from simple feature list
+       # to multiple cargo args:
+       # --features feature1 --features feature2 ...
+       # this format is chosen because 2 other methods of
+       # listing features (space OR comma separated) require
+       # more fiddling with strings we'd like to avoid here.
+       myfeatures=( ${myfeatures[@]/#/--features } )
+
+       readonly ECARGO_ARGS=( ${myfeatures[@]} ${@} ${ECARGO_EXTRA_ARGS} )
+
+       [[ ${ECARGO_ARGS[@]} ]] && einfo "Configured with: ${ECARGO_ARGS[@]}"
 }
 
 # @FUNCTION: cargo_src_compile
@@ -159,25 +222,32 @@ cargo_gen_config() {
 cargo_src_compile() {
        debug-print-function ${FUNCNAME} "$@"
 
-       export CARGO_HOME="${ECARGO_HOME}"
+       tc-export AR CC CXX
 
-       tc-export AR CC
-
-       cargo build $(usex debug "" --release) "$@" \
-               || die "cargo build failed"
+       set -- cargo build $(usex debug "" --release) ${ECARGO_ARGS[@]} "$@"
+       einfo "${@}"
+       "${@}" || die "cargo build failed"
 }
 
 # @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()
+
 cargo_src_install() {
        debug-print-function ${FUNCNAME} "$@"
 
-       cargo install --path ${CARGO_INSTALL_PATH} \
-               --root="${ED}/usr" $(usex debug --debug "") "$@" \
-               || die "cargo install failed"
-       rm -f "${ED}/usr/.crates.toml"
-       rm -f "${ED}/usr/.crates2.json"
+       set -- cargo install $(has --path ${@} || echo --path ./) \
+               --root "${ED}/usr" \
+               $(usex debug --debug "") \
+               ${ECARGO_ARGS[@]} "$@"
+       einfo "${@}"
+       "${@}" || die "cargo install failed"
+
+       rm -f "${ED}/usr/.crates.toml" || die
+       rm -f "${ED}/usr/.crates2.json" || die
 
        [ -d "${S}/man" ] && doman "${S}/man" || return 0
 }
@@ -188,8 +258,9 @@ cargo_src_install() {
 cargo_src_test() {
        debug-print-function ${FUNCNAME} "$@"
 
-       cargo test $(usex debug "" --release) "$@" \
-               || die "cargo test failed"
+       set -- cargo test $(usex debug "" --release) ${ECARGO_ARGS[@]} "$@"
+       einfo "${@}"
+       "${@}" || die "cargo test failed"
 }
 
 fi

Reply via email to