Skip to contentSkip to conversation

Improve handling of debug builds in cargo packages

PATCH In review Moritz Bitsch via buildroot 2 participants 10 messages Oct 19, 2022 – Oct 24, 2022

lore
Share & tools
lore
#0 lore raw

cover Improve handling of debug builds in cargo packages

From: Moritz Bitsch <[email protected]>

Cargo supports two build modes: debug and release, where debug is the default.

In debug build mode most optimizations are disabled, resulting in slow runtime.

It is also possible to instruct rustc to generate debug symbols independent of build mode, using RUSTFLAGS.

Moritz Bitsch (2):
package/pkg-cargo.mk: fix handling of runtime debug builds package/pkg-cargo.mk: generate debug symbols when BR2_ENABLE_DEBUG is
set

 package/pkg-cargo.mk | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
Signature · 6 lines
2.37.3

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#1 lore raw

PATCH 1/2 package/pkg-cargo.mk: fix handling of runtime debug builds

From: Moritz Bitsch <[email protected]>

The cargo --release flag is used to create optimized builds, not supplying --release has major performance implications.

---
 package/pkg-cargo.mk | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
package/pkg-cargo.mk +1 -1
index f7e3f39503..e0556252fa 100644
@@ -119,7 +119,7 @@ define $(2)_BUILD_CMDS
 		$$($(2)_CARGO_ENV) \
 		cargo build \
 			--offline \
-			$$(if $$(BR2_ENABLE_DEBUG),,--release) \
+			$$(if $$(BR2_ENABLE_RUNTIME_DEBUG),,--release) \
 			--manifest-path Cargo.toml \
 			--locked \
 			$$($(2)_CARGO_BUILD_OPTS)
Signature · 6 lines
2.37.3

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#2 lore raw

Re: [Buildroot] [PATCH 1/2] package/pkg-cargo.mk: fix handling of runtime debug builds

Quoted 5 lines · Moritz Bitsch via buildroot

From: Moritz Bitsch <[email protected]>

The cargo --release flag is used to create optimized builds, not supplying --release has major performance implications.

Is there an env variable we can use to set this instead?

