diff --git a/src/cli/build.rs b/src/cli/build.rs index 65ff759..861e046 100644 --- a/src/cli/build.rs +++ b/src/cli/build.rs @@ -1,5 +1,5 @@ use std::{ - env::consts::EXE_EXTENSION, + env::consts, io::Cursor, path::{Path, PathBuf}, process::ExitCode, @@ -10,6 +10,7 @@ use async_zip::base::read::seek::ZipFileReader; use clap::Parser; use console::style; use directories::BaseDirs; +use mlua::Compiler; use once_cell::sync::Lazy; use thiserror::Error; use tokio::{ @@ -18,7 +19,7 @@ use tokio::{ }; use tokio_util::compat::{FuturesAsyncReadCompatExt, TokioAsyncReadCompatExt}; -use crate::standalone::metadata::Metadata; +use crate::standalone::metadata::{Metadata, CURRENT_EXE}; const TARGET_BASE_DIR: Lazy = Lazy::new(|| { BaseDirs::new() @@ -50,7 +51,7 @@ impl BuildCommand { pub async fn run(self) -> Result { let output_path = self .output - .unwrap_or_else(|| self.input.with_extension(EXE_EXTENSION)); + .unwrap_or_else(|| self.input.with_extension(consts::EXE_EXTENSION)); let input_path_displayed = self.input.display(); @@ -60,8 +61,7 @@ impl BuildCommand { .context("failed to read input file")?; // Dynamically derive the base executable path based on the CLI arguments provided - let (to_cross_compile, base_exe_path, output_path) = - get_base_exe_path(self.target, output_path).await?; + let (base_exe_path, output_path) = get_base_exe_path(self.target, output_path).await?; // Read the contents of the lune interpreter as our starting point println!( @@ -70,12 +70,12 @@ impl BuildCommand { style(input_path_displayed).underlined() ); let patched_bin = Metadata::create_env_patched_bin( - if to_cross_compile { - Some(base_exe_path) - } else { - None - }, + base_exe_path, source_code.clone(), + Compiler::new() + .set_optimization_level(2) + .set_coverage_level(0) + .set_debug_level(1), ) .await .context("failed to create patched binary")?; @@ -127,13 +127,23 @@ pub enum BasePathDiscoveryError { async fn get_base_exe_path( target: Option, output_path: PathBuf, -) -> Result<(bool, PathBuf, PathBuf), BasePathDiscoveryError> { +) -> Result<(PathBuf, PathBuf), BasePathDiscoveryError> { if let Some(target_inner) = target { + let current_target = format!("{}-{}", consts::OS, consts::ARCH); + let target_exe_extension = match target_inner.as_str() { "windows-x86_64" => "exe", _ => "", }; + if target_inner == current_target { + // If the target is the host target, just use the current executable + return Ok(( + CURRENT_EXE.to_path_buf(), + output_path.with_extension(consts::EXE_EXTENSION), + )); + } + let path = TARGET_BASE_DIR.join(format!("lune-{target_inner}.{target_exe_extension}")); // Create the target base directory in the lune home if it doesn't already exist @@ -150,9 +160,13 @@ async fn get_base_exe_path( cache_target(target_inner, target_exe_extension, &path).await?; } - Ok((true, path, output_path.with_extension(target_exe_extension))) + Ok((path, output_path.with_extension(target_exe_extension))) } else { - Ok((false, PathBuf::new(), output_path)) + // If the target flag was not specified, just use the current executable + Ok(( + CURRENT_EXE.to_path_buf(), + output_path.with_extension(consts::EXE_EXTENSION), + )) } } diff --git a/src/standalone/metadata.rs b/src/standalone/metadata.rs index ad54b5d..e546196 100644 --- a/src/standalone/metadata.rs +++ b/src/standalone/metadata.rs @@ -5,10 +5,9 @@ use mlua::Compiler as LuaCompiler; use once_cell::sync::Lazy; use tokio::fs; -const MAGIC: &[u8; 8] = b"cr3sc3nt"; - -static CURRENT_EXE: Lazy = +pub const CURRENT_EXE: Lazy = Lazy::new(|| env::current_exe().expect("failed to get current exe")); +const MAGIC: &[u8; 8] = b"cr3sc3nt"; /* TODO: Right now all we do is append the bytecode to the end @@ -50,17 +49,14 @@ impl Metadata { Creates a patched standalone binary from the given script contents. */ pub async fn create_env_patched_bin( - base_exe_path: Option, + base_exe_path: PathBuf, script_contents: impl Into>, + compiler: LuaCompiler, ) -> Result> { - let mut patched_bin = fs::read(base_exe_path.unwrap_or(CURRENT_EXE.to_path_buf())).await?; + let mut patched_bin = fs::read(base_exe_path).await?; // Compile luau input into bytecode - let bytecode = LuaCompiler::new() - .set_optimization_level(2) - .set_coverage_level(0) - .set_debug_level(1) - .compile(script_contents.into()); + let bytecode = compiler.compile(script_contents.into()); // Append the bytecode / metadata to the end let meta = Self { bytecode };