Skip to content

Commit bf93bc0

Browse files
vdagonneauasomers
authored andcommitted
feature-gate most Nix functions
Using features reduces build time and size for consumer crates. By default all features are enabled.
1 parent da49e4f commit bf93bc0

20 files changed

+755
-141
lines changed

.cirrus.yml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ build: &BUILD
2020
- $TOOL +$TOOLCHAIN $BUILD $ZFLAGS --target $TARGET --all-targets
2121
- $TOOL +$TOOLCHAIN doc $ZFLAGS --no-deps --target $TARGET
2222
- $TOOL +$TOOLCHAIN clippy $ZFLAGS --target $TARGET -- $CLIPPYFLAGS
23+
- if [ -z "$NOHACK" ]; then $TOOL install cargo-hack; fi
24+
- if [ -z "$NOHACK" ]; then $TOOL hack $ZFLAGS check --target $TARGET --each-feature; fi
2325

2426
# Tests that do require executing the binaries
2527
test: &TEST
@@ -50,14 +52,16 @@ task:
5052
- cargo build --target i686-unknown-freebsd
5153
- cargo doc --no-deps --target i686-unknown-freebsd
5254
- cargo test --target i686-unknown-freebsd
55+
i386_feature_script:
56+
- . $HOME/.cargo/env
57+
- cargo hack check --each-feature --target i686-unknown-freebsd
5358
before_cache_script: rm -rf $CARGO_HOME/registry/index
5459

5560
# Test OSX in a full VM
5661
task:
57-
matrix:
58-
- name: OSX x86_64
59-
env:
60-
TARGET: x86_64-apple-darwin
62+
name: OSX x86_64
63+
env:
64+
TARGET: x86_64-apple-darwin
6165
osx_instance:
6266
image: catalina-xcode
6367
setup_script:
@@ -192,13 +196,17 @@ task:
192196
# https://github.com/rust-embedded/cross/issues/535
193197
- name: iOS aarch64
194198
env:
199+
# cargo hack tries to invoke the iphonesimulator SDK for iOS
200+
NOHACK: 1
195201
TARGET: aarch64-apple-ios
196202
# Rustup only supports cross-building from arbitrary hosts for iOS at
197203
# 1.49.0 and above. Below that it's possible to cross-build from an OSX
198204
# host, but OSX VMs are more expensive than Linux VMs.
199205
TOOLCHAIN: 1.49.0
200206
- name: iOS x86_64
201207
env:
208+
# cargo hack tries to invoke the iphonesimulator SDK for iOS
209+
NOHACK: 1
202210
TARGET: x86_64-apple-ios
203211
TOOLCHAIN: 1.49.0
204212
# Cross testing on powerpc fails with "undefined reference to renameat2".
@@ -222,6 +230,8 @@ task:
222230
TARGET: x86_64-unknown-netbsd
223231
- name: Redox x86_64
224232
env:
233+
# cargo-hack needs a newer compiler
234+
NOHACK: 1
225235
TARGET: x86_64-unknown-redox
226236
# Redox requires a nightly compiler.
227237
# If stuff breaks, change nightly to the date in the toolchain_*
@@ -269,6 +279,6 @@ task:
269279
image: rustlang/rust:nightly
270280
setup_script:
271281
- cargo update -Zminimal-versions
272-
script:
282+
check_script:
273283
- cargo check
274284
before_cache_script: rm -rf $CARGO_HOME/registry/index

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
66
## [0.23.0] - 2021-09-28
77
### Added
88

