Skip to main content

cargo_zigbuild/
zig.rs

1use std::env;
2use std::ffi::OsStr;
3#[cfg(target_family = "unix")]
4use std::fs::OpenOptions;
5use std::io::Write;
6#[cfg(target_family = "unix")]
7use std::os::unix::fs::OpenOptionsExt;
8use std::path::{Path, PathBuf};
9use std::process::{self, Command};
10use std::str;
11use std::sync::OnceLock;
12
13use anyhow::{Context, Result, anyhow, bail};
14use fs_err as fs;
15use path_slash::PathBufExt;
16use serde::Deserialize;
17use target_lexicon::{Architecture, Environment, OperatingSystem, Triple};
18
19use crate::linux::ARM_FEATURES_H;
20use crate::macos::{LIBCHARSET_TBD, LIBICONV_TBD};
21
22/// Zig linker wrapper
23#[derive(Clone, Debug, clap::Subcommand)]
24pub enum Zig {
25    /// `zig cc` wrapper
26    #[command(name = "cc")]
27    Cc {
28        /// `zig cc` arguments
29        #[arg(num_args = 1.., trailing_var_arg = true)]
30        args: Vec<String>,
31    },
32    /// `zig c++` wrapper
33    #[command(name = "c++")]
34    Cxx {
35        /// `zig c++` arguments
36        #[arg(num_args = 1.., trailing_var_arg = true)]
37        args: Vec<String>,
38    },
39    /// `zig ar` wrapper
40    #[command(name = "ar")]
41    Ar {
42        /// `zig ar` arguments
43        #[arg(num_args = 1.., trailing_var_arg = true)]
44        args: Vec<String>,
45    },
46    /// `zig ranlib` wrapper
47    #[command(name = "ranlib")]
48    Ranlib {
49        /// `zig ranlib` arguments
50        #[arg(num_args = 1.., trailing_var_arg = true)]
51        args: Vec<String>,
52    },
53    /// `zig lib` wrapper
54    #[command(name = "lib")]
55    Lib {
56        /// `zig lib` arguments
57        #[arg(num_args = 1.., trailing_var_arg = true)]
58        args: Vec<String>,
59    },
60    /// `zig dlltool` wrapper
61    #[command(name = "dlltool")]
62    Dlltool {
63        /// `zig dlltool` arguments
64        #[arg(num_args = 1.., trailing_var_arg = true)]
65        args: Vec<String>,
66    },
67}
68
69struct TargetInfo {
70    target: Option<String>,
71}
72
73impl TargetInfo {
74    fn new(target: Option<&String>) -> Self {
75        Self {
76            target: target.cloned(),
77        }
78    }
79
80    // Architecture helpers
81    fn is_arm(&self) -> bool {
82        self.target
83            .as_ref()
84            .map(|x| x.starts_with("arm"))
85            .unwrap_or_default()
86    }
87
88    fn is_aarch64(&self) -> bool {
89        self.target
90            .as_ref()
91            .map(|x| x.starts_with("aarch64"))
92            .unwrap_or_default()
93    }
94
95    fn is_aarch64_be(&self) -> bool {
96        self.target
97            .as_ref()
98            .map(|x| x.starts_with("aarch64_be"))
99            .unwrap_or_default()
100    }
101
102    fn is_i386(&self) -> bool {
103        self.target
104            .as_ref()
105            .map(|x| x.starts_with("i386"))
106            .unwrap_or_default()
107    }
108
109    fn is_i686(&self) -> bool {
110        self.target
111            .as_ref()
112            .map(|x| x.starts_with("i686") || x.starts_with("x86-"))
113            .unwrap_or_default()
114    }
115
116    fn is_riscv64(&self) -> bool {
117        self.target
118            .as_ref()
119            .map(|x| x.starts_with("riscv64"))
120            .unwrap_or_default()
121    }
122
123    fn is_riscv32(&self) -> bool {
124        self.target
125            .as_ref()
126            .map(|x| x.starts_with("riscv32"))
127            .unwrap_or_default()
128    }
129
130    fn is_mips32(&self) -> bool {
131        self.target
132            .as_ref()
133            .map(|x| x.starts_with("mips") && !x.starts_with("mips64"))
134            .unwrap_or_default()
135    }
136
137    // libc helpers
138    fn is_musl(&self) -> bool {
139        self.target
140            .as_ref()
141            .map(|x| x.contains("musl"))
142            .unwrap_or_default()
143    }
144
145    // Platform helpers
146    fn is_macos(&self) -> bool {
147        self.target
148            .as_ref()
149            .map(|x| x.contains("macos") || x.contains("maccatalyst"))
150            .unwrap_or_default()
151    }
152
153    fn is_darwin(&self) -> bool {
154        self.target
155            .as_ref()
156            .map(|x| x.contains("darwin"))
157            .unwrap_or_default()
158    }
159
160    fn is_apple_platform(&self) -> bool {
161        self.target
162            .as_ref()
163            .map(|x| {
164                x.contains("macos")
165                    || x.contains("darwin")
166                    || x.contains("ios")
167                    || x.contains("tvos")
168                    || x.contains("watchos")
169                    || x.contains("visionos")
170                    || x.contains("maccatalyst")
171            })
172            .unwrap_or_default()
173    }
174
175    fn is_ios(&self) -> bool {
176        self.target
177            .as_ref()
178            .map(|x| x.contains("ios") && !x.contains("visionos"))
179            .unwrap_or_default()
180    }
181
182    fn is_tvos(&self) -> bool {
183        self.target
184            .as_ref()
185            .map(|x| x.contains("tvos"))
186            .unwrap_or_default()
187    }
188
189    fn is_watchos(&self) -> bool {
190        self.target
191            .as_ref()
192            .map(|x| x.contains("watchos"))
193            .unwrap_or_default()
194    }
195
196    fn is_visionos(&self) -> bool {
197        self.target
198            .as_ref()
199            .map(|x| x.contains("visionos"))
200            .unwrap_or_default()
201    }
202
203    /// Returns the appropriate Apple CPU for the platform
204    fn apple_cpu(&self) -> &'static str {
205        if self.is_macos() || self.is_darwin() {
206            "apple_m1" // M-series for macOS
207        } else if self.is_visionos() {
208            "apple_m2" // M2 for Apple Vision Pro
209        } else if self.is_watchos() {
210            "apple_s5" // S-series for Apple Watch
211        } else if self.is_ios() || self.is_tvos() {
212            "apple_a14" // A-series for iOS/tvOS (iPhone 12 era - good baseline)
213        } else {
214            "generic"
215        }
216    }
217
218    fn is_freebsd(&self) -> bool {
219        self.target
220            .as_ref()
221            .map(|x| x.contains("freebsd"))
222            .unwrap_or_default()
223    }
224
225    fn is_windows_gnu(&self) -> bool {
226        self.target
227            .as_ref()
228            .map(|x| x.contains("windows-gnu"))
229            .unwrap_or_default()
230    }
231
232    fn is_windows_msvc(&self) -> bool {
233        self.target
234            .as_ref()
235            .map(|x| x.contains("windows-msvc"))
236            .unwrap_or_default()
237    }
238
239    fn is_ohos(&self) -> bool {
240        self.target
241            .as_ref()
242            .map(|x| x.contains("ohos"))
243            .unwrap_or_default()
244    }
245}
246
247impl Zig {
248    /// Execute the underlying zig command
249    pub fn execute(&self) -> Result<()> {
250        match self {
251            Zig::Cc { args } => self.execute_compiler("cc", args),
252            Zig::Cxx { args } => self.execute_compiler("c++", args),
253            Zig::Ar { args } => self.execute_tool("ar", args),
254            Zig::Ranlib { args } => self.execute_compiler("ranlib", args),
255            Zig::Lib { args } => self.execute_compiler("lib", args),
256            Zig::Dlltool { args } => self.execute_dlltool(args),
257        }
258    }
259
260    /// Execute zig dlltool command
261    /// Filter out unsupported options for older zig versions (< 0.12)
262    pub fn execute_dlltool(&self, cmd_args: &[String]) -> Result<()> {
263        let zig_version = Zig::zig_version()?;
264        let needs_filtering = zig_version.major == 0 && zig_version.minor < 12;
265
266        if !needs_filtering {
267            return self.execute_tool("dlltool", cmd_args);
268        }
269
270        // Filter out --no-leading-underscore, --temp-prefix, and -t (short form)
271        // These options are not supported by zig dlltool in versions < 0.12
272        let mut filtered_args = Vec::with_capacity(cmd_args.len());
273        let mut skip_next = false;
274        for arg in cmd_args {
275            if skip_next {
276                skip_next = false;
277                continue;
278            }
279            if arg == "--no-leading-underscore" {
280                continue;
281            }
282            if arg == "--temp-prefix" || arg == "-t" {
283                // Skip this arg and the next one (the value)
284                skip_next = true;
285                continue;
286            }
287            // Handle --temp-prefix=value and -t=value forms
288            if arg.starts_with("--temp-prefix=") || arg.starts_with("-t=") {
289                continue;
290            }
291            filtered_args.push(arg.clone());
292        }
293
294        self.execute_tool("dlltool", &filtered_args)
295    }
296
297    /// Execute zig cc/c++ command
298    pub fn execute_compiler(&self, cmd: &str, cmd_args: &[String]) -> Result<()> {
299        let target = cmd_args
300            .iter()
301            .position(|x| x == "-target")
302            .and_then(|index| cmd_args.get(index + 1));
303        let target_info = TargetInfo::new(target);
304
305        let rustc_ver = match env::var("CARGO_ZIGBUILD_RUSTC_VERSION") {
306            Ok(version) => version.parse()?,
307            Err(_) => rustc_version::version()?,
308        };
309        let zig_version = Zig::zig_version()?;
310
311        let mut new_cmd_args = Vec::with_capacity(cmd_args.len());
312        let mut skip_next_arg = false;
313        let mut seen_target = false;
314        for arg in cmd_args {
315            if skip_next_arg {
316                skip_next_arg = false;
317                continue;
318            }
319            // Our wrapper script already passes the correct -target;
320            // skip any duplicate -target from rustc to avoid conflicts
321            // (e.g. rustc passes arm64 which zig doesn't recognize for some targets)
322            if arg == "-target" {
323                if seen_target {
324                    skip_next_arg = true;
325                    continue;
326                }
327                seen_target = true;
328            }
329            let args = if arg.starts_with('@') && arg.ends_with("linker-arguments") {
330                vec![self.process_linker_response_file(
331                    arg,
332                    &rustc_ver,
333                    &zig_version,
334                    &target_info,
335                )?]
336            } else {
337                match self.filter_linker_arg(arg, &rustc_ver, &zig_version, &target_info) {
338                    FilteredArg::Keep(filtered) => filtered,
339                    FilteredArg::Skip => continue,
340                    FilteredArg::SkipWithNext => {
341                        skip_next_arg = true;
342                        continue;
343                    }
344                }
345            };
346            new_cmd_args.extend(args);
347        }
348
349        if target_info.is_mips32() {
350            // See https://github.com/ziglang/zig/issues/4925#issuecomment-1499823425
351            new_cmd_args.push("-Wl,-z,notext".to_string());
352        }
353
354        if target_info.is_windows_gnu() && (zig_version.major, zig_version.minor) >= (0, 16) {
355            new_cmd_args.push("-lcompiler_rt".to_string());
356        }
357
358        if self.has_undefined_dynamic_lookup(cmd_args) {
359            new_cmd_args.push("-Wl,-undefined=dynamic_lookup".to_string());
360        }
361        if target_info.is_macos() {
362            if self.should_add_libcharset(cmd_args, &zig_version) {
363                new_cmd_args.push("-lcharset".to_string());
364            }
365            self.add_macos_specific_args(&mut new_cmd_args, &zig_version)?;
366        }
367
368        // For Zig >= 0.15 with macOS, set SDKROOT environment variable
369        // if it exists, instead of passing --sysroot
370        let mut command = Self::command()?;
371        if (zig_version.major, zig_version.minor) >= (0, 15)
372            && let Some(sdkroot) = Self::macos_sdk_root()
373        {
374            command.env("SDKROOT", sdkroot);
375        }
376
377        let mut child = command
378            .arg(cmd)
379            .args(new_cmd_args)
380            .spawn()
381            .with_context(|| format!("Failed to run `zig {cmd}`"))?;
382        let status = child.wait().expect("Failed to wait on zig child process");
383        if !status.success() {
384            process::exit(status.code().unwrap_or(1));
385        }
386        Ok(())
387    }
388
389    fn process_linker_response_file(
390        &self,
391        arg: &str,
392        rustc_ver: &rustc_version::Version,
393        zig_version: &semver::Version,
394        target_info: &TargetInfo,
395    ) -> Result<String> {
396        // rustc passes arguments to linker via an @-file when arguments are too long
397        // See https://github.com/rust-lang/rust/issues/41190
398        // and https://github.com/rust-lang/rust/blob/87937d3b6c302dfedfa5c4b94d0a30985d46298d/compiler/rustc_codegen_ssa/src/back/link.rs#L1373-L1382
399        let content_bytes = fs::read(arg.trim_start_matches('@'))?;
400        let content = if target_info.is_windows_msvc() {
401            if content_bytes[0..2] != [255, 254] {
402                bail!(
403                    "linker response file `{}` didn't start with a utf16 BOM",
404                    &arg
405                );
406            }
407            let content_utf16: Vec<u16> = content_bytes[2..]
408                .chunks_exact(2)
409                .map(|a| u16::from_ne_bytes([a[0], a[1]]))
410                .collect();
411            String::from_utf16(&content_utf16).with_context(|| {
412                format!(
413                    "linker response file `{}` didn't contain valid utf16 content",
414                    &arg
415                )
416            })?
417        } else {
418            String::from_utf8(content_bytes).with_context(|| {
419                format!(
420                    "linker response file `{}` didn't contain valid utf8 content",
421                    &arg
422                )
423            })?
424        };
425        let mut link_args: Vec<_> = filter_linker_args(
426            content.split('\n').map(|s| s.to_string()),
427            rustc_ver,
428            zig_version,
429            target_info,
430        );
431        if self.has_undefined_dynamic_lookup(&link_args) {
432            link_args.push("-Wl,-undefined=dynamic_lookup".to_string());
433        }
434        if target_info.is_macos() && self.should_add_libcharset(&link_args, zig_version) {
435            link_args.push("-lcharset".to_string());
436        }
437        if target_info.is_windows_msvc() {
438            let new_content = link_args.join("\n");
439            let mut out = Vec::with_capacity((1 + new_content.len()) * 2);
440            // start the stream with a UTF-16 BOM
441            for c in std::iter::once(0xFEFF).chain(new_content.encode_utf16()) {
442                // encode in little endian
443                out.push(c as u8);
444                out.push((c >> 8) as u8);
445            }
446            fs::write(arg.trim_start_matches('@'), out)?;
447        } else {
448            fs::write(arg.trim_start_matches('@'), link_args.join("\n").as_bytes())?;
449        }
450        Ok(arg.to_string())
451    }
452
453    fn filter_linker_arg(
454        &self,
455        arg: &str,
456        rustc_ver: &rustc_version::Version,
457        zig_version: &semver::Version,
458        target_info: &TargetInfo,
459    ) -> FilteredArg {
460        filter_linker_arg(arg, rustc_ver, zig_version, target_info)
461    }
462}
463
464enum FilteredArg {
465    Keep(Vec<String>),
466    Skip,
467    SkipWithNext,
468}
469
470fn filter_linker_args(
471    args: impl IntoIterator<Item = String>,
472    rustc_ver: &rustc_version::Version,
473    zig_version: &semver::Version,
474    target_info: &TargetInfo,
475) -> Vec<String> {
476    let mut result = Vec::new();
477    let mut skip_next = false;
478    for arg in args {
479        if skip_next {
480            skip_next = false;
481            continue;
482        }
483        match filter_linker_arg(&arg, rustc_ver, zig_version, target_info) {
484            FilteredArg::Keep(filtered) => result.extend(filtered),
485            FilteredArg::Skip => {}
486            FilteredArg::SkipWithNext => {
487                skip_next = true;
488            }
489        }
490    }
491    result
492}
493
494fn filter_linker_arg(
495    arg: &str,
496    rustc_ver: &rustc_version::Version,
497    zig_version: &semver::Version,
498    target_info: &TargetInfo,
499) -> FilteredArg {
500    if arg == "-lgcc_s" {
501        return FilteredArg::Keep(vec!["-lunwind".to_string()]);
502    } else if arg.starts_with("--target=") {
503        return FilteredArg::Skip;
504    } else if arg.starts_with("-e") && arg.len() > 2 && !arg.starts_with("-export") {
505        let entry = &arg[2..];
506        return FilteredArg::Keep(vec![format!("-Wl,--entry={}", entry)]);
507    }
508    if (target_info.is_arm() || target_info.is_windows_gnu())
509        && arg.ends_with(".rlib")
510        && arg.contains("libcompiler_builtins-")
511    {
512        return FilteredArg::Skip;
513    }
514    if target_info.is_windows_gnu() {
515        #[allow(clippy::if_same_then_else)]
516        if arg == "-lgcc_eh"
517            && ((zig_version.major, zig_version.minor) < (0, 14) || target_info.is_i686())
518        {
519            return FilteredArg::Keep(vec!["-lc++".to_string()]);
520        } else if arg.ends_with("rsbegin.o") || arg.ends_with("rsend.o") {
521            if target_info.is_i686() {
522                return FilteredArg::Skip;
523            }
524        } else if arg == "-Wl,-Bdynamic" && (zig_version.major, zig_version.minor) >= (0, 11) {
525            return FilteredArg::Keep(vec!["-Wl,-search_paths_first".to_owned()]);
526        } else if arg == "-lwindows" || arg == "-l:libpthread.a" || arg == "-lgcc" {
527            return FilteredArg::Skip;
528        } else if arg == "-Wl,--disable-auto-image-base"
529            || arg == "-Wl,--dynamicbase"
530            || arg == "-Wl,--large-address-aware"
531            || (arg.starts_with("-Wl,")
532                && (arg.ends_with("/list.def") || arg.ends_with("\\list.def")))
533        {
534            return FilteredArg::Skip;
535        } else if arg == "-lmsvcrt" {
536            return FilteredArg::Skip;
537        }
538    } else if arg == "-Wl,--no-undefined-version"
539        || arg == "-Wl,-znostart-stop-gc"
540        // See https://github.com/rust-lang/rust/pull/155453
541        || arg == "-Wl,--fix-cortex-a53-843419"
542        || arg.starts_with("-Wl,-plugin-opt")
543    {
544        return FilteredArg::Skip;
545    }
546    if target_info.is_musl() || target_info.is_ohos() {
547        if (arg.ends_with(".o") && arg.contains("self-contained") && arg.contains("crt"))
548            || arg == "-Wl,-melf_i386"
549        {
550            return FilteredArg::Skip;
551        }
552        if rustc_ver.major == 1
553            && rustc_ver.minor < 59
554            && arg.ends_with(".rlib")
555            && arg.contains("liblibc-")
556        {
557            return FilteredArg::Skip;
558        }
559        if arg == "-lc" {
560            return FilteredArg::Skip;
561        }
562    }
563    // zig cc only supports -Wp,-MD, -Wp,-MMD, and -Wp,-MT;
564    // strip all other -Wp, args (e.g. -Wp,-U_FORTIFY_SOURCE from CMake)
565    // https://github.com/ziglang/zig/blob/0.15.2/src/main.zig#L2798
566    if arg.starts_with("-Wp,")
567        && !arg.starts_with("-Wp,-MD")
568        && !arg.starts_with("-Wp,-MMD")
569        && !arg.starts_with("-Wp,-MT")
570    {
571        return FilteredArg::Skip;
572    }
573    if arg.starts_with("-march=") {
574        if target_info.is_arm() || target_info.is_i386() {
575            return FilteredArg::Skip;
576        } else if target_info.is_riscv64() {
577            return FilteredArg::Keep(vec!["-march=generic_rv64".to_string()]);
578        } else if target_info.is_riscv32() {
579            return FilteredArg::Keep(vec!["-march=generic_rv32".to_string()]);
580        } else if arg.starts_with("-march=armv")
581            && (target_info.is_aarch64() || target_info.is_aarch64_be())
582        {
583            let march_value = arg.strip_prefix("-march=").unwrap();
584            let features = if let Some(pos) = march_value.find('+') {
585                &march_value[pos..]
586            } else {
587                ""
588            };
589            let base_cpu = if target_info.is_apple_platform() {
590                target_info.apple_cpu()
591            } else {
592                "generic"
593            };
594            let mut result = vec![format!("-mcpu={}{}", base_cpu, features)];
595            if features.contains("+crypto") {
596                result.append(&mut vec!["-Xassembler".to_owned(), arg.to_string()]);
597            }
598            return FilteredArg::Keep(result);
599        }
600    }
601    if target_info.is_apple_platform() {
602        if (zig_version.major, zig_version.minor) < (0, 16) {
603            if arg.starts_with("-Wl,-exported_symbols_list,") {
604                return FilteredArg::Skip;
605            }
606            if arg == "-Wl,-exported_symbols_list" {
607                return FilteredArg::SkipWithNext;
608            }
609        }
610        if arg == "-Wl,-dylib" {
611            return FilteredArg::Skip;
612        }
613    }
614    // Handle two-arg form on all platforms (cross-compilation from non-Apple hosts)
615    if (zig_version.major, zig_version.minor) < (0, 16) {
616        if arg == "-Wl,-exported_symbols_list" || arg == "-Wl,--dynamic-list" {
617            return FilteredArg::SkipWithNext;
618        }
619        if arg.starts_with("-Wl,-exported_symbols_list,") || arg.starts_with("-Wl,--dynamic-list,")
620        {
621            return FilteredArg::Skip;
622        }
623    }
624    if target_info.is_freebsd() {
625        let ignored_libs = ["-lkvm", "-lmemstat", "-lprocstat", "-ldevstat"];
626        if ignored_libs.contains(&arg) {
627            return FilteredArg::Skip;
628        }
629    }
630    FilteredArg::Keep(vec![arg.to_string()])
631}
632
633impl Zig {
634    fn has_undefined_dynamic_lookup(&self, args: &[String]) -> bool {
635        let undefined = args
636            .iter()
637            .position(|x| x == "-undefined")
638            .and_then(|i| args.get(i + 1));
639        matches!(undefined, Some(x) if x == "dynamic_lookup")
640    }
641
642    fn should_add_libcharset(&self, args: &[String], zig_version: &semver::Version) -> bool {
643        // See https://github.com/apple-oss-distributions/libiconv/blob/a167071feb7a83a01b27ec8d238590c14eb6faff/xcodeconfig/libiconv.xcconfig
644        if (zig_version.major, zig_version.minor) >= (0, 12) {
645            args.iter().any(|x| x == "-liconv") && !args.iter().any(|x| x == "-lcharset")
646        } else {
647            false
648        }
649    }
650
651    fn add_macos_specific_args(
652        &self,
653        new_cmd_args: &mut Vec<String>,
654        zig_version: &semver::Version,
655    ) -> Result<()> {
656        let sdkroot = Self::macos_sdk_root();
657        if (zig_version.major, zig_version.minor) >= (0, 12) {
658            // Zig 0.12.0+ requires passing `--sysroot`
659            // However, for Zig 0.15+, we should use SDKROOT environment variable instead
660            // to avoid issues with library paths being interpreted relative to sysroot
661            if let Some(ref sdkroot) = sdkroot
662                && (zig_version.major, zig_version.minor) < (0, 15)
663            {
664                new_cmd_args.push(format!("--sysroot={}", sdkroot.display()));
665            }
666            // For Zig >= 0.15, SDKROOT will be set as environment variable
667        }
668        if let Some(ref sdkroot) = sdkroot {
669            if (zig_version.major, zig_version.minor) < (0, 15) {
670                // For zig < 0.15, we need to explicitly add SDK paths with --sysroot
671                new_cmd_args.extend_from_slice(&[
672                    "-isystem".to_string(),
673                    format!("{}", sdkroot.join("usr").join("include").display()),
674                    format!("-L{}", sdkroot.join("usr").join("lib").display()),
675                    format!(
676                        "-F{}",
677                        sdkroot
678                            .join("System")
679                            .join("Library")
680                            .join("Frameworks")
681                            .display()
682                    ),
683                    "-DTARGET_OS_IPHONE=0".to_string(),
684                ]);
685            } else {
686                // For zig >= 0.15 with SDKROOT, we still need to add framework paths
687                // Use -iframework for framework header search
688                new_cmd_args.extend_from_slice(&[
689                    "-isystem".to_string(),
690                    format!("{}", sdkroot.join("usr").join("include").display()),
691                    format!("-L{}", sdkroot.join("usr").join("lib").display()),
692                    format!(
693                        "-F{}",
694                        sdkroot
695                            .join("System")
696                            .join("Library")
697                            .join("Frameworks")
698                            .display()
699                    ),
700                    // Also add the SYSTEM framework search path
701                    "-iframework".to_string(),
702                    format!(
703                        "{}",
704                        sdkroot
705                            .join("System")
706                            .join("Library")
707                            .join("Frameworks")
708                            .display()
709                    ),
710                    "-DTARGET_OS_IPHONE=0".to_string(),
711                ]);
712            }
713        }
714
715        // Add the deps directory that contains `.tbd` files to the library search path
716        let cache_dir = cache_dir();
717        let deps_dir = cache_dir.join("deps");
718        fs::create_dir_all(&deps_dir)?;
719        write_tbd_files(&deps_dir)?;
720        new_cmd_args.push("-L".to_string());
721        new_cmd_args.push(format!("{}", deps_dir.display()));
722        Ok(())
723    }
724
725    /// Execute zig ar/ranlib command
726    pub fn execute_tool(&self, cmd: &str, cmd_args: &[String]) -> Result<()> {
727        let mut child = Self::command()?
728            .arg(cmd)
729            .args(cmd_args)
730            .spawn()
731            .with_context(|| format!("Failed to run `zig {cmd}`"))?;
732        let status = child.wait().expect("Failed to wait on zig child process");
733        if !status.success() {
734            process::exit(status.code().unwrap_or(1));
735        }
736        Ok(())
737    }
738
739    /// Build the zig command line
740    pub fn command() -> Result<Command> {
741        let (zig, zig_args) = Self::find_zig()?;
742        let mut cmd = Command::new(zig);
743        cmd.args(zig_args);
744        Ok(cmd)
745    }
746
747    fn zig_version() -> Result<semver::Version> {
748        static ZIG_VERSION: OnceLock<semver::Version> = OnceLock::new();
749
750        if let Some(version) = ZIG_VERSION.get() {
751            return Ok(version.clone());
752        }
753        // Check for cached version from environment variable first
754        if let Ok(version_str) = env::var("CARGO_ZIGBUILD_ZIG_VERSION")
755            && let Ok(version) = semver::Version::parse(&version_str)
756        {
757            return Ok(ZIG_VERSION.get_or_init(|| version).clone());
758        }
759        let output = Self::command()?.arg("version").output()?;
760        let version_str =
761            str::from_utf8(&output.stdout).context("`zig version` didn't return utf8 output")?;
762        let version = semver::Version::parse(version_str.trim())?;
763        Ok(ZIG_VERSION.get_or_init(|| version).clone())
764    }
765
766    /// Search for `python -m ziglang` first and for `zig` second.
767    pub fn find_zig() -> Result<(PathBuf, Vec<String>)> {
768        static ZIG_PATH: OnceLock<(PathBuf, Vec<String>)> = OnceLock::new();
769
770        if let Some(cached) = ZIG_PATH.get() {
771            return Ok(cached.clone());
772        }
773        let result = Self::find_zig_python()
774            .or_else(|_| Self::find_zig_bin())
775            .context("Failed to find zig")?;
776        Ok(ZIG_PATH.get_or_init(|| result).clone())
777    }
778
779    /// Detect the plain zig binary
780    fn find_zig_bin() -> Result<(PathBuf, Vec<String>)> {
781        let zig_path = zig_path()?;
782        let output = Command::new(&zig_path).arg("version").output()?;
783
784        let version_str = str::from_utf8(&output.stdout).with_context(|| {
785            format!("`{} version` didn't return utf8 output", zig_path.display())
786        })?;
787        Self::validate_zig_version(version_str)?;
788        Ok((zig_path, Vec::new()))
789    }
790
791    /// Detect the Python ziglang package
792    fn find_zig_python() -> Result<(PathBuf, Vec<String>)> {
793        let python_path = python_path()?;
794        let output = Command::new(&python_path)
795            .args(["-m", "ziglang", "version"])
796            .output()?;
797
798        let version_str = str::from_utf8(&output.stdout).with_context(|| {
799            format!(
800                "`{} -m ziglang version` didn't return utf8 output",
801                python_path.display()
802            )
803        })?;
804        Self::validate_zig_version(version_str)?;
805        Ok((python_path, vec!["-m".to_string(), "ziglang".to_string()]))
806    }
807
808    fn validate_zig_version(version: &str) -> Result<()> {
809        let min_ver = semver::Version::new(0, 9, 0);
810        let version = semver::Version::parse(version.trim())?;
811        if version >= min_ver {
812            Ok(())
813        } else {
814            bail!(
815                "zig version {} is too old, need at least {}",
816                version,
817                min_ver
818            )
819        }
820    }
821
822    /// Find zig lib directory
823    pub fn lib_dir() -> Result<PathBuf> {
824        static LIB_DIR: OnceLock<PathBuf> = OnceLock::new();
825
826        if let Some(cached) = LIB_DIR.get() {
827            return Ok(cached.clone());
828        }
829        let (zig, zig_args) = Self::find_zig()?;
830        let zig_version = Self::zig_version()?;
831        let output = Command::new(zig).args(zig_args).arg("env").output()?;
832        let parse_zon_lib_dir = || -> Result<PathBuf> {
833            let output_str =
834                str::from_utf8(&output.stdout).context("`zig env` didn't return utf8 output")?;
835            let lib_dir = output_str
836                .find(".lib_dir")
837                .and_then(|idx| {
838                    let bytes = output_str.as_bytes();
839                    let mut start = idx;
840                    while start < bytes.len() && bytes[start] != b'"' {
841                        start += 1;
842                    }
843                    if start >= bytes.len() {
844                        return None;
845                    }
846                    let mut end = start + 1;
847                    while end < bytes.len() && bytes[end] != b'"' {
848                        end += 1;
849                    }
850                    if end >= bytes.len() {
851                        return None;
852                    }
853                    Some(&output_str[start + 1..end])
854                })
855                .context("Failed to parse lib_dir from `zig env` ZON output")?;
856            Ok(PathBuf::from(lib_dir))
857        };
858        let lib_dir = if zig_version >= semver::Version::new(0, 15, 0) {
859            parse_zon_lib_dir()?
860        } else {
861            serde_json::from_slice::<ZigEnv>(&output.stdout)
862                .map(|zig_env| PathBuf::from(zig_env.lib_dir))
863                .or_else(|_| parse_zon_lib_dir())?
864        };
865        Ok(LIB_DIR.get_or_init(|| lib_dir).clone())
866    }
867
868    fn add_env_if_missing<K, V>(command: &mut Command, name: K, value: V)
869    where
870        K: AsRef<OsStr>,
871        V: AsRef<OsStr>,
872    {
873        let command_env_contains_no_key =
874            |name: &K| !command.get_envs().any(|(key, _)| name.as_ref() == key);
875
876        if command_env_contains_no_key(&name) && env::var_os(&name).is_none() {
877            command.env(name, value);
878        }
879    }
880
881    pub(crate) fn apply_command_env(
882        manifest_path: Option<&Path>,
883        release: bool,
884        cargo: &cargo_options::CommonOptions,
885        cmd: &mut Command,
886        enable_zig_ar: bool,
887    ) -> Result<()> {
888        // setup zig as linker
889        let cargo_config = cargo_config2::Config::load()?;
890        // Use targets from CLI args, or fall back to cargo config's build.target
891        let config_targets;
892        let raw_targets: &[String] = if cargo.target.is_empty() {
893            if let Some(targets) = &cargo_config.build.target {
894                config_targets = targets
895                    .iter()
896                    .map(|t| t.triple().to_string())
897                    .collect::<Vec<_>>();
898                &config_targets
899            } else {
900                &cargo.target
901            }
902        } else {
903            &cargo.target
904        };
905        let rust_targets = raw_targets
906            .iter()
907            .map(|target| target.split_once('.').map(|(t, _)| t).unwrap_or(target))
908            .collect::<Vec<&str>>();
909        let rustc_meta = rustc_version::version_meta()?;
910        Self::add_env_if_missing(
911            cmd,
912            "CARGO_ZIGBUILD_RUSTC_VERSION",
913            rustc_meta.semver.to_string(),
914        );
915        let host_target = &rustc_meta.host;
916        for (parsed_target, raw_target) in rust_targets.iter().zip(raw_targets) {
917            let env_target = parsed_target.replace('-', "_");
918            let zig_wrapper = prepare_zig_linker(raw_target, &cargo_config)?;
919
920            if is_mingw_shell() {
921                let zig_cc = zig_wrapper.cc.to_slash_lossy();
922                let zig_cxx = zig_wrapper.cxx.to_slash_lossy();
923                Self::add_env_if_missing(cmd, format!("CC_{env_target}"), &*zig_cc);
924                Self::add_env_if_missing(cmd, format!("CXX_{env_target}"), &*zig_cxx);
925                if !parsed_target.contains("wasm") {
926                    Self::add_env_if_missing(
927                        cmd,
928                        format!("CARGO_TARGET_{}_LINKER", env_target.to_uppercase()),
929                        &*zig_cc,
930                    );
931                }
932            } else {
933                Self::add_env_if_missing(cmd, format!("CC_{env_target}"), &zig_wrapper.cc);
934                Self::add_env_if_missing(cmd, format!("CXX_{env_target}"), &zig_wrapper.cxx);
935                if !parsed_target.contains("wasm") {
936                    Self::add_env_if_missing(
937                        cmd,
938                        format!("CARGO_TARGET_{}_LINKER", env_target.to_uppercase()),
939                        &zig_wrapper.cc,
940                    );
941                }
942            }
943
944            Self::add_env_if_missing(cmd, format!("RANLIB_{env_target}"), &zig_wrapper.ranlib);
945            // Only setup AR when explicitly asked to
946            // because it need special executable name handling, see src/bin/cargo-zigbuild.rs
947            if enable_zig_ar {
948                if parsed_target.contains("msvc") {
949                    Self::add_env_if_missing(cmd, format!("AR_{env_target}"), &zig_wrapper.lib);
950                } else {
951                    Self::add_env_if_missing(cmd, format!("AR_{env_target}"), &zig_wrapper.ar);
952                }
953            }
954
955            Self::setup_os_deps(manifest_path, release, cargo)?;
956
957            let cmake_toolchain_file_env = format!("CMAKE_TOOLCHAIN_FILE_{env_target}");
958            if env::var_os(&cmake_toolchain_file_env).is_none()
959                && env::var_os(format!("CMAKE_TOOLCHAIN_FILE_{parsed_target}")).is_none()
960                && env::var_os("TARGET_CMAKE_TOOLCHAIN_FILE").is_none()
961                && env::var_os("CMAKE_TOOLCHAIN_FILE").is_none()
962                && let Ok(cmake_toolchain_file) =
963                    Self::setup_cmake_toolchain(parsed_target, &zig_wrapper, enable_zig_ar)
964            {
965                cmd.env(cmake_toolchain_file_env, cmake_toolchain_file);
966            }
967
968            // On Windows, cmake defaults to the Visual Studio generator which ignores
969            // CMAKE_C_COMPILER from the toolchain file. Force Ninja to ensure zig cc
970            // is used for cross-compilation.
971            // See https://github.com/rust-cross/cargo-zigbuild/issues/174
972            if cfg!(target_os = "windows")
973                && env::var_os("CMAKE_GENERATOR").is_none()
974                && which::which("ninja").is_ok()
975            {
976                cmd.env("CMAKE_GENERATOR", "Ninja");
977            }
978
979            if raw_target.contains("windows-gnu") {
980                cmd.env("WINAPI_NO_BUNDLED_LIBRARIES", "1");
981                // Add the cache directory to PATH so rustc can find architecture-specific dlltool
982                // (e.g., x86_64-w64-mingw32-dlltool), but only if no system dlltool exists
983                // If system mingw-w64 dlltool exists, prefer it over zig's dlltool
984                let triple: Triple = parsed_target.parse().unwrap_or_else(|_| Triple::unknown());
985                if !has_system_dlltool(&triple.architecture) {
986                    // zig_wrapper.ar lives in the per-exe wrapper dir
987                    let wrapper_dir = zig_wrapper.ar.parent().unwrap();
988                    let existing_path = env::var_os("PATH").unwrap_or_default();
989                    let paths = std::iter::once(wrapper_dir.to_path_buf())
990                        .chain(env::split_paths(&existing_path));
991                    if let Ok(new_path) = env::join_paths(paths) {
992                        cmd.env("PATH", new_path);
993                    }
994                }
995            }
996
997            if raw_target.contains("apple-darwin")
998                && let Some(sdkroot) = Self::macos_sdk_root()
999                && env::var_os("PKG_CONFIG_SYSROOT_DIR").is_none()
1000            {
1001                // Set PKG_CONFIG_SYSROOT_DIR for pkg-config crate
1002                cmd.env("PKG_CONFIG_SYSROOT_DIR", sdkroot);
1003            }
1004
1005            // Enable unstable `target-applies-to-host` option automatically
1006            // when target is the same as host but may have specified glibc version
1007            if host_target == parsed_target {
1008                if !matches!(rustc_meta.channel, rustc_version::Channel::Nightly) {
1009                    // Hack to use the unstable feature on stable Rust
1010                    // https://github.com/rust-lang/cargo/pull/9753#issuecomment-1022919343
1011                    cmd.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "nightly");
1012                }
1013                cmd.env("CARGO_UNSTABLE_TARGET_APPLIES_TO_HOST", "true");
1014                cmd.env("CARGO_TARGET_APPLIES_TO_HOST", "false");
1015            }
1016
1017            // Pass options used by zig cc down to bindgen, if possible
1018            let mut options = Self::collect_zig_cc_options(&zig_wrapper, raw_target)
1019                .context("Failed to collect `zig cc` options")?;
1020            if raw_target.contains("apple-darwin") {
1021                // everyone seems to miss `#import <TargetConditionals.h>`...
1022                options.push("-DTARGET_OS_IPHONE=0".to_string());
1023            }
1024            let escaped_options = shell_words::join(options.iter().map(|s| &s[..]));
1025            let bindgen_env = "BINDGEN_EXTRA_CLANG_ARGS";
1026            let fallback_value = env::var(bindgen_env);
1027            for target in [&env_target[..], parsed_target] {
1028                let name = format!("{bindgen_env}_{target}");
1029                if let Ok(mut value) = env::var(&name).or(fallback_value.clone()) {
1030                    if shell_words::split(&value).is_err() {
1031                        // bindgen treats the whole string as a single argument if split fails
1032                        value = shell_words::quote(&value).into_owned();
1033                    }
1034                    if !value.is_empty() {
1035                        value.push(' ');
1036                    }
1037                    value.push_str(&escaped_options);
1038                    unsafe { env::set_var(name, value) };
1039                } else {
1040                    unsafe { env::set_var(name, escaped_options.clone()) };
1041                }
1042            }
1043        }
1044        Ok(())
1045    }
1046
1047    /// Collects compiler options used by `zig cc` for given target.
1048    /// Used for the case where `zig cc` cannot be used but underlying options should be retained,
1049    /// for example, as in bindgen (which requires libclang.so and thus is independent from zig).
1050    fn collect_zig_cc_options(zig_wrapper: &ZigWrapper, raw_target: &str) -> Result<Vec<String>> {
1051        #[derive(Debug, PartialEq, Eq)]
1052        enum Kind {
1053            Normal,
1054            Framework,
1055        }
1056
1057        #[derive(Debug)]
1058        struct PerLanguageOptions {
1059            glibc_minor_ver: Option<u32>,
1060            include_paths: Vec<(Kind, String)>,
1061        }
1062
1063        fn collect_per_language_options(
1064            program: &Path,
1065            ext: &str,
1066            raw_target: &str,
1067        ) -> Result<PerLanguageOptions> {
1068            // We can't use `-x c` or `-x c++` because pre-0.11 Zig doesn't handle them
1069            let empty_file_path = cache_dir().join(format!(".intentionally-empty-file.{ext}"));
1070            if !empty_file_path.exists() {
1071                fs::write(&empty_file_path, "")?;
1072            }
1073
1074            let output = Command::new(program)
1075                .arg("-E")
1076                .arg(&empty_file_path)
1077                .arg("-v")
1078                .output()?;
1079            // Clang always generates UTF-8 regardless of locale, so this is okay.
1080            let stderr = String::from_utf8(output.stderr)?;
1081            if !output.status.success() {
1082                bail!(
1083                    "Failed to run `zig cc -v` with status {}: {}",
1084                    output.status,
1085                    stderr.trim(),
1086                );
1087            }
1088
1089            // Collect some macro definitions from cc1 options. We can't directly use
1090            // them though, as we can't distinguish options added by zig from options
1091            // added by clang driver (e.g. `__GCC_HAVE_DWARF2_CFI_ASM`).
1092            let glibc_minor_ver = if let Some(start) = stderr.find("__GLIBC_MINOR__=") {
1093                let stderr = &stderr[start + 16..];
1094                let end = stderr
1095                    .find(|c: char| !c.is_ascii_digit())
1096                    .unwrap_or(stderr.len());
1097                stderr[..end].parse().ok()
1098            } else {
1099                None
1100            };
1101
1102            let start = stderr
1103                .find("#include <...> search starts here:")
1104                .ok_or_else(|| anyhow!("Failed to parse `zig cc -v` output"))?
1105                + 34;
1106            let end = stderr
1107                .find("End of search list.")
1108                .ok_or_else(|| anyhow!("Failed to parse `zig cc -v` output"))?;
1109
1110            let mut include_paths = Vec::new();
1111            for mut line in stderr[start..end].lines() {
1112                line = line.trim();
1113                let mut kind = Kind::Normal;
1114                if line.ends_with(" (framework directory)") {
1115                    line = line[..line.len() - 22].trim();
1116                    kind = Kind::Framework;
1117                } else if line.ends_with(" (headermap)") {
1118                    bail!("C/C++ search path includes header maps, which are not supported");
1119                }
1120                if !line.is_empty() {
1121                    include_paths.push((kind, line.to_owned()));
1122                }
1123            }
1124
1125            // In openharmony, we should add search header path by default which is useful for bindgen.
1126            if raw_target.contains("ohos") {
1127                let ndk = env::var("OHOS_NDK_HOME").expect("Can't get NDK path");
1128                include_paths.push((Kind::Normal, format!("{}/native/sysroot/usr/include", ndk)));
1129            }
1130
1131            Ok(PerLanguageOptions {
1132                include_paths,
1133                glibc_minor_ver,
1134            })
1135        }
1136
1137        let c_opts = collect_per_language_options(&zig_wrapper.cc, "c", raw_target)?;
1138        let cpp_opts = collect_per_language_options(&zig_wrapper.cxx, "cpp", raw_target)?;
1139
1140        // Ensure that `c_opts` and `cpp_opts` are almost identical in the way we expect.
1141        if c_opts.glibc_minor_ver != cpp_opts.glibc_minor_ver {
1142            bail!(
1143                "`zig cc` gives a different glibc minor version for C ({:?}) and C++ ({:?})",
1144                c_opts.glibc_minor_ver,
1145                cpp_opts.glibc_minor_ver,
1146            );
1147        }
1148        let c_paths = c_opts.include_paths;
1149        let mut cpp_paths = cpp_opts.include_paths;
1150        let cpp_pre_len = cpp_paths
1151            .iter()
1152            .position(|p| {
1153                p == c_paths
1154                    .iter()
1155                    .find(|(kind, _)| *kind == Kind::Normal)
1156                    .unwrap()
1157            })
1158            .unwrap_or_default();
1159        let cpp_post_len = cpp_paths.len()
1160            - cpp_paths
1161                .iter()
1162                .position(|p| p == c_paths.last().unwrap())
1163                .unwrap_or_default()
1164            - 1;
1165
1166        // <digression>
1167        //
1168        // So, why we do need all of these?
1169        //
1170        // Bindgen wouldn't look at our `zig cc` (which doesn't contain `libclang.so` anyway),
1171        // but it does collect include paths from the local clang and feed them to `libclang.so`.
1172        // We want those include paths to come from our `zig cc` instead of the local clang.
1173        // There are three main mechanisms possible:
1174        //
1175        // 1. Replace the local clang with our version.
1176        //
1177        //    Bindgen, internally via clang-sys, recognizes `CLANG_PATH` and `PATH`.
1178        //    They are unfortunately a global namespace and simply setting them may break
1179        //    existing build scripts, so we can't confidently override them.
1180        //
1181        //    Clang-sys can also look at target-prefixed clang if arguments contain `-target`.
1182        //    Unfortunately clang-sys can only recognize `-target xxx`, which very slightly
1183        //    differs from what bindgen would pass (`-target=xxx`), so this is not yet possible.
1184        //
1185        //    It should be also noted that we need to collect not only include paths
1186        //    but macro definitions added by Zig, for example `-D__GLIBC_MINOR__`.
1187        //    Clang-sys can't do this yet, so this option seems less robust than we want.
1188        //
1189        // 2. Set the environment variable `BINDGEN_EXTRA_CLANG_ARGS` and let bindgen to
1190        //    append them to arguments passed to `libclang.so`.
1191        //
1192        //    This unfortunately means that we have the same set of arguments for C and C++.
1193        //    Also we have to support older versions of clang, as old as clang 5 (2017).
1194        //    We do have options like `-c-isystem` (cc1 only) and `-cxx-isystem`,
1195        //    but we need to be aware of other options may affect our added options
1196        //    and this requires a nitty gritty of clang driver and cc1---really annoying.
1197        //
1198        // 3. Fix either bindgen or clang-sys or Zig to ease our jobs.
1199        //
1200        //    This is not the option for now because, even after fixes, we have to support
1201        //    older versions of bindgen or Zig which won't have those fixes anyway.
1202        //    But it seems that minor changes to bindgen can indeed fix lots of issues
1203        //    we face, so we are looking for them in the future.
1204        //
1205        // For this reason, we chose the option 2 and overrode `BINDGEN_EXTRA_CLANG_ARGS`.
1206        // The following therefore assumes some understanding about clang option handling,
1207        // including what the heck is cc1 (see the clang FAQ) and how driver options get
1208        // translated to cc1 options (no documentation at all, as it's supposedly unstable).
1209        // Fortunately for us, most (but not all) `-i...` options are passed through cc1.
1210        //
1211        // If you do experience weird compilation errors during bindgen, there's a chance
1212        // that this code has overlooked some edge cases. You can put `.clang_arg("-###")`
1213        // to print the final cc1 options, which would give a lot of information about
1214        // how it got screwed up and help a lot when we fix the issue.
1215        //
1216        // </digression>
1217
1218        let mut args = Vec::new();
1219
1220        // Never include default include directories,
1221        // otherwise `__has_include` will be totally confused.
1222        args.push("-nostdinc".to_owned());
1223
1224        // Add various options for libc++ and glibc.
1225        // Should match what `Compilation.zig` internally does:
1226        //
1227        // https://github.com/ziglang/zig/blob/0.9.0/src/Compilation.zig#L3390-L3427
1228        // https://github.com/ziglang/zig/blob/0.9.1/src/Compilation.zig#L3408-L3445
1229        // https://github.com/ziglang/zig/blob/0.10.0/src/Compilation.zig#L4163-L4211
1230        // https://github.com/ziglang/zig/blob/0.10.1/src/Compilation.zig#L4240-L4288
1231        if raw_target.contains("musl") || raw_target.contains("ohos") {
1232            args.push("-D_LIBCPP_HAS_MUSL_LIBC".to_owned());
1233            // for musl or openharmony
1234            // https://github.com/ziglang/zig/pull/16098
1235            args.push("-D_LARGEFILE64_SOURCE".to_owned());
1236        }
1237        args.extend(
1238            [
1239                "-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS",
1240                "-D_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS",
1241                "-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS",
1242                "-D_LIBCPP_PSTL_CPU_BACKEND_SERIAL",
1243                "-D_LIBCPP_ABI_VERSION=1",
1244                "-D_LIBCPP_ABI_NAMESPACE=__1",
1245                "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST",
1246                // Required by zig 0.15+ libc++ for streambuf and other I/O headers
1247                "-D_LIBCPP_HAS_LOCALIZATION=1",
1248                "-D_LIBCPP_HAS_WIDE_CHARACTERS=1",
1249                "-D_LIBCPP_HAS_UNICODE=1",
1250                "-D_LIBCPP_HAS_THREADS=1",
1251                "-D_LIBCPP_HAS_MONOTONIC_CLOCK",
1252            ]
1253            .into_iter()
1254            .map(ToString::to_string),
1255        );
1256        if let Some(ver) = c_opts.glibc_minor_ver {
1257            // Handled separately because we have no way to infer this without Zig
1258            args.push(format!("-D__GLIBC_MINOR__={ver}"));
1259        }
1260
1261        for (kind, path) in cpp_paths.drain(..cpp_pre_len) {
1262            if kind != Kind::Normal {
1263                // may also be Kind::Framework on macOS
1264                continue;
1265            }
1266            // Ideally this should be `-stdlib++-isystem`, which can be disabled by
1267            // passing `-nostdinc++`, but it is fairly new: https://reviews.llvm.org/D64089
1268            //
1269            // (Also note that `-stdlib++-isystem` is a driver-only option,
1270            // so it will be moved relative to other `-isystem` options against our will.)
1271            args.push("-cxx-isystem".to_owned());
1272            args.push(path);
1273        }
1274
1275        for (kind, path) in c_paths {
1276            match kind {
1277                Kind::Normal => {
1278                    // A normal `-isystem` is preferred over `-cxx-isystem` by cc1...
1279                    args.push("-Xclang".to_owned());
1280                    args.push("-c-isystem".to_owned());
1281                    args.push("-Xclang".to_owned());
1282                    args.push(path.clone());
1283                    args.push("-cxx-isystem".to_owned());
1284                    args.push(path);
1285                }
1286                Kind::Framework => {
1287                    args.push("-iframework".to_owned());
1288                    args.push(path);
1289                }
1290            }
1291        }
1292
1293        for (kind, path) in cpp_paths.drain(cpp_paths.len() - cpp_post_len..) {
1294            assert!(kind == Kind::Normal);
1295            args.push("-cxx-isystem".to_owned());
1296            args.push(path);
1297        }
1298
1299        Ok(args)
1300    }
1301
1302    fn setup_os_deps(
1303        manifest_path: Option<&Path>,
1304        release: bool,
1305        cargo: &cargo_options::CommonOptions,
1306    ) -> Result<()> {
1307        for target in &cargo.target {
1308            if target.contains("apple") {
1309                let target_dir = if let Some(target_dir) = cargo.target_dir.clone() {
1310                    target_dir.join(target)
1311                } else {
1312                    let manifest_path = manifest_path.unwrap_or_else(|| Path::new("Cargo.toml"));
1313                    if !manifest_path.exists() {
1314                        // cargo install doesn't pass a manifest path so `Cargo.toml` in cwd may not exist
1315                        continue;
1316                    }
1317                    let metadata = cargo_metadata::MetadataCommand::new()
1318                        .manifest_path(manifest_path)
1319                        .no_deps()
1320                        .exec()?;
1321                    metadata.target_directory.into_std_path_buf().join(target)
1322                };
1323                let profile = match cargo.profile.as_deref() {
1324                    Some("dev" | "test") => "debug",
1325                    Some("release" | "bench") => "release",
1326                    Some(profile) => profile,
1327                    None => {
1328                        if release {
1329                            "release"
1330                        } else {
1331                            "debug"
1332                        }
1333                    }
1334                };
1335                let deps_dir = target_dir.join(profile).join("deps");
1336                fs::create_dir_all(&deps_dir)?;
1337                if !target_dir.join("CACHEDIR.TAG").is_file() {
1338                    // Create a CACHEDIR.TAG file to exclude target directory from backup
1339                    let _ = write_file(
1340                        &target_dir.join("CACHEDIR.TAG"),
1341                        "Signature: 8a477f597d28d172789f06886806bc55
1342# This file is a cache directory tag created by cargo.
1343# For information about cache directory tags see https://bford.info/cachedir/
1344",
1345                    );
1346                }
1347                write_tbd_files(&deps_dir)?;
1348            } else if target.contains("arm") && target.contains("linux") {
1349                // See https://github.com/ziglang/zig/issues/3287
1350                if let Ok(lib_dir) = Zig::lib_dir() {
1351                    let arm_features_h = lib_dir
1352                        .join("libc")
1353                        .join("glibc")
1354                        .join("sysdeps")
1355                        .join("arm")
1356                        .join("arm-features.h");
1357                    if !arm_features_h.is_file() {
1358                        fs::write(arm_features_h, ARM_FEATURES_H)?;
1359                    }
1360                }
1361            } else if target.contains("windows-gnu")
1362                && let Ok(lib_dir) = Zig::lib_dir()
1363            {
1364                let lib_common = lib_dir.join("libc").join("mingw").join("lib-common");
1365                let synchronization_def = lib_common.join("synchronization.def");
1366                if !synchronization_def.is_file() {
1367                    let api_ms_win_core_synch_l1_2_0_def =
1368                        lib_common.join("api-ms-win-core-synch-l1-2-0.def");
1369                    // Ignore error
1370                    fs::copy(api_ms_win_core_synch_l1_2_0_def, synchronization_def).ok();
1371                }
1372            }
1373        }
1374        Ok(())
1375    }
1376
1377    fn setup_cmake_toolchain(
1378        target: &str,
1379        zig_wrapper: &ZigWrapper,
1380        enable_zig_ar: bool,
1381    ) -> Result<PathBuf> {
1382        // Place cmake toolchain files alongside the other wrappers in the
1383        // per-exe directory to avoid races between parallel builds.
1384        let wrapper_dir = zig_wrapper.cc.parent().unwrap();
1385        let cmake = wrapper_dir.join("cmake");
1386        fs::create_dir_all(&cmake)?;
1387
1388        let toolchain_file = cmake.join(format!("{target}-toolchain.cmake"));
1389        let triple: Triple = target.parse()?;
1390        let os = triple.operating_system.to_string();
1391        let arch = triple.architecture.to_string();
1392        let (system_name, system_processor) = match (os.as_str(), arch.as_str()) {
1393            ("darwin", "x86_64") => ("Darwin", "x86_64"),
1394            ("darwin", "aarch64") => ("Darwin", "arm64"),
1395            ("linux", arch) => {
1396                let cmake_arch = match arch {
1397                    "powerpc" => "ppc",
1398                    "powerpc64" => "ppc64",
1399                    "powerpc64le" => "ppc64le",
1400                    _ => arch,
1401                };
1402                ("Linux", cmake_arch)
1403            }
1404            ("windows", "x86_64") => ("Windows", "AMD64"),
1405            ("windows", "i686") => ("Windows", "X86"),
1406            ("windows", "aarch64") => ("Windows", "ARM64"),
1407            (os, arch) => (os, arch),
1408        };
1409        let mut content = format!(
1410            r#"
1411set(CMAKE_SYSTEM_NAME {system_name})
1412set(CMAKE_SYSTEM_PROCESSOR {system_processor})
1413set(CMAKE_C_COMPILER {cc})
1414set(CMAKE_CXX_COMPILER {cxx})
1415set(CMAKE_RANLIB {ranlib})
1416set(CMAKE_C_LINKER_DEPFILE_SUPPORTED FALSE)
1417set(CMAKE_CXX_LINKER_DEPFILE_SUPPORTED FALSE)"#,
1418            system_name = system_name,
1419            system_processor = system_processor,
1420            cc = zig_wrapper.cc.to_slash_lossy(),
1421            cxx = zig_wrapper.cxx.to_slash_lossy(),
1422            ranlib = zig_wrapper.ranlib.to_slash_lossy(),
1423        );
1424        if enable_zig_ar {
1425            content.push_str(&format!(
1426                "\nset(CMAKE_AR {})\n",
1427                zig_wrapper.ar.to_slash_lossy()
1428            ));
1429        }
1430        // When cross-compiling to Darwin from a non-macOS host, CMake requires
1431        // install_name_tool and otool which don't exist on Linux/Windows.
1432        // Provide our own install_name_tool implementation via symlink wrapper,
1433        // and a no-op script for otool (not needed for builds) if no system otool exists.
1434        if system_name == "Darwin" && !cfg!(target_os = "macos") {
1435            let exe_ext = if cfg!(windows) { ".exe" } else { "" };
1436            let install_name_tool = wrapper_dir.join(format!("install_name_tool{exe_ext}"));
1437            symlink_wrapper(&install_name_tool)?;
1438            content.push_str(&format!(
1439                "\nset(CMAKE_INSTALL_NAME_TOOL {})",
1440                install_name_tool.to_slash_lossy()
1441            ));
1442
1443            if which::which("otool").is_err() {
1444                let script_ext = if cfg!(windows) { "bat" } else { "sh" };
1445                let otool = cmake.join(format!("otool.{script_ext}"));
1446                write_noop_script(&otool)?;
1447                content.push_str(&format!("\nset(CMAKE_OTOOL {})", otool.to_slash_lossy()));
1448            }
1449        }
1450        // Prevent cmake from searching the host system's include and library paths,
1451        // which can conflict with zig's bundled headers (e.g. __COLD in sys/cdefs.h).
1452        // See https://github.com/rust-cross/cargo-zigbuild/issues/268
1453        content.push_str(
1454            r#"
1455set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
1456set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
1457set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
1458set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)"#,
1459        );
1460        write_file(&toolchain_file, &content)?;
1461        Ok(toolchain_file)
1462    }
1463
1464    #[cfg(target_os = "macos")]
1465    fn macos_sdk_root() -> Option<PathBuf> {
1466        static SDK_ROOT: OnceLock<Option<PathBuf>> = OnceLock::new();
1467
1468        SDK_ROOT
1469            .get_or_init(|| match env::var_os("SDKROOT") {
1470                Some(sdkroot) if !sdkroot.is_empty() => Some(sdkroot.into()),
1471                _ => {
1472                    let output = Command::new("xcrun")
1473                        .args(["--sdk", "macosx", "--show-sdk-path"])
1474                        .output()
1475                        .ok()?;
1476                    if output.status.success() {
1477                        let stdout = String::from_utf8(output.stdout).ok()?;
1478                        let stdout = stdout.trim();
1479                        if !stdout.is_empty() {
1480                            return Some(stdout.into());
1481                        }
1482                    }
1483                    None
1484                }
1485            })
1486            .clone()
1487    }
1488
1489    #[cfg(not(target_os = "macos"))]
1490    fn macos_sdk_root() -> Option<PathBuf> {
1491        match env::var_os("SDKROOT") {
1492            Some(sdkroot) if !sdkroot.is_empty() => Some(sdkroot.into()),
1493            _ => None,
1494        }
1495    }
1496}
1497
1498fn write_file(path: &Path, content: &str) -> Result<(), anyhow::Error> {
1499    let existing_content = fs::read_to_string(path).unwrap_or_default();
1500    if existing_content != content {
1501        fs::write(path, content)?;
1502    }
1503    Ok(())
1504}
1505
1506/// Write a no-op shell/batch script for use as a placeholder tool.
1507/// Used for macOS-specific tools (install_name_tool, otool) when cross-compiling
1508/// to Darwin from non-macOS hosts.
1509#[cfg(target_family = "unix")]
1510fn write_noop_script(path: &Path) -> Result<()> {
1511    let content = "#!/bin/sh\nexit 0\n";
1512    let existing = fs::read_to_string(path).unwrap_or_default();
1513    if existing != content {
1514        OpenOptions::new()
1515            .create(true)
1516            .write(true)
1517            .truncate(true)
1518            .mode(0o700)
1519            .open(path)?
1520            .write_all(content.as_bytes())?;
1521    }
1522    Ok(())
1523}
1524
1525#[cfg(not(target_family = "unix"))]
1526fn write_noop_script(path: &Path) -> Result<()> {
1527    let content = "@echo off\r\nexit /b 0\r\n";
1528    let existing = fs::read_to_string(path).unwrap_or_default();
1529    if existing != content {
1530        fs::write(path, content)?;
1531    }
1532    Ok(())
1533}
1534
1535fn write_tbd_files(deps_dir: &Path) -> Result<(), anyhow::Error> {
1536    write_file(&deps_dir.join("libiconv.tbd"), LIBICONV_TBD)?;
1537    write_file(&deps_dir.join("libcharset.1.tbd"), LIBCHARSET_TBD)?;
1538    write_file(&deps_dir.join("libcharset.tbd"), LIBCHARSET_TBD)?;
1539    Ok(())
1540}
1541
1542fn cache_dir() -> PathBuf {
1543    env::var("CARGO_ZIGBUILD_CACHE_DIR")
1544        .ok()
1545        .map(|s| s.into())
1546        .or_else(dirs::cache_dir)
1547        // If the really is no cache dir, cwd will also do
1548        .unwrap_or_else(|| env::current_dir().expect("Failed to get current dir"))
1549        .join(env!("CARGO_PKG_NAME"))
1550        .join(env!("CARGO_PKG_VERSION"))
1551}
1552
1553#[derive(Debug, Deserialize)]
1554struct ZigEnv {
1555    lib_dir: String,
1556}
1557
1558/// zig wrapper paths
1559#[derive(Debug, Clone)]
1560pub struct ZigWrapper {
1561    pub cc: PathBuf,
1562    pub cxx: PathBuf,
1563    pub ar: PathBuf,
1564    pub ranlib: PathBuf,
1565    pub lib: PathBuf,
1566}
1567
1568#[derive(Debug, Clone, Default, PartialEq)]
1569struct TargetFlags {
1570    pub target_cpu: String,
1571    pub target_feature: String,
1572}
1573
1574impl TargetFlags {
1575    pub fn parse_from_encoded(encoded: &OsStr) -> Result<Self> {
1576        let mut parsed = Self::default();
1577
1578        let f = rustflags::from_encoded(encoded);
1579        for flag in f {
1580            if let rustflags::Flag::Codegen { opt, value } = flag {
1581                let key = opt.replace('-', "_");
1582                match key.as_str() {
1583                    "target_cpu" => {
1584                        if let Some(value) = value {
1585                            parsed.target_cpu = value;
1586                        }
1587                    }
1588                    "target_feature" => {
1589                        // See https://github.com/rust-lang/rust/blob/7e3ba5b8b7556073ab69822cc36b93d6e74cd8c9/compiler/rustc_session/src/options.rs#L1233
1590                        if let Some(value) = value {
1591                            if !parsed.target_feature.is_empty() {
1592                                parsed.target_feature.push(',');
1593                            }
1594                            parsed.target_feature.push_str(&value);
1595                        }
1596                    }
1597                    _ => {}
1598                }
1599            }
1600        }
1601        Ok(parsed)
1602    }
1603}
1604
1605/// Prepare wrapper scripts for `zig cc` and `zig c++` and returns their paths
1606///
1607/// We want to use `zig cc` as linker and c compiler. We want to call `python -m ziglang cc`, but
1608/// cargo only accepts a path to an executable as linker, so we add a wrapper script. We then also
1609/// use the wrapper script to pass arguments and substitute an unsupported argument.
1610///
1611/// We create different files for different args because otherwise cargo might skip recompiling even
1612/// if the linker target changed
1613#[allow(clippy::blocks_in_conditions)]
1614pub fn prepare_zig_linker(
1615    target: &str,
1616    cargo_config: &cargo_config2::Config,
1617) -> Result<ZigWrapper> {
1618    let (rust_target, abi_suffix) = target.split_once('.').unwrap_or((target, ""));
1619    let abi_suffix = if abi_suffix.is_empty() {
1620        String::new()
1621    } else {
1622        if abi_suffix
1623            .split_once('.')
1624            .filter(|(x, y)| {
1625                !x.is_empty()
1626                    && x.chars().all(|c| c.is_ascii_digit())
1627                    && !y.is_empty()
1628                    && y.chars().all(|c| c.is_ascii_digit())
1629            })
1630            .is_none()
1631        {
1632            bail!("Malformed zig target abi suffix.")
1633        }
1634        format!(".{abi_suffix}")
1635    };
1636    let triple: Triple = rust_target
1637        .parse()
1638        .with_context(|| format!("Unsupported Rust target '{rust_target}'"))?;
1639    let arch = triple.architecture.to_string();
1640    let target_env = match (triple.architecture, triple.environment) {
1641        (Architecture::Mips32(..), Environment::Gnu) => Environment::Gnueabihf,
1642        (Architecture::Powerpc, Environment::Gnu) => Environment::Gnueabihf,
1643        (_, Environment::GnuLlvm) => Environment::Gnu,
1644        (_, environment) => environment,
1645    };
1646    let file_ext = if cfg!(windows) { "bat" } else { "sh" };
1647    let file_target = target.trim_end_matches('.');
1648
1649    let mut cc_args = vec![
1650        // prevent stripping
1651        "-g".to_owned(),
1652        // disable sanitizers
1653        "-fno-sanitize=all".to_owned(),
1654    ];
1655
1656    // TODO: Maybe better to assign mcpu according to:
1657    // rustc --target <target> -Z unstable-options --print target-spec-json
1658    let zig_mcpu_default = match triple.operating_system {
1659        OperatingSystem::Linux => {
1660            match arch.as_str() {
1661                // zig uses _ instead of - in cpu features
1662                "arm" => match target_env {
1663                    Environment::Gnueabi | Environment::Musleabi => "generic+v6+strict_align",
1664                    Environment::Gnueabihf | Environment::Musleabihf => {
1665                        "generic+v6+strict_align+vfp2-d32"
1666                    }
1667                    _ => "",
1668                },
1669                "armv5te" => "generic+soft_float+strict_align",
1670                "armv7" => "generic+v7a+vfp3-d32+thumb2-neon",
1671                arch_str @ ("i586" | "i686") => {
1672                    if arch_str == "i586" {
1673                        "pentium"
1674                    } else {
1675                        "pentium4"
1676                    }
1677                }
1678                "riscv64gc" => "generic_rv64+m+a+f+d+c",
1679                "s390x" => "z10-vector",
1680                _ => "",
1681            }
1682        }
1683        _ => "",
1684    };
1685
1686    // Override mcpu from RUSTFLAGS if provided. The override happens when
1687    // commands like `cargo-zigbuild build` are invoked.
1688    // Currently we only override according to target_cpu.
1689    let zig_mcpu_override = {
1690        let rust_flags = cargo_config.rustflags(rust_target)?.unwrap_or_default();
1691        let encoded_rust_flags = rust_flags.encode()?;
1692        let target_flags = TargetFlags::parse_from_encoded(OsStr::new(&encoded_rust_flags))?;
1693        // Note: zig uses _ instead of - for target_cpu and target_feature
1694        // target_cpu may be empty string, which means target_cpu is not specified.
1695        target_flags.target_cpu.replace('-', "_")
1696    };
1697
1698    if !zig_mcpu_override.is_empty() {
1699        cc_args.push(format!("-mcpu={zig_mcpu_override}"));
1700    } else if !zig_mcpu_default.is_empty() {
1701        cc_args.push(format!("-mcpu={zig_mcpu_default}"));
1702    }
1703
1704    match triple.operating_system {
1705        OperatingSystem::Linux => {
1706            let zig_arch = match arch.as_str() {
1707                // zig uses _ instead of - in cpu features
1708                "arm" => "arm",
1709                "armv5te" => "arm",
1710                "armv7" => "arm",
1711                "i586" | "i686" => {
1712                    let zig_version = Zig::zig_version()?;
1713                    if zig_version.major == 0 && zig_version.minor >= 11 {
1714                        "x86"
1715                    } else {
1716                        "i386"
1717                    }
1718                }
1719                "riscv64gc" => "riscv64",
1720                "s390x" => "s390x",
1721                _ => arch.as_str(),
1722            };
1723            let mut zig_target_env = target_env.to_string();
1724
1725            let zig_version = Zig::zig_version()?;
1726
1727            // Since Zig 0.15.0, arm-linux-ohos changed to arm-linux-ohoseabi
1728            // We need to follow the change but target_lexicon follow the LLVM target(https://github.com/bytecodealliance/target-lexicon/pull/123).
1729            // So we use string directly.
1730            if zig_version >= semver::Version::new(0, 15, 0)
1731                && arch.as_str() == "armv7"
1732                && target_env == Environment::Ohos
1733            {
1734                zig_target_env = "ohoseabi".to_string();
1735            }
1736
1737            cc_args.push("-target".to_string());
1738            cc_args.push(format!("{zig_arch}-linux-{zig_target_env}{abi_suffix}"));
1739        }
1740        OperatingSystem::MacOSX { .. } | OperatingSystem::Darwin(_) => {
1741            let zig_version = Zig::zig_version()?;
1742            // Zig 0.10.0 switched macOS ABI to none
1743            // see https://github.com/ziglang/zig/pull/11684
1744            if zig_version > semver::Version::new(0, 9, 1) {
1745                cc_args.push("-target".to_string());
1746                cc_args.push(format!("{arch}-macos-none{abi_suffix}"));
1747            } else {
1748                cc_args.push("-target".to_string());
1749                cc_args.push(format!("{arch}-macos-gnu{abi_suffix}"));
1750            }
1751        }
1752        OperatingSystem::Windows => {
1753            let zig_arch = match arch.as_str() {
1754                "i686" => {
1755                    let zig_version = Zig::zig_version()?;
1756                    if zig_version.major == 0 && zig_version.minor >= 11 {
1757                        "x86"
1758                    } else {
1759                        "i386"
1760                    }
1761                }
1762                arch => arch,
1763            };
1764            cc_args.push("-target".to_string());
1765            cc_args.push(format!("{zig_arch}-windows-{target_env}{abi_suffix}"));
1766        }
1767        OperatingSystem::Emscripten => {
1768            cc_args.push("-target".to_string());
1769            cc_args.push(format!("{arch}-emscripten{abi_suffix}"));
1770        }
1771        OperatingSystem::Wasi => {
1772            cc_args.push("-target".to_string());
1773            cc_args.push(format!("{arch}-wasi{abi_suffix}"));
1774        }
1775        OperatingSystem::WasiP1 => {
1776            cc_args.push("-target".to_string());
1777            cc_args.push(format!("{arch}-wasi.0.1.0{abi_suffix}"));
1778        }
1779        OperatingSystem::IOS(_) if triple.environment == Environment::Macabi => {
1780            // Mac Catalyst (aarch64-apple-ios-macabi / x86_64-apple-ios-macabi)
1781            // maps to zig's maccatalyst target
1782            cc_args.push("-target".to_string());
1783            cc_args.push(format!("{arch}-maccatalyst-none{abi_suffix}"));
1784        }
1785        OperatingSystem::Freebsd => {
1786            let zig_arch = match arch.as_str() {
1787                "i686" => {
1788                    let zig_version = Zig::zig_version()?;
1789                    if zig_version.major == 0 && zig_version.minor >= 11 {
1790                        "x86"
1791                    } else {
1792                        "i386"
1793                    }
1794                }
1795                arch => arch,
1796            };
1797            cc_args.push("-target".to_string());
1798            cc_args.push(format!("{zig_arch}-freebsd"));
1799        }
1800        OperatingSystem::Openbsd => {
1801            cc_args.push("-target".to_string());
1802            cc_args.push(format!("{arch}-openbsd"));
1803        }
1804        OperatingSystem::Unknown => {
1805            if triple.architecture == Architecture::Wasm32
1806                || triple.architecture == Architecture::Wasm64
1807            {
1808                cc_args.push("-target".to_string());
1809                cc_args.push(format!("{arch}-freestanding{abi_suffix}"));
1810            } else {
1811                bail!("unsupported target '{rust_target}'")
1812            }
1813        }
1814        _ => bail!(format!("unsupported target '{rust_target}'")),
1815    };
1816
1817    let zig_linker_dir = cache_dir();
1818    fs::create_dir_all(&zig_linker_dir)?;
1819
1820    if triple.operating_system == OperatingSystem::Linux {
1821        if matches!(
1822            triple.environment,
1823            Environment::Gnu
1824                | Environment::Gnuspe
1825                | Environment::Gnux32
1826                | Environment::Gnueabi
1827                | Environment::Gnuabi64
1828                | Environment::GnuIlp32
1829                | Environment::Gnueabihf
1830        ) {
1831            let glibc_version = if abi_suffix.is_empty() {
1832                (2, 17)
1833            } else {
1834                let mut parts = abi_suffix[1..].split('.');
1835                let major: usize = parts.next().unwrap().parse()?;
1836                let minor: usize = parts.next().unwrap().parse()?;
1837                (major, minor)
1838            };
1839            // See https://github.com/ziglang/zig/issues/9485
1840            if glibc_version < (2, 28) {
1841                use crate::linux::{FCNTL_H, FCNTL_MAP};
1842
1843                let zig_version = Zig::zig_version()?;
1844                if zig_version.major == 0 && zig_version.minor < 11 {
1845                    let fcntl_map = zig_linker_dir.join("fcntl.map");
1846                    let existing_content = fs::read_to_string(&fcntl_map).unwrap_or_default();
1847                    if existing_content != FCNTL_MAP {
1848                        fs::write(&fcntl_map, FCNTL_MAP)?;
1849                    }
1850                    let fcntl_h = zig_linker_dir.join("fcntl.h");
1851                    let existing_content = fs::read_to_string(&fcntl_h).unwrap_or_default();
1852                    if existing_content != FCNTL_H {
1853                        fs::write(&fcntl_h, FCNTL_H)?;
1854                    }
1855
1856                    cc_args.push(format!("-Wl,--version-script={}", fcntl_map.display()));
1857                    cc_args.push("-include".to_string());
1858                    cc_args.push(fcntl_h.display().to_string());
1859                }
1860            }
1861        } else if matches!(
1862            triple.environment,
1863            Environment::Musl
1864                | Environment::Muslabi64
1865                | Environment::Musleabi
1866                | Environment::Musleabihf
1867        ) {
1868            use crate::linux::MUSL_WEAK_SYMBOLS_MAPPING_SCRIPT;
1869
1870            let zig_version = Zig::zig_version()?;
1871            let rustc_version = rustc_version::version_meta()?.semver;
1872
1873            // as zig 0.11.0 is released, its musl has been upgraded to 1.2.4 with break changes
1874            // but rust is still with musl 1.2.3
1875            // we need this workaround before rust 1.72
1876            // https://github.com/ziglang/zig/pull/16098
1877            if (zig_version.major, zig_version.minor) >= (0, 11)
1878                && (rustc_version.major, rustc_version.minor) < (1, 72)
1879            {
1880                let weak_symbols_map = zig_linker_dir.join("musl_weak_symbols_map.ld");
1881                fs::write(&weak_symbols_map, MUSL_WEAK_SYMBOLS_MAPPING_SCRIPT)?;
1882
1883                cc_args.push(format!("-Wl,-T,{}", weak_symbols_map.display()));
1884            }
1885        }
1886    }
1887
1888    // Use platform-specific quoting: shell_words for Unix (single quotes),
1889    // custom quoting for Windows batch files (double quotes)
1890    let cc_args_str = join_args_for_script(&cc_args);
1891
1892    // Put all generated wrappers and symlinks in a per-exe subdirectory so
1893    // that parallel builds driven by different binaries (e.g. multiple maturin
1894    // instances in separate temp venvs) never clobber each other.
1895    // See https://github.com/rust-cross/cargo-zigbuild/issues/318
1896    let current_exe = resolve_current_exe()?;
1897    let exe_hash = crc::Crc::<u16>::new(&crc::CRC_16_IBM_SDLC)
1898        .checksum(current_exe.as_os_str().as_encoded_bytes());
1899    let wrapper_dir = zig_linker_dir
1900        .join("wrappers")
1901        .join(format!("{:x}", exe_hash));
1902    fs::create_dir_all(&wrapper_dir)?;
1903
1904    let hash = crc::Crc::<u16>::new(&crc::CRC_16_IBM_SDLC).checksum(cc_args_str.as_bytes());
1905    let zig_cc = wrapper_dir.join(format!("zigcc-{file_target}-{:x}.{file_ext}", hash));
1906    let zig_cxx = wrapper_dir.join(format!("zigcxx-{file_target}-{:x}.{file_ext}", hash));
1907    let zig_ranlib = wrapper_dir.join(format!("zigranlib.{file_ext}"));
1908    let zig_version = Zig::zig_version()?;
1909    write_linker_wrapper(&zig_cc, "cc", &cc_args_str, &zig_version)?;
1910    write_linker_wrapper(&zig_cxx, "c++", &cc_args_str, &zig_version)?;
1911    write_linker_wrapper(&zig_ranlib, "ranlib", "", &zig_version)?;
1912
1913    let exe_ext = if cfg!(windows) { ".exe" } else { "" };
1914    let zig_ar = wrapper_dir.join(format!("ar{exe_ext}"));
1915    symlink_wrapper(&zig_ar)?;
1916    let zig_lib = wrapper_dir.join(format!("lib{exe_ext}"));
1917    symlink_wrapper(&zig_lib)?;
1918
1919    // Create dlltool symlinks for Windows GNU targets, but only if no system dlltool exists
1920    // On Windows hosts, rustc looks for "dlltool.exe"
1921    // On non-Windows hosts, rustc looks for architecture-specific names
1922    //
1923    // See https://github.com/rust-lang/rust/blob/a18e6d9d1473d9b25581dd04bef6c7577999631c/compiler/rustc_codegen_ssa/src/back/archive.rs#L275-L309
1924    if matches!(triple.operating_system, OperatingSystem::Windows)
1925        && matches!(triple.environment, Environment::Gnu)
1926    {
1927        // Only create zig dlltool wrapper if no system dlltool is found
1928        // System dlltool (from mingw-w64) handles raw-dylib better than zig's dlltool
1929        if !has_system_dlltool(&triple.architecture) {
1930            let dlltool_name = get_dlltool_name(&triple.architecture);
1931            let zig_dlltool = wrapper_dir.join(format!("{dlltool_name}{exe_ext}"));
1932            symlink_wrapper(&zig_dlltool)?;
1933        }
1934    }
1935
1936    Ok(ZigWrapper {
1937        cc: zig_cc,
1938        cxx: zig_cxx,
1939        ar: zig_ar,
1940        ranlib: zig_ranlib,
1941        lib: zig_lib,
1942    })
1943}
1944
1945/// Resolve the current executable path, preferring the test override env var.
1946fn resolve_current_exe() -> Result<PathBuf> {
1947    if let Ok(exe) = env::var("CARGO_BIN_EXE_cargo-zigbuild") {
1948        Ok(PathBuf::from(exe))
1949    } else {
1950        Ok(env::current_exe()?)
1951    }
1952}
1953
1954fn symlink_wrapper(target: &Path) -> Result<()> {
1955    let current_exe = resolve_current_exe()?;
1956    #[cfg(windows)]
1957    {
1958        if !target.exists() {
1959            // symlink on Windows requires admin privileges so we use hardlink instead
1960            if std::fs::hard_link(&current_exe, target).is_err() {
1961                // hard_link doesn't support cross-device links so we fallback to copy
1962                std::fs::copy(&current_exe, target)?;
1963            }
1964        }
1965    }
1966
1967    #[cfg(unix)]
1968    {
1969        if !target.exists() {
1970            if fs::read_link(target).is_ok() {
1971                // remove broken symlink
1972                fs::remove_file(target)?;
1973            }
1974            std::os::unix::fs::symlink(current_exe, target)?;
1975        }
1976    }
1977    Ok(())
1978}
1979
1980/// Join arguments for Unix shell script using shell_words (single quotes)
1981#[cfg(target_family = "unix")]
1982fn join_args_for_script<I, S>(args: I) -> String
1983where
1984    I: IntoIterator<Item = S>,
1985    S: AsRef<str>,
1986{
1987    shell_words::join(args)
1988}
1989
1990/// Quote a string for Windows batch file (cmd.exe)
1991///
1992/// - `%` expands even inside quotes, so we escape it as `%%`.
1993/// - We disable delayed expansion in the wrapper script, so `!` should not expand.
1994/// - Internal `"` are escaped by doubling them (`""`).
1995#[cfg(not(target_family = "unix"))]
1996fn quote_for_batch(s: &str) -> String {
1997    let needs_quoting_or_escaping = s.is_empty()
1998        || s.contains(|c: char| {
1999            matches!(
2000                c,
2001                ' ' | '\t' | '"' | '&' | '|' | '<' | '>' | '^' | '%' | '(' | ')' | '!'
2002            )
2003        });
2004
2005    if !needs_quoting_or_escaping {
2006        return s.to_string();
2007    }
2008
2009    let mut out = String::with_capacity(s.len() + 8);
2010    out.push('"');
2011    for c in s.chars() {
2012        match c {
2013            '"' => out.push_str("\"\""),
2014            '%' => out.push_str("%%"),
2015            _ => out.push(c),
2016        }
2017    }
2018    out.push('"');
2019    out
2020}
2021
2022/// Join arguments for Windows batch file using double quotes
2023#[cfg(not(target_family = "unix"))]
2024fn join_args_for_script<I, S>(args: I) -> String
2025where
2026    I: IntoIterator<Item = S>,
2027    S: AsRef<str>,
2028{
2029    args.into_iter()
2030        .map(|s| quote_for_batch(s.as_ref()))
2031        .collect::<Vec<_>>()
2032        .join(" ")
2033}
2034
2035/// Write a zig cc wrapper batch script for unix
2036#[cfg(target_family = "unix")]
2037fn write_linker_wrapper(
2038    path: &Path,
2039    command: &str,
2040    args: &str,
2041    zig_version: &semver::Version,
2042) -> Result<()> {
2043    let mut buf = Vec::<u8>::new();
2044    let current_exe = resolve_current_exe()?;
2045    writeln!(&mut buf, "#!/bin/sh")?;
2046
2047    // Export zig version to avoid spawning `zig version` subprocess
2048    writeln!(
2049        &mut buf,
2050        "export CARGO_ZIGBUILD_ZIG_VERSION={}",
2051        zig_version
2052    )?;
2053
2054    // Pass through SDKROOT if it exists at runtime
2055    writeln!(&mut buf, "if [ -n \"$SDKROOT\" ]; then export SDKROOT; fi")?;
2056
2057    writeln!(
2058        &mut buf,
2059        "exec \"{}\" zig {} -- {} \"$@\"",
2060        current_exe.display(),
2061        command,
2062        args
2063    )?;
2064
2065    // Try not to write the file again if it's already the same.
2066    // This is more friendly for cache systems like ccache, which by default
2067    // uses mtime to determine if a recompilation is needed.
2068    let existing_content = fs::read(path).unwrap_or_default();
2069    if existing_content != buf {
2070        OpenOptions::new()
2071            .create(true)
2072            .write(true)
2073            .truncate(true)
2074            .mode(0o700)
2075            .open(path)?
2076            .write_all(&buf)?;
2077    }
2078    Ok(())
2079}
2080
2081/// Write a zig cc wrapper batch script for windows
2082#[cfg(not(target_family = "unix"))]
2083fn write_linker_wrapper(
2084    path: &Path,
2085    command: &str,
2086    args: &str,
2087    zig_version: &semver::Version,
2088) -> Result<()> {
2089    let mut buf = Vec::<u8>::new();
2090    let current_exe = resolve_current_exe()?;
2091    let current_exe = if is_mingw_shell() {
2092        current_exe.to_slash_lossy().to_string()
2093    } else {
2094        current_exe.display().to_string()
2095    };
2096    writeln!(&mut buf, "@echo off")?;
2097    // Prevent `!VAR!` expansion surprises (delayed expansion) in user-controlled args.
2098    writeln!(&mut buf, "setlocal DisableDelayedExpansion")?;
2099    // Set zig version to avoid spawning `zig version` subprocess
2100    writeln!(&mut buf, "set CARGO_ZIGBUILD_ZIG_VERSION={}", zig_version)?;
2101    writeln!(
2102        &mut buf,
2103        "\"{}\" zig {} -- {} %*",
2104        adjust_canonicalization(current_exe),
2105        command,
2106        args
2107    )?;
2108
2109    let existing_content = fs::read(path).unwrap_or_default();
2110    if existing_content != buf {
2111        fs::write(path, buf)?;
2112    }
2113    Ok(())
2114}
2115
2116pub(crate) fn is_mingw_shell() -> bool {
2117    env::var_os("MSYSTEM").is_some() && env::var_os("SHELL").is_some()
2118}
2119
2120// https://stackoverflow.com/a/50323079/3549270
2121#[cfg(target_os = "windows")]
2122pub fn adjust_canonicalization(p: String) -> String {
2123    const VERBATIM_PREFIX: &str = r#"\\?\"#;
2124    if p.starts_with(VERBATIM_PREFIX) {
2125        p[VERBATIM_PREFIX.len()..].to_string()
2126    } else {
2127        p
2128    }
2129}
2130
2131fn python_path() -> Result<PathBuf> {
2132    let python = env::var("CARGO_ZIGBUILD_PYTHON_PATH").unwrap_or_else(|_| "python3".to_string());
2133    Ok(which::which(python)?)
2134}
2135
2136fn zig_path() -> Result<PathBuf> {
2137    let zig = env::var("CARGO_ZIGBUILD_ZIG_PATH").unwrap_or_else(|_| "zig".to_string());
2138    Ok(which::which(zig)?)
2139}
2140
2141/// Get the dlltool executable name for the given architecture
2142/// On Windows, rustc looks for "dlltool.exe"
2143/// On non-Windows hosts, rustc looks for architecture-specific names
2144fn get_dlltool_name(arch: &Architecture) -> &'static str {
2145    if cfg!(windows) {
2146        "dlltool"
2147    } else {
2148        match arch {
2149            Architecture::X86_64 => "x86_64-w64-mingw32-dlltool",
2150            Architecture::X86_32(_) => "i686-w64-mingw32-dlltool",
2151            Architecture::Aarch64(_) => "aarch64-w64-mingw32-dlltool",
2152            _ => "dlltool",
2153        }
2154    }
2155}
2156
2157/// Check if a dlltool for the given architecture exists in PATH
2158/// Returns true if found, false otherwise
2159fn has_system_dlltool(arch: &Architecture) -> bool {
2160    which::which(get_dlltool_name(arch)).is_ok()
2161}
2162
2163#[cfg(test)]
2164mod tests {
2165    use super::*;
2166
2167    #[test]
2168    fn test_target_flags() {
2169        let cases = [
2170            // Input, TargetCPU, TargetFeature
2171            ("-C target-feature=-crt-static", "", "-crt-static"),
2172            ("-C target-cpu=native", "native", ""),
2173            (
2174                "--deny warnings --codegen target-feature=+crt-static",
2175                "",
2176                "+crt-static",
2177            ),
2178            ("-C target_cpu=skylake-avx512", "skylake-avx512", ""),
2179            ("-Ctarget_cpu=x86-64-v3", "x86-64-v3", ""),
2180            (
2181                "-C target-cpu=native --cfg foo -C target-feature=-avx512bf16,-avx512bitalg",
2182                "native",
2183                "-avx512bf16,-avx512bitalg",
2184            ),
2185            (
2186                "--target x86_64-unknown-linux-gnu --codegen=target-cpu=x --codegen=target-cpu=x86-64",
2187                "x86-64",
2188                "",
2189            ),
2190            (
2191                "-Ctarget-feature=+crt-static -Ctarget-feature=+avx",
2192                "",
2193                "+crt-static,+avx",
2194            ),
2195        ];
2196
2197        for (input, expected_target_cpu, expected_target_feature) in cases.iter() {
2198            let args = cargo_config2::Flags::from_space_separated(input);
2199            let encoded_rust_flags = args.encode().unwrap();
2200            let flags = TargetFlags::parse_from_encoded(OsStr::new(&encoded_rust_flags)).unwrap();
2201            assert_eq!(flags.target_cpu, *expected_target_cpu, "{}", input);
2202            assert_eq!(flags.target_feature, *expected_target_feature, "{}", input);
2203        }
2204    }
2205
2206    #[test]
2207    fn test_join_args_for_script() {
2208        // Test basic arguments without special characters
2209        let args = vec!["-target", "x86_64-linux-gnu"];
2210        let result = join_args_for_script(&args);
2211        assert!(result.contains("-target"));
2212        assert!(result.contains("x86_64-linux-gnu"));
2213    }
2214
2215    #[test]
2216    #[cfg(not(target_family = "unix"))]
2217    fn test_quote_for_batch() {
2218        // Simple argument without special characters - no quoting needed
2219        assert_eq!(quote_for_batch("-target"), "-target");
2220        assert_eq!(quote_for_batch("x86_64-linux-gnu"), "x86_64-linux-gnu");
2221
2222        // Arguments with spaces need quoting
2223        assert_eq!(
2224            quote_for_batch("C:\\Users\\John Doe\\path"),
2225            "\"C:\\Users\\John Doe\\path\""
2226        );
2227
2228        // Empty string needs quoting
2229        assert_eq!(quote_for_batch(""), "\"\"");
2230
2231        // Arguments with special batch characters need quoting
2232        assert_eq!(quote_for_batch("foo&bar"), "\"foo&bar\"");
2233        assert_eq!(quote_for_batch("foo|bar"), "\"foo|bar\"");
2234        assert_eq!(quote_for_batch("foo<bar"), "\"foo<bar\"");
2235        assert_eq!(quote_for_batch("foo>bar"), "\"foo>bar\"");
2236        assert_eq!(quote_for_batch("foo^bar"), "\"foo^bar\"");
2237        assert_eq!(quote_for_batch("foo%bar"), "\"foo%bar\"");
2238
2239        // Internal double quotes are escaped by doubling
2240        assert_eq!(quote_for_batch("foo\"bar"), "\"foo\"\"bar\"");
2241    }
2242
2243    #[test]
2244    #[cfg(not(target_family = "unix"))]
2245    fn test_join_args_for_script_windows() {
2246        // Test with path containing spaces
2247        let args = vec![
2248            "-target",
2249            "x86_64-linux-gnu",
2250            "-L",
2251            "C:\\Users\\John Doe\\path",
2252        ];
2253        let result = join_args_for_script(&args);
2254        // The path with space should be quoted
2255        assert!(result.contains("\"C:\\Users\\John Doe\\path\""));
2256        // Simple args should not be quoted
2257        assert!(result.contains("-target"));
2258        assert!(!result.contains("\"-target\""));
2259    }
2260
2261    fn make_rustc_ver(major: u64, minor: u64, patch: u64) -> rustc_version::Version {
2262        rustc_version::Version::new(major, minor, patch)
2263    }
2264
2265    fn make_zig_ver(major: u64, minor: u64, patch: u64) -> semver::Version {
2266        semver::Version::new(major, minor, patch)
2267    }
2268
2269    fn run_filter(args: &[&str], target: Option<&str>, zig_ver: (u64, u64)) -> Vec<String> {
2270        let rustc_ver = make_rustc_ver(1, 80, 0);
2271        let zig_version = make_zig_ver(0, zig_ver.0, zig_ver.1);
2272        let target_info = TargetInfo::new(target.map(|s| s.to_string()).as_ref());
2273        filter_linker_args(
2274            args.iter().map(|s| s.to_string()),
2275            &rustc_ver,
2276            &zig_version,
2277            &target_info,
2278        )
2279    }
2280
2281    fn run_filter_one(arg: &str, target: Option<&str>, zig_ver: (u64, u64)) -> Vec<String> {
2282        run_filter(&[arg], target, zig_ver)
2283    }
2284
2285    fn run_filter_one_rustc(
2286        arg: &str,
2287        target: Option<&str>,
2288        zig_ver: (u64, u64),
2289        rustc_minor: u64,
2290    ) -> Vec<String> {
2291        let rustc_ver = make_rustc_ver(1, rustc_minor, 0);
2292        let zig_version = make_zig_ver(0, zig_ver.0, zig_ver.1);
2293        let target_info = TargetInfo::new(target.map(|s| s.to_string()).as_ref());
2294        filter_linker_args(
2295            std::iter::once(arg.to_string()),
2296            &rustc_ver,
2297            &zig_version,
2298            &target_info,
2299        )
2300    }
2301
2302    #[test]
2303    fn test_filter_common_replacements() {
2304        let linux = Some("x86_64-unknown-linux-gnu");
2305        // -lgcc_s -> -lunwind
2306        assert_eq!(run_filter_one("-lgcc_s", linux, (13, 0)), vec!["-lunwind"]);
2307        // --target= stripped (already passed via -target)
2308        assert!(run_filter_one("--target=x86_64-unknown-linux-gnu", linux, (13, 0)).is_empty());
2309        // -e<entry> transformed to -Wl,--entry=<entry>
2310        assert_eq!(
2311            run_filter_one("-emain", linux, (13, 0)),
2312            vec!["-Wl,--entry=main"]
2313        );
2314        // -export-* should NOT be transformed
2315        assert_eq!(
2316            run_filter_one("-export-dynamic", linux, (13, 0)),
2317            vec!["-export-dynamic"]
2318        );
2319    }
2320
2321    #[test]
2322    fn test_filter_compiler_builtins_removed() {
2323        for target in &["armv7-unknown-linux-gnueabihf", "x86_64-pc-windows-gnu"] {
2324            let result = run_filter_one(
2325                "/path/to/libcompiler_builtins-abc123.rlib",
2326                Some(target),
2327                (13, 0),
2328            );
2329            assert!(
2330                result.is_empty(),
2331                "compiler_builtins should be removed for {target}"
2332            );
2333        }
2334    }
2335
2336    #[test]
2337    fn test_filter_windows_gnu_args() {
2338        let gnu = Some("x86_64-pc-windows-gnu");
2339        // Args that should be removed entirely
2340        let removed: &[&str] = &[
2341            "-lwindows",
2342            "-l:libpthread.a",
2343            "-lgcc",
2344            "-Wl,--disable-auto-image-base",
2345            "-Wl,--dynamicbase",
2346            "-Wl,--large-address-aware",
2347            "-Wl,/path/to/list.def",
2348            "-Wl,C:\\path\\to\\list.def",
2349            "-lmsvcrt",
2350        ];
2351        for arg in removed {
2352            let result = run_filter_one(arg, gnu, (13, 0));
2353            assert!(result.is_empty(), "{arg} should be removed for windows-gnu");
2354        }
2355        // Args that get replaced
2356        let replaced: &[(&str, (u64, u64), &str)] = &[
2357            ("-lgcc_eh", (13, 0), "-lc++"),
2358            ("-Wl,-Bdynamic", (13, 0), "-Wl,-search_paths_first"),
2359        ];
2360        for (arg, zig_ver, expected) in replaced {
2361            let result = run_filter_one(arg, gnu, *zig_ver);
2362            assert_eq!(result, vec![*expected], "filter({arg})");
2363        }
2364        // -lgcc_eh kept on zig >= 0.14 for x86_64
2365        let result = run_filter_one("-lgcc_eh", gnu, (14, 0));
2366        assert_eq!(result, vec!["-lgcc_eh"]);
2367    }
2368
2369    #[test]
2370    fn test_filter_windows_gnu_rsbegin() {
2371        // i686: rsbegin.o filtered out
2372        let result = run_filter_one("/path/to/rsbegin.o", Some("i686-pc-windows-gnu"), (13, 0));
2373        assert!(result.is_empty());
2374        // x86_64: rsbegin.o kept
2375        let result = run_filter_one("/path/to/rsbegin.o", Some("x86_64-pc-windows-gnu"), (13, 0));
2376        assert_eq!(result, vec!["/path/to/rsbegin.o"]);
2377    }
2378
2379    #[test]
2380    fn test_filter_unsupported_linker_args() {
2381        let linux = Some("x86_64-unknown-linux-gnu");
2382        let removed: &[&str] = &[
2383            "-Wl,--no-undefined-version",
2384            "-Wl,-znostart-stop-gc",
2385            "-Wl,--fix-cortex-a53-843419",
2386            "-Wl,-plugin-opt=O2",
2387        ];
2388        for arg in removed {
2389            let result = run_filter_one(arg, linux, (13, 0));
2390            assert!(result.is_empty(), "{arg} should be removed");
2391        }
2392    }
2393
2394    #[test]
2395    fn test_filter_wp_args() {
2396        let linux = Some("x86_64-unknown-linux-gnu");
2397        // Unsupported -Wp, args should be removed
2398        for arg in &[
2399            "-Wp,-U_FORTIFY_SOURCE",
2400            "-Wp,-DFOO=1",
2401            "-Wp,-MF,/tmp/t.d",
2402            "-Wp,-MQ,foo",
2403            "-Wp,-MP",
2404        ] {
2405            let result = run_filter_one(arg, linux, (13, 0));
2406            assert!(result.is_empty(), "{arg} should be removed");
2407        }
2408        // Supported -Wp, args should be kept (-MD, -MMD, -MT)
2409        for arg in &["-Wp,-MD,/tmp/test.d", "-Wp,-MMD,/tmp/test.d", "-Wp,-MT,foo"] {
2410            let result = run_filter_one(arg, linux, (13, 0));
2411            assert_eq!(result, vec![*arg], "{arg} should be kept");
2412        }
2413        // bare -U and -D should be kept (zig cc supports them directly)
2414        let result = run_filter_one("-U_FORTIFY_SOURCE", linux, (13, 0));
2415        assert_eq!(result, vec!["-U_FORTIFY_SOURCE"]);
2416        let result = run_filter_one("-DFOO=1", linux, (13, 0));
2417        assert_eq!(result, vec!["-DFOO=1"]);
2418    }
2419
2420    #[test]
2421    fn test_filter_musl_args() {
2422        let musl = Some("x86_64-unknown-linux-musl");
2423        let removed: &[&str] = &["/path/self-contained/crt1.o", "-lc"];
2424        for arg in removed {
2425            let result = run_filter_one(arg, musl, (13, 0));
2426            assert!(result.is_empty(), "{arg} should be removed for musl");
2427        }
2428        // -Wl,-melf_i386 for i686 musl
2429        let result = run_filter_one("-Wl,-melf_i386", Some("i686-unknown-linux-musl"), (13, 0));
2430        assert!(result.is_empty());
2431        // liblibc removed for old rustc (<1.59), kept for new
2432        let result = run_filter_one_rustc("/path/to/liblibc-abc123.rlib", musl, (13, 0), 58);
2433        assert!(result.is_empty());
2434        let result = run_filter_one_rustc("/path/to/liblibc-abc123.rlib", musl, (13, 0), 59);
2435        assert_eq!(result, vec!["/path/to/liblibc-abc123.rlib"]);
2436    }
2437
2438    #[test]
2439    fn test_filter_march_args() {
2440        // (input, target, expected)
2441        let cases: &[(&str, &str, &[&str])] = &[
2442            // arm: removed
2443            ("-march=armv7-a", "armv7-unknown-linux-gnueabihf", &[]),
2444            // riscv64: replaced
2445            (
2446                "-march=rv64gc",
2447                "riscv64gc-unknown-linux-gnu",
2448                &["-march=generic_rv64"],
2449            ),
2450            // riscv32: replaced
2451            (
2452                "-march=rv32imac",
2453                "riscv32imac-unknown-none-elf",
2454                &["-march=generic_rv32"],
2455            ),
2456            // aarch64 armv: converted to -mcpu=generic
2457            (
2458                "-march=armv8.4-a",
2459                "aarch64-unknown-linux-gnu",
2460                &["-mcpu=generic"],
2461            ),
2462            // aarch64 armv with crypto: adds -Xassembler
2463            (
2464                "-march=armv8.4-a+crypto",
2465                "aarch64-unknown-linux-gnu",
2466                &[
2467                    "-mcpu=generic+crypto",
2468                    "-Xassembler",
2469                    "-march=armv8.4-a+crypto",
2470                ],
2471            ),
2472            // apple aarch64: uses apple cpu name
2473            (
2474                "-march=armv8.4-a",
2475                "aarch64-apple-darwin",
2476                &["-mcpu=apple_m1"],
2477            ),
2478        ];
2479        for (input, target, expected) in cases {
2480            let result = run_filter_one(input, Some(target), (13, 0));
2481            assert_eq!(&result, expected, "filter({input}, {target})");
2482        }
2483    }
2484
2485    #[test]
2486    fn test_filter_apple_args() {
2487        let darwin = Some("aarch64-apple-darwin");
2488        let result = run_filter_one("-Wl,-dylib", darwin, (13, 0));
2489        assert!(result.is_empty());
2490    }
2491
2492    #[test]
2493    fn test_filter_freebsd_libs_removed() {
2494        for lib in &["-lkvm", "-lmemstat", "-lprocstat", "-ldevstat"] {
2495            let result = run_filter_one(lib, Some("x86_64-unknown-freebsd"), (13, 0));
2496            assert!(result.is_empty(), "{lib} should be removed for freebsd");
2497        }
2498    }
2499
2500    #[test]
2501    fn test_filter_exported_symbols_list_two_arg_apple() {
2502        let result = run_filter(
2503            &[
2504                "-arch",
2505                "arm64",
2506                "-Wl,-exported_symbols_list",
2507                "-Wl,/tmp/rustcXXX/list",
2508                "-o",
2509                "output.dylib",
2510            ],
2511            Some("aarch64-apple-darwin"),
2512            (13, 0),
2513        );
2514        assert_eq!(result, vec!["-arch", "arm64", "-o", "output.dylib"]);
2515    }
2516
2517    #[test]
2518    fn test_filter_exported_symbols_list_two_arg_cross_platform() {
2519        let result = run_filter(
2520            &[
2521                "-arch",
2522                "arm64",
2523                "-Wl,-exported_symbols_list",
2524                "-Wl,C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\rustcXXX\\list",
2525                "-o",
2526                "output.dylib",
2527            ],
2528            None,
2529            (13, 0),
2530        );
2531        assert_eq!(result, vec!["-arch", "arm64", "-o", "output.dylib"]);
2532    }
2533
2534    #[test]
2535    fn test_filter_exported_symbols_list_single_arg_comma() {
2536        let result = run_filter(
2537            &[
2538                "-Wl,-exported_symbols_list,/tmp/rustcXXX/list",
2539                "-o",
2540                "output.dylib",
2541            ],
2542            Some("aarch64-apple-darwin"),
2543            (13, 0),
2544        );
2545        assert_eq!(result, vec!["-o", "output.dylib"]);
2546    }
2547
2548    #[test]
2549    fn test_filter_exported_symbols_list_not_filtered_zig_016() {
2550        let result = run_filter(
2551            &[
2552                "-Wl,-exported_symbols_list",
2553                "-Wl,/tmp/rustcXXX/list",
2554                "-o",
2555                "output.dylib",
2556            ],
2557            Some("aarch64-apple-darwin"),
2558            (16, 0),
2559        );
2560        assert_eq!(
2561            result,
2562            vec![
2563                "-Wl,-exported_symbols_list",
2564                "-Wl,/tmp/rustcXXX/list",
2565                "-o",
2566                "output.dylib"
2567            ]
2568        );
2569    }
2570
2571    #[test]
2572    fn test_filter_dynamic_list_two_arg() {
2573        let result = run_filter(
2574            &[
2575                "-Wl,--dynamic-list",
2576                "-Wl,/tmp/rustcXXX/list",
2577                "-o",
2578                "output.so",
2579            ],
2580            Some("x86_64-unknown-linux-gnu"),
2581            (13, 0),
2582        );
2583        assert_eq!(result, vec!["-o", "output.so"]);
2584    }
2585
2586    #[test]
2587    fn test_filter_dynamic_list_single_arg_comma() {
2588        let result = run_filter(
2589            &["-Wl,--dynamic-list,/tmp/rustcXXX/list", "-o", "output.so"],
2590            Some("x86_64-unknown-linux-gnu"),
2591            (13, 0),
2592        );
2593        assert_eq!(result, vec!["-o", "output.so"]);
2594    }
2595
2596    #[test]
2597    fn test_filter_preserves_normal_args() {
2598        let result = run_filter(
2599            &["-arch", "arm64", "-lSystem", "-lc", "-o", "output"],
2600            Some("aarch64-apple-darwin"),
2601            (13, 0),
2602        );
2603        assert_eq!(
2604            result,
2605            vec!["-arch", "arm64", "-lSystem", "-lc", "-o", "output"]
2606        );
2607    }
2608
2609    #[test]
2610    fn test_filter_skip_next_at_end_of_args() {
2611        let result = run_filter(
2612            &["-o", "output", "-Wl,-exported_symbols_list"],
2613            Some("aarch64-apple-darwin"),
2614            (13, 0),
2615        );
2616        assert_eq!(result, vec!["-o", "output"]);
2617    }
2618}