diff mbox series

[1/1] package/pkg-cargo: configure cargo profiles

Message ID 20221021224448.3502942-1-james.hilliard1@gmail.com
State Accepted
Headers show
Series [1/1] package/pkg-cargo: configure cargo profiles | expand

Commit Message

James Hilliard Oct. 21, 2022, 10:44 p.m. UTC
The cargo profile flags currently only select a cargo build flag which
does not accurately reflect the configured build settings, setting the
cargo build flag also only works for packages using the cargo
infrastructure directly and not with other packages using the cargo
environment such as pyo3 based python packages(which indirectly invoke
cargo build via the setuptools-rust and maturn python build backends).

To ensure that cargo uses the correct profile settings for the build
we need to set env variables for the two root profiles(dev/release)
based on the buildroot build settings.

For reference the default cargo root profile settings are:
https://doc.rust-lang.org/cargo/reference/profiles.html#default-profiles

As applications not using cargo infrastructure do not have a standard
way of configuring a specific build profile due to the selected
profile being determined by the cargo command line flags, the only
reliable way to configure profile settings is to set both root
profiles in the environment as this will ensure that they are used
regardless of which cargo build flag(such as --debug or --release) is
used.

Cc: Moritz Bitsch <moritz@h6t.eu>
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 package/pkg-cargo.mk | 100 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 100 insertions(+)

Comments

Moritz Bitsch Oct. 24, 2022, 5:43 p.m. UTC | #1
Am 22.10.22 um 00:44 schrieb James Hilliard:
> The cargo profile flags currently only select a cargo build flag which
> does not accurately reflect the configured build settings, setting the
> cargo build flag also only works for packages using the cargo
> infrastructure directly and not with other packages using the cargo
> environment such as pyo3 based python packages(which indirectly invoke
> cargo build via the setuptools-rust and maturn python build backends).
> 
> To ensure that cargo uses the correct profile settings for the build
> we need to set env variables for the two root profiles(dev/release)
> based on the buildroot build settings.
> 
> For reference the default cargo root profile settings are:
> https://doc.rust-lang.org/cargo/reference/profiles.html#default-profiles
> 
> As applications not using cargo infrastructure do not have a standard
> way of configuring a specific build profile due to the selected
> profile being determined by the cargo command line flags, the only
> reliable way to configure profile settings is to set both root
> profiles in the environment as this will ensure that they are used
> regardless of which cargo build flag(such as --debug or --release) is
> used.
> 
> Cc: Moritz Bitsch <moritz@h6t.eu>
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>

Tested-by: Moritz Bitsch <moritz@h6t.eu>

