Skip to content

Commit

Permalink
[WIP] Fix build on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Zollerboy1 committed Apr 26, 2024
1 parent 6a61cd2 commit 54197b9
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 41 deletions.
20 changes: 11 additions & 9 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion driver/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "juice"
name = "juice-driver"
version = "0.1.0"
edition = "2021"
rust-version = { workspace = true }
Expand All @@ -9,9 +9,13 @@ rust-version = { workspace = true }
[dependencies]
async-once-cell = "0.5.3"
async-process = "2.1.0"
cfg-if = "1.0.0"
clap = { version = "4.5.1", features = ["derive"] }
derive_more = { workspace = true }
juice-frontend = { workspace = true }
tempfile = "3.10.1"
tokio = { version = "1.36.0", features = ["full"] }
which = "6.0.0"

[target.'cfg(target_os = "linux")'.dependencies]
itertools = "0.12.1"
2 changes: 0 additions & 2 deletions driver/src/driver/main/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(target_os = "macos")]
mod macos;
mod task;

use std::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod os;

use std::{
borrow::Cow,
env,
Expand All @@ -14,8 +16,8 @@ use std::{

use async_process::Command;
use tempfile::Builder as TempFileBuilder;
use which::which;

use self::os::get_linker_path_and_args;
use super::Action;
use crate::{
cli::OutputFilePath,
Expand Down Expand Up @@ -299,31 +301,7 @@ pub struct LinkingTask {

impl LinkingTask {
pub async fn new(inputs: Vec<Box<dyn ErasedTask>>, output_path: OutputFilePath) -> DriverResult<Self> {
#[cfg(target_os = "macos")]
let executable_path = which("ld").map_err(|err| DriverError::ExecutableNotFound {
executable_name: String::from("ld"),
error: err,
})?;
#[cfg(not(target_os = "macos"))]
let executable_path = compile_error!("This platform is not supported yet");

#[cfg(target_os = "macos")]
let mut arguments = vec![
OsStr::new("-syslibroot"),
super::macos::get_sdk_path().await?.as_os_str(),
OsStr::new("-lSystem"),
];
#[cfg(not(target_os = "macos"))]
let mut arguments = compile_error!("This platform is not supported yet");

arguments.push(OsStr::new("-o"));
arguments.push(output_path.as_os_str());

let mut arguments = arguments.into_iter().map(OsString::from).collect::<Vec<_>>();

for input in &inputs {
arguments.push(input.get_output_path().as_os_str().to_owned());
}
let (executable_path, arguments) = get_linker_path_and_args(&inputs, &output_path).await?;

Ok(Self {
executable_path,
Expand Down
38 changes: 38 additions & 0 deletions driver/src/driver/main/task/os/linux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::{ffi::OsString, path::PathBuf};

use itertools::Itertools as _;
use which::which;

use crate::{
cli::OutputFilePath,
driver::{main::task::ErasedTask, Error as DriverError, Result as DriverResult},
};

pub fn find_linker() -> DriverResult<PathBuf> {
const LINKER_NAMES: [&str; 5] = ["lld", "ld.lld", "gold", "ld.gold", "ld"];

LINKER_NAMES
.iter()
.map(which)
.find_or_last(Result::is_ok)
.unwrap()
.map_err(|err| DriverError::ExecutableNotFound {
executable_name: String::from("ld"),
error: err,
})
}

pub async fn get_linker_path_and_args(
inputs: &Vec<Box<dyn ErasedTask>>,
output_path: &OutputFilePath,
) -> DriverResult<(PathBuf, Vec<OsString>)> {
let executable_path = find_linker()?;

let mut arguments = vec![OsString::from("-o"), output_path.as_os_str().to_owned()];

for input in inputs {
arguments.push(input.get_output_path().as_os_str().to_owned());
}

Ok((executable_path, arguments))
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
ffi::OsStr,
ffi::{OsStr, OsString},
os::unix::{ffi::OsStrExt as _, process::ExitStatusExt as _},
path::{Path, PathBuf},
};
Expand All @@ -8,9 +8,12 @@ use async_once_cell::OnceCell;
use async_process::Command;
use which::which;

use crate::driver::{Error as DriverError, Result as DriverResult};
use crate::{
cli::OutputFilePath,
driver::{main::task::ErasedTask, Error as DriverError, Result as DriverResult},
};

pub async fn get_sdk_path() -> DriverResult<&'static Path> {
async fn get_sdk_path() -> DriverResult<&'static Path> {
static SDK_PATH: OnceCell<PathBuf> = OnceCell::new();

SDK_PATH
Expand Down Expand Up @@ -51,3 +54,30 @@ pub async fn get_sdk_path() -> DriverResult<&'static Path> {
.await
.map(AsRef::as_ref)
}

pub async fn get_linker_path_and_args(
inputs: &Vec<Box<dyn ErasedTask>>,
output_path: &OutputFilePath,
) -> DriverResult<(PathBuf, Vec<OsString>)> {
let executable_path = which("ld").map_err(|err| DriverError::ExecutableNotFound {
executable_name: String::from("ld"),
error: err,
})?;

let mut arguments = vec![
OsStr::new("-syslibroot"),
get_sdk_path().await?.as_os_str(),
OsStr::new("-lSystem"),
];

arguments.push(OsStr::new("-o"));
arguments.push(output_path.as_os_str());

let mut arguments = arguments.into_iter().map(OsString::from).collect::<Vec<_>>();

for input in inputs {
arguments.push(input.get_output_path().as_os_str().to_owned());
}

Ok((executable_path, arguments))
}
26 changes: 26 additions & 0 deletions driver/src/driver/main/task/os/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] {
mod linux;

pub use self::linux::get_linker_path_and_args;
} else if #[cfg(target_os = "macos")] {
mod macos;

pub use self::macos::get_linker_path_and_args;
} else {
use std::path::PathBuf;
use std::ffi::OsString;

use crate::{
cli::OutputFilePath,
driver::{main::task::ErasedTask, Result as DriverResult},
};

pub async fn get_linker_path_and_args(
_inputs: &Vec<Box<dyn ErasedTask>>,
_output_path: &OutputFilePath,
) -> DriverResult<(PathBuf, Vec<OsString>)> {
compile_error!("Unsupported OS")
}
}
}
1 change: 1 addition & 0 deletions frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ juice-macros = { workspace = true }
lasso = "0.7.2"
num-bigint = "0.4.4"
take_mut = "0.2.2"

0 comments on commit 54197b9

Please sign in to comment.