9+
- Added fine-grained features flags. Most Nix functionality can now be
10+
conditionally enabled. By default, all features are enabled.
11+
(#[TODO](https://github.com/nix-rust/nix/pull/TODO))
912
- Added the `LocalPeerCred` sockopt.
1013
(#[1482](https://github.com/nix-rust/nix/pull/1482))
1114
- Added `TimeSpec::from_duration` and `TimeSpec::from_timespec`

Cargo.toml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ categories = ["os::unix-apis"]
1010
include = ["src/**/*", "LICENSE", "README.md", "CHANGELOG.md"]
1111

1212
[package.metadata.docs.rs]
13+
rustdoc-args = ["--cfg", "docsrs"]
1314
targets = [
1415
"x86_64-unknown-linux-gnu",
1516
"aarch64-linux-android",
@@ -32,6 +33,52 @@ cfg-if = "1.0"
3233
[target.'cfg(not(target_os = "redox"))'.dependencies]
3334
memoffset = "0.6.3"
3435

36+
[features]
37+
default = [
38+
"acct", "aio", "dir", "env", "event", "features", "fs",
39+
"hostname",
40+
"inotify", "ioctl", "kmod", "mman", "mount", "mqueue",
41+
"net", "personality", "process", "poll", "pthread", "ptrace", "quota",
42+
"reboot",
43+
"resource", "sched", "signal",
44+
"socket",
45+
"term", "time", "ucontext", "uio",
46+
"users", "zerocopy",
47+
]
48+
49+
acct = []
50+
aio = []
51+
dir = ["fs"]
52+
env = []
53+
event = []
54+
features = []
55+
fs = []
56+
hostname = []
57+
inotify = []
58+
ioctl = []
59+
kmod = []
60+
mman = []
61+
mount = ["uio"]
62+
mqueue = ["fs"]
63+
net = ["socket"]
64+
personality = []
65+
poll = []
66+
pthread = []
67+
ptrace = ["process"]
68+
quota = []
69+
process = []
70+
reboot = []
71+
resource = []
72+
sched = ["process"]
73+
signal = ["process"]
74+
socket = []
75+
term = []
76+
time = []
77+
ucontext = ["signal"]
78+
uio = []
79+
users = ["features"]
80+
zerocopy = ["fs", "uio"]
81+
3582
[target.'cfg(target_os = "dragonfly")'.build-dependencies]
3683
cc = "1"
3784

src/fcntl.rs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ use std::ffi::OsString;
55
use std::os::raw;
66
use std::os::unix::ffi::OsStringExt;
77
use std::os::unix::io::RawFd;
8-
use crate::sys::stat::Mode;
9-
use crate::{NixPath, Result};
108

119
#[cfg(any(target_os = "android", target_os = "linux"))]
1210
use std::ptr; // For splice and copy_file_range
13-
#[cfg(any(target_os = "android", target_os = "linux"))]
14-
use crate::sys::uio::IoVec; // For vmsplice
11+
#[cfg(feature = "fs")]
12+
use crate::{
13+
NixPath,
14+
Result,
15+
sys::stat::Mode
16+
};
1517

1618
#[cfg(any(
1719
target_os = "linux",
@@ -22,10 +24,13 @@ use crate::sys::uio::IoVec; // For vmsplice
2224
target_env = "uclibc",
2325
target_os = "freebsd"
2426
))]
25-
pub use self::posix_fadvise::*;
27+
#[cfg(feature = "fs")]
28+
pub use self::posix_fadvise::{PosixFadviseAdvice, posix_fadvise};
2629

2730
#[cfg(not(target_os = "redox"))]
31+
#[cfg(any(feature = "fs", feature = "process"))]
2832
libc_bitflags! {
33+
#[cfg_attr(docsrs, doc(cfg(any(feature = "fs", feature = "process"))))]
2934
pub struct AtFlags: c_int {
3035
AT_REMOVEDIR;
3136
AT_SYMLINK_FOLLOW;
@@ -39,8 +44,10 @@ libc_bitflags! {
3944
}
4045
}
4146

47+
#[cfg(any(feature = "fs", feature = "term"))]
4248
libc_bitflags!(
4349
/// Configuration options for opened files.
50+
#[cfg_attr(docsrs, doc(cfg(any(feature = "fs", feature = "term"))))]
4451
pub struct OFlag: c_int {
4552
/// Mask for the access mode of the file.
4653
O_ACCMODE;
@@ -165,6 +172,9 @@ libc_bitflags!(
165172
}
166173
);
167174

175+
feature!{
176+
#![feature = "fs"]
177+
168178
// The conversion is not identical on all operating systems.
169179
#[allow(clippy::useless_conversion)]
170180
pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
@@ -209,19 +219,24 @@ pub fn renameat<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(
209219
})??;
210220
Errno::result(res).map(drop)
211221
}
222+
}
212223

213224
#[cfg(all(
214225
target_os = "linux",
215226
target_env = "gnu",
216227
))]
228+
#[cfg(feature = "fs")]
217229
libc_bitflags! {
230+
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
218231
pub struct RenameFlags: u32 {
219232
RENAME_EXCHANGE;
220233
RENAME_NOREPLACE;
221234
RENAME_WHITEOUT;
222235
}
223236
}
224237

238+
feature!{
239+
#![feature = "fs"]
225240
#[cfg(all(
226241
target_os = "linux",
227242
target_env = "gnu",
@@ -348,6 +363,7 @@ pub(crate) fn at_rawfd(fd: Option<RawFd>) -> raw::c_int {
348363
Some(fd) => fd,
349364
}
350365
}
366+
}
351367

352368
#[cfg(any(target_os = "android", target_os = "linux"))]
353369
libc_bitflags!(
@@ -364,14 +380,19 @@ libc_bitflags!(
364380
}
365381
);
366382

383+
#[cfg(feature = "fs")]
367384
libc_bitflags!(
368385
/// Additional configuration flags for `fcntl`'s `F_SETFD`.
386+
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
369387
pub struct FdFlag: c_int {
370388
/// The file descriptor will automatically be closed during a successful `execve(2)`.
371389
FD_CLOEXEC;
372390
}
373391
);
374392

