Repository: rust-cross/cargo-xwin (language: python)

Issue:
Use `cargo` build target
Instead of adding `--target` to each command it would save time if `xwin build` and `test` read cargo configs.

Given this `.cargo/config`:
```toml
[build]
target = "x86_64-pc-windows-msvc"
```

I expect that `cargo xwin build` gives the same result as `cargo xwin build --target x86_64-pc-windows-msvc`. However, the former fails on mac os:
```
error: linker `link.exe` not found
  |
  = note: No such file or directory (os error 2)

note: the msvc targets depend on the msvc linker but `link.exe` was not found

note: please ensure that VS 2013, VS 2015, VS 2017, VS 2019 or VS 2022 was installed with the Visual C++ option
```



Discussion / hints:

Fix patch:
diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml
index 04285b9..d31d6f1 100644
--- a/.github/workflows/CI.yml
+++ b/.github/workflows/CI.yml
@@ -77,9 +77,10 @@ jobs:
           rustup target add x86_64-pc-windows-msvc
 
           cargo run --release xwin build --target x86_64-pc-windows-msvc --manifest-path tests/libhello/Cargo.toml
-          cargo run --release xwin build --target x86_64-pc-windows-msvc --manifest-path tests/hello-windows/Cargo.toml
           cargo run --release xwin build --target x86_64-pc-windows-msvc --manifest-path tests/hello-tls/Cargo.toml
           cargo run --release xwin build --target x86_64-pc-windows-msvc --manifest-path tests/hello-rustls/Cargo.toml
+          # Test using default build target from config
+          cargo run --release xwin build --manifest-path tests/hello-windows/Cargo.toml
           # Test CMake support
           cargo run --release xwin build --target x86_64-pc-windows-msvc --manifest-path tests/crfsuite-rs/Cargo.toml
       - name: xwin run - x86_64
diff --git a/Cargo.lock b/Cargo.lock
index d85477f..171b3c1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -106,6 +106,7 @@ dependencies = [
  "clap",
  "dirs",
  "fs-err",
+ "indicatif",
  "path-slash",
  "which",
  "xwin",
diff --git a/src/build.rs b/src/build.rs
index ec252a5..e49c59b 100644
--- a/src/build.rs
+++ b/src/build.rs
@@ -188,7 +188,24 @@ impl Build {
         let cache_dir = xwin_cache_dir.parent().unwrap();
         env_paths.push(cache_dir.to_path_buf());
 
-        for target in &self.cargo.target {
+        let mut targets = self.cargo.target.clone();
+        if targets.is_empty() {
+            let workdir = self
+                .cargo
+                .manifest_path
+                .as_ref()
+                .and_then(|p| p.parent().map(|x| x.to_path_buf()))
+                .or_else(|| env::current_dir().ok())
+                .unwrap();
+            if let Some(build_target) = default_build_target_from_config(&workdir)? {
+                // if no target is specified, use the default build target
+                // Note that this is required, otherwise it may fail with link errors
+                build.arg("--target").arg(&build_target);
+                targets.push(build_target);
+            }
+        }
+
+        for target in &targets {
             if target.contains("msvc") {
                 self.setup_msvc_crt(xwin_cache_dir.clone())?;
                 let env_target = target.to_lowercase().replace('-', "_");
@@ -546,6 +563,28 @@ fn rustc_target_bin_dir() -> Result<PathBuf> {
     Ok(bin_dir)
 }
 
+fn default_build_target_from_config(workdir: &Path) -> Result<Option<String>> {
+    let output = Command::new("cargo")
+        .current_dir(workdir)
+        .args(&[
+            "config",
+            "get",
+            "-Z",
+            "unstable-options",
+            "--format",
+            "json-value",
+            "build.target",
+        ])
+        .env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "nightly")
+        .output()?;
+    if !output.status.success() {
+        return Ok(None);
+    }
+    let stdout = String::from_utf8(output.stdout)?;
+    let target = stdout.trim().trim_matches('"');
+    Ok(Some(target.to_string()))
+}
+
 /// Symlink Rust provided llvm tool component
 fn symlink_llvm_tool(
     tool: &str,


Test patch:
diff --git a/tests/hello-windows/.cargo/config.toml b/tests/hello-windows/.cargo/config.toml
new file mode 100644
index 0000000..33806ae
--- /dev/null
+++ b/tests/hello-windows/.cargo/config.toml
@@ -0,0 +1,2 @@
+[build]
+target = "x86_64-pc-windows-msvc"