From 5a373048088894b114cc9435690e7fc5ce83b497 Mon Sep 17 00:00:00 2001 From: zhangjun Date: Mon, 24 Apr 2023 15:34:19 +0800 Subject: [PATCH 1/2] add build in local path --- pyoxidizer/src/environment.rs | 2 +- pyoxidizer/src/project_building.rs | 95 +++++++++++++++++++- pyoxidizer/src/project_layout.rs | 2 + pyoxidizer/src/starlark/file_resource.rs | 11 ++- pyoxidizer/src/starlark/python_executable.rs | 13 ++- 5 files changed, 115 insertions(+), 8 deletions(-) diff --git a/pyoxidizer/src/environment.rs b/pyoxidizer/src/environment.rs index 228b2f6f3..57a2da664 100644 --- a/pyoxidizer/src/environment.rs +++ b/pyoxidizer/src/environment.rs @@ -259,7 +259,7 @@ pub struct Environment { /// /// Should only be set if we're running a binary from a `cargo` command, /// as the environment variables this keys off of are set by `cargo`. - cargo_target_directory: Option, + pub cargo_target_directory: Option, /// Directory to use for caching things. cache_dir: PathBuf, diff --git a/pyoxidizer/src/project_building.rs b/pyoxidizer/src/project_building.rs index 57a04fb6c..ef5409a9a 100644 --- a/pyoxidizer/src/project_building.rs +++ b/pyoxidizer/src/project_building.rs @@ -16,6 +16,7 @@ use { }, anyhow::{anyhow, Context, Result}, apple_sdk::AppleSdk, + duct::cmd, log::warn, starlark_dialect_build_targets::ResolvedTarget, @@ -26,6 +27,7 @@ use { path::{Path, PathBuf}, }, }; +use serde::{Deserialize, Serialize}; /// Find a pyoxidizer.toml configuration file by walking directory ancestry. pub fn find_pyoxidizer_config_file(start_dir: &Path) -> Option { @@ -422,11 +424,36 @@ pub fn build_python_executable<'a>( opt_level: &str, release: bool, ) -> Result> { + let json = serde_json::to_string( &env.pyoxidizer_source.as_pyembed_location())?; + println!("env in as_pyembed_location {}",json); + let p = &env.cargo_target_directory; + println!("env in cargo_target_directory {}",p.clone().get_or_insert(PathBuf::new()).display()); let cargo_exe = env .ensure_rust_toolchain(Some(target_triple)) .context("resolving Rust toolchain")? .cargo_exe; + build_python_executable_temp( + env, + bin_name, + exe, + target_triple, + opt_level, + release, + cargo_exe, + ) + +} + +fn build_python_executable_temp<'a>( + env: &Environment, + bin_name: &str, + exe: &'a (dyn PythonBinaryBuilder + 'a), + target_triple: &str, + opt_level: &str, + release: bool, + cargo_exe: PathBuf, +)->Result>{ let temp_dir = env.temporary_directory("pyoxidizer")?; // Directory needs to have name of project. @@ -467,8 +494,74 @@ pub fn build_python_executable<'a>( build.exe_path = None; temp_dir.close().context("closing temporary directory")?; + Ok(build) +} +/// Build a Python executable with local or init Rust project. +/// +/// Returns the binary data constituting the built executable. +pub fn build_python_executable_local<'a>( + env: &Environment, + bin_name: &str, + exe: &'a (dyn PythonBinaryBuilder + 'a), + target_triple: &str, + opt_level: &str, + release: bool, + cwd: PathBuf, +) -> Result> { + let json = serde_json::to_string( &env.pyoxidizer_source.as_pyembed_location())?; + println!("build_python_executable_local env in as_pyembed_location {}",json); + let p = &env.cargo_target_directory; + println!("build_python_executable_local env in cargo_target_directory {}",p.clone().get_or_insert(PathBuf::new()).display()); + let cargo_exe = env + .ensure_rust_toolchain(Some(target_triple)) + .context("resolving Rust toolchain")? + .cargo_exe; + + let toml = cwd.join("Cargo.toml"); + println!("toml.exists() is {}",toml.exists()); + println!("toml.display() is {}",toml.display()); + if toml.exists() { + // Directory needs to have name of project. + let project_path = cwd.clone(); + let build_path = cwd.join("build"); + let artifacts_path = cwd.join("artifacts"); + let mut build = build_executable_with_rust_project( + env, + &project_path, + bin_name, + exe, + &build_path, + &artifacts_path, + target_triple, + opt_level, + release, + // Always build with locked because we crated a Cargo.lock with the + // Rust project we just created. + true, + // Don't include license for self because the Rust project is temporary and its + // licensing isn't material. + false, + ) + .context("building executable with Rust project")?; + + // Blank out the path since it is in the temporary directory. + build.exe_path = None; + + Ok(build) + } else { + build_python_executable_temp( + env, + bin_name, + exe, + target_triple, + opt_level, + release, + cargo_exe, + ) + } + + - Ok(build) } /// Build artifacts needed by the pyembed crate. diff --git a/pyoxidizer/src/project_layout.rs b/pyoxidizer/src/project_layout.rs index 8411d2f50..b589348af 100644 --- a/pyoxidizer/src/project_layout.rs +++ b/pyoxidizer/src/project_layout.rs @@ -337,6 +337,8 @@ pub fn write_application_manifest(project_dir: &Path, program_name: &str) -> Res } /// How to define the ``pyembed`` crate dependency. +/// +#[derive( Debug, Clone, PartialEq,Serialize)] pub enum PyembedLocation { /// Use a specific version, installed from the crate registry. /// diff --git a/pyoxidizer/src/starlark/file_resource.rs b/pyoxidizer/src/starlark/file_resource.rs index 9da0cf32e..c7b51d0a7 100644 --- a/pyoxidizer/src/starlark/file_resource.rs +++ b/pyoxidizer/src/starlark/file_resource.rs @@ -11,7 +11,7 @@ use { python_package_resource::PythonPackageResourceValue, }, crate::{ - project_building::build_python_executable, + project_building::build_python_executable_local, py_packaging::{binary::PythonBinaryBuilder, resource::AddToFileManifest}, }, anyhow::{anyhow, Context, Result}, @@ -29,7 +29,7 @@ use { starlark_signature_extraction, starlark_signatures, }, }, - std::{ops::DerefMut, path::Path}, + std::{ops::DerefMut, path::{Path,PathBuf}}, tugger::starlark::file_manifest::FileManifestValue, }; @@ -42,10 +42,15 @@ pub fn file_manifest_add_python_executable( target: &str, release: bool, opt_level: &str, + cwd: PathBuf, ) -> Result<()> { const LABEL: &str = "FileManifest.add_python_executable()"; + println!("running file_manifest_add_python_executable "); - let build = build_python_executable(env, &exe.name(), exe, target, opt_level, release) + println!("manifest run_path is {}", manifest.run_path.get_or_insert(PathBuf::new()).display()); + println!("manifest paths is {}", manifest.paths().unwrap()); + + let build = build_python_executable_local(env, &exe.name(), exe, target, opt_level, release,cwd) .context("building Python executable")?; let content = FileEntry::new_from_data(build.exe_data.clone(), true); diff --git a/pyoxidizer/src/starlark/python_executable.rs b/pyoxidizer/src/starlark/python_executable.rs index b68bbdf35..52545ddf3 100644 --- a/pyoxidizer/src/starlark/python_executable.rs +++ b/pyoxidizer/src/starlark/python_executable.rs @@ -18,7 +18,7 @@ use { }, crate::{ licensing::licenses_from_cargo_manifest, - project_building::build_python_executable, + project_building::build_python_executable_local, py_packaging::binary::PythonBinaryBuilder, py_packaging::binary::{PackedResourcesLoadMode, WindowsRuntimeDllsMode}, }, @@ -84,13 +84,19 @@ pub fn build_internal( ) -> Result<(ResolvedTarget, PathBuf)> { // Build an executable by writing out a temporary Rust project // and building it. - let build = build_python_executable( + println!("running build_internal "); + + println!("cwd is {}",context.cwd.display()); + println!("config_path is {}",context.config_path.display()); + + let build = build_python_executable_local( context.env(), &exe.name(), &**exe, &context.build_target_triple, &context.build_opt_level, context.build_release, + context.cwd.clone(), ) .context("building Python executable")?; @@ -827,7 +833,7 @@ impl PythonExecutableValue { /// PythonExecutable.to_file_manifest(prefix) pub fn to_file_manifest(&self, type_values: &TypeValues, prefix: String) -> ValueResult { const LABEL: &str = "PythonExecutable.to_file_manifest()"; - + let pyoxidizer_context_value = get_context(type_values)?; let pyoxidizer_context = pyoxidizer_context_value .downcast_ref::() @@ -850,6 +856,7 @@ impl PythonExecutableValue { &pyoxidizer_context.build_target_triple, pyoxidizer_context.build_release, &pyoxidizer_context.build_opt_level, + pyoxidizer_context.cwd.clone(), ) .context("adding PythonExecutable to FileManifest") })?; From ebcf76e8fb877ac8cda3a6756571e0f68fe0649e Mon Sep 17 00:00:00 2001 From: zhangjun Date: Mon, 24 Apr 2023 15:53:31 +0800 Subject: [PATCH 2/2] remove all debug info --- pyoxidizer/src/environment.rs | 2 +- pyoxidizer/src/project_building.rs | 16 +--------------- pyoxidizer/src/project_layout.rs | 2 -- pyoxidizer/src/starlark/file_resource.rs | 4 ---- pyoxidizer/src/starlark/python_executable.rs | 9 ++------- 5 files changed, 4 insertions(+), 29 deletions(-) diff --git a/pyoxidizer/src/environment.rs b/pyoxidizer/src/environment.rs index 57a2da664..228b2f6f3 100644 --- a/pyoxidizer/src/environment.rs +++ b/pyoxidizer/src/environment.rs @@ -259,7 +259,7 @@ pub struct Environment { /// /// Should only be set if we're running a binary from a `cargo` command, /// as the environment variables this keys off of are set by `cargo`. - pub cargo_target_directory: Option, + cargo_target_directory: Option, /// Directory to use for caching things. cache_dir: PathBuf, diff --git a/pyoxidizer/src/project_building.rs b/pyoxidizer/src/project_building.rs index ef5409a9a..59bdd1ef1 100644 --- a/pyoxidizer/src/project_building.rs +++ b/pyoxidizer/src/project_building.rs @@ -16,7 +16,6 @@ use { }, anyhow::{anyhow, Context, Result}, apple_sdk::AppleSdk, - duct::cmd, log::warn, starlark_dialect_build_targets::ResolvedTarget, @@ -27,7 +26,6 @@ use { path::{Path, PathBuf}, }, }; -use serde::{Deserialize, Serialize}; /// Find a pyoxidizer.toml configuration file by walking directory ancestry. pub fn find_pyoxidizer_config_file(start_dir: &Path) -> Option { @@ -424,10 +422,6 @@ pub fn build_python_executable<'a>( opt_level: &str, release: bool, ) -> Result> { - let json = serde_json::to_string( &env.pyoxidizer_source.as_pyembed_location())?; - println!("env in as_pyembed_location {}",json); - let p = &env.cargo_target_directory; - println!("env in cargo_target_directory {}",p.clone().get_or_insert(PathBuf::new()).display()); let cargo_exe = env .ensure_rust_toolchain(Some(target_triple)) .context("resolving Rust toolchain")? @@ -508,20 +502,14 @@ pub fn build_python_executable_local<'a>( release: bool, cwd: PathBuf, ) -> Result> { - let json = serde_json::to_string( &env.pyoxidizer_source.as_pyembed_location())?; - println!("build_python_executable_local env in as_pyembed_location {}",json); - let p = &env.cargo_target_directory; - println!("build_python_executable_local env in cargo_target_directory {}",p.clone().get_or_insert(PathBuf::new()).display()); let cargo_exe = env .ensure_rust_toolchain(Some(target_triple)) .context("resolving Rust toolchain")? .cargo_exe; let toml = cwd.join("Cargo.toml"); - println!("toml.exists() is {}",toml.exists()); - println!("toml.display() is {}",toml.display()); + if toml.exists() { - // Directory needs to have name of project. let project_path = cwd.clone(); let build_path = cwd.join("build"); let artifacts_path = cwd.join("artifacts"); @@ -560,8 +548,6 @@ pub fn build_python_executable_local<'a>( ) } - - } /// Build artifacts needed by the pyembed crate. diff --git a/pyoxidizer/src/project_layout.rs b/pyoxidizer/src/project_layout.rs index b589348af..8411d2f50 100644 --- a/pyoxidizer/src/project_layout.rs +++ b/pyoxidizer/src/project_layout.rs @@ -337,8 +337,6 @@ pub fn write_application_manifest(project_dir: &Path, program_name: &str) -> Res } /// How to define the ``pyembed`` crate dependency. -/// -#[derive( Debug, Clone, PartialEq,Serialize)] pub enum PyembedLocation { /// Use a specific version, installed from the crate registry. /// diff --git a/pyoxidizer/src/starlark/file_resource.rs b/pyoxidizer/src/starlark/file_resource.rs index c7b51d0a7..21e2a1aba 100644 --- a/pyoxidizer/src/starlark/file_resource.rs +++ b/pyoxidizer/src/starlark/file_resource.rs @@ -45,10 +45,6 @@ pub fn file_manifest_add_python_executable( cwd: PathBuf, ) -> Result<()> { const LABEL: &str = "FileManifest.add_python_executable()"; - println!("running file_manifest_add_python_executable "); - - println!("manifest run_path is {}", manifest.run_path.get_or_insert(PathBuf::new()).display()); - println!("manifest paths is {}", manifest.paths().unwrap()); let build = build_python_executable_local(env, &exe.name(), exe, target, opt_level, release,cwd) .context("building Python executable")?; diff --git a/pyoxidizer/src/starlark/python_executable.rs b/pyoxidizer/src/starlark/python_executable.rs index 52545ddf3..8e2dfcc3f 100644 --- a/pyoxidizer/src/starlark/python_executable.rs +++ b/pyoxidizer/src/starlark/python_executable.rs @@ -82,13 +82,8 @@ pub fn build_internal( target: &str, context: &PyOxidizerEnvironmentContext, ) -> Result<(ResolvedTarget, PathBuf)> { - // Build an executable by writing out a temporary Rust project - // and building it. - println!("running build_internal "); - - println!("cwd is {}",context.cwd.display()); - println!("config_path is {}",context.config_path.display()); - + // Build an executable if can't find local toml file + // will writing out a temporary Rust project and building it. let build = build_python_executable_local( context.env(), &exe.name(),