From 8ab4aefaa9d1f2a58e57e76aba3d27ab6a585caa Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 21 Oct 2021 09:50:29 +1000 Subject: [PATCH] pyoxidizer: allow specifying an existing project crate --- pyoxidizer/src/project_building.rs | 64 ++++++++++++++++++------------ 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/pyoxidizer/src/project_building.rs b/pyoxidizer/src/project_building.rs index 45612d897..9024c0e96 100644 --- a/pyoxidizer/src/project_building.rs +++ b/pyoxidizer/src/project_building.rs @@ -368,7 +368,8 @@ pub fn build_executable_with_rust_project<'a>( }) } -/// Build a Python executable using a temporary Rust project. +/// Build a Python executable using a temporary Rust project, or +/// the one specified by the PYOXIDIZER_CRATE_FOLDER env var. /// /// Returns the binary data constituting the built executable. pub fn build_python_executable<'a>( @@ -380,30 +381,43 @@ pub fn build_python_executable<'a>( opt_level: &str, release: bool, ) -> Result> { - let cargo_exe = env - .ensure_rust_toolchain(logger, Some(target_triple)) - .context("resolving Rust toolchain")? - .cargo_exe; - - let temp_dir = tempfile::Builder::new() - .prefix("pyoxidizer") - .tempdir() - .context("creating temp directory")?; - - // Directory needs to have name of project. - let project_path = temp_dir.path().join(bin_name); - let build_path = temp_dir.path().join("build"); - let artifacts_path = temp_dir.path().join("artifacts"); - - initialize_project( - &env.pyoxidizer_source, - &project_path, - &cargo_exe, - None, - &[], - exe.windows_subsystem(), - ) - .context("initializing project")?; + let temp_dir: tempfile::TempDir; + let project_path: PathBuf; + + // Existing Rust project provided? + let root = if let Ok(folder) = std::env::var("PYOXIDIZER_CRATE_FOLDER") { + project_path = PathBuf::from(folder); + &project_path + } else { + // No existing project, we'll need to create a temporary one. + let cargo_exe = env + .ensure_rust_toolchain(logger, Some(target_triple)) + .context("resolving Rust toolchain")? + .cargo_exe; + + temp_dir = tempfile::Builder::new() + .prefix("pyoxidizer") + .tempdir() + .context("creating temp directory")?; + + // Directory needs to have name of project. + project_path = temp_dir.path().join(bin_name); + + initialize_project( + &env.pyoxidizer_source, + &project_path, + &cargo_exe, + None, + &[], + exe.windows_subsystem(), + ) + .context("initializing project")?; + + temp_dir.path() + }; + + let build_path = root.join("build"); + let artifacts_path = root.join("artifacts"); let mut build = build_executable_with_rust_project( env,