Skip to content

Commit 91fabc0

Browse files
committed
create binary in build process
1 parent 2c00c26 commit 91fabc0

File tree

7 files changed

+40
-24
lines changed

7 files changed

+40
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target
2+
**/build

examples/cool_app/config.toml

Lines changed: 0 additions & 2 deletions
This file was deleted.

examples/cool_app/lib/math.lv

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/cool_app/main.lv

Lines changed: 0 additions & 4 deletions
This file was deleted.

examples/cool_app/num.lv

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/blush/mod.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ use crate::{
55
blush::combiner::Combiner, checker::Checker,
66
codegen::emitters::x86_64_linux_nasm::CodeGenerator, ir::IRGenerator, parser::Parser,
77
};
8+
use std::fs::File;
9+
use std::io::Write;
10+
use std::process::Command;
811
use std::{
912
error::Error,
1013
fs,
@@ -18,7 +21,7 @@ pub mod printer;
1821
pub struct Blush {}
1922

2023
impl Blush {
21-
pub fn build(path: String) -> Result<(), Box<dyn Error>> {
24+
pub fn build(path: &str, just_asm: bool) -> Result<(), Box<dyn Error>> {
2225
let path_buf = PathBuf::from(path);
2326
let file_tree = collect_files(&path_buf)?;
2427

@@ -69,7 +72,37 @@ impl Blush {
6972
let ir = IRGenerator::new(checker.types).program_ir(&checked_program);
7073
let asm = CodeGenerator::new().gen_asm(&ir);
7174

72-
println!("\n---\n{asm}\n---\n");
75+
if just_asm {
76+
println!("\n{asm}\n");
77+
} else {
78+
// make build directory if it doesn't exist
79+
let path = Path::new(path).join("build");
80+
fs::create_dir_all(&path)?;
81+
82+
// generate asm file
83+
let mut out_file = File::create(path.join("out.asm"))?;
84+
out_file.write_all(asm.as_bytes())?;
85+
86+
// assemble asm into object file
87+
Command::new("nasm")
88+
.arg("-f")
89+
.arg("elf64")
90+
.arg("-o")
91+
.arg(path.join("out.o"))
92+
.arg(path.join("out.asm"))
93+
.output()?;
94+
95+
// link object file into executable
96+
Command::new("ld")
97+
.arg("-o")
98+
.arg(path.join(package_name))
99+
.arg(path.join("out.o"))
100+
.output()?;
101+
102+
// delete intermediate files
103+
fs::remove_file(path.join("out.asm"))?;
104+
fs::remove_file(path.join("out.o"))?;
105+
}
73106

74107
Ok(())
75108
}

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ pub static VERSION: &str = env!("CARGO_PKG_VERSION");
2323
struct Cli {
2424
#[command(subcommand)]
2525
command: Commands,
26+
27+
#[arg(long)]
28+
asm: bool,
2629
}
2730

2831
#[derive(Subcommand)]
@@ -39,7 +42,7 @@ fn main() {
3942
match cli.command {
4043
Commands::Build { path } => {
4144
println!();
42-
let res = Blush::build(path);
45+
let res = Blush::build(&path, cli.asm);
4346
match res {
4447
Ok(()) => {
4548
blush::printer::success("Build complete!");

0 commit comments

Comments
 (0)