Some packages use cargo without the cargo infrastructure, for example we have a number of pyo3(https://pyo3.rs/) based packages that use the cargo env but not the cargo build commands from here.

Quoted 22 lines
---
 package/pkg-cargo.mk | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
package/pkg-cargo.mk +1 -2
index f7e3f39503..e0556252fa 100644
@@ -119,7 +119,7 @@ define $(2)_BUILD_CMDS
                $$($(2)_CARGO_ENV) \
                cargo build \
                        --offline \
-                       $$(if $$(BR2_ENABLE_DEBUG),,--release) \
+                       $$(if $$(BR2_ENABLE_RUNTIME_DEBUG),,--release) \
                        --manifest-path Cargo.toml \
                        --locked \
                        $$($(2)_CARGO_BUILD_OPTS)
--

2.37.3

Quoted 4 lines

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#3 lore raw

Re: [Buildroot] [PATCH 1/2] package/pkg-cargo.mk: fix handling of runtime debug builds

Am 20.10.22 um 00:12 schrieb James Hilliard:

Quoted 11 lines
Quoted 4 lines · Moritz Bitsch via buildroot

From: Moritz Bitsch <[email protected]>

The cargo --release flag is used to create optimized builds, not supplying --release has major performance implications.

Is there an env variable we can use to set this instead?

Some packages use cargo without the cargo infrastructure, for example we have a number of pyo3(https://pyo3.rs/) based packages that use the cargo env but not the cargo build commands from here.

I could not find any env variable for switching profiles.

I had a look at python-rtoml, and the used setuptools-rust: supplying --debug to the "setup.py build" removes the --release flag from the cargo invocation.

Quoted 21 lines
Quoted 21 lines
---
  package/pkg-cargo.mk | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
package/pkg-cargo.mk +1 -2
index f7e3f39503..e0556252fa 100644
@@ -119,7 +119,7 @@ define $(2)_BUILD_CMDS
                 $$($(2)_CARGO_ENV) \
                 cargo build \
                         --offline \
-                       $$(if $$(BR2_ENABLE_DEBUG),,--release) \
+                       $$(if $$(BR2_ENABLE_RUNTIME_DEBUG),,--release) \
                         --manifest-path Cargo.toml \
                         --locked \
                         $$($(2)_CARGO_BUILD_OPTS)
--

2.37.3

Quoted 4 lines
Quoted 4 lines

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#4 lore raw

Re: [Buildroot] [PATCH 1/2] package/pkg-cargo.mk: fix handling of runtime debug builds

Quoted 11 lines · Moritz Bitsch

Am 20.10.22 um 00:12 schrieb James Hilliard:

Quoted 8 lines
Quoted 4 lines · Moritz Bitsch via buildroot

From: Moritz Bitsch <[email protected]>

The cargo --release flag is used to create optimized builds, not supplying --release has major performance implications.

Is there an env variable we can use to set this instead?

Quoted 8 lines
Quoted 3 lines

Some packages use cargo without the cargo infrastructure, for example we have a number of pyo3(https://pyo3.rs/) based packages that use the cargo env but not the cargo build commands from here.

I could not find any env variable for switching profiles.

I had a look at python-rtoml, and the used setuptools-rust: supplying --debug to
the "setup.py build" removes the --release flag from the cargo invocation.

Hmm, there are two pyo3 build backends, only setuptools-rust uses setup.py, we also have maturin which is pep517 based.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
---
  package/pkg-cargo.mk | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
package/pkg-cargo.mk +1 -2
index f7e3f39503..e0556252fa 100644
@@ -119,7 +119,7 @@ define $(2)_BUILD_CMDS
                 $$($(2)_CARGO_ENV) \
                 cargo build \
                         --offline \
-                       $$(if $$(BR2_ENABLE_DEBUG),,--release) \
+                       $$(if $$(BR2_ENABLE_RUNTIME_DEBUG),,--release) \
                         --manifest-path Cargo.toml \
                         --locked \
                         $$($(2)_CARGO_BUILD_OPTS)
--

2.37.3

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#5 lore raw

Re: [Buildroot] [PATCH 1/2] package/pkg-cargo.mk: fix handling of runtime debug builds

Am 21.10.22 um 18:41 schrieb James Hilliard:

Quoted 15 lines · Moritz Bitsch
Quoted 14 lines

Am 20.10.22 um 00:12 schrieb James Hilliard:

Quoted 11 lines
Quoted 4 lines · Moritz Bitsch via buildroot

From: Moritz Bitsch <[email protected]>

The cargo --release flag is used to create optimized builds, not supplying --release has major performance implications.

Is there an env variable we can use to set this instead?

Some packages use cargo without the cargo infrastructure, for example we have a number of pyo3(https://pyo3.rs/) based packages that use the cargo env but not the cargo build commands from here.

Quoted 8 lines
Quoted 5 lines

I could not find any env variable for switching profiles.

I had a look at python-rtoml, and the used setuptools-rust: supplying --debug to
the "setup.py build" removes the --release flag from the cargo invocation.

Hmm, there are two pyo3 build backends, only setuptools-rust uses setup.py, we also have maturin which is pep517 based.

I'm in no way a a python expert, so take my words with a grain of salt.

I could not find a way to specify what build profile is used by maturin when in a pep517 build. Even if maturin supports the -r flag for release builds when called directly.

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
---
   package/pkg-cargo.mk | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
package/pkg-cargo.mk +1 -2
index f7e3f39503..e0556252fa 100644
@@ -119,7 +119,7 @@ define $(2)_BUILD_CMDS
                  $$($(2)_CARGO_ENV) \
                  cargo build \
                          --offline \
-                       $$(if $$(BR2_ENABLE_DEBUG),,--release) \
+                       $$(if $$(BR2_ENABLE_RUNTIME_DEBUG),,--release) \
                          --manifest-path Cargo.toml \
                          --locked \
                          $$($(2)_CARGO_BUILD_OPTS)
--

2.37.3

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#6 lore raw

Re: [Buildroot] [PATCH 1/2] package/pkg-cargo.mk: fix handling of runtime debug builds

Quoted 12 lines · Moritz Bitsch

Am 21.10.22 um 18:41 schrieb James Hilliard:

Quoted 10 lines · Moritz Bitsch
Quoted 9 lines

Am 20.10.22 um 00:12 schrieb James Hilliard:

Quoted 6 lines
Quoted 4 lines · Moritz Bitsch via buildroot

From: Moritz Bitsch <[email protected]>

The cargo --release flag is used to create optimized builds, not supplying --release has major performance implications.

Looking at this further the cargo --release flag merely tells cargo to use the release profile rather than the dev profile, however the issue of optimizations being enabled or not is really a matter of what the profile configuration is, right now we are effectively choosing between the default release or default dev profile configuration which does not properly match the build settings.

Instead what we really want to do is configure all cargo profiles based on the selected buildroot build settings.

Unfortunately cargo doesn't have any env variable that selects a profile, but we can sidestep this simply by configuring both root profiles(which other profiles inherit from).

I think the existing cargo build flags are fine, I sent a patch which should fix the profile configurations so that they match the buildroot build settings, let me know if this fixes the optimization issue for you:
https://lore.kernel.org/buildroot/[email protected]/

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 5 lines

Is there an env variable we can use to set this instead?

Some packages use cargo without the cargo infrastructure, for example we have a number of pyo3(https://pyo3.rs/) based packages that use the cargo env but not the cargo build commands from here.

I could not find any env variable for switching profiles.

I had a look at python-rtoml, and the used setuptools-rust: supplying --debug to
the "setup.py build" removes the --release flag from the cargo invocation.

Quoted 8 lines
Quoted 2 lines

Hmm, there are two pyo3 build backends, only setuptools-rust uses setup.py, we also have maturin which is pep517 based.

I'm in no way a a python expert, so take my words with a grain of salt.

I could not find a way to specify what build profile is used by maturin when in a pep517 build. Even if maturin supports the -r flag for release builds when called directly.

I think the real issue is actually not that the wrong build profile is being used
so much as the build profile configuration itself is wrong.

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
---
   package/pkg-cargo.mk | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
package/pkg-cargo.mk +1 -2
index f7e3f39503..e0556252fa 100644
@@ -119,7 +119,7 @@ define $(2)_BUILD_CMDS
                  $$($(2)_CARGO_ENV) \
                  cargo build \
                          --offline \
-                       $$(if $$(BR2_ENABLE_DEBUG),,--release) \
+                       $$(if $$(BR2_ENABLE_RUNTIME_DEBUG),,--release) \
                          --manifest-path Cargo.toml \
                          --locked \
                          $$($(2)_CARGO_BUILD_OPTS)
--

2.37.3

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#7 lore raw

Re: [Buildroot] [PATCH 1/2] package/pkg-cargo.mk: fix handling of runtime debug builds

Am 22.10.22 um 00:57 schrieb James Hilliard:

Quoted 23 lines · Moritz Bitsch
Quoted 12 lines

Am 21.10.22 um 18:41 schrieb James Hilliard:

Quoted 10 lines · Moritz Bitsch
Quoted 9 lines

Am 20.10.22 um 00:12 schrieb James Hilliard:

Quoted 6 lines
Quoted 4 lines · Moritz Bitsch via buildroot

From: Moritz Bitsch <[email protected]>

The cargo --release flag is used to create optimized builds, not supplying --release has major performance implications.

Looking at this further the cargo --release flag merely tells cargo to use the release profile rather than the dev profile, however the issue of optimizations being enabled or not is really a matter of what the profile configuration is, right now we are effectively choosing between the default release or default dev profile configuration which does not properly match the build settings.

Instead what we really want to do is configure all cargo profiles based on the selected buildroot build settings.

Quoted 8 lines

Unfortunately cargo doesn't have any env variable that selects a profile, but we can sidestep this simply by configuring both root profiles(which other profiles inherit from).

I think the existing cargo build flags are fine, I sent a patch which should fix the profile configurations so that they match the buildroot build settings, let me know if this fixes the optimization issue for you:
https://lore.kernel.org/buildroot/[email protected]/

Thanks, for the detailed response. The mentioned patch works fine for my use case. I can reproduce the performance issue/improvement when switching between different optimization levels.

Quoted 16 lines
Quoted 16 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Is there an env variable we can use to set this instead?

Some packages use cargo without the cargo infrastructure, for example we have a number of pyo3(https://pyo3.rs/) based packages that use the cargo env but not the cargo build commands from here.

Quoted 8 lines
Quoted 5 lines

I could not find any env variable for switching profiles.

I had a look at python-rtoml, and the used setuptools-rust: supplying --debug to
the "setup.py build" removes the --release flag from the cargo invocation.

Hmm, there are two pyo3 build backends, only setuptools-rust uses setup.py, we also have maturin which is pep517 based.

I'm in no way a a python expert, so take my words with a grain of salt.

Quoted 7 lines
Quoted 3 lines

I could not find a way to specify what build profile is used by maturin when in a pep517 build. Even if maturin supports the -r flag for release builds when called directly.

I think the real issue is actually not that the wrong build profile is being used
so much as the build profile configuration itself is wrong.

After looking into some crates with different behavior when built in debug or release mode (for example rust-embed) I think that most of them check for enabled debug_assertions, which you handle in your patch.

I think the conditional --release switch should be removed. Your patch is by far the more precise solution.

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
---
    package/pkg-cargo.mk | 2 +-
    1 file changed, 1 insertion(+), 1 deletion(-)
package/pkg-cargo.mk +1 -2
index f7e3f39503..e0556252fa 100644
@@ -119,7 +119,7 @@ define $(2)_BUILD_CMDS
                   $$($(2)_CARGO_ENV) \
                   cargo build \
                           --offline \
-                       $$(if $$(BR2_ENABLE_DEBUG),,--release) \
+                       $$(if $$(BR2_ENABLE_RUNTIME_DEBUG),,--release) \
                           --manifest-path Cargo.toml \
                           --locked \
                           $$($(2)_CARGO_BUILD_OPTS)
--

2.37.3

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#8 lore raw

Re: [Buildroot] [PATCH 1/2] package/pkg-cargo.mk: fix handling of runtime debug builds

Quoted 29 lines · Moritz Bitsch

Am 22.10.22 um 00:57 schrieb James Hilliard:

Quoted 27 lines · Moritz Bitsch
Quoted 12 lines

Am 21.10.22 um 18:41 schrieb James Hilliard:

Quoted 10 lines · Moritz Bitsch
Quoted 9 lines

Am 20.10.22 um 00:12 schrieb James Hilliard:

Quoted 6 lines
Quoted 4 lines · Moritz Bitsch via buildroot

From: Moritz Bitsch <[email protected]>

The cargo --release flag is used to create optimized builds, not supplying --release has major performance implications.

Looking at this further the cargo --release flag merely tells cargo to use the release profile rather than the dev profile, however the issue of optimizations being enabled or not is really a matter of what the profile configuration is, right now we are effectively choosing between the default release or default dev profile configuration which does not properly match the build settings.

Instead what we really want to do is configure all cargo profiles based on the selected buildroot build settings.

Unfortunately cargo doesn't have any env variable that selects a profile, but we can sidestep this simply by configuring both root profiles(which other profiles inherit from).

Quoted 8 lines
Quoted 4 lines

I think the existing cargo build flags are fine, I sent a patch which should fix the profile configurations so that they match the buildroot build settings, let me know if this fixes the optimization issue for you:
https://lore.kernel.org/buildroot/[email protected]/

Thanks, for the detailed response. The mentioned patch works fine for my use case. I can reproduce the performance issue/improvement when switching between different optimization levels.

Great, can you reply to that patch with a "Tested-by:" so that patchwork picks it up?

Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Is there an env variable we can use to set this instead?

Some packages use cargo without the cargo infrastructure, for example we have a number of pyo3(https://pyo3.rs/) based packages that use the cargo env but not the cargo build commands from here.

Quoted 8 lines
Quoted 5 lines

I could not find any env variable for switching profiles.

I had a look at python-rtoml, and the used setuptools-rust: supplying --debug to
the "setup.py build" removes the --release flag from the cargo invocation.

Hmm, there are two pyo3 build backends, only setuptools-rust uses setup.py, we also have maturin which is pep517 based.

I'm in no way a a python expert, so take my words with a grain of salt.

I could not find a way to specify what build profile is used by maturin when in a pep517 build. Even if maturin supports the -r flag for release builds when called directly.

Quoted 7 lines
Quoted 3 lines

I think the real issue is actually not that the wrong build profile is being used
so much as the build profile configuration itself is wrong.

After looking into some crates with different behavior when built in debug or release mode (for example rust-embed) I think that most of them check for enabled debug_assertions, which you handle in your patch.

There are still ways for cargo packages to potentially change behavior based on the selected profile even when the profile configurations are otherwise identical.

Quoted 3 lines

I think the conditional --release switch should be removed. Your patch is by far the more precise solution.

I think we should probably still leave the --release switch as it's possible for cargo package build scripts to detect the configured profile using the PROFILE build script env variable which gets set by cargo:
https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts

There's probably not many cases where this will matter but it does appear to be more correct to set the --release switch as well when possible.

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
---
    package/pkg-cargo.mk | 2 +-
    1 file changed, 1 insertion(+), 1 deletion(-)
package/pkg-cargo.mk +1 -2
index f7e3f39503..e0556252fa 100644
@@ -119,7 +119,7 @@ define $(2)_BUILD_CMDS
                   $$($(2)_CARGO_ENV) \
                   cargo build \
                           --offline \
-                       $$(if $$(BR2_ENABLE_DEBUG),,--release) \
+                       $$(if $$(BR2_ENABLE_RUNTIME_DEBUG),,--release) \
                           --manifest-path Cargo.toml \
                           --locked \
                           $$($(2)_CARGO_BUILD_OPTS)
--

2.37.3

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#9 lore raw

PATCH 2/2 package/pkg-cargo.mk: generate debug symbols when BR2_ENABLE_DEBUG is set

In reply to Moritz Bitsch via buildroot

From: Moritz Bitsch <[email protected]>

Adding -g to RUSTFLAGS enables debug symbol generation in rustc

---
 package/pkg-cargo.mk | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
package/pkg-cargo.mk +7 -1
index e0556252fa..7ce7867f67 100644
@@ -51,9 +51,15 @@ PKG_CARGO_ENV = \
 # and should be removed when fixed upstream
 #
 ifeq ($(NORMALIZED_ARCH),arm)
-	PKG_CARGO_ENV += RUSTFLAGS="-Clink-arg=-Wl,--allow-multiple-definition"
+	PKG_CARGO_RUSTFLAGS += -Clink-arg=-Wl,--allow-multiple-definition
 endif
 
+ifeq ($(BR2_ENABLE_DEBUG),y)
+	PKG_CARGO_RUSTFLAGS += -g
+endif
+
+PKG_CARGO_ENV += RUSTFLAGS=$(subst $(space),$(comma),$(strip $(PKG_CARGO_RUSTFLAGS)))
+
 HOST_PKG_CARGO_ENV = \
 	$(PKG_COMMON_CARGO_ENV)
 
Signature · 6 lines
2.37.3

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot