Repository: slint-ui/slint (language: rust)

Issue:
Yocto cross-compilation broken with Rust & CMake
After commit 18300429c1cb62c3b1a9a6fdca64dc09367ea878 , slint-compiler invokes a configure script for jemalloc which seems to use autoconf to cross-compile.

When building within a Yocto environment, CC, etc. are set to the target compiler, and during the Slint-cpp crate build we have some build scripts that use cc.rs to build for the host. Therefore we set CC_${host} and cc.rs uses that instead.

As per https://github.com/slint-ui/slint/commit/3c37c51ab5b0d63e06a7c89f00c73ad5acf35297 the same trick would work for jemalloc's configure to get past `checking whether the C compiler works... no`, but then we hit this:

```
  checking how to run the C preprocessor... aarch64-poky-linux-gcc -E  -mcpu=cortex-a57 -march=armv8-a+crc -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/runner/work/slint/yocto-sdk/sysroots/cortexa57-poky-linux
```

and that can't work.



Discussion / hints:
I tried setting HOST_CPP in https://github.com/slint-ui/slint/commit/bbc25d494cfb195c8c06910e9856a18b906614c9 but that didn't help: 

https://github.com/slint-ui/slint/actions/runs/12973562980/job/36182368807

same error.

Same problem with CPP_$target:

https://github.com/slint-ui/slint/commit/b1e5761e1d75bef4f9dc415f765c97c2b0a85228
Maybe we need to make a change in corrosion?
We can also make the jemalloc a feature in slint-compiler and not enable it when cross compiling.
Or we just don't use jemalloc in the slint-compiler at all, that is not performance critical.
I think upstream needs to be fixed (maybe just the crate).

I think a feature makes sense, and we opt in for the binaries that we build in the CI.

Fix patch:
diff --git a/api/cpp/CMakeLists.txt b/api/cpp/CMakeLists.txt
index cef1213b948..846aa69248b 100644
--- a/api/cpp/CMakeLists.txt
+++ b/api/cpp/CMakeLists.txt
@@ -136,6 +136,21 @@ endif()
 
 if(SLINT_FEATURE_COMPILER AND NOT SLINT_COMPILER)
     corrosion_set_hostbuild(slint-compiler)
+    set_property(
+        TARGET slint-compiler
+        PROPERTY CORROSION_NO_DEFAULT_FEATURES
+        ON
+    )
+    list(APPEND slint_compiler_features "software-renderer")
+    # Enable jemalloc, except when cross-compiling. (#7463)
+    if(NOT CMAKE_CROSSCOMPILING)
+        list(APPEND slint_compiler_features "jemalloc")
+    endif()
+    set_property(
+        TARGET slint-compiler
+        PROPERTY CORROSION_FEATURES
+        ${slint_compiler_features}
+    )
 endif()
 
 if (SLINT_BUILD_RUNTIME)
diff --git a/tools/compiler/Cargo.toml b/tools/compiler/Cargo.toml
index 9f9019f4e2d..de99e843530 100644
--- a/tools/compiler/Cargo.toml
+++ b/tools/compiler/Cargo.toml
@@ -19,8 +19,10 @@ path = "main.rs"
 
 [features]
 software-renderer = ["i-slint-compiler/software-renderer"]
+jemalloc = ["dep:tikv-jemallocator"]
 
-default = ["software-renderer"]
+# Keep defaults in sync with defaults applied in api/cpp/CMakeLists.txt for slint-compiler
+default = ["software-renderer", "jemalloc"]
 
 [dependencies]
 i-slint-compiler = { workspace = true, features = ["default", "display-diagnostics", "bundle-translations", "cpp", "rust"]}
@@ -31,4 +33,4 @@ spin_on = { workspace = true }
 itertools = { workspace = true }
 
 [target.'cfg(not(target_env = "msvc"))'.dependencies]
-tikv-jemallocator = { workspace = true }
+tikv-jemallocator = { workspace = true, optional = true }
diff --git a/tools/compiler/main.rs b/tools/compiler/main.rs
index b674ed71cc3..0e2261d991b 100644
--- a/tools/compiler/main.rs
+++ b/tools/compiler/main.rs
@@ -7,10 +7,10 @@ use i_slint_compiler::*;
 use itertools::Itertools;
 use std::io::{BufWriter, Write};
 
-#[cfg(not(target_env = "msvc"))]
+#[cfg(all(feature = "jemalloc", not(target_env = "msvc")))]
 use tikv_jemallocator::Jemalloc;
 
-#[cfg(not(target_env = "msvc"))]
+#[cfg(all(feature = "jemalloc", not(target_env = "msvc")))]
 #[global_allocator]
 static GLOBAL: Jemalloc = Jemalloc;
 


Test patch: