nac3artiq: embed linker script, put intermediate objects in temp dir

This commit is contained in:
Sebastien Bourdeauducq 2021-11-06 13:03:45 +08:00
parent 1fea51d9b3
commit 7d66195eae
5 changed files with 36 additions and 4 deletions

24
Cargo.lock generated
View File

@ -492,6 +492,7 @@ dependencies = [
"nac3parser", "nac3parser",
"parking_lot", "parking_lot",
"pyo3", "pyo3",
"tempfile",
] ]
[[package]] [[package]]
@ -920,6 +921,15 @@ version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "runkernel" name = "runkernel"
version = "0.1.0" version = "0.1.0"
@ -1058,6 +1068,20 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "tempfile"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22"
dependencies = [
"cfg-if",
"libc",
"rand",
"redox_syscall",
"remove_dir_all",
"winapi",
]
[[package]] [[package]]
name = "term" name = "term"
version = "0.7.0" version = "0.7.0"

View File

@ -12,7 +12,7 @@
pkgs.rustPlatform.buildRustPackage { pkgs.rustPlatform.buildRustPackage {
name = "nac3artiq"; name = "nac3artiq";
src = self; src = self;
cargoSha256 = "sha256-o/3QRbe1WEOA7NPyO1bXCLxUxUWBex8bPcApl/aT040="; cargoSha256 = "sha256-otKLhr58HYMjVXAof6AdObNpggPnvK6qOl7I+4LWIP8=";
nativeBuildInputs = [ pkgs.python3 pkgs.llvm_12 ]; nativeBuildInputs = [ pkgs.python3 pkgs.llvm_12 ];
buildInputs = [ pkgs.python3 pkgs.libffi pkgs.libxml2 pkgs.llvm_12 ]; buildInputs = [ pkgs.python3 pkgs.libffi pkgs.libxml2 pkgs.llvm_12 ];
cargoBuildFlags = [ "--package" "nac3artiq" ]; cargoBuildFlags = [ "--package" "nac3artiq" ];

View File

@ -12,5 +12,6 @@ crate-type = ["cdylib"]
pyo3 = { version = "0.14", features = ["extension-module"] } pyo3 = { version = "0.14", features = ["extension-module"] }
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["llvm12-0"] } inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["llvm12-0"] }
parking_lot = "0.11" parking_lot = "0.11"
tempfile = "3"
nac3parser = { path = "../nac3parser" } nac3parser = { path = "../nac3parser" }
nac3core = { path = "../nac3core" } nac3core = { path = "../nac3core" }

View File

@ -1,6 +1,5 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::fs; use std::fs;
use std::path::Path;
use std::process::Command; use std::process::Command;
use std::sync::Arc; use std::sync::Arc;
@ -26,6 +25,8 @@ use nac3core::{
typecheck::{type_inferencer::PrimitiveStore, typedef::Type}, typecheck::{type_inferencer::PrimitiveStore, typedef::Type},
}; };
use tempfile::{self, TempDir};
use crate::{codegen::ArtiqCodeGenerator, symbol_resolver::Resolver}; use crate::{codegen::ArtiqCodeGenerator, symbol_resolver::Resolver};
mod codegen; mod codegen;
@ -68,6 +69,7 @@ struct Nac3 {
to_be_registered: Vec<PyObject>, to_be_registered: Vec<PyObject>,
primitive_ids: PrimitivePythonId, primitive_ids: PrimitivePythonId,
global_value_ids: Arc<Mutex<HashSet<u64>>>, global_value_ids: Arc<Mutex<HashSet<u64>>>,
working_directory: TempDir,
} }
impl Nac3 { impl Nac3 {
@ -283,6 +285,9 @@ impl Nac3 {
.unwrap(), .unwrap(),
}; };
let working_directory = tempfile::Builder::new().prefix("nac3-").tempdir().unwrap();
fs::write(working_directory.path().join("kernel.ld"), include_bytes!("kernel.ld")).unwrap();
Ok(Nac3 { Ok(Nac3 {
isa, isa,
time_fns, time_fns,
@ -296,6 +301,7 @@ impl Nac3 {
pyid_to_type: Default::default(), pyid_to_type: Default::default(),
to_be_registered: Default::default(), to_be_registered: Default::default(),
global_value_ids: Default::default(), global_value_ids: Default::default(),
working_directory
}) })
} }
@ -405,6 +411,7 @@ impl Nac3 {
calls: instance.calls, calls: instance.calls,
}; };
let isa = self.isa; let isa = self.isa;
let working_directory = self.working_directory.path().to_owned();
let f = Arc::new(WithCall::new(Box::new(move |module| { let f = Arc::new(WithCall::new(Box::new(move |module| {
let builder = PassManagerBuilder::create(); let builder = PassManagerBuilder::create();
builder.set_optimization_level(OptimizationLevel::Default); builder.set_optimization_level(OptimizationLevel::Default);
@ -436,7 +443,7 @@ impl Nac3 {
.write_to_file( .write_to_file(
module, module,
FileType::Object, FileType::Object,
Path::new(&format!("{}.o", module.get_name().to_str().unwrap())), &working_directory.join(&format!("{}.o", module.get_name().to_str().unwrap())),
) )
.expect("couldn't write module to file"); .expect("couldn't write module to file");
}))); })));
@ -460,7 +467,7 @@ impl Nac3 {
"module.elf".to_string(), "module.elf".to_string(),
]; ];
if isa != Isa::Host { if isa != Isa::Host {
linker_args.push("-Tkernel.ld".to_string()); linker_args.push("-T".to_string() + self.working_directory.path().join("kernel.ld").to_str().unwrap());
} }
linker_args.extend(thread_names.iter().map(|name| name.to_owned() + ".o")); linker_args.extend(thread_names.iter().map(|name| name.to_owned() + ".o"));
if let Ok(linker_status) = Command::new("ld.lld").args(linker_args).status() { if let Ok(linker_status) = Command::new("ld.lld").args(linker_args).status() {