> ---
>   package/pkg-cargo.mk | 100 +++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 100 insertions(+)
> 
> diff --git a/package/pkg-cargo.mk b/package/pkg-cargo.mk
> index f7e3f39503..4945770079 100644
> --- a/package/pkg-cargo.mk
> +++ b/package/pkg-cargo.mk
> @@ -46,6 +46,106 @@ PKG_CARGO_ENV = \
>   	CARGO_BUILD_TARGET="$(RUSTC_TARGET_NAME)" \
>   	CARGO_TARGET_$(call UPPERCASE,$(RUSTC_TARGET_NAME))_LINKER=$(notdir $(TARGET_CROSS))gcc
>   
> +# We always set both CARGO_PROFILE_DEV and CARGO_PROFILE_RELEASE
> +# as we are unable to select a build profile using the environment.
> +#
> +# Other cargo profiles generally derive from these two profiles.
> +
> +# Disable incremental compilation to match release default.
> +#
> +# Set codegen-units to release default.
> +#
> +# Set split-debuginfo to default off for ELF platforms.
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_INCREMENTAL="false" \
> +	CARGO_PROFILE_RELEASE_INCREMENTAL="false" \
> +	CARGO_PROFILE_DEV_CODEGEN_UNITS="16" \
> +	CARGO_PROFILE_RELEASE_CODEGEN_UNITS="16" \
> +	CARGO_PROFILE_DEV_SPLIT_DEBUGINFO="off" \
> +	CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO="off"
> +
> +# Set the optimization level with the release default as fallback.
> +ifeq ($(BR2_OPTIMIZE_0),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_OPT_LEVEL="0" \
> +	CARGO_PROFILE_RELEASE_OPT_LEVEL="0"
> +else ifeq ($(BR2_OPTIMIZE_1),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_OPT_LEVEL="1" \
> +	CARGO_PROFILE_RELEASE_OPT_LEVEL="1"
> +else ifeq ($(BR2_OPTIMIZE_2),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_OPT_LEVEL="2" \
> +	CARGO_PROFILE_RELEASE_OPT_LEVEL="2"
> +else ifeq ($(BR2_OPTIMIZE_3),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_OPT_LEVEL="3" \
> +	CARGO_PROFILE_RELEASE_OPT_LEVEL="3"
> +else ifeq ($(BR2_OPTIMIZE_G),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_OPT_LEVEL="0" \
> +	CARGO_PROFILE_RELEASE_OPT_LEVEL="0"
> +else ifeq ($(BR2_OPTIMIZE_S),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_OPT_LEVEL="s" \
> +	CARGO_PROFILE_RELEASE_OPT_LEVEL="s"
> +else ifeq ($(BR2_OPTIMIZE_FAST),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_OPT_LEVEL="3" \
> +	CARGO_PROFILE_RELEASE_OPT_LEVEL="3"
> +else
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_OPT_LEVEL="3" \
> +	CARGO_PROFILE_RELEASE_OPT_LEVEL="3"
> +endif
> +
> +ifeq ($(BR2_ENABLE_LTO),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_LTO="true" \
> +	CARGO_PROFILE_RELEASE_LTO="true"
> +else
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_LTO="false" \
> +	CARGO_PROFILE_RELEASE_LTO="false"
> +endif
> +
> +
> +ifeq ($(BR2_ENABLE_DEBUG),y)
> +ifeq ($(BR2_DEBUG_3),y)
> +# full debug info
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_DEBUG="2" \
> +	CARGO_PROFILE_RELEASE_DEBUG="2"
> +else
> +# line tables only
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_DEBUG="1" \
> +	CARGO_PROFILE_RELEASE_DEBUG="1"
> +endif
> +else
> +# no debug info
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_DEBUG="0" \
> +	CARGO_PROFILE_RELEASE_DEBUG="0"
> +endif
> +
> +# Enabling debug-assertions enables the runtime debug_assert! macro.
> +#
> +# Enabling overflow-checks enables runtime panic on integer overflow.
> +ifeq ($(BR2_ENABLE_RUNTIME_DEBUG),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_DEBUG_ASSERTIONS="true" \
> +	CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS="true" \
> +	CARGO_PROFILE_DEV_OVERFLOW_CHECKS="true" \
> +	CARGO_PROFILE_RELEASE_OVERFLOW_CHECKS="true"
> +else
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_DEBUG_ASSERTIONS="false" \
> +	CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS="false" \
> +	CARGO_PROFILE_DEV_OVERFLOW_CHECKS="false" \
> +	CARGO_PROFILE_RELEASE_OVERFLOW_CHECKS="false"
> +endif
> +
>   #
>   # This is a workaround for https://github.com/rust-lang/compiler-builtins/issues/420
>   # and should be removed when fixed upstream
James Hilliard Nov. 7, 2023, 9:19 p.m. UTC | #2
On Fri, Oct 21, 2022 at 4:45 PM James Hilliard
<james.hilliard1@gmail.com> wrote:
>
> The cargo profile flags currently only select a cargo build flag which
> does not accurately reflect the configured build settings, setting the
> cargo build flag also only works for packages using the cargo
> infrastructure directly and not with other packages using the cargo
> environment such as pyo3 based python packages(which indirectly invoke
> cargo build via the setuptools-rust and maturn python build backends).
>
> To ensure that cargo uses the correct profile settings for the build
> we need to set env variables for the two root profiles(dev/release)
> based on the buildroot build settings.
>
> For reference the default cargo root profile settings are:
> https://doc.rust-lang.org/cargo/reference/profiles.html#default-profiles
>
> As applications not using cargo infrastructure do not have a standard
> way of configuring a specific build profile due to the selected
> profile being determined by the cargo command line flags, the only
> reliable way to configure profile settings is to set both root
> profiles in the environment as this will ensure that they are used
> regardless of which cargo build flag(such as --debug or --release) is
> used.
>
> Cc: Moritz Bitsch <moritz@h6t.eu>
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
Tested-by: Adam Duskett <adam.duskett@amarulasolutions.com>
Reviewed-by: Adam Duskett <adam.duskett@amarulasolutions.com>
> ---
>  package/pkg-cargo.mk | 100 +++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 100 insertions(+)
>
> diff --git a/package/pkg-cargo.mk b/package/pkg-cargo.mk
> index f7e3f39503..4945770079 100644
> --- a/package/pkg-cargo.mk
> +++ b/package/pkg-cargo.mk
> @@ -46,6 +46,106 @@ PKG_CARGO_ENV = \
>         CARGO_BUILD_TARGET="$(RUSTC_TARGET_NAME)" \
>         CARGO_TARGET_$(call UPPERCASE,$(RUSTC_TARGET_NAME))_LINKER=$(notdir $(TARGET_CROSS))gcc
>
> +# We always set both CARGO_PROFILE_DEV and CARGO_PROFILE_RELEASE
> +# as we are unable to select a build profile using the environment.
> +#
> +# Other cargo profiles generally derive from these two profiles.
> +
> +# Disable incremental compilation to match release default.
> +#
> +# Set codegen-units to release default.
> +#
> +# Set split-debuginfo to default off for ELF platforms.
> +PKG_CARGO_ENV += \
> +       CARGO_PROFILE_DEV_INCREMENTAL="false" \
> +       CARGO_PROFILE_RELEASE_INCREMENTAL="false" \
> +       CARGO_PROFILE_DEV_CODEGEN_UNITS="16" \
> +       CARGO_PROFILE_RELEASE_CODEGEN_UNITS="16" \
> +       CARGO_PROFILE_DEV_SPLIT_DEBUGINFO="off" \
> +       CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO="off"
> +
> +# Set the optimization level with the release default as fallback.
> +ifeq ($(BR2_OPTIMIZE_0),y)
> +PKG_CARGO_ENV += \
> +       CARGO_PROFILE_DEV_OPT_LEVEL="0" \
> +       CARGO_PROFILE_RELEASE_OPT_LEVEL="0"
> +else ifeq ($(BR2_OPTIMIZE_1),y)
> +PKG_CARGO_ENV += \
> +       CARGO_PROFILE_DEV_OPT_LEVEL="1" \
> +       CARGO_PROFILE_RELEASE_OPT_LEVEL="1"
> +else ifeq ($(BR2_OPTIMIZE_2),y)
> +PKG_CARGO_ENV += \
> +       CARGO_PROFILE_DEV_OPT_LEVEL="2" \
> +       CARGO_PROFILE_RELEASE_OPT_LEVEL="2"
> +else ifeq ($(BR2_OPTIMIZE_3),y)
> +PKG_CARGO_ENV += \
> +       CARGO_PROFILE_DEV_OPT_LEVEL="3" \
> +       CARGO_PROFILE_RELEASE_OPT_LEVEL="3"
> +else ifeq ($(BR2_OPTIMIZE_G),y)
> +PKG_CARGO_ENV += \
> +       CARGO_PROFILE_DEV_OPT_LEVEL="0" \
> +       CARGO_PROFILE_RELEASE_OPT_LEVEL="0"
> +else ifeq ($(BR2_OPTIMIZE_S),y)
> +PKG_CARGO_ENV += \
> +       CARGO_PROFILE_DEV_OPT_LEVEL="s" \
> +       CARGO_PROFILE_RELEASE_OPT_LEVEL="s"
> +else ifeq ($(BR2_OPTIMIZE_FAST),y)
> +PKG_CARGO_ENV += \
> +       CARGO_PROFILE_DEV_OPT_LEVEL="3" \
> +       CARGO_PROFILE_RELEASE_OPT_LEVEL="3"
> +else
> +PKG_CARGO_ENV += \
> +       CARGO_PROFILE_DEV_OPT_LEVEL="3" \
> +       CARGO_PROFILE_RELEASE_OPT_LEVEL="3"
> +endif
> +
> +ifeq ($(BR2_ENABLE_LTO),y)
> +PKG_CARGO_ENV += \
> +       CARGO_PROFILE_DEV_LTO="true" \
> +       CARGO_PROFILE_RELEASE_LTO="true"
> +else
> +PKG_CARGO_ENV += \
> +       CARGO_PROFILE_DEV_LTO="false" \
> +       CARGO_PROFILE_RELEASE_LTO="false"
> +endif
> +
> +
> +ifeq ($(BR2_ENABLE_DEBUG),y)
> +ifeq ($(BR2_DEBUG_3),y)
> +# full debug info
> +PKG_CARGO_ENV += \
> +       CARGO_PROFILE_DEV_DEBUG="2" \
> +       CARGO_PROFILE_RELEASE_DEBUG="2"
> +else
> +# line tables only
> +PKG_CARGO_ENV += \
> +       CARGO_PROFILE_DEV_DEBUG="1" \
> +       CARGO_PROFILE_RELEASE_DEBUG="1"
> +endif
> +else
> +# no debug info
> +PKG_CARGO_ENV += \
> +       CARGO_PROFILE_DEV_DEBUG="0" \
> +       CARGO_PROFILE_RELEASE_DEBUG="0"
> +endif
> +
> +# Enabling debug-assertions enables the runtime debug_assert! macro.
> +#
> +# Enabling overflow-checks enables runtime panic on integer overflow.
> +ifeq ($(BR2_ENABLE_RUNTIME_DEBUG),y)
> +PKG_CARGO_ENV += \
> +       CARGO_PROFILE_DEV_DEBUG_ASSERTIONS="true" \
> +       CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS="true" \
> +       CARGO_PROFILE_DEV_OVERFLOW_CHECKS="true" \
> +       CARGO_PROFILE_RELEASE_OVERFLOW_CHECKS="true"
> +else
> +PKG_CARGO_ENV += \
> +       CARGO_PROFILE_DEV_DEBUG_ASSERTIONS="false" \
> +       CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS="false" \
> +       CARGO_PROFILE_DEV_OVERFLOW_CHECKS="false" \
> +       CARGO_PROFILE_RELEASE_OVERFLOW_CHECKS="false"
> +endif
> +
>  #
>  # This is a workaround for https://github.com/rust-lang/compiler-builtins/issues/420
>  # and should be removed when fixed upstream
> --
> 2.34.1
>
Arnout Vandecappelle Feb. 6, 2024, 2:35 p.m. UTC | #3
Hi James,

  There's really no reason why this should have taken more than a year, but 
finally I applied to master.

On 22/10/2022 00:44, James Hilliard wrote:
> The cargo profile flags currently only select a cargo build flag which
> does not accurately reflect the configured build settings, setting the
> cargo build flag also only works for packages using the cargo
> infrastructure directly and not with other packages using the cargo
> environment such as pyo3 based python packages(which indirectly invoke
> cargo build via the setuptools-rust and maturn python build backends).

  I slightly rewrote the commit message to hopefully make it even clearer.


> To ensure that cargo uses the correct profile settings for the build
> we need to set env variables for the two root profiles(dev/release)
> based on the buildroot build settings.
> 
> For reference the default cargo root profile settings are:
> https://doc.rust-lang.org/cargo/reference/profiles.html#default-profiles

  I checked this page, and there are three configs we are not setting at the 
moment: strip, panic and rpath. They default to the same value for dev and 
release, and those values look appropriate for Buildroot:

- strip = "none" - OK because we strip in post-build.
- panic = "unwind" - sounds OK to me
- rpath = "false" - OK because all libs should be installed in /usr/lib.

  Ideally this should have been mentioned in the commit message as well. I 
didn't do that because I wasn't 100% sure of the above - so can the people in Cc 
of this mail check if it looks correct?

  Regards,
  Arnout

> 
> As applications not using cargo infrastructure do not have a standard
> way of configuring a specific build profile due to the selected
> profile being determined by the cargo command line flags, the only
> reliable way to configure profile settings is to set both root
> profiles in the environment as this will ensure that they are used
> regardless of which cargo build flag(such as --debug or --release) is
> used.
> 
> Cc: Moritz Bitsch <moritz@h6t.eu>
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> ---
>   package/pkg-cargo.mk | 100 +++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 100 insertions(+)
> 
> diff --git a/package/pkg-cargo.mk b/package/pkg-cargo.mk
> index f7e3f39503..4945770079 100644
> --- a/package/pkg-cargo.mk
> +++ b/package/pkg-cargo.mk
> @@ -46,6 +46,106 @@ PKG_CARGO_ENV = \
>   	CARGO_BUILD_TARGET="$(RUSTC_TARGET_NAME)" \
>   	CARGO_TARGET_$(call UPPERCASE,$(RUSTC_TARGET_NAME))_LINKER=$(notdir $(TARGET_CROSS))gcc
>   
> +# We always set both CARGO_PROFILE_DEV and CARGO_PROFILE_RELEASE
> +# as we are unable to select a build profile using the environment.
> +#
> +# Other cargo profiles generally derive from these two profiles.
> +
> +# Disable incremental compilation to match release default.
> +#
> +# Set codegen-units to release default.
> +#
> +# Set split-debuginfo to default off for ELF platforms.
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_INCREMENTAL="false" \
> +	CARGO_PROFILE_RELEASE_INCREMENTAL="false" \
> +	CARGO_PROFILE_DEV_CODEGEN_UNITS="16" \
> +	CARGO_PROFILE_RELEASE_CODEGEN_UNITS="16" \
> +	CARGO_PROFILE_DEV_SPLIT_DEBUGINFO="off" \
> +	CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO="off"
> +
> +# Set the optimization level with the release default as fallback.
> +ifeq ($(BR2_OPTIMIZE_0),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_OPT_LEVEL="0" \
> +	CARGO_PROFILE_RELEASE_OPT_LEVEL="0"
> +else ifeq ($(BR2_OPTIMIZE_1),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_OPT_LEVEL="1" \
> +	CARGO_PROFILE_RELEASE_OPT_LEVEL="1"
> +else ifeq ($(BR2_OPTIMIZE_2),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_OPT_LEVEL="2" \
> +	CARGO_PROFILE_RELEASE_OPT_LEVEL="2"
> +else ifeq ($(BR2_OPTIMIZE_3),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_OPT_LEVEL="3" \
> +	CARGO_PROFILE_RELEASE_OPT_LEVEL="3"
> +else ifeq ($(BR2_OPTIMIZE_G),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_OPT_LEVEL="0" \
> +	CARGO_PROFILE_RELEASE_OPT_LEVEL="0"
> +else ifeq ($(BR2_OPTIMIZE_S),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_OPT_LEVEL="s" \
> +	CARGO_PROFILE_RELEASE_OPT_LEVEL="s"
> +else ifeq ($(BR2_OPTIMIZE_FAST),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_OPT_LEVEL="3" \
> +	CARGO_PROFILE_RELEASE_OPT_LEVEL="3"
> +else
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_OPT_LEVEL="3" \
> +	CARGO_PROFILE_RELEASE_OPT_LEVEL="3"
> +endif
> +
> +ifeq ($(BR2_ENABLE_LTO),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_LTO="true" \
> +	CARGO_PROFILE_RELEASE_LTO="true"
> +else
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_LTO="false" \
> +	CARGO_PROFILE_RELEASE_LTO="false"
> +endif
> +
> +
> +ifeq ($(BR2_ENABLE_DEBUG),y)
> +ifeq ($(BR2_DEBUG_3),y)
> +# full debug info
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_DEBUG="2" \
> +	CARGO_PROFILE_RELEASE_DEBUG="2"
> +else
> +# line tables only
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_DEBUG="1" \
> +	CARGO_PROFILE_RELEASE_DEBUG="1"
> +endif
> +else
> +# no debug info
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_DEBUG="0" \
> +	CARGO_PROFILE_RELEASE_DEBUG="0"
> +endif
> +
> +# Enabling debug-assertions enables the runtime debug_assert! macro.
> +#
> +# Enabling overflow-checks enables runtime panic on integer overflow.
> +ifeq ($(BR2_ENABLE_RUNTIME_DEBUG),y)
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_DEBUG_ASSERTIONS="true" \
> +	CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS="true" \
> +	CARGO_PROFILE_DEV_OVERFLOW_CHECKS="true" \
> +	CARGO_PROFILE_RELEASE_OVERFLOW_CHECKS="true"
> +else
> +PKG_CARGO_ENV += \
> +	CARGO_PROFILE_DEV_DEBUG_ASSERTIONS="false" \
> +	CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS="false" \
> +	CARGO_PROFILE_DEV_OVERFLOW_CHECKS="false" \
> +	CARGO_PROFILE_RELEASE_OVERFLOW_CHECKS="false"
> +endif
> +
>   #
>   # This is a workaround for https://github.com/rust-lang/compiler-builtins/issues/420
>   # and should be removed when fixed upstream
diff mbox series

Patch

diff --git a/package/pkg-cargo.mk b/package/pkg-cargo.mk
index f7e3f39503..4945770079 100644
--- a/package/pkg-cargo.mk
+++ b/package/pkg-cargo.mk
@@ -46,6 +46,106 @@  PKG_CARGO_ENV = \
 	CARGO_BUILD_TARGET="$(RUSTC_TARGET_NAME)" \
 	CARGO_TARGET_$(call UPPERCASE,$(RUSTC_TARGET_NAME))_LINKER=$(notdir $(TARGET_CROSS))gcc
 
+# We always set both CARGO_PROFILE_DEV and CARGO_PROFILE_RELEASE
+# as we are unable to select a build profile using the environment.
+#
+# Other cargo profiles generally derive from these two profiles.
+
+# Disable incremental compilation to match release default.
+#
+# Set codegen-units to release default.
+#
+# Set split-debuginfo to default off for ELF platforms.
+PKG_CARGO_ENV += \
+	CARGO_PROFILE_DEV_INCREMENTAL="false" \
+	CARGO_PROFILE_RELEASE_INCREMENTAL="false" \
+	CARGO_PROFILE_DEV_CODEGEN_UNITS="16" \
+	CARGO_PROFILE_RELEASE_CODEGEN_UNITS="16" \
+	CARGO_PROFILE_DEV_SPLIT_DEBUGINFO="off" \
+	CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO="off"
+
+# Set the optimization level with the release default as fallback.
+ifeq ($(BR2_OPTIMIZE_0),y)
+PKG_CARGO_ENV += \
+	CARGO_PROFILE_DEV_OPT_LEVEL="0" \
+	CARGO_PROFILE_RELEASE_OPT_LEVEL="0"
+else ifeq ($(BR2_OPTIMIZE_1),y)
+PKG_CARGO_ENV += \
+	CARGO_PROFILE_DEV_OPT_LEVEL="1" \
+	CARGO_PROFILE_RELEASE_OPT_LEVEL="1"
+else ifeq ($(BR2_OPTIMIZE_2),y)
+PKG_CARGO_ENV += \
+	CARGO_PROFILE_DEV_OPT_LEVEL="2" \
+	CARGO_PROFILE_RELEASE_OPT_LEVEL="2"
+else ifeq ($(BR2_OPTIMIZE_3),y)
+PKG_CARGO_ENV += \
+	CARGO_PROFILE_DEV_OPT_LEVEL="3" \
+	CARGO_PROFILE_RELEASE_OPT_LEVEL="3"
+else ifeq ($(BR2_OPTIMIZE_G),y)
+PKG_CARGO_ENV += \
+	CARGO_PROFILE_DEV_OPT_LEVEL="0" \
+	CARGO_PROFILE_RELEASE_OPT_LEVEL="0"
+else ifeq ($(BR2_OPTIMIZE_S),y)
+PKG_CARGO_ENV += \
+	CARGO_PROFILE_DEV_OPT_LEVEL="s" \
+	CARGO_PROFILE_RELEASE_OPT_LEVEL="s"
+else ifeq ($(BR2_OPTIMIZE_FAST),y)
+PKG_CARGO_ENV += \
+	CARGO_PROFILE_DEV_OPT_LEVEL="3" \
+	CARGO_PROFILE_RELEASE_OPT_LEVEL="3"
+else
+PKG_CARGO_ENV += \
+	CARGO_PROFILE_DEV_OPT_LEVEL="3" \
+	CARGO_PROFILE_RELEASE_OPT_LEVEL="3"
+endif
+
+ifeq ($(BR2_ENABLE_LTO),y)
+PKG_CARGO_ENV += \
+	CARGO_PROFILE_DEV_LTO="true" \
+	CARGO_PROFILE_RELEASE_LTO="true"
+else
+PKG_CARGO_ENV += \
+	CARGO_PROFILE_DEV_LTO="false" \
+	CARGO_PROFILE_RELEASE_LTO="false"
+endif
+
+
+ifeq ($(BR2_ENABLE_DEBUG),y)
+ifeq ($(BR2_DEBUG_3),y)
+# full debug info
+PKG_CARGO_ENV += \
+	CARGO_PROFILE_DEV_DEBUG="2" \
+	CARGO_PROFILE_RELEASE_DEBUG="2"
+else
+# line tables only
+PKG_CARGO_ENV += \
+	CARGO_PROFILE_DEV_DEBUG="1" \
+	CARGO_PROFILE_RELEASE_DEBUG="1"
+endif
+else
+# no debug info
+PKG_CARGO_ENV += \
+	CARGO_PROFILE_DEV_DEBUG="0" \
+	CARGO_PROFILE_RELEASE_DEBUG="0"
+endif
+
+# Enabling debug-assertions enables the runtime debug_assert! macro.
+#
+# Enabling overflow-checks enables runtime panic on integer overflow.
+ifeq ($(BR2_ENABLE_RUNTIME_DEBUG),y)
+PKG_CARGO_ENV += \
+	CARGO_PROFILE_DEV_DEBUG_ASSERTIONS="true" \
+	CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS="true" \
+	CARGO_PROFILE_DEV_OVERFLOW_CHECKS="true" \
+	CARGO_PROFILE_RELEASE_OVERFLOW_CHECKS="true"
+else
+PKG_CARGO_ENV += \
+	CARGO_PROFILE_DEV_DEBUG_ASSERTIONS="false" \
+	CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS="false" \
+	CARGO_PROFILE_DEV_OVERFLOW_CHECKS="false" \
+	CARGO_PROFILE_RELEASE_OVERFLOW_CHECKS="false"
+endif
+
 #
 # This is a workaround for https://github.com/rust-lang/compiler-builtins/issues/420
 # and should be removed when fixed upstream