Skip to content

Make build on Windows #6

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 5 commits into from
Mar 28, 2022
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
28 changes: 26 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ edition = "2018"
[dependencies]
getopts = "0.2"
anyhow = "1.0"
jemallocator = "0.3.2"
libc = "0.2"

[target.'cfg(windows)'.dependencies]
kernel32-sys = "0.2.2"
winapi = { version = "0.3.6", features = [ "synchapi" ] }

[target.'cfg(not(target_env = "msvc"))'.dependencies]
jemallocator = "0.3.2"

[dev-dependencies]
tempfile = "3.3.0"

Expand Down
21 changes: 21 additions & 0 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ use crate::canon::{canon_path, canon_path_in_place};
use crate::densemap::{self, DenseMap};
use std::collections::HashMap;
use std::hash::Hasher;

#[cfg(unix)]
use std::os::unix::fs::MetadataExt;

#[cfg(windows)]
use std::os::windows::fs::MetadataExt;

/// Hash value used to identify a given instance of a Build's execution;
/// compared to verify whether a Build is up to date.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -272,10 +277,26 @@ pub enum MTime {
Stamp(u32),
}

#[cfg(windows)]
fn mtime_from_filetime(mut filetime: u64) -> i64 {
// FILETIME is in 100-nanosecond increments since 1600. Convert to seconds since 2000.
filetime /= 1000000000 / 100; // 100ns -> s.

// 1600 epoch -> 2000 epoch (subtract 400 years).
use std::convert::TryInto;
(filetime - 12622770400).try_into().unwrap()
}

