Skip to content

Commit

Permalink
feat: The generated obj files that are added to the linker are append…
Browse files Browse the repository at this point in the history
…ed at generation time only
  • Loading branch information
TheRustifyer committed Jul 2, 2024
1 parent 54f8b56 commit 5421500
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 164 deletions.
12 changes: 8 additions & 4 deletions zork++/src/lib/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,14 @@ impl<'a> ZorkCache<'a> {
.find(|mi| module_impl_model.file() == (*mi).path())
}

pub fn get_source_cmd<T: TranslationUnit>(&mut self, module_impl_model: &T) -> Option<&mut SourceCommandLine>{
self.generated_commands.sources.iter_mut().find(|mi|
module_impl_model.file() == (*mi).path()
)
pub fn get_source_cmd<T: TranslationUnit>(
&mut self,
module_impl_model: &T,
) -> Option<&mut SourceCommandLine> {
self.generated_commands
.sources
.iter_mut()
.find(|mi| module_impl_model.file() == (*mi).path())
}

/// Returns a [`Option`] of [`CommandDetails`] if the file is persisted already in the cache
Expand Down
15 changes: 10 additions & 5 deletions zork++/src/lib/cli/output/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,6 @@ impl IntoIterator for &Arguments {
self.0.clone().into_iter()
}
}
/* impl FromIterator<Vec<&Argument>> for Arguments {
fn from_iter<T: IntoIterator<Item = T>>(iter: T) -> Self {
todo!()
}
} */

impl FromIterator<Argument> for Arguments {
fn from_iter<I: IntoIterator<Item = Argument>>(iter: I) -> Self {
Expand All @@ -202,6 +197,16 @@ impl FromIterator<Argument> for Arguments {
}
}

impl FromIterator<Argument> for &Arguments {
fn from_iter<I: IntoIterator<Item = Argument>>(iter: I) -> Self {
let mut vec = Vec::new();
for item in iter {
vec.push(item);
}
&Arguments(vec)
}
}

impl<'a> FromIterator<&'a Argument> for Arguments {
fn from_iter<I: IntoIterator<Item = &'a Argument>>(iter: I) -> Arguments {
let mut vec = Vec::new();
Expand Down
21 changes: 13 additions & 8 deletions zork++/src/lib/cli/output/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn run_generated_commands(
.collect::<Vec<&mut SourceCommandLine>>();

for translation_unit_cmd in translation_units {
// Join the concrete args of any translation unit with the general flyweights
// Join the concrete args of any translation unit with the ones held in the flyweights
let translation_unit_cmd_args: Arguments = general_args
.iter()
.chain(compiler_specific_shared_args.iter())
Expand All @@ -75,13 +75,17 @@ pub fn run_generated_commands(
}

if !cache.generated_commands.linker.args.is_empty() {
// TODO: consider to join this into the
// all args iterator
log::debug!("Processing the linker command line...");
let linker_cmdline_args = general_args
.iter()
.chain(compiler_specific_shared_args.iter())
.chain(cache.generated_commands.linker.args.iter())
.collect::<Arguments>(); // TODO: can we avoid clone and own all the args? just use the
// .iter() as a view?

let r = execute_command(
program_data,
&cache.generated_commands.linker.args,
&linker_cmdline_args,
&env_args,
);
cache.generated_commands.linker.execution_result = CommandExecutionResult::from(&r);
Expand All @@ -96,6 +100,7 @@ pub fn run_generated_commands(
}

Ok(CommandExecutionResult::Success) // TODO: consider a new variant, like AllSuccedeed
// or better, change the return for something better
}

/// Executes a new [`std::process::Command`] to run the generated binary
Expand Down Expand Up @@ -132,7 +137,6 @@ pub fn autorun_generated_binary(
fn execute_command<T, S>(
model: &ZorkModel,
arguments: T,
// cache: &ZorkCache,
env_vars: &EnvVars,
) -> Result<ExitStatus, Report>
where
Expand All @@ -155,7 +159,6 @@ where
.with_context(|| format!("[{compiler}] - Command {arguments} failed!"))
}

/// The pieces and details for the generated command line
/// for some translation unit
///
/// * args* : member that holds all the cmd arguments that will be passed to the compiler driver
Expand Down Expand Up @@ -224,12 +227,14 @@ impl LinkerCommandLine {
/// in order to add it to the files that will be linked to generate the final product
/// in the two-phase compilation model
pub fn add_buildable_at(&mut self, path: &Path) {
self.built_files.push(path.to_path_buf());
self.args.push(Argument::from(path));
}

// TODO: just maybe a Cow for everyone?

/// Owned version of TODO link
pub fn add_owned_buildable_at(&mut self, path: PathBuf) {
self.built_files.push(path);
self.args.push(path.into());
}
}

Expand Down
74 changes: 59 additions & 15 deletions zork++/src/lib/compiler/data_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};

use crate::{
bounds::ExtraArgs,
cache::ZorkCache,
cli::output::arguments::{clang_args, Argument, Arguments},
project_model::compiler::{CppCompiler, StdLib},
project_model::ZorkModel,
Expand Down Expand Up @@ -58,11 +59,13 @@ impl IntoIterator for CommonArgs {
/// command line for every translation unit, regardeless the underlying choosen compiler
pub fn compiler_common_arguments_factory(
model: &ZorkModel<'_>,
cache: &ZorkCache<'_>,
) -> Box<dyn CompilerCommonArguments> {
// TODO: consider having a union (enum) instead of a fat ptr
// TODO: consider having a union (enum) instead of a fat ptr, so we can serialize the data
// and introduce a lifetime on the Argument type to use Cow instead of String
match model.compiler.cpp_compiler {
CppCompiler::CLANG => Box::new(ClangCommonArgs::new(model)),
CppCompiler::MSVC => Box::new(MsvcCommonArgs::new()),
CppCompiler::MSVC => Box::new(MsvcCommonArgs::new(model, cache)),
CppCompiler::GCC => Box::new(GccCommonArgs::new()),
}
}
Expand Down Expand Up @@ -101,13 +104,32 @@ impl CompilerCommonArguments for ClangCommonArgs {
#[typetag::serde]
impl CompilerCommonArguments for MsvcCommonArgs {
fn get_args(&self) -> Arguments {
Arguments::default()
let mut args = Arguments::default();
args.create_and_push(&self.exception_handling_model);
args.create_and_push(&self.no_logo);
args.create_and_push(&self.no_compile);

args.create_and_push(&self.ifc_search_dir);
args.create_and_push(&*self.ifc_search_dir_value);

args.create_and_push("/reference");
args.create_and_push(format! {
"std={}", self.stdlib_ref_path.display()
});
args.create_and_push("/reference");
args.create_and_push(format! {
"std.compat={}", self.c_compat_stdlib_ref_path.display()
});
args
}
}
#[typetag::serde]
impl CompilerCommonArguments for GccCommonArgs {
fn get_args(&self) -> Arguments {
Arguments::default()
let mut args = Arguments::default();
args.create_and_push("-fmodules-ts");
args.create_and_push("-c");
args
}
}

Expand Down Expand Up @@ -140,27 +162,49 @@ impl ClangCommonArgs {
#[derive(Serialize, Deserialize, Default, Debug, Clone)]
pub struct MsvcCommonArgs {
exception_handling_model: Cow<'static, str>,
/* no_logo: &'a str,
no_compile: &'a str, // TODO: should be in the general and pass in the model? */
// ref_stdlib: &'static str, // TODO: this are tecnically two args, /reference and the value
// ref_stdlib_compat: &'static str, // TODO: this are tecnically two args, /reference and the value
// TODO: split the dual cases per switches
// TODO: can we have switches like tuples? like switch-value pairs?
no_logo: Cow<'static, str>,
no_compile: Cow<'static, str>,
reference: Cow<'static, str>,
ifc_search_dir: Cow<'static, str>,
ifc_search_dir_value: Cow<'static, Path>,
stdlib_ref_path: Cow<'static, Path>,
c_compat_stdlib_ref_path: Cow<'static, Path>,
}
impl MsvcCommonArgs {
pub fn new() -> Self {
pub fn new(model: &ZorkModel<'_>, cache: &ZorkCache<'_>) -> Self {
let out_dir: &Path = model.build.output_dir.as_ref();

Self {
exception_handling_model: Cow::Borrowed("/EHsc"),
/* no_logo: "nologo",
no_compile: "/c", */
no_logo: Cow::Borrowed("/nologo"),
no_compile: Cow::Borrowed("/c"),
reference: Cow::Borrowed("/reference"),

ifc_search_dir: Cow::Borrowed("/ifcSearchDir"),
ifc_search_dir_value: Cow::Owned(
out_dir
.join(model.compiler.cpp_compiler.as_ref())
.join("modules")
.join("interfaces"),
),
stdlib_ref_path: Cow::Owned(cache.compilers_metadata.msvc.stdlib_bmi_path.clone()),
c_compat_stdlib_ref_path: Cow::Owned(
cache.compilers_metadata.msvc.c_stdlib_bmi_path.clone(),
),
}
}
}

#[derive(Serialize, Deserialize, Default, Debug, Clone)]
pub struct GccCommonArgs {}
pub struct GccCommonArgs {
compile_but_dont_link: Cow<'static, str>,
modules_ts: Cow<'static, str>,
}
impl GccCommonArgs {
pub fn new() -> Self {
Self {}
Self {
compile_but_dont_link: Cow::Borrowed("-c"),
modules_ts: Cow::Borrowed("-fmodules-ts"),
}
}
}
Loading

0 comments on commit 5421500

Please sign in to comment.