Skip to content

Commit

Permalink
try to build on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Feb 23, 2025
1 parent 2105171 commit 9e62968
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 12 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source ~/.cargo/env
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,13 @@ jobs:
- run: apt-get remove curl ca-certificates openssl --yes
- run: cargo build
- run: ./target/debug/pkgx +git

windows:
needs: fmt
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo test --all-features
env:
RUSTFLAGS: "-D warnings"
23 changes: 23 additions & 0 deletions crates/cli/src/execve.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#[cfg(unix)]
use nix::unistd::execve as nix_execve;
#[cfg(unix)]
use std::ffi::CString;

use std::{collections::HashMap, error::Error};

#[cfg(unix)]
pub fn execve(
cmd: String,
mut args: Vec<String>,
Expand Down Expand Up @@ -47,3 +51,22 @@ pub fn execve(

Ok(())
}

#[cfg(windows)]
use std::process::{Command, exit};

#[cfg(windows)]
pub fn execve(
cmd: String,
args: Vec<String>,
env: HashMap<String, String>,
) -> Result<(), Box<dyn Error>> {
Command::new(cmd)
.args(args)
.envs(env)
.spawn()
.expect("Failed to execute process");

// If you want behavior similar to `execve`, exit the current process
exit(0);
}
14 changes: 9 additions & 5 deletions crates/lib/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use tokio_util::compat::FuturesAsyncReadCompatExt;
// futures::io::AsyncRead.
use futures::stream::TryStreamExt;

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

use crate::{
cellar,
client::build_client,
Expand Down Expand Up @@ -203,9 +206,10 @@ async fn make_symlink(
.file_name()
.ok_or_else(|| anyhow::anyhow!("Could not get the base name of the installation path"))?;

match std::os::unix::fs::symlink(target, &symlink_path) {
Ok(_) => Ok(()),
Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => Ok(()),
Err(err) => Err(err.into()),
}
#[cfg(not(windows))]
std::os::unix::fs::symlink(target, &symlink_path)?;
#[cfg(windows)]
symlink_dir(target, symlink_path)?;

Ok(())
}
3 changes: 2 additions & 1 deletion crates/lib/src/pantry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,10 @@ impl<'de> Deserialize<'de> for Provides {
ProvidesHelper::Map(map) => {
#[cfg(target_os = "macos")]
let key = "darwin";

#[cfg(target_os = "linux")]
let key = "linux";
#[cfg(windows)]
let key = "windows";

if let Some(values) = map.get(key) {
Ok(Provides(values.clone()))
Expand Down
6 changes: 4 additions & 2 deletions crates/lib/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl Serialize for Installation {
pub enum Host {
Darwin,
Linux,
Windows
}

// These are only used per build at present
Expand All @@ -101,8 +102,8 @@ pub fn host() -> (Host, Arch) {
let host = Host::Darwin;
#[cfg(target_os = "linux")]
let host = Host::Linux;
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
panic!("Unsupported platform");
#[cfg(windows)]
let host = Host::Windows;

#[cfg(target_arch = "aarch64")]
let arch = Arch::Arm64;
Expand All @@ -119,6 +120,7 @@ impl fmt::Display for Host {
let os_str = match self {
Host::Linux => "linux",
Host::Darwin => "darwin",
Host::Windows => "windows",
};
write!(f, "{}", os_str)
}
Expand Down
20 changes: 16 additions & 4 deletions crates/lib/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{error::Error, os::unix::fs::PermissionsExt, path::Path};
use std::{error::Error, path::Path};
#[cfg(not(windows))]
use std::os::unix::fs::PermissionsExt;

pub async fn find_program(arg: &str, paths: &Vec<String>) -> Result<String, Box<dyn Error>> {
if arg.starts_with("/") {
Expand All @@ -13,9 +15,19 @@ pub async fn find_program(arg: &str, paths: &Vec<String>) -> Result<String, Box<
}
for path in paths {
let full_path = Path::new(&path).join(arg);
if let Ok(metadata) = full_path.metadata() {
if full_path.is_file() && (metadata.permissions().mode() & 0o111 != 0) {
return Ok(full_path.to_str().unwrap().to_string());
if full_path.is_file() {
#[cfg(unix)]
if let Ok(metadata) = full_path.metadata() {
if metadata.permissions().mode() & 0o111 != 0 {
return Ok(full_path.to_str().unwrap().to_string());
}
}
#[cfg(windows)]
if let Some(ext) = full_path.extension() {
match ext.to_str() {
Some("exe") | Some("bat") | Some("cmd") => return Ok(full_path.to_str().unwrap().to_string()),
_ => {}
}
}
}
}
Expand Down

0 comments on commit 9e62968

Please sign in to comment.