Skip to content

Simplify features and detect based on OS #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: cargo install cross

- name: Build FreeBSD tool
run: cross build --target=x86_64-unknown-freebsd --no-default-features --features cross_freebsd
run: cross build --target=x86_64-unknown-freebsd --no-default-features -p framework_lib

- name: Upload FreeBSD App
uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -93,15 +93,15 @@ jobs:

# Build debug library first to fail fast
- name: Build library (Windows)
run: cargo build -p framework_lib --no-default-features --features "windows"
run: cargo build -p framework_lib

- name: Build Windows tool
run: |
cargo build -p framework_tool --no-default-features --features "windows"
cargo build -p framework_tool --no-default-features --features "windows" --release
cargo build -p framework_tool
cargo build -p framework_tool --release

- name: Check if Windows tool can start
run: cargo run --no-default-features --features "windows" -- --help --release
run: cargo run -- --help --release

# Upload release build so that vcruntime is statically linked
- name: Upload Windows App
Expand Down
21 changes: 4 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,10 @@ Building on Windows or in general with fewer features:

```ps1
# Build the library and tool
cargo build --no-default-features --features "windows"
cargo build

# Running the tool
cargo run --no-default-features --features "windows"
```

Cross compile from Linux to FreeBSD:

```sh
# One time, install cross tool
cargo install cross

# Make sure docker is started as well
sudo systemctl start docker

# Build
cross build --target=x86_64-unknown-freebsd --no-default-features --features unix
cargo run
```

## Running
Expand Down Expand Up @@ -375,8 +362,8 @@ Keyboard backlight: 0%
sudo pkg install hidapi

# Build the library and tool
cargo build --no-default-features --features freebsd
cargo build

