From 7d66195eae7498ec51c8dbd7f2c9d1382679673a Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sat, 6 Nov 2021 13:03:45 +0800 Subject: [PATCH] nac3artiq: embed linker script, put intermediate objects in temp dir --- Cargo.lock | 24 ++++++++++++++++++++++++ flake.nix | 2 +- nac3artiq/Cargo.toml | 1 + nac3artiq/{demo => src}/kernel.ld | 0 nac3artiq/src/lib.rs | 13 ++++++++++--- 5 files changed, 36 insertions(+), 4 deletions(-) rename nac3artiq/{demo => src}/kernel.ld (100%) diff --git a/Cargo.lock b/Cargo.lock index 0490281c5..2d46da031 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -492,6 +492,7 @@ dependencies = [ "nac3parser", "parking_lot", "pyo3", + "tempfile", ] [[package]] @@ -920,6 +921,15 @@ version = "0.6.25" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "runkernel" version = "0.1.0" @@ -1058,6 +1068,20 @@ dependencies = [ "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]] name = "term" version = "0.7.0" diff --git a/flake.nix b/flake.nix index cc7546383..8cf2ef116 100644 --- a/flake.nix +++ b/flake.nix @@ -12,7 +12,7 @@ pkgs.rustPlatform.buildRustPackage { name = "nac3artiq"; src = self; - cargoSha256 = "sha256-o/3QRbe1WEOA7NPyO1bXCLxUxUWBex8bPcApl/aT040="; + cargoSha256 = "sha256-otKLhr58HYMjVXAof6AdObNpggPnvK6qOl7I+4LWIP8="; nativeBuildInputs = [ pkgs.python3 pkgs.llvm_12 ]; buildInputs = [ pkgs.python3 pkgs.libffi pkgs.libxml2 pkgs.llvm_12 ]; cargoBuildFlags = [ "--package" "nac3artiq" ]; diff --git a/nac3artiq/Cargo.toml b/nac3artiq/Cargo.toml index 5f6f8fd04..96161a9ad 100644 --- a/nac3artiq/Cargo.toml +++ b/nac3artiq/Cargo.toml @@ -12,5 +12,6 @@ crate-type = ["cdylib"] pyo3 = { version = "0.14", features = ["extension-module"] } inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["llvm12-0"] } parking_lot = "0.11" +tempfile = "3" nac3parser = { path = "../nac3parser" } nac3core = { path = "../nac3core" } diff --git a/nac3artiq/demo/kernel.ld b/nac3artiq/src/kernel.ld similarity index 100% rename from nac3artiq/demo/kernel.ld rename to nac3artiq/src/kernel.ld diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index d683e8483..8bd191095 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -1,6 +1,5 @@ use std::collections::{HashMap, HashSet}; use std::fs; -use std::path::Path; use std::process::Command; use std::sync::Arc; @@ -26,6 +25,8 @@ use nac3core::{ typecheck::{type_inferencer::PrimitiveStore, typedef::Type}, }; +use tempfile::{self, TempDir}; + use crate::{codegen::ArtiqCodeGenerator, symbol_resolver::Resolver}; mod codegen; @@ -68,6 +69,7 @@ struct Nac3 { to_be_registered: Vec, primitive_ids: PrimitivePythonId, global_value_ids: Arc>>, + working_directory: TempDir, } impl Nac3 { @@ -283,6 +285,9 @@ impl Nac3 { .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 { isa, time_fns, @@ -296,6 +301,7 @@ impl Nac3 { pyid_to_type: Default::default(), to_be_registered: Default::default(), global_value_ids: Default::default(), + working_directory }) } @@ -405,6 +411,7 @@ impl Nac3 { calls: instance.calls, }; let isa = self.isa; + let working_directory = self.working_directory.path().to_owned(); let f = Arc::new(WithCall::new(Box::new(move |module| { let builder = PassManagerBuilder::create(); builder.set_optimization_level(OptimizationLevel::Default); @@ -436,7 +443,7 @@ impl Nac3 { .write_to_file( module, 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"); }))); @@ -460,7 +467,7 @@ impl Nac3 { "module.elf".to_string(), ]; 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")); if let Ok(linker_status) = Command::new("ld.lld").args(linker_args).status() {