How do I exclude a file from being built on OS X?
Asked Answered
C

2

6

I have src/bin/linux-only.rs which does some things which work on Linux only (e.g. libc bindings which only exist on Linux). I want to exclude that file from being built on OS X.

I started putting #[cfg(target_os = "linux")] on every block in linux-only.rs but that is cluttering up the source code beyond any reason.

Is there a nicer way to do this?

Clinkstone answered 27/9, 2016 at 19:56 Comment(0)
P
10

Writing #![cfg(target_os = "linux")] (note the exclamation mark) at the top of the file will work for the whole file (as long as it contains a single module), not just for the next block (item). Source: Rust reference.

Edit: if you can move that file into its own crate, you could take advantage of Cargo's platform-specific dependencies.

Parental answered 27/9, 2016 at 20:3 Comment(4)
Sure! Now it's clear. With the exclamation it applies to the whole "block", as it's at the top of the file it apples to the whole module. Thanks.Clinkstone
one problem remains though: cargo build complains now with error: main function not found. How would I avoid that?Clinkstone
@PhilippKeller: You can provide a #[cfg(not(target_os = "linux"))] fn main() {}, however I fear here that annotating within the module is messy. Are you sure there is no way to simply tell cargo to only consider the file on Linux?Enate
I couldn't find anything in the cargo.toml docs. The thing that comes close is the [doc.crates.io/manifest.html#the-features-section](features) section, but that's for handing over arguments which were given with cargo to the #[cfg attributesClinkstone
C
0

it looks like that's currently not possible. I filed a feature request

Clinkstone answered 30/9, 2016 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.