Build System & Cross Compilation
Relevant source files
The following files were used as context for generating this wiki page:
The rust-battery project utilizes the Cargo build system to manage dependencies and compilation across a wide array of operating systems. Its primary objective is to provide a unified, cross-platform API for accessing battery information while abstracting the underlying system-specific interfaces, such as Linux sysfs, macOS IOKit, and Windows Power Management.
The build configuration is designed to handle hardware-level interactions by conditionally compiling platform-specific modules. This ensures that the resulting binary only includes the necessary code for the target environment, maintaining a small footprint and preventing compilation errors related to missing system headers or APIs on incompatible platforms.
Sources: Cargo.toml:1-12, src/lib.rs:1-15, src/platform/mod.rs:1-35
Dependency Management & Features
The project manages its build through Cargo.toml, specifying a Minimum Supported Rust Version (MSRV) and various feature flags. Dependencies are categorized by target OS to facilitate cross-compilation.
Build Configuration
| Option | Value / Description |
|---|---|
| Rust Version | 1.89+ |
| Edition | 2021 |
| Build Script | build.rs |
| License | ISC |
Sources: Cargo.toml:1-12
Feature Gates
The project includes optional features that can be toggled during the build process:
config-schema: Enablesschemarsandserdesupport for generating JSON schemas and serializing data structures.
Sources: Cargo.toml:14-15
Target-Specific Compilation
The build system heavily utilizes cfg attributes to pull in different dependencies based on the target operating system. This is critical because each platform uses a different kernel interface to query battery data.
The diagram above illustrates how the build system branches dependencies based on the compilation target.
Sources: Cargo.toml:22-54, src/platform/mod.rs:1-35
Platform-Specific Dependency Mapping
| Platform | Key Dependencies | Interface Type |
|---|---|---|
| Linux | lazycell | Sysfs |
| macOS/iOS | objc2-io-kit, mach2, libc | IOKit / Objective-C |
| Windows | windows-sys | Win32 Power API |
| FreeBSD | nix, libc | IOCTL |
| NetBSD | nix, plist, libc | SysMon / Plist |
Sources: Cargo.toml:22-54, src/platform/mod.rs:1-35
Platform Abstraction Layer
The internal architecture uses a modular approach in src/platform/mod.rs to alias system-specific types to common names used by the library.
This structure allows the high-level Manager and Battery types to remain platform-agnostic while the build system selects the correct implementation at compile time.
Sources: src/platform/mod.rs:1-35, src/lib.rs:25-34
Quality Control & Compliance
The build process is reinforced by deny.toml, which ensures that dependencies meet specific security and licensing standards.
- License Checking: Explicitly allows
Apache-2.0,ISC,MIT, andUnicodelicenses. - Bans: Allows multiple versions of the same crate but warns against wildcard dependencies.
- Sources: Restricts allowed registries to the standard
crates.io-index.
Sources: deny.toml:1-45
Summary
The rust-battery build system is a robust configuration of Cargo features and conditional compilation. By leveraging cfg-if and target-specific dependency blocks, it provides a seamless cross-compilation experience for developers across seven different operating system families. This architecture ensures that platform-specific complexities (like macOS objc2 migration or Windows windows-sys integration) are contained within their respective modules, presenting a clean SI-unit-compliant API to the end user.
Sources: CHANGELOG.md:10-50, src/platform/mod.rs:1-35