From fa7e229eb3223d7f574f9872ad75de3739b1cb6e Mon Sep 17 00:00:00 2001 From: Fabio Valentini Date: Feb 16 2024 19:28:30 +0000 Subject: add macros for overriding installation of crate sources and binaries --- diff --git a/macros.d/macros.cargo b/macros.d/macros.cargo index e99f5a6..293fe5a 100644 --- a/macros.d/macros.cargo +++ b/macros.d/macros.cargo @@ -42,6 +42,22 @@ # the current working directory is a "binary" crate %__cargo_is_bin() [ $(%{__cargo_to_rpm} --path Cargo.toml is-bin) -eq 1 ] +# cargo_install_lib: macro to control whether library sources are installed +# by the cargo_install macro +# +# If this macro is set to 0 in a spec file, then crate sources will not be +# installed to crate_instdir even if a library target is auto-detected or the +# crate defines a [lib] target explicitly. +%cargo_install_lib 1 + +# cargo_install_bin: macro to control whether binary targets are installed +# by the cargo_install macro +# +# If this macro is set to 0 in a spec file, then no executables will be +# installed to _bindir even if binary targets are auto-detected or the crate +# defines one or more [[bin]] targets explicitly. +%cargo_install_bin 1 + # cargo_prep: macro to set up build environment for cargo projects # # This involves four steps: @@ -171,7 +187,7 @@ EOF}}\ %cargo_install(t:naf:)\ (\ set -euo pipefail \ -if %{__cargo_is_lib} ; then \ +if %{__cargo_is_lib} && [ %{cargo_install_lib} -eq 1 ] ; then \ CRATE_NAME=$(%{__cargo_to_rpm} --path Cargo.toml name) \ CRATE_VERSION=$(%{__cargo_to_rpm} --path Cargo.toml version) \ REG_DIR=%{buildroot}%{cargo_registry}/$CRATE_NAME-$CRATE_VERSION \ @@ -183,7 +199,7 @@ if %{__cargo_is_lib} ; then \ %{__rm} -f $REG_DIR/Cargo.toml.deps \ echo '{"files":{},"package":""}' > $REG_DIR/.cargo-checksum.json \ fi \ -if %{__cargo_is_bin} ; then \ +if %{__cargo_is_bin} && [ %{cargo_install_bin} -eq 1 ] ; then \ %{shrink: \ %{__cargo} install \ %{__cargo_common_opts} \ diff --git a/tests/test_macros_cargo.py b/tests/test_macros_cargo.py index e02c45c..e67ea04 100644 --- a/tests/test_macros_cargo.py +++ b/tests/test_macros_cargo.py @@ -144,7 +144,7 @@ def test_cargo_prep_vendor(evaluater): "", "cat >> .cargo/config << EOF", "[source.vendored-sources]", - f'directory = "vendor"', + 'directory = "vendor"', "", "[source.crates-io]", 'registry = "https://crates.io"', @@ -281,24 +281,24 @@ def test_cargo_install(evaluater): awk = evaluater("%__awk")[0] assert [line.rstrip() for line in evaluater("%cargo_install")[0].splitlines()] == [ - f"(", - f"set -euo pipefail", - f"if {cargo_is_lib} ; then", + "(", + "set -euo pipefail", + f"if {cargo_is_lib} && [ 1 -eq 1 ] ; then", f" CRATE_NAME=$({cargo_to_rpm} --path Cargo.toml name)", f" CRATE_VERSION=$({cargo_to_rpm} --path Cargo.toml version)", f" REG_DIR={buildroot}{cargo_registry}/$CRATE_NAME-$CRATE_VERSION", - f" /usr/bin/mkdir -p $REG_DIR", + " /usr/bin/mkdir -p $REG_DIR", f" {awk} -i inplace -v INPLACE_SUFFIX=.deps '/^\\[((.+\\.)?((dev|build)-)?dependencies|features)/{{f=1;next}} /^\\[/{{f=0}}; !f' Cargo.toml", f" {cargo} package -l | grep -w -E -v 'Cargo.(lock|toml.orig)' | xargs -d '\\n' /usr/bin/cp --parents -a -t $REG_DIR", - f" /usr/bin/mv Cargo.toml{{.deps,}}", - f" /usr/bin/cp -a Cargo.toml $REG_DIR/Cargo.toml", - f" /usr/bin/rm -f $REG_DIR/Cargo.toml.deps", - f' echo \'{{"files":{{}},"package":""}}\' > $REG_DIR/.cargo-checksum.json', - f"fi", - f"if {cargo_is_bin} ; then", + " /usr/bin/mv Cargo.toml{.deps,}", + " /usr/bin/cp -a Cargo.toml $REG_DIR/Cargo.toml", + " /usr/bin/rm -f $REG_DIR/Cargo.toml.deps", + ' echo \'{"files":{},"package":""}\' > $REG_DIR/.cargo-checksum.json', + "fi", + f"if {cargo_is_bin} && [ 1 -eq 1 ] ; then", f" {cargo} install {cargo_common_opts} --profile rpm --no-track --path .", - f"fi", - f")", + "fi", + ")", ]