edlang/lib/edlang_driver/tests/common.rs

86 lines
2.2 KiB
Rust
Raw Normal View History

2024-02-14 07:53:47 +00:00
use std::{
borrow::Cow,
fmt,
path::{Path, PathBuf},
2024-02-16 10:48:15 +00:00
process::Child,
2024-02-14 07:53:47 +00:00
};
use ariadne::Source;
use edlang_codegen_llvm::linker::{link_binary, link_shared_lib};
use edlang_lowering::lower_modules;
use edlang_session::{DebugInfo, OptLevel, Session};
use tempfile::TempDir;
#[derive(Debug, Clone)]
struct TestError(Cow<'static, str>);
impl fmt::Display for TestError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for TestError {}
#[derive(Debug)]
pub struct CompileResult {
pub folder: TempDir,
pub object_file: PathBuf,
pub binary_file: PathBuf,
}
pub fn compile_program(
source: &str,
name: &str,
library: bool,
) -> Result<CompileResult, Box<dyn std::error::Error>> {
let modules = edlang_parser::parse_ast(source).unwrap();
2024-02-14 07:53:47 +00:00
let test_dir = tempfile::tempdir()?;
let test_dir_path = test_dir.path();
let target_dir = test_dir_path.join("build_artifacts/");
if !target_dir.exists() {
std::fs::create_dir_all(&target_dir)?;
}
let output_file = target_dir.join(PathBuf::from(name));
let output_file = if library {
output_file.with_extension(Session::get_platform_library_ext())
} else if cfg!(target_os = "windows") {
output_file.with_extension("exe")
} else {
output_file.with_extension("")
};
let session = Session {
file_path: PathBuf::from(name),
debug_info: DebugInfo::Full,
optlevel: OptLevel::Default,
source: Source::from(source.to_string()),
library,
target_dir,
output_file,
output_llvm: false,
output_asm: false,
};
let program_ir = lower_modules(&modules)?;
2024-02-14 07:53:47 +00:00
let object_path = edlang_codegen_llvm::compile(&session, &program_ir)?;
if library {
link_shared_lib(&object_path, &session.output_file)?;
} else {
link_binary(&object_path, &session.output_file)?;
}
Ok(CompileResult {
folder: test_dir,
object_file: object_path,
binary_file: session.output_file,
})
}
2024-02-16 10:48:15 +00:00
pub fn run_program(program: &Path, args: &[&str]) -> Result<Child, std::io::Error> {
std::process::Command::new(dbg!(program)).args(args).spawn()
2024-02-14 07:53:47 +00:00
}