Repository: randombit/botan-rs (language: rust)

Issue:
botan-src does not respect cargo's target triple when invoking configure.py
I'm trying to build a binary on my Apple Silicon mac mini which depends on botan-rs with enabled features `vendored` and `botan3`. Since we want to create a universal binary with `lipo`, we have to build targets `aarch64-apple-darwin` and `x86_64-apple-darwin`. 


When `botan-src` builds the C library, it calls `configure.py` but the target initially passed to `cargo` is completely ignored.
Although there is a list of options that can be overridden with `BOTAN_CONFIGURE_` env vars, I think `--os` and `--cpu` should be set depending on `cargo --target` and not via `BOTAN_CONFIGURE_CPU` and `BOTAN_CONFIGURE_OS`. I cannot think of any situation where setting CPU and OS to something other than what has been passed in `--target` would make sense.


Dumping the environment in `pub fn build() -> (String, String)` shows
```
CARGO_CFG_TARGET_ARCH=aarch64
CARGO_CFG_TARGET_ENDIAN=little
CARGO_CFG_TARGET_ENV=
CARGO_CFG_TARGET_FAMILY=unix
CARGO_CFG_TARGET_OS=macos
CARGO_CFG_TARGET_POINTER_WIDTH=64
CARGO_CFG_TARGET_VENDOR=apple
CARGO_CFG_UNIX=
...
TARGET=aarch64-apple-darwin
```
So the build script definitely knows what should be built, but doesn't pass the information to `configure.py`.


- ### What I did:
```
$ cargo build -p botan --features vendored,botan3 --target x86_64-apple-darwin
```


- ### What I expected:
The library is built for x86_64


- ### What actually happened:
The library was built for aarch64 according to `ld` and the log file
```
   INFO: configure.py invoked with options "--with-build-dir=/Users/username/src/work/botan-rs/target/debug/build/botan-sys-b2a7d959a11e9080/out/botan --build-targets=static --without-documentation --no-install-python-module --distribution-info=https://crates.io/crates/botan-src --with-debug-info"
   INFO: Configuring to build Botan 3.5.0 (revision git:2e5c7d3eb0dcb63d087fc3e7c92010aa82509bbf)
   INFO: Python version: "3.12.5 (main, Aug  6 2024, 19:08:49) [Clang 15.0.0 (clang-1500.3.9.4)]"
   INFO: Autodetected platform information: OS="Darwin" machine="arm64" proc="arm"
   INFO: Guessing target OS is darwin (use --os to set)
   INFO: Guessing to use compiler xcode (use --cc or CXX to set)
   INFO: Guessing target processor is a arm64 (use --cpu to set)
   INFO: Using /etc/ssl/cert.pem as system certificate store
   INFO: Auto-detected compiler version xcode 15.3
   INFO: Auto-detected compiler arch arm64
   INFO: Target is xcode:15.3-macos-arm64
   INFO: Assuming target arm64 is little endian
```


Discussion / hints:

Fix patch:
diff --git a/botan-src/src/lib.rs b/botan-src/src/lib.rs
index ce3e68f..515768f 100644
--- a/botan-src/src/lib.rs
+++ b/botan-src/src/lib.rs
@@ -27,6 +27,13 @@ fn configure(build_dir: &str) {
     configure.arg("--without-documentation");
     configure.arg("--no-install-python-module");
     configure.arg("--distribution-info=https://crates.io/crates/botan-src");
+
+    configure.arg(format!(
+        "--cpu={}",
+        env::var("CARGO_CFG_TARGET_ARCH").unwrap()
+    ));
+    configure.arg(format!("--os={}", env::var("CARGO_CFG_TARGET_OS").unwrap()));
+
     #[cfg(debug_assertions)]
     configure.arg("--with-debug-info");
 
@@ -36,11 +43,8 @@ fn configure(build_dir: &str) {
     configure.arg("--amalgamation");
 
     let args = [
-        "--os",
-        "--cpu",
         "--compiler-cache",
         "--cc",
-        "--cc-min-version",
         "--cc-bin",
         "--cc-abi-flags",
         "--cxxflags",
@@ -49,28 +53,15 @@ fn configure(build_dir: &str) {
         "--ar-command",
         "--ar-options",
         "--msvc-runtime",
-        "--with-endian",
-        "--with-os-features",
-        "--without-os-features",
         "--system-cert-bundle",
-        "--with-local-config",
-        "--boost-library-name",
         "--module-policy",
         "--enable-modules",
         "--disable-modules",
-        "--library-suffix",
-        "--prefix",
-        "--libdir",
-        "--includedir",
-        "--link-method",
     ];
 
     let flags = [
         "--optimize-for-size",
-        "--no-optimizations",
         "--amalgamation",
-        "--minimized-build",
-        "--with-openssl",
         "--with-commoncrypto",
         "--with-sqlite3",
     ];


Test patch: