Skip to content

Commit

Permalink
Refactored and cleaned up remaining workspace comilation code
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Dec 9, 2024
1 parent 1507a42 commit 2a1d816
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 224 deletions.
38 changes: 3 additions & 35 deletions src/cli/build/invoke.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use super::BuildCommand;
use super::{supported_targets::warn_if_unsupported_target, BuildCommand};
use crate::{
c,
cli::CliInvoke,
compiler::Compiler,
diagnostics::{DiagnosticFlags, Diagnostics, WarningDiagnostic},
diagnostics::{DiagnosticFlags, Diagnostics},
single_file_only::compile_single_file_only,
source_files::SourceFiles,
target::{Target, TargetArch, TargetOs},
text::{IntoText, IntoTextStream},
unerror::unerror,
workspace::compile_workspace,
Expand All @@ -26,7 +25,7 @@ impl CliInvoke for BuildCommand {
return Err(());
};

ensure_supported_target(&target, &diagnostics);
warn_if_unsupported_target(&target, &diagnostics);

let mut compiler = Compiler {
options,
Expand All @@ -47,37 +46,6 @@ impl CliInvoke for BuildCommand {
}
}

fn ensure_supported_target(target: &Target, diagnostics: &Diagnostics) {
if target.arch().is_none() {
diagnostics.push(WarningDiagnostic::plain(
"Target architecture is not supported, falling back to best guess",
));
}

if target.os().is_none() {
diagnostics.push(WarningDiagnostic::plain(
"Target os is not supported, falling back to best guess",
));
}

match target.os().zip(target.arch()) {
Some((TargetOs::Windows, TargetArch::X86_64)) => (),
Some((TargetOs::Windows, TargetArch::Aarch64)) => (),
Some((TargetOs::Mac, TargetArch::X86_64)) => (),
Some((TargetOs::Mac, TargetArch::Aarch64)) => (),
Some((TargetOs::Linux, TargetArch::X86_64)) => (),
Some((TargetOs::Linux, TargetArch::Aarch64)) => (),
Some((TargetOs::FreeBsd, TargetArch::X86_64)) => (),
None => (),
#[allow(unreachable_patterns)]
_ => {
diagnostics.push(WarningDiagnostic::plain(
"Host os/architecture configuration is not officially supported, taking best guess",
));
}
}
}

fn compile_header(compiler: &Compiler, filepath: &Path) -> Result<(), ()> {
let source_files = compiler.source_files;

Expand Down
1 change: 1 addition & 0 deletions src/cli/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod invoke;
mod options;
mod parse;
mod supported_targets;

pub use options::BuildOptions;

Expand Down
35 changes: 35 additions & 0 deletions src/cli/build/supported_targets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::{
diagnostics::{Diagnostics, WarningDiagnostic},
target::{Target, TargetArch, TargetOs},
};

pub fn warn_if_unsupported_target(target: &Target, diagnostics: &Diagnostics) {
if target.arch().is_none() {
diagnostics.push(WarningDiagnostic::plain(
"Target architecture is not supported, falling back to best guess",
));
}

if target.os().is_none() {
diagnostics.push(WarningDiagnostic::plain(
"Target os is not supported, falling back to best guess",
));
}

match target.os().zip(target.arch()) {
Some((TargetOs::Windows, TargetArch::X86_64)) => (),
Some((TargetOs::Windows, TargetArch::Aarch64)) => (),
Some((TargetOs::Mac, TargetArch::X86_64)) => (),
Some((TargetOs::Mac, TargetArch::Aarch64)) => (),
Some((TargetOs::Linux, TargetArch::X86_64)) => (),
Some((TargetOs::Linux, TargetArch::Aarch64)) => (),
Some((TargetOs::FreeBsd, TargetArch::X86_64)) => (),
None => (),
#[allow(unreachable_patterns)]
_ => {
diagnostics.push(WarningDiagnostic::plain(
"Host os/architecture configuration is not officially supported, taking best guess",
));
}
}
}
2 changes: 1 addition & 1 deletion src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl<'a> Compiler<'a> {
&self.options.target
}

pub fn maybe_execute_result(&self, output_binary_filepath: &Path) -> Result<(), ()> {
pub fn execute_result(&self, output_binary_filepath: &Path) -> Result<(), ()> {
if !self.options.excute_result {
return Ok(());
}
Expand Down
65 changes: 65 additions & 0 deletions src/workspace/export_and_link.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use crate::{compiler::Compiler, ir, llvm_backend::llvm_backend, resolved, unerror::unerror};
use std::{
ffi::OsString,
fs::create_dir_all,
path::{Path, PathBuf},
time::Duration,
};

#[derive(Clone, Debug)]
pub struct ExportDetails {
pub linking_duration: Duration,
pub executable_filepath: PathBuf,
}

pub fn export_and_link(
compiler: &mut Compiler,
project_folder: &Path,
resolved_ast: &resolved::Ast,
ir_module: &ir::Module,
) -> Result<ExportDetails, ()> {
let target = &compiler.options.target;
let project_name = project_name(project_folder);

let binary_artifacts_folder = project_folder.join("bin");
let object_files_folder = project_folder.join("obj");
create_dir_all(&binary_artifacts_folder).expect("failed to create bin folder");
create_dir_all(&object_files_folder).expect("failed to create obj folder");

let object_file_filepath =
object_files_folder.join(target.default_object_file_name(&project_name));

let executable_filepath =
binary_artifacts_folder.join(target.default_executable_name(&project_name));

let linking_duration = unerror(
unsafe {
llvm_backend(
compiler,
&ir_module,
&resolved_ast,
&object_file_filepath,
&executable_filepath,
&compiler.diagnostics,
)
},
compiler.source_files,
)?;

Ok(ExportDetails {
linking_duration,
executable_filepath,
})
}

fn project_name(project_folder: &Path) -> OsString {
project_folder
.file_name()
.map(OsString::from)
.or_else(|| {
std::env::current_dir()
.ok()
.and_then(|dir| dir.file_name().map(OsString::from))
})
.unwrap_or_else(|| OsString::from("main"))
}
Loading

0 comments on commit 2a1d816

Please sign in to comment.