mirror of
https://github.com/lune-org/lune.git
synced 2025-04-10 21:40:54 +01:00
refactor: specify when to cross compile more explicitly
* The build command now checks whether the target specified is the same as the host. If so, it proceeds to use the current executable. * Instead of returning a `to_cross_compile` boolean, `get_base_exe_path` now directly returns the path to the current exe if required.
This commit is contained in:
parent
903de08931
commit
0070f132b0
2 changed files with 33 additions and 23 deletions
|
@ -1,5 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
env::consts::EXE_EXTENSION,
|
env::consts,
|
||||||
io::Cursor,
|
io::Cursor,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
process::ExitCode,
|
process::ExitCode,
|
||||||
|
@ -10,6 +10,7 @@ use async_zip::base::read::seek::ZipFileReader;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use console::style;
|
use console::style;
|
||||||
use directories::BaseDirs;
|
use directories::BaseDirs;
|
||||||
|
use mlua::Compiler;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
|
@ -18,7 +19,7 @@ use tokio::{
|
||||||
};
|
};
|
||||||
use tokio_util::compat::{FuturesAsyncReadCompatExt, TokioAsyncReadCompatExt};
|
use tokio_util::compat::{FuturesAsyncReadCompatExt, TokioAsyncReadCompatExt};
|
||||||
|
|
||||||
use crate::standalone::metadata::Metadata;
|
use crate::standalone::metadata::{Metadata, CURRENT_EXE};
|
||||||
|
|
||||||
const TARGET_BASE_DIR: Lazy<PathBuf> = Lazy::new(|| {
|
const TARGET_BASE_DIR: Lazy<PathBuf> = Lazy::new(|| {
|
||||||
BaseDirs::new()
|
BaseDirs::new()
|
||||||
|
@ -50,7 +51,7 @@ impl BuildCommand {
|
||||||
pub async fn run(self) -> Result<ExitCode> {
|
pub async fn run(self) -> Result<ExitCode> {
|
||||||
let output_path = self
|
let output_path = self
|
||||||
.output
|
.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();
|
let input_path_displayed = self.input.display();
|
||||||
|
|
||||||
|
@ -60,8 +61,7 @@ impl BuildCommand {
|
||||||
.context("failed to read input file")?;
|
.context("failed to read input file")?;
|
||||||
|
|
||||||
// Dynamically derive the base executable path based on the CLI arguments provided
|
// Dynamically derive the base executable path based on the CLI arguments provided
|
||||||
let (to_cross_compile, base_exe_path, output_path) =
|
let (base_exe_path, output_path) = get_base_exe_path(self.target, output_path).await?;
|
||||||
get_base_exe_path(self.target, output_path).await?;
|
|
||||||
|
|
||||||
// Read the contents of the lune interpreter as our starting point
|
// Read the contents of the lune interpreter as our starting point
|
||||||
println!(
|
println!(
|
||||||
|
@ -70,12 +70,12 @@ impl BuildCommand {
|
||||||
style(input_path_displayed).underlined()
|
style(input_path_displayed).underlined()
|
||||||
);
|
);
|
||||||
let patched_bin = Metadata::create_env_patched_bin(
|
let patched_bin = Metadata::create_env_patched_bin(
|
||||||
if to_cross_compile {
|
base_exe_path,
|
||||||
Some(base_exe_path)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
},
|
|
||||||
source_code.clone(),
|
source_code.clone(),
|
||||||
|
Compiler::new()
|
||||||
|
.set_optimization_level(2)
|
||||||
|
.set_coverage_level(0)
|
||||||
|
.set_debug_level(1),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.context("failed to create patched binary")?;
|
.context("failed to create patched binary")?;
|
||||||
|
@ -127,13 +127,23 @@ pub enum BasePathDiscoveryError {
|
||||||
async fn get_base_exe_path(
|
async fn get_base_exe_path(
|
||||||
target: Option<String>,
|
target: Option<String>,
|
||||||
output_path: PathBuf,
|
output_path: PathBuf,
|
||||||
) -> Result<(bool, PathBuf, PathBuf), BasePathDiscoveryError> {
|
) -> Result<(PathBuf, PathBuf), BasePathDiscoveryError> {
|
||||||
if let Some(target_inner) = target {
|
if let Some(target_inner) = target {
|
||||||
|
let current_target = format!("{}-{}", consts::OS, consts::ARCH);
|
||||||
|
|
||||||
let target_exe_extension = match target_inner.as_str() {
|
let target_exe_extension = match target_inner.as_str() {
|
||||||
"windows-x86_64" => "exe",
|
"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}"));
|
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
|
// 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?;
|
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 {
|
} 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),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,10 +5,9 @@ use mlua::Compiler as LuaCompiler;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
|
|
||||||
const MAGIC: &[u8; 8] = b"cr3sc3nt";
|
pub const CURRENT_EXE: Lazy<PathBuf> =
|
||||||
|
|
||||||
static CURRENT_EXE: Lazy<PathBuf> =
|
|
||||||
Lazy::new(|| env::current_exe().expect("failed to get current exe"));
|
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
|
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.
|
Creates a patched standalone binary from the given script contents.
|
||||||
*/
|
*/
|
||||||
pub async fn create_env_patched_bin(
|
pub async fn create_env_patched_bin(
|
||||||
base_exe_path: Option<PathBuf>,
|
base_exe_path: PathBuf,
|
||||||
script_contents: impl Into<Vec<u8>>,
|
script_contents: impl Into<Vec<u8>>,
|
||||||
|
compiler: LuaCompiler,
|
||||||
) -> Result<Vec<u8>> {
|
) -> Result<Vec<u8>> {
|
||||||
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
|
// Compile luau input into bytecode
|
||||||
let bytecode = LuaCompiler::new()
|
let bytecode = compiler.compile(script_contents.into());
|
||||||
.set_optimization_level(2)
|
|
||||||
.set_coverage_level(0)
|
|
||||||
.set_debug_level(1)
|
|
||||||
.compile(script_contents.into());
|
|
||||||
|
|
||||||
// Append the bytecode / metadata to the end
|
// Append the bytecode / metadata to the end
|
||||||
let meta = Self { bytecode };
|
let meta = Self { bytecode };
|
||||||
|
|
Loading…
Add table
Reference in a new issue