Add conditional compilation for unix-only crates

Instead of configuring which crates to --exclude in
test_config.py, we can use conditional compilation to
exclude code that is not supported on windows.

This allows more fine-grained control and also allows
us to use plain cargo for building without complicated
configuration and exclusions.

BUG=b:265829867
TEST=cargo test --lib --bins --workspace
	--target=x86_64-pc-windows-gnu
	--features=all-mingw64

Change-Id: I8422c3f08053bc27d9896b220876a56bd25543d6
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4165868
Reviewed-by: Vikram Auradkar <auradkar@google.com>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Dennis Kempin 2023-01-17 12:24:29 -08:00 committed by crosvm LUCI
parent 4c66bd3e7f
commit acc162000f
34 changed files with 141 additions and 88 deletions

View file

@ -41,7 +41,7 @@ linker = "arm-linux-gnueabihf-gcc"
linker = "aarch64-linux-gnu-gcc"
[target.x86_64-pc-windows-gnu]
runner = "wine64"
runner = "wine64-stable"
[env]
PKG_CONFIG_SYSROOT_DIR_armv7-unknown-linux-gnueabihf = "/usr/arm-linux-gnueabihf"

View file

@ -86,6 +86,8 @@
//! [`abortable`](futures::future::abortable). However keep in mind that on backends like io_uring,
//! cancelling the future may not cancel the underlying IO operation.
#![cfg(unix)]
mod blocking;
mod enter;
mod event;

View file

@ -4,7 +4,7 @@ version = "0.1.0"
authors = ["The ChromiumOS Authors"]
edition = "2021"
[dependencies]
[target.'cfg(unix)'.dependencies]
libc = "*"
serde = { version = "*", features = [ "derive" ] }
wire_format_derive = { path = "wire_format_derive", version = "*" }

View file

@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#![cfg(unix)]
extern crate libc;
#[macro_use]

View file

