simple src_configure implementation inspired by cmake.eclass

Closes: https://bugs.gentoo.org/721936

Signed-off-by: Georgy Yakovlev <gyakov...@gentoo.org>
---
 eclass/cargo.eclass | 77 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 70 insertions(+), 7 deletions(-)

diff --git a/eclass/cargo.eclass b/eclass/cargo.eclass
index ccbf87aa9a6..77c8e90755b 100644
--- a/eclass/cargo.eclass
+++ b/eclass/cargo.eclass
@@ -22,7 +22,7 @@ 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"
 
@@ -34,6 +34,29 @@ ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
 # Allows overriding the default cwd to run cargo install from
 : ${CARGO_INSTALL_PATH:=.}
 
+# @VARIABLE: myfeatures
+# @DEFAULT_UNSET
+# @DESCRIPTION:
+# Optional cargo features defined as bash array. Should be defined before 
calling
+# src_configure.
+# If this array is not empty, --no-default-features is passed to cargo.
+# To enable default crate features in that case you can add 'default' to the 
array.
+# 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 has x11 and wayland as features, and enables 
default set.
+# @CODE
+# src_configure() {
+#      local myfeatures=(
+#              default
+#              $(usex X x11 '')
+#              $(usev wayland)
+#      )
+#      cargo_src_configure
+# }
+# @CODE
+
 # @FUNCTION: cargo_crate_uris
 # @DESCRIPTION:
 # Generates the URIs to put in SRC_URI to help fetch dependencies.
@@ -112,6 +135,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 +175,47 @@ 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.
+# Example for package that explicitly builds only 'foo' binary and
+# enables single feature 'barfeature', disabling default feature set.
+# will pass '--no-default-features --features barfeature --bin foo'
+# in src_{compile,test,install}
+# @CODE
+# src_configure() {
+#      local myfeatures=(
+#              barfeature
+#      )
+#      cargo_src_configure --bin foo
+# }
+# @CODE
+
+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 } )
+
+       # prepend --no-default features if myfeatures array is not empty, 
append extra args
+       readonly ECARGO_ARGS=( ${myfeatures:+--no-default-features 
${myfeatures[@]}} ${@} )
+
+       [[ ${ECARGO_ARGS[@]} ]] && einfo "Configured with: ${ECARGO_ARGS[@]}"
 }
 
 # @FUNCTION: cargo_src_compile
@@ -159,11 +224,9 @@ cargo_gen_config() {
 cargo_src_compile() {
        debug-print-function ${FUNCNAME} "$@"
 
-       export CARGO_HOME="${ECARGO_HOME}"
-
        tc-export AR CC
 
-       cargo build $(usex debug "" --release) "$@" \
+       cargo build $(usex debug "" --release) ${ECARGO_ARGS[@]} "$@" \
                || die "cargo build failed"
 }
 
@@ -173,8 +236,8 @@ cargo_src_compile() {
 cargo_src_install() {
        debug-print-function ${FUNCNAME} "$@"
 
-       cargo install --path ${CARGO_INSTALL_PATH} \
-               --root="${ED}/usr" $(usex debug --debug "") "$@" \
+       cargo install --path ${CARGO_INSTALL_PATH} --root="${ED}/usr" \
+               $(usex debug --debug "") ${ECARGO_ARGS[@]} "$@" \
                || die "cargo install failed"
        rm -f "${ED}/usr/.crates.toml"
        rm -f "${ED}/usr/.crates2.json"
@@ -188,7 +251,7 @@ cargo_src_install() {
 cargo_src_test() {
        debug-print-function ${FUNCNAME} "$@"
 
-       cargo test $(usex debug "" --release) "$@" \
+       cargo test $(usex debug "" --release) ${ECARGO_ARGS[@]} "$@" \
                || die "cargo test failed"
 }
 
-- 
2.27.0


Reply via email to