/// stat() an on-disk path, producing its MTime.
pub fn stat(path: &str) -> std::io::Result<MTime> {
// TODO: Support timestamps with better-than-seconds resolution.
// TODO: On Windows, use FindFirstFileEx()/FindNextFile() to get timestamps per
// directory, for better stat perf.
Ok(match std::fs::metadata(path) {
#[cfg(unix)]
Ok(meta) => MTime::Stamp(meta.mtime() as u32),
#[cfg(windows)]
Ok(meta) => MTime::Stamp(mtime_from_filetime(meta.last_write_time()) as u32),
Err(err) => {
if err.kind() == std::io::ErrorKind::NotFound {
MTime::Missing
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ mod task;
pub mod trace;
pub mod work;

#[cfg(not(target_env = "msvc"))]
use jemallocator::Jemalloc;

#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;
20 changes: 20 additions & 0 deletions src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::graph::BuildId;
use crate::work::BuildState;
use crate::work::StateCounts;

#[cfg(unix)]
#[allow(clippy::uninit_assumed_init)]
pub fn get_terminal_cols() -> Option<usize> {
unsafe {
Expand All @@ -21,6 +22,25 @@ pub fn get_terminal_cols() -> Option<usize> {
}
}

#[cfg(windows)]
#[allow(clippy::uninit_assumed_init)]
pub fn get_terminal_cols() -> Option<usize> {
extern crate winapi;
extern crate kernel32;
use kernel32::{GetConsoleScreenBufferInfo, GetStdHandle};
let console = unsafe { GetStdHandle(winapi::um::winbase::STD_OUTPUT_HANDLE) };
if console == winapi::um::handleapi::INVALID_HANDLE_VALUE {
return None;
}
unsafe {
let mut csbi = ::std::mem::MaybeUninit::uninit().assume_init();
if GetConsoleScreenBufferInfo(console, &mut csbi) == 0 {
return None;
}
Some(csbi.dwSize.X as usize)
}
}

/// Compute the message to display on the console for a given build.
pub fn build_message(build: &Build) -> &str {
build
Expand Down
2 changes: 2 additions & 0 deletions src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
//! and let the parent properly print that progress. This also lets us still
//! write out pending debug traces, too.

#[cfg(unix)]
extern "C" fn sigint_handler(_sig: libc::c_int) {
// Do nothing; SA_RESETHAND should clear the handler.
}

#[cfg(unix)]
pub fn register_sigint() {
// Safety: registering a signal handler is libc unsafe code.
unsafe {
Expand Down
126 changes: 124 additions & 2 deletions src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ use crate::depfile;
use crate::graph::BuildId;
use crate::scanner::Scanner;
use anyhow::{anyhow, bail};
use std::io::Write;
use std::os::unix::process::ExitStatusExt;
use std::sync::mpsc;
use std::time::{Duration, Instant};

#[cfg(unix)]
use std::io::Write;

#[cfg(unix)]
use std::os::unix::process::ExitStatusExt;

#[cfg(windows)]
extern crate winapi;

pub struct FinishedTask {
/// A (faked) "thread id", used to put different finished builds in different
/// tracks in a performance trace.
Expand Down Expand Up @@ -51,6 +58,7 @@ fn read_depfile(path: &str) -> anyhow::Result<Vec<String>> {

/// Executes a build task as a subprocess.
/// Returns an Err() if we failed outside of the process itself.
#[cfg(unix)]
fn run_task(cmdline: &str, depfile: Option<&str>) -> anyhow::Result<TaskResult> {
let mut cmd = std::process::Command::new("/bin/sh")
.arg("-c")
Expand Down Expand Up @@ -84,6 +92,120 @@ fn run_task(cmdline: &str, depfile: Option<&str>) -> anyhow::Result<TaskResult>
})
}

#[cfg(windows)]
fn zeroed_startupinfo() -> winapi::um::processthreadsapi::STARTUPINFOA {
winapi::um::processthreadsapi::STARTUPINFOA {
cb: 0,
lpReserved: std::ptr::null_mut(),
lpDesktop: std::ptr::null_mut(),
lpTitle: std::ptr::null_mut(),
dwX: 0,
dwY: 0,
dwXSize: 0,
dwYSize: 0,
dwXCountChars: 0,
dwYCountChars: 0,
dwFillAttribute: 0,
dwFlags: 0,
wShowWindow: 0,
cbReserved2: 0,
lpReserved2: std::ptr::null_mut(),
hStdInput: winapi::um::handleapi::INVALID_HANDLE_VALUE,
hStdOutput: winapi::um::handleapi::INVALID_HANDLE_VALUE,
hStdError: winapi::um::handleapi::INVALID_HANDLE_VALUE,
}
}

#[cfg(windows)]
fn zeroed_process_information() -> winapi::um::processthreadsapi::PROCESS_INFORMATION {
winapi::um::processthreadsapi::PROCESS_INFORMATION {
hProcess: std::ptr::null_mut(),
hThread: std::ptr::null_mut(),
dwProcessId: 0,
dwThreadId: 0,
}
}

#[cfg(windows)]
fn run_task(cmdline: &str, depfile: Option<&str>) -> anyhow::Result<TaskResult> {
// Don't want to run `cmd /c` since that limits cmd line length to 8192 bytes.
// std::process::Command can't take a string and pass it through to CreateProcess unchanged,
// so call that ourselves.

// TODO: Set this to just 0 for console pool jobs.
let process_flags = winapi::um::winbase::CREATE_NEW_PROCESS_GROUP;

let mut startup_info = zeroed_startupinfo();
startup_info.cb = std::mem::size_of::<winapi::um::processthreadsapi::STARTUPINFOA>() as u32;
startup_info.dwFlags = winapi::um::winbase::STARTF_USESTDHANDLES;

let mut process_info = zeroed_process_information();

let mut mut_cmdline = cmdline.to_string() + "\0";

let create_process_success = unsafe {
winapi::um::processthreadsapi::CreateProcessA(
std::ptr::null_mut(),
mut_cmdline.as_mut_ptr() as *mut i8,
std::ptr::null_mut(),
std::ptr::null_mut(),
/*inherit handles = */ winapi::shared::ntdef::TRUE.into(),
process_flags,
std::ptr::null_mut(),
std::ptr::null_mut(),
&mut startup_info,
&mut process_info,
)
};
if create_process_success == 0 {
// TODO: better error?
let error = unsafe { winapi::um::errhandlingapi::GetLastError() };
bail!("CreateProcessA failed: {}", error);
}

unsafe {
winapi::um::handleapi::CloseHandle(process_info.hThread);
}

unsafe {
winapi::um::synchapi::WaitForSingleObject(
process_info.hProcess,
winapi::um::winbase::INFINITE,
);
}

let mut exit_code: u32 = 0;
unsafe {
winapi::um::processthreadsapi::GetExitCodeProcess(process_info.hProcess, &mut exit_code);
}

unsafe {
winapi::um::handleapi::CloseHandle(process_info.hProcess);
}

let mut output = Vec::new();
// TODO: Set up pipes so that we can print the process's output.
//output.append(&mut cmd.stdout);
//output.append(&mut cmd.stderr);
let success = exit_code == 0;

let mut discovered_deps: Option<Vec<String>> = None;
if success {
discovered_deps = match depfile {
None => None,
Some(deps) => Some(read_depfile(deps)?),
};
} else {
// Command failed.
}

Ok(TaskResult {
success,
output,
discovered_deps,
})
}

/// Tracks faked "thread ids" -- integers assigned to build tasks to track
/// paralllelism in perf trace output.
struct ThreadIds {
Expand Down
5 changes: 4 additions & 1 deletion src/work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use crate::densemap::DenseMap;
use crate::graph::*;
use crate::progress;
use crate::progress::Progress;
use crate::signal;
use crate::task;
use crate::trace;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::time::Duration;

#[cfg(unix)]
use crate::signal;

/// Build steps go through this sequence of states.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum BuildState {
Expand Down Expand Up @@ -631,6 +633,7 @@ impl<'a> Work<'a> {
// Returns a Result for failures, but we must clean up the progress before
// returning the result to the caller.
fn run_without_cleanup(&mut self) -> anyhow::Result<Option<usize>> {
#[cfg(unix)]
signal::register_sigint();
let mut tasks_done = 0;
while self.build_states.unfinished() {
Expand Down
Loading