@ -5,49 +5,59 @@
#![cfg(not(test))]
#![no_main]
use std::convert::TryInto;
#[cfg(unix)]
mod fuzzer {
use std::convert::TryInto;
use cros_fuzz::fuzz_target;
use devices::virtio::create_descriptor_chain;
use devices::virtio::DescriptorType;
use devices::virtio::Reader;
use devices::virtio::Writer;
use fuse::fuzzing::fuzz_server;
use vm_memory::GuestAddress;
use vm_memory::GuestMemory;
use cros_fuzz::fuzz_target;
use devices::virtio::create_descriptor_chain;
use devices::virtio::DescriptorType;
use devices::virtio::Reader;
use devices::virtio::Writer;
use fuse::fuzzing::fuzz_server;
use vm_memory::GuestAddress;
use vm_memory::GuestMemory;
const MEM_SIZE: u64 = 256 * 1024 * 1024;
const BUFFER_ADDR: GuestAddress = GuestAddress(0x100);
const MEM_SIZE: u64 = 256 * 1024 * 1024;
const BUFFER_ADDR: GuestAddress = GuestAddress(0x100);
thread_local! {
static GUEST_MEM: GuestMemory = GuestMemory::new(&[(GuestAddress(0), MEM_SIZE)]).unwrap();
thread_local! {
static GUEST_MEM: GuestMemory = GuestMemory::new(&[(GuestAddress(0), MEM_SIZE)]).unwrap();
}
fuzz_target!(|data| {
use DescriptorType::*;
GUEST_MEM.with(|mem| {
mem.write_all_at_addr(data, BUFFER_ADDR).unwrap();
let chain = create_descriptor_chain(
mem,
GuestAddress(0),
BUFFER_ADDR,
vec![
(Readable, data.len().try_into().unwrap()),
(
Writable,
(MEM_SIZE as u32)
.saturating_sub(data.len().try_into().unwrap())
.saturating_sub(0x100),
),
],
0,
)
.unwrap();
let r = Reader::new(mem.clone(), chain.clone()).unwrap();
let w = Writer::new(mem.clone(), chain).unwrap();
fuzz_server(r, w);
});
});
}
fuzz_target!(|data| {
use DescriptorType::*;
#[cfg(not(unix))]
mod fuzzer {
use cros_fuzz::fuzz_target;
GUEST_MEM.with(|mem| {
mem.write_all_at_addr(data, BUFFER_ADDR).unwrap();
let chain = create_descriptor_chain(
mem,
GuestAddress(0),
BUFFER_ADDR,
vec![
(Readable, data.len().try_into().unwrap()),
(
Writable,
(MEM_SIZE as u32)
.saturating_sub(data.len().try_into().unwrap())
.saturating_sub(0x100),
),
],
0,
)
.unwrap();
let r = Reader::new(mem.clone(), chain.clone()).unwrap();
let w = Writer::new(mem.clone(), chain).unwrap();
fuzz_server(r, w);
});
});
fuzz_target!(|_data| {});
}

View file

@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#![cfg(unix)]
#![cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#![allow(non_camel_case_types)]

View file

@ -4,6 +4,8 @@
//! FUSE (Filesystem in Userspace) server and filesystem mounting support.
#![cfg(unix)]
use std::ffi::FromBytesWithNulError;
use std::fs::File;
use std::io;

View file

@ -4,25 +4,39 @@
use std::process::exit;
use anyhow::Context;
use anyhow::Result;
use gpu_display::*;
#[cfg(unix)]
mod platform {
use anyhow::Context;
use anyhow::Result;
fn run() -> Result<()> {
let mut disp = GpuDisplay::open_wayland(None::<&str>).context("open_wayland")?;
let surface_id = disp
.create_surface(None, 1280, 1024, SurfaceType::Scanout)
.context("create_surface")?;
disp.flip(surface_id);
disp.commit(surface_id).context("commit")?;
while !disp.close_requested(surface_id) {
disp.dispatch_events().context("dispatch_events")?;
use gpu_display::*;
pub fn run() -> Result<()> {
let mut disp = GpuDisplay::open_wayland(None::<&str>).context("open_wayland")?;
let surface_id = disp
.create_surface(None, 1280, 1024, SurfaceType::Scanout)
.context("create_surface")?;
disp.flip(surface_id);
disp.commit(surface_id).context("commit")?;
while !disp.close_requested(surface_id) {
disp.dispatch_events().context("dispatch_events")?;
}
Ok(())
}
}
#[cfg(not(unix))]
mod platform {
use anyhow::anyhow;
use anyhow::Result;
pub fn run() -> Result<()> {
Err(anyhow!("Only supported on unix targets"))
}
Ok(())
}
fn main() {
if let Err(e) = run() {
if let Err(e) = platform::run() {
eprintln!("error: {:#}", e);
exit(1);
}

View file

@ -4,6 +4,8 @@
//! Safe wrapper over the Linux `io_uring` system calls.
#![cfg(unix)]
mod bindings;
mod syscalls;
mod uring;

View file

@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#![cfg(unix)]
use std::collections::BTreeSet;
use std::fs::File;
use std::fs::OpenOptions;

View file

@ -6,6 +6,8 @@
//!
//! New code should use the `hypervisor` crate instead.
#![cfg(unix)]
mod cap;
use std::cell::RefCell;

View file

@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#![cfg(unix)]
#![cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use base::MemoryMappingBuilder;

View file

@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#![cfg(unix)]
use base::pagesize;
use base::Event;
use base::FromRawDescriptor;

View file

@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#![cfg(unix)]
#![cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use base::MemoryMappingBuilder;

View file

@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#![cfg(unix)]
#![cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use kvm::*;

View file

@ -4,6 +4,7 @@
//! Bindings for the Linux KVM (Kernel Virtual Machine) API.
#![cfg(unix)]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

View file

@ -10,6 +10,11 @@ fn main() {
return;
}
// ffmpeg is currently only supported on unix
if std::env::var("CARGO_CFG_UNIX").is_err() {
return;
}
// Match all ffmpeg 5.0 versions with which our generated bindings are compatible.
Config::new()
.range_version("59".."60")

View file

@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#![cfg(unix)]
pub mod avcodec;
mod avutil;
pub use avutil::*;

View file

@ -8,6 +8,11 @@ fn main() {
return;
}
// libva is unix only
if std::env::var("CARGO_CFG_UNIX").is_err() {
return;
}
match pkg_config::probe_library("libva") {
Ok(_) => (),
Err(e) => panic!("Libva not found: {}", e),

View file

@ -7,6 +7,7 @@
//! The starting point to using this crate is to open a [`Display`], from which a [`Context`] and
//! [`Surface`]s can be allocated and used for doing actual work.
#![cfg(unix)]
#![deny(missing_docs)]
mod bindings;

View file

@ -4,6 +4,8 @@
//! Bindings for the `libvda` video decoder and encoder libraries.
#![cfg(unix)]
pub mod decode;
pub mod encode;

View file

@ -4,6 +4,8 @@
//! Integration tests using LibVDA fake decode implemenation.
#![cfg(unix)]
use libvda::decode::*;
use libvda::*;

View file

@ -4,6 +4,8 @@
//! Integration tests using LibVDA fake encode implementation.
#![cfg(unix)]
use libvda::encode::*;
use libvda::*;

View file

@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#![cfg(unix)]
pub use crate::generated::plugin::*;
/// Converts protobuf representation of CpuId data into KVM format.

View file

@ -4,6 +4,8 @@
//! Exported interface to basic qcow functionality to be used from C.
#![cfg(unix)]
use std::ffi::CStr;
use std::fs::OpenOptions;
use std::os::raw::c_char;

View file

@ -7,7 +7,7 @@ edition = "2021"
[features]
trace_marker = ["cros_tracing/trace_marker"]
[dependencies]
[target.'cfg(unix)'.dependencies]
anyhow = "*"
base = { path = "../base" }
cros_tracing = { path = "../cros_tracing" }

View file

@ -4,6 +4,7 @@
//! crate for the vmm-swap feature.
#![cfg(unix)]
#![deny(missing_docs)]
mod file;

View file

@ -4,6 +4,8 @@
//! Integration tests for vmm-swap feature
#![cfg(unix)]
mod common;
use base::pagesize;

View file

@ -5,6 +5,8 @@
//! Integration tests for [PageHandler]. these are more than unit tests since [PageHandler] rely on
//! the userfaultfd(2) kernel feature.
#![cfg(unix)]
mod common;
use std::array;

View file

@ -54,34 +54,6 @@ class TestOption(enum.Enum):
#
# Please add a bug number when restricting a tests.
# This is just too big to keep in main list for now
WIN64_DISABLED_CRATES = [
"cros_asyncv2",
"cros-fuzz",
"crosvm_plugin",
"crosvm-fuzz",
"ffi",
"ffmpeg",
"fuse",
"fuzz",
"gpu_display",
"io_uring",
"kvm",
"libcras_stub",
"libva",
"libvda",
"minijail-sys",
"minijail",
"p9",
"qcow_utils",
"rutabaga_gralloc",
"swap",
"system_api_stub",
"tpm2-sys",
"tpm2",
"usb_util",
]
CRATE_OPTIONS: Dict[str, List[TestOption]] = {
"hypervisor": [
TestOption.DO_NOT_RUN_AARCH64,
@ -99,9 +71,6 @@ CRATE_OPTIONS: Dict[str, List[TestOption]] = {
"net_util": [TestOption.REQUIRES_ROOT],
}
for name in WIN64_DISABLED_CRATES:
CRATE_OPTIONS[name] = CRATE_OPTIONS.get(name, []) + [TestOption.DO_NOT_BUILD_WIN64]
BUILD_FEATURES: Dict[str, str] = {
"x86_64-unknown-linux-gnu": "linux-x86_64",
"aarch64-unknown-linux-gnu": "linux-aarch64",

View file

@ -65,6 +65,11 @@ fn main() -> Result<()> {
return Ok(());
}
// libtpm2 is unix only
if std::env::var("CARGO_CFG_UNIX").is_err() {
return Ok(());
}
// Use tpm2 package from the standard system location if available.
if pkg_config::Config::new()
.statik(true)

View file

@ -4,6 +4,8 @@
//! Bindings for the TPM2 simulator library.
#![cfg(unix)]
use std::os::raw::c_int;
use std::os::raw::c_uchar;
use std::os::raw::c_uint;

View file

@ -4,6 +4,8 @@
//! TPM2 (Trusted Platform Module 2.0) simulator.
#![cfg(unix)]
use std::os::raw::c_int;
use std::os::raw::c_uint;
use std::ptr;

View file

@ -5,6 +5,7 @@
//! USB device access and descriptor manipulation.
mod descriptor;
#[cfg(unix)]
mod device;
mod error;
mod types;
@ -13,8 +14,11 @@ pub use self::descriptor::parse_usbfs_descriptors;
pub use self::descriptor::ConfigDescriptorTree;
pub use self::descriptor::DeviceDescriptorTree;
pub use self::descriptor::InterfaceDescriptorTree;
#[cfg(unix)]
pub use self::device::Device;
#[cfg(unix)]
pub use self::device::Transfer;
#[cfg(unix)]
pub use self::device::TransferStatus;
pub use self::error::Error;
pub use self::error::Result;