393+
feature!{
394+
#![feature = "fs"]
395+
375396
#[cfg(not(target_os = "redox"))]
376397
#[derive(Debug, Eq, Hash, PartialEq)]
377398
#[non_exhaustive]
@@ -455,6 +476,7 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
455476
Errno::result(res)
456477
}
457478

479+
// TODO: convert to libc_enum
458480
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
459481
#[non_exhaustive]
460482
pub enum FlockArg {
@@ -483,10 +505,13 @@ pub fn flock(fd: RawFd, arg: FlockArg) -> Result<()> {
483505

484506
Errno::result(res).map(drop)
485507
}
508+
}
486509

487510
#[cfg(any(target_os = "android", target_os = "linux"))]
511+
#[cfg(feature = "zerocopy")]
488512
libc_bitflags! {
489513
/// Additional flags to `splice` and friends.
514+
#[cfg_attr(docsrs, doc(cfg(feature = "zerocopy")))]
490515
pub struct SpliceFFlags: c_uint {
491516
/// Request that pages be moved instead of copied.
492517
///
@@ -505,6 +530,9 @@ libc_bitflags! {
505530
}
506531
}
507532

533+
feature!{
534+
#![feature = "zerocopy"]
535+
508536
/// Copy a range of data from one file to another
509537
///
510538
/// The `copy_file_range` system call performs an in-kernel copy between
@@ -577,7 +605,12 @@ pub fn tee(fd_in: RawFd, fd_out: RawFd, len: usize, flags: SpliceFFlags) -> Resu
577605
}
578606

579607
#[cfg(any(target_os = "linux", target_os = "android"))]
580-
pub fn vmsplice(fd: RawFd, iov: &[IoVec<&[u8]>], flags: SpliceFFlags) -> Result<usize> {
608+
pub fn vmsplice(
609+
fd: RawFd,
610+
iov: &[crate::sys::uio::IoVec<&[u8]>],
611+
flags: SpliceFFlags
612+
) -> Result<usize>
613+
{
581614
let ret = unsafe {
582615
libc::vmsplice(
583616
fd,
@@ -588,10 +621,13 @@ pub fn vmsplice(fd: RawFd, iov: &[IoVec<&[u8]>], flags: SpliceFFlags) -> Result<
588621
};
589622
Errno::result(ret).map(|r| r as usize)
590623
}
624+
}
591625

592626
#[cfg(any(target_os = "linux"))]
627+
#[cfg(feature = "fs")]
593628
libc_bitflags!(
594629
/// Mode argument flags for fallocate determining operation performed on a given range.
630+
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
595631
pub struct FallocateFlags: c_int {
596632
/// File size is not changed.
597633
///
@@ -620,11 +656,15 @@ libc_bitflags!(
620656
}
621657
);
622658

659+
feature!{
660+
#![feature = "fs"]
661+
623662
/// Manipulates file space.
624663
///
625664
/// Allows the caller to directly manipulate the allocated disk space for the
626665
/// file referred to by fd.
627666
#[cfg(any(target_os = "linux"))]
667+
#[cfg(feature = "fs")]
628668
pub fn fallocate(
629669
fd: RawFd,
630670
mode: FallocateFlags,
@@ -635,6 +675,7 @@ pub fn fallocate(
635675
Errno::result(res).map(drop)
636676
}
637677

678+
638679
#[cfg(any(
639680
target_os = "linux",
640681
target_os = "android",
@@ -649,9 +690,11 @@ mod posix_fadvise {
649690
use std::os::unix::io::RawFd;
650691
use crate::Result;
651692

693+
#[cfg(feature = "fs")]
652694
libc_enum! {
653695
#[repr(i32)]
654696
#[non_exhaustive]
697+
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
655698
pub enum PosixFadviseAdvice {
656699
POSIX_FADV_NORMAL,
657700
POSIX_FADV_SEQUENTIAL,
@@ -662,6 +705,8 @@ mod posix_fadvise {
662705
}
663706
}
664707

708+
feature!{
709+
#![feature = "fs"]
665710
pub fn posix_fadvise(
666711
fd: RawFd,
667712
offset: libc::off_t,
@@ -676,6 +721,7 @@ mod posix_fadvise {
676721
Err(Errno::from_i32(res))
677722
}
678723
}
724+
}
679725
}
680726

681727
#[cfg(any(
@@ -694,3 +740,4 @@ pub fn posix_fallocate(fd: RawFd, offset: libc::off_t, len: libc::off_t) -> Resu
694740
Ok(errno) => Err(Errno::from_i32(errno)),
695741
}
696742
}
743+
}

0 commit comments

Comments
 (0)