# Running the tool
cargo run --no-default-features --features freebsd
cargo run
```
85 changes: 30 additions & 55 deletions framework_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,10 @@ rust-version = "1.74"
build = "build.rs"

[features]
default = ["linux"]
# Linux/FreeBSD
unix = ["std", "raw_pio", "smbios", "dep:nix", "dep:libc"]
linux = ["unix", "linux_pio", "cros_ec_driver", "hidapi", "rusb"]
freebsd = ["unix", "freebsd_pio", "hidapi", "rusb"]
# hidapi and rusb don't seem to build in the cross container at the moment
cross_freebsd = ["unix", "freebsd_pio"]
# Windows does not have the cros_ec driver nor raw port I/O access to userspace
windows = ["std", "smbios", "dep:windows", "win_driver", "raw_pio", "hidapi", "rusb", "dep:wmi"]
smbios = ["dep:smbios-lib"]
std = ["dep:clap", "dep:clap-num", "dep:clap-verbosity-flag", "dep:env_logger", "smbios-lib?/std"]
default = ["hidapi", "rusb"]
rusb = ["dep:rusb"]
hidapi = ["dep:hidapi"]
uefi = [
"dep:plain", "raw_pio", "smbios", "lazy_static/spin_no_std", "dep:uefi", "dep:uefi-services",
# Otherwise I get: `LLVM ERROR: Do not know how to split the result of this operator!`
# Seems to be a Ruset/LLVM bug when SSE is enabled.
# See: https://github.com/rust-lang/rust/issues/61721
"sha2/force-soft"
]

# EC communication via Port I/O on FreeBSD
freebsd_pio = ["redox_hwio/std"]
# EC communication via Port I/O on Linux
linux_pio = ["dep:libc", "redox_hwio/std"]
# EC communication via raw Port I/O (e.g. UEFI or other ring 0 code)
raw_pio = []
# EC communication via cros_ec driver on Linux
cros_ec_driver = []

# Chromium EC driver by DHowett
win_driver = []
uefi = [ "lazy_static/spin_no_std" ]

[build-dependencies]
built = { version = "0.5", features = ["chrono", "git2"] }
Expand All @@ -48,39 +20,42 @@ built = { version = "0.5", features = ["chrono", "git2"] }
lazy_static = "1.4.0"
sha2 = { version = "0.10.8", default-features = false, features = [ "force-soft" ] }
regex = { version = "1.11.1", default-features = false }
redox_hwio = { git = "https://github.com/FrameworkComputer/rust-hwio", branch = "freebsd", default-features = false }
libc = { version = "0.2.155", optional = true }
clap = { version = "4.5", features = ["derive", "cargo"], optional = true }
clap-num = { version = "1.2.0", optional = true }
clap-verbosity-flag = { version = "2.2.1", optional = true }
nix = { version = "0.29.0", features = ["ioctl", "user"], optional = true }
num = { version = "0.4", default-features = false }
num-derive = { version = "0.4", default-features = false }
num-traits = { version = "0.2", default-features = false }
env_logger = { version = "0.11", optional = true }
log = { version = "0.4", default-features = true }
uefi = { version = "0.20", features = ["alloc"], optional = true }
uefi-services = { version = "0.17", optional = true }
plain = { version = "0.2.3", optional = true }
spin = { version = "0.9.8", optional = false }
hidapi = { version = "2.6.3", optional = true, features = [ "windows-native" ] }
rusb = { version = "0.9.4", optional = true }
spin = { version = "0.9.8" }
no-std-compat = { version = "0.4.1", features = [ "alloc" ] }
guid_macros = { path = "../guid_macros" }
wmi = { version = "0.15.0", optional = true }
hidapi = { version = "2.6.3", features = [ "windows-native" ], optional = true }
rusb = { version = "0.9.4", optional = true }

[target.'cfg(target_os = "uefi")'.dependencies]
uefi = { version = "0.20", features = ["alloc"] }
uefi-services = "0.17"
plain = "0.2.3"
redox_hwio = { git = "https://github.com/FrameworkComputer/rust-hwio", branch = "freebsd", default-features = false }
smbios-lib = { git = "https://github.com/FrameworkComputer/smbios-lib.git", branch = "no-std", default-features = false }

[target.'cfg(windows)'.dependencies]
wmi = "0.15.0"
smbios-lib = { git = "https://github.com/FrameworkComputer/smbios-lib.git", branch = "no-std" }
env_logger = "0.11"
clap = { version = "4.5", features = ["derive", "cargo"] }
clap-num = { version = "1.2.0" }
clap-verbosity-flag = { version = "2.2.1" }

[dependencies.smbios-lib]
git = "https://github.com/FrameworkComputer/smbios-lib.git"
branch = "no-std"
optional = true
default-features = false
# Local development
#path = "../../smbios-lib"
# After my changes are upstreamed
#version = "0.9.0"
[target.'cfg(unix)'.dependencies]
libc = "0.2.155"
nix = { version = "0.29.0", features = ["ioctl", "user"] }
redox_hwio = { git = "https://github.com/FrameworkComputer/rust-hwio", branch = "freebsd" }
smbios-lib = { git = "https://github.com/FrameworkComputer/smbios-lib.git", branch = "no-std" }
env_logger = "0.11"
clap = { version = "4.5", features = ["derive", "cargo"] }
clap-num = { version = "1.2.0" }
clap-verbosity-flag = { version = "2.2.1" }

[dependencies.windows]
optional = true
[target.'cfg(windows)'.dependencies.windows]
version = "0.59.0"
features = [
"Win32_Foundation",
Expand Down
6 changes: 3 additions & 3 deletions framework_lib/src/capsule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
use std::prelude::v1::*;

use core::prelude::rust_2021::derive;
#[cfg(all(not(feature = "uefi"), feature = "std"))]
#[cfg(not(feature = "uefi"))]
use std::fs::File;
#[cfg(all(not(feature = "uefi"), feature = "std"))]
#[cfg(not(feature = "uefi"))]
use std::io::prelude::*;

#[cfg(not(feature = "uefi"))]
Expand Down Expand Up @@ -180,7 +180,7 @@ pub fn dump_winux_image(data: &[u8], header: &DisplayCapsule, filename: &str) {

let image = &data[header_len..image_size];

#[cfg(all(not(feature = "uefi"), feature = "std"))]
#[cfg(not(feature = "uefi"))]
{
let mut file = File::create(filename).unwrap();
file.write_all(image).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions framework_lib/src/chromium_ec/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ pub trait EcRequestRaw<R> {
{
let response = self.send_command_vec_extra(ec, extra_data)?;
// TODO: The Windows driver seems to return 20 more bytes than expected
#[cfg(feature = "win_driver")]
#[cfg(windows)]
let expected = response.len() != std::mem::size_of::<R>() + 20;
#[cfg(not(feature = "win_driver"))]
#[cfg(not(windows))]
let expected = response.len() != std::mem::size_of::<R>();
if expected {
return Err(EcError::DeviceError(format!(
Expand Down
24 changes: 15 additions & 9 deletions framework_lib/src/chromium_ec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ use num_derive::FromPrimitive;

pub mod command;
pub mod commands;
#[cfg(feature = "cros_ec_driver")]
#[cfg(target_os = "linux")]
mod cros_ec;
pub mod i2c_passthrough;
pub mod input_deck;
#[cfg(not(windows))]
mod portio;
#[cfg(not(windows))]
mod portio_mec;
#[cfg(feature = "win_driver")]
#[allow(dead_code)]
mod protocol;
#[cfg(windows)]
mod windows;

use alloc::format;
Expand Down Expand Up @@ -224,15 +228,15 @@ impl Default for CrosEc {
fn available_drivers() -> Vec<CrosEcDriverType> {
let mut drivers = vec![];

#[cfg(feature = "win_driver")]
#[cfg(windows)]
drivers.push(CrosEcDriverType::Windows);

#[cfg(feature = "cros_ec_driver")]
#[cfg(target_os = "linux")]
if std::path::Path::new(cros_ec::DEV_PATH).exists() {
drivers.push(CrosEcDriverType::CrosEc);
}

#[cfg(not(feature = "windows"))]
#[cfg(not(windows))]
drivers.push(CrosEcDriverType::Portio);

drivers
Expand Down Expand Up @@ -1320,10 +1324,11 @@ impl CrosEcDriver for CrosEc {

// TODO: Change this function to return EcResult instead and print the error only in UI code
print_err(match self.driver {
#[cfg(not(windows))]
CrosEcDriverType::Portio => portio::read_memory(offset, length),
#[cfg(feature = "win_driver")]
#[cfg(windows)]
CrosEcDriverType::Windows => windows::read_memory(offset, length),
#[cfg(feature = "cros_ec_driver")]
#[cfg(target_os = "linux")]
CrosEcDriverType::CrosEc => cros_ec::read_memory(offset, length),
_ => Err(EcError::DeviceError("No EC driver available".to_string())),
})
Expand All @@ -1341,10 +1346,11 @@ impl CrosEcDriver for CrosEc {
}

match self.driver {
#[cfg(not(windows))]
CrosEcDriverType::Portio => portio::send_command(command, command_version, data),
#[cfg(feature = "win_driver")]
#[cfg(windows)]
CrosEcDriverType::Windows => windows::send_command(command, command_version, data),
#[cfg(feature = "cros_ec_driver")]
#[cfg(target_os = "linux")]
CrosEcDriverType::CrosEc => cros_ec::send_command(command, command_version, data),
_ => Err(EcError::DeviceError("No EC driver available".to_string())),
}
Expand Down
Loading
Loading