Skip to content

Commit c2c68e0

Browse files
committed
Tests are now handled by cargo test instead of a separate makefile, simplifing build process
1 parent 6cb8a66 commit c2c68e0

File tree

6 files changed

+77
-34
lines changed

6 files changed

+77
-34
lines changed

makefile

+1-25
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,7 @@ RUST_FLAGS =
66
# this function compiles a particular test lib
77
compile_lib = cd test && rustc -O --emit=mir --crate-type=lib $(1).rs && $(DEBUGER) rustc $(RUST_FLAGS) --crate-type lib -O -Z codegen-backend=$(CODEGEN_BACKEND) $(1).rs -o libs/$(1).rlib
88

9-
test: setup build_backend identity branches binops casts types calls references structs libc nbody hello
10-
setup:
11-
cd test && mkdir -p libs
12-
build_backend:
13-
cargo build
14-
calls:
15-
$(call compile_lib,calls)
16-
libc:
17-
$(call compile_lib,libc)
18-
identity:
19-
$(call compile_lib,identity)
20-
binops:
21-
$(call compile_lib,binops)
22-
references:
23-
$(call compile_lib,references)
24-
nbody:
25-
$(call compile_lib,nbody)
26-
casts:
27-
$(call compile_lib,casts)
28-
types:
29-
$(call compile_lib,types)
30-
branches:
31-
$(call compile_lib,branches)
32-
structs:
33-
$(call compile_lib,structs)
9+
test: hello
3410
hello:
3511
cd test && $(DEBUGER) rustc $(RUST_FLAGS) -O -Z codegen-backend=$(CODEGEN_BACKEND) hello.rs && \
3612
ilasm hello && \

src/assembly_exporter/ilasm_exporter.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::{base_ir::BaseIR, clr_method::CLRMethod, types::Type, IString};
1+
use crate::{
2+
assembly_exporter::AssemblyExportError, base_ir::BaseIR, clr_method::CLRMethod, types::Type,
3+
IString,
4+
};
25

