FAQ - Help and questions

How do I add a dependency that is only used on Windows or only on macOS?

#cargo#cfg#windows#macospublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Put it in a target-specific table in Cargo.toml, for example [target.'cfg(windows)'.dependencies], and guard the code that uses it with the same cfg. Cargo then does not build the crate on other systems. The Windows case was built and run; the macOS table uses the same syntax and was not built here.

Checked with rustc 1.93.0, cargo 1.93.0, Windows 11, windows-sys 0.61.2; built in release mode and run on Windows. No macOS build was made.

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

A crate for the Windows API is useless on a Mac, and a crate for Apple's frameworks cannot build on Windows. Cargo lets you attach dependencies to a target condition, using the same cfg expressions as the code:

Cargo.tomltoml
[package]
name = "demo"
version = "0.1.0"
edition = "2021"

# Only compiled and linked on Windows:
[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.61", features = ["Win32_UI_WindowsAndMessaging"] }

# The same syntax for macOS (an example; not built here):
# [target.'cfg(target_os = "macos")'.dependencies]
# some-macos-crate = "1"
src/main.rsrust
#[cfg(windows)]
fn beep_name() -> &'static str {
    // Referencing the constant proves the crate is really linked on this platform.
    let _ = windows_sys::Win32::UI::WindowsAndMessaging::MB_OK;
    "windows-sys is available"
}

#[cfg(not(windows))]
fn beep_name() -> &'static str {
    "no windows-sys on this system"
}

fn main() {
    println!("{}", beep_name());
}
text
windows-sys is available

The dependency is declared for Windows only, so on another system Cargo does not compile it; the code that uses it must be guarded by the matching #[cfg], or the other system's build fails with "unresolved crate".

Other forms that work the same way: [target.'cfg(unix)'.dependencies], [target.'cfg(not(windows))'.dependencies], and a full target triple such as [target.x86_64-pc-windows-msvc.dependencies]. The same applies to [target.….build-dependencies] and [target.….dev-dependencies].

Cargo's Cargo.lock still records every dependency for every system, so the lock file is the same on every machine and builds stay reproducible.

Common mistakes#

  • Putting the dependency in [dependencies] and guarding only the code. Cargo still downloads and builds it on every system, and it may fail to build.
  • Enabling every feature of a big API crate. Turn on only the API areas you call (Win32_UI_WindowsAndMessaging above); build time falls.
  • Writing cfg(macos). There is no such cfg; use cfg(target_os = "macos").

Can I check the macOS build on a Windows PC?#

For pure-Rust code you can ask the compiler to type-check for another target if the standard library for that target is installed (with rustup, rustup target add aarch64-apple-darwin, then cargo check --target aarch64-apple-darwin). That was not tried here, because this machine's toolchain was installed without rustup. Crates that need Apple's SDK (linking, C code) will not build without it, so keep a macOS CI job.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet