Skip to content

Commit

Permalink
feat: multientry option for build added
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmasoldi3r committed Dec 26, 2023
1 parent 72f25fb commit 64f7376
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 31 deletions.
28 changes: 19 additions & 9 deletions janus/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
dir::create_dist_dirs,
display::get_bar,
errors::ExitCode,
janusfile::{DependencyList, JanusBuild, JanusProject, OutputFormat},
janusfile::{DependencyList, JanusBuild, JanusProject, OutputFormat, PathBufOrPathBufList},
};

pub enum CompilationError {}
Expand All @@ -45,7 +45,7 @@ pub struct CompilationInfo {
pub format: OutputFormat,
pub target: CompilationTarget,
pub module_system: ModuleSystem,
pub main: PathBuf,
pub main: PathBufOrPathBufList,
pub no_std: bool,
pub mode: CompilationMode,
}
Expand All @@ -59,9 +59,10 @@ impl CompilationHost {
/// Attempt to compile a single file.
fn compile_file(&self, info: &CompilationInfo, file_path: &PathBuf) -> PathBuf {
let mut cmd = Command::new("saturnus");
if info.mode == CompilationMode::Bin && &info.main == file_path {
// Here we should inject STD if no no-std flag is provided.
}
// TODO: Review this
// if info.mode == CompilationMode::Bin && &info.main == file_path {
// // Here we should inject STD if no no-std flag is provided.
// }
let mut out = info
.output
.join("cache")
Expand All @@ -75,9 +76,18 @@ impl CompilationHost {
.arg(file_path.to_str().unwrap())
.arg("-o")
.arg(&out);
if &info.main != file_path {
// If main, omit no STD
cmd.arg("--no-std");
// If not the main file, skip the STD header injection
match &info.main {
PathBufOrPathBufList::PathBuf(main) => {
if main != file_path {
cmd.arg("--no-std");
}
}
PathBufOrPathBufList::PathBufList(mains) => {
if !mains.contains(file_path) {
cmd.arg("--no-std");
}
}
}
// Handle the final execution of the command.
match cmd.spawn() {
Expand Down Expand Up @@ -213,7 +223,7 @@ impl CompilationHost {
} = info;
let output = get_output_folder(output);
let source = get_source_folder(source);
let main = main.unwrap_or("src/main.saturn".into());
let main = main.unwrap_or(PathBufOrPathBufList::PathBuf("src/main.saturn".into()));
let no_std = no_std.unwrap_or(false);
let format = match format.unwrap_or("dir".to_owned()).as_str() {
"flat" => OutputFormat::FlatDirectory,
Expand Down
24 changes: 17 additions & 7 deletions janus/src/compilation/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,29 @@ impl FilePipeline {
exclude: &HashSet<PathBuf>,
) {
let pb = get_bar(glob::glob("./dist/cache/objects/**/*.lua").unwrap().count() as u64);
let mut main: Option<PathBuf> = None;
let mut mains: Vec<PathBuf> = vec![];
let mut file_out = match info.target {
CompilationTarget::Lua => {
let out_path = info.output.join("target").join("main.lua");
File::create(output.unwrap_or(out_path)).unwrap()
}
};
let mut main_path = objects_base_path.join(info.main.strip_prefix(&info.source).unwrap());
main_path.set_extension("lua");
let mut main_paths = match &info.main {
crate::janusfile::PathBufOrPathBufList::PathBuf(main) => {
vec![objects_base_path.join(main.strip_prefix(&info.source).unwrap())]
}
crate::janusfile::PathBufOrPathBufList::PathBufList(mains) => mains
.iter()
.map(|main| objects_base_path.join(main.strip_prefix(&info.source).unwrap()))
.collect(),
};
for path in &mut main_paths {
path.set_extension("lua");
}
for entry in glob::glob("./dist/cache/objects/**/*.lua").unwrap() {
let entry = entry.expect("Could not unwrap an entry path");
if &entry == &main_path {
main = Some(entry.clone());
if main_paths.contains(&entry) {
mains.push(entry.clone());
continue;
}
if exclude.contains(&entry) {
Expand Down Expand Up @@ -65,8 +75,8 @@ impl FilePipeline {
file_out.write(b"\nend;").unwrap();
pb.inc(1);
}
if let Some(entry) = main {
pb.set_message("Collecting main file...");
for entry in mains {
pb.set_message("Collecting entry files...");
let src = fs::read_to_string(entry).unwrap();
file_out.write(b"\n").unwrap();
file_out.write_all(&src.as_bytes()).unwrap();
Expand Down
9 changes: 8 additions & 1 deletion janus/src/janusfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ pub struct ModulesOptions {
pub external: Option<Vec<PathBuf>>,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum PathBufOrPathBufList {
PathBuf(PathBuf),
PathBufList(Vec<PathBuf>),
}

#[derive(Debug, Deserialize, Serialize, Default)]
pub struct JanusBuild {
pub source: Option<PathBuf>,
pub output: Option<PathBuf>,
pub main: Option<PathBuf>,
pub main: Option<PathBufOrPathBufList>,
pub format: Option<String>,
pub target: Option<String>,
pub module_system: Option<String>,
Expand Down
40 changes: 27 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ struct Args {
help = "Specifies the output target runtime, only lua is supported for now."
)]
target: Option<String>,
#[arg(
short,
long = "bin",
help = "Compiles the Saturnus script into an executable binary"
)]
binary: bool,
// This is now part of Janus
// #[arg(
// short,
// long = "bin",
// help = "Compiles the Saturnus script into an executable binary"
// )]
// binary: bool,
#[arg(
short = 'p',
long = "print",
Expand Down Expand Up @@ -85,18 +86,16 @@ struct Args {
help = "Outputs the saturnus code to stdout preprocessed but without compiling"
)]
dump_saturnus: bool,
// May be useful in the future
// #[arg(
// long,
// help = "Dumps the std library compiled"
// )]
// dump_std: bool,
#[arg(
short = 'm',
long = "mod",
help = "Additional module root paths to load"
)]
modules: Vec<String>,
#[arg(long, help = "Extracts the STD library compiled")]
extract_std_compiled: Option<PathBuf>,
#[arg(long, help = "Extracts the STD library raw (Saturnus code)")]
extract_std_raw: Option<PathBuf>,
}

fn get_default_output(str: &Path) -> String {
Expand Down Expand Up @@ -130,9 +129,11 @@ struct CompilationOptions {
// }
// }

const STD_SRC: &'static str = include_str!("assets/std.saturn");

fn precompile_std(compiler: &dyn Visitor) -> Result<(String, md5::Digest), RuntimeError> {
// Precompile STD
let std_src = include_str!("assets/std.saturn");
let std_src = STD_SRC;
let std_src = Script::parse(std_src.to_owned()).unwrap();
let std_src = compiler
.visit_script(Builder::new(" "), &std_src)
Expand Down Expand Up @@ -234,6 +235,19 @@ fn main() {
};
use std::fs::read_to_string;

{
let compiler = lua::visitor::LuaEmitter::new(InputFileInfo {
full_path: PathBuf::from("std.saturn"),
});
// Dump STD if provided
if let Some(raw_std) = &args.extract_std_raw {
std::fs::write(raw_std, STD_SRC).unwrap();
}
if let Some(compiled_std) = &args.extract_std_compiled {
std::fs::write(compiled_std, precompile_std(&compiler).unwrap().0).unwrap();
}
}

// Read input files
let in_path = Path::new(&args.input);
let out_path = args.clone().output.unwrap_or(get_default_output(in_path));
Expand Down
2 changes: 1 addition & 1 deletion update
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

git pull
cargo install --path .
cd runtime && cargo build --release
cd runtime && cargo build --release && cd ..
cargo install --path janus
1 change: 1 addition & 0 deletions update.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ cargo install --path .
# Change directory to 'runtime' and build with release configuration
Set-Location -Path "runtime"
cargo build --release
Set-Location -Path ".."

# Install Janus using Cargo
cargo install --path janus

0 comments on commit 64f7376

Please sign in to comment.