36
use super::{AssemblyExporter, ClassInfo};
47
#[must_use]
@@ -22,13 +25,14 @@ impl AssemblyExporter for ILASMExporter {
2225
fn add_method(&mut self, method: CLRMethod) {
2326
self.methods.push(method);
2427
}
25-
fn finalize(self, final_path: &std::path::Path) -> Result<(), super::AssemblyExportError> {
28+
fn finalize(self, final_path: &std::path::Path) -> Result<(), AssemblyExportError> {
2629
use std::io::Write;
2730
//println!("final_path:{final_path:?}");
28-
let directory = final_path
31+
let directory = absolute_path(final_path)
32+
.map_err(|io| AssemblyExportError::CouldNotCanonalizePath(io, final_path.to_owned()))?
2933
.parent()
3034
.expect("Can't get the target directory")
31-
.canonicalize()?;
35+
.to_owned();
3236

3337
let mut out_path = directory.clone();
3438
out_path.set_file_name(final_path.file_name().expect("Target file has no name!"));
@@ -39,11 +43,12 @@ impl AssemblyExporter for ILASMExporter {
3943
); //final_path.expect("Could not canonialize path!");
4044

4145
let cil_path = out_path.with_extension("il");
42-
let cil = self.get_cil()?;
46+
let cil = self.get_cil().expect("Could not get cil");
4347
println!("cil_path:{cil_path:?}");
4448
std::fs::File::create(&cil_path)
4549
.expect("Could not create file")
46-
.write_all(cil.as_bytes())?;
50+
.write_all(cil.as_bytes())
51+
.expect("Could not write bytes");
4752
let asm_type = "/dll";
4853
let target = format!(
4954
"/output:{out_path}",
@@ -61,6 +66,15 @@ impl AssemblyExporter for ILASMExporter {
6166
Ok(())
6267
}
6368
}
69+
fn absolute_path(path: &std::path::Path) -> std::io::Result<std::path::PathBuf> {
70+
if path.has_root() {
71+
Ok(path.to_owned())
72+
} else {
73+
let mut abs_path = std::env::current_dir()?;
74+
abs_path.extend(path);
75+
Ok(abs_path)
76+
}
77+
}
6478
impl ILASMExporter {
6579
fn version(&self) -> (u8, u8, u8, u8) {
6680
(0, 0, 0, 0)

src/assembly_exporter/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ pub(crate) trait AssemblyExporter: Sized {
6060
for method in asm.methods() {
6161
asm_exporter.add_method(method.clone());
6262
}
63-
asm_exporter.finalize(final_path)
63+
Ok(asm_exporter
64+
.finalize(final_path)
65+
.expect("Could not export assembly"))
6466
}
6567
}
6668
#[derive(Debug)]
6769
pub(crate) enum AssemblyExportError {
6870
InvalidIL,
71+
CouldNotCanonalizePath(std::io::Error, std::path::PathBuf),
6972
IoError(std::io::Error),
7073
}
7174
impl From<std::io::Error> for AssemblyExportError {

src/compile_test.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
macro_rules! test_lib {
2+
($test_name:ident) => {
3+
#[test]
4+
fn $test_name() {
5+
// Ensures the test directory is present
6+
std::fs::create_dir_all("./test/out").expect("Could not setup the test env");
7+
// Builds the backend if neceasry
8+
std::process::Command::new("cargo")
9+
.args(["build"])
10+
.output()
11+
.expect("could not build the backend");
12+
// Compiles the test project
13+
let out = std::process::Command::new("rustc")
14+
.current_dir("./test/out")
15+
.args([
16+
"-O",
17+
"--crate-type=lib",
18+
"-Z",
19+
BACKEND_PATH,
20+
concat!("../", stringify!($test_name), ".rs"),
21+
"-o",
22+
concat!("./", stringify!($test_name), ".dll"),
23+
])
24+
.output()
25+
.expect("failed to execute process");
26+
// If stderr is not empty, then something went wrong, so print the stdout and stderr for debuging.
27+
if !out.stderr.is_empty() {
28+
let stdout = String::from_utf8(out.stdout)
29+
.expect("rustc error contained non-UTF8 characters.");
30+
let stderr = String::from_utf8(out.stderr)
31+
.expect("rustc error contained non-UTF8 characters.");
32+
panic!("stdout:\n{stdout}\nstderr:\n{stderr}");
33+
}
34+
}
35+
};
36+
}
37+
#[cfg(test)]
38+
const BACKEND_PATH: &str = "codegen-backend=../../target/debug/librustc_codegen_clr.so";
39+
test_lib! {binops}
40+
test_lib! {branches}
41+
test_lib! {calls}
42+
test_lib! {casts}
43+
test_lib! {identity}
44+
test_lib! {libc}
45+
test_lib! {nbody}
46+
test_lib! {references}
47+
test_lib! {structs}
48+
49+
test_lib! {types}

src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use assembly::*;
4646
mod base_ir;
4747
use base_ir::BaseIR;
4848
mod assembly_exporter;
49+
mod compile_test;
4950
mod projection;
5051
mod statement;
5152
mod types;
@@ -205,7 +206,7 @@ impl CodegenBackend for MyBackend {
205206
&final_assembly,
206207
&path,
207208
)
208-
.expect("Could not create the final assembly!");
209+
.expect("Could not create the final asm!");
209210
Ok(())
210211
}
211212
}

test/calls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
fn test(){}
22
pub extern "C" fn call_test(){test()}
33
fn sqr_mag(x:f32,y:f32)->f32{x*x + y*y}
4-
pub extern "C" fn distance(x1:f32,y1:f32,x2:f32,y2:f32)->f32{sqr_mag(x1 - x2,y1 - y1)}
4+
pub extern "C" fn distance(x1:f32,y1:f32,x2:f32,y2:f32)->f32{sqr_mag(x1 - x2,y1 - y2)}
55
/*
66
#[no_mangle]
77
pub extern fn for_loop()->i32{

0 commit comments

Comments
 (0)