Skip to content

Commit bcccbe3

Browse files
daeindygreg
authored andcommitted
pyoxidizer: add an example of how to compile a Rust project
Closes #467.
1 parent 071a31d commit bcccbe3

File tree

2 files changed

+59
-24
lines changed

2 files changed

+59
-24
lines changed

pyoxidizer/docs/pyoxidizer_rust_projects.rst

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,7 @@ interpreter use the ``snmalloc`` allocator.
164164
Using Cargo With Generated Rust Projects
165165
========================================
166166

167-
Rust developers will probably want to use ``cargo`` instead of ``pyoxidizer``
168-
for building auto-generated Rust projects. This is supported, but behavior can
169-
be very finicky.
170-
167+
Building from a Rust project is not turn-key like PyOxidizer is.
171168
PyOxidizer has to do some non-conventional things to get Rust projects to
172169
build in very specific ways. Commands like ``pyoxidizer build`` abstract
173170
away all of this complexity for you.
@@ -216,3 +213,41 @@ file contains some commented out settings that may need to be set for some
216213
configurations (e.g. the ``standalone_static`` Windows distributions). Please
217214
consult this file if running into build errors when not building through
218215
``pyoxidizer``.
216+
217+
An Example and Further Reference
218+
==================================
219+
220+
Starting from a project freshly created with ``pyoxidizer init-rust-project sample``,
221+
you'll first need to generate the build artifacts::
222+
223+
$ PYOXIDIZER_EXECUTABLE=$HOME/.cargo/bin/pyoxidizer \
224+
PYO3_PYTHON=$HOME/python/install/bin/python3.9 \
225+
PYOXIDIZER_CONFIG=$(pwd)/pyoxidizer.bzl \
226+
TARGET=x86_64-apple-darwin \
227+
CARGO_MANIFEST_DIR=. \
228+
OUT_DIR=target/out \
229+
PROFILE=debug \
230+
pyoxidizer run-build-script build.rs
231+
232+
That will put the artifacts in target/out.
233+
234+
Then you can run cargo to build your crate::
235+
236+
$ PYOXIDIZER_REUSE_ARTIFACTS=1 \
237+
PYOXIDIZER_ARTIFACT_DIR=$(pwd)/target/out \
238+
PYOXIDIZER_EXECUTABLE=$HOME/.cargo/bin/pyoxidizer \
239+
PYOXIDIZER_CONFIG=$(pwd)/pyoxidizer.bzl \
240+
PYO3_CONFIG_FILE=$(pwd)/target/out/pyo3-build-config-file.txt cargo \
241+
build --no-default-features --features \
242+
"build-mode-prebuilt-artifacts global-allocator-jemalloc allocator-jemalloc"
243+
244+
After building, you should find an executable in target/debug/.
245+
246+
Note that currently this does not produce any files that have been redirected to the filesystem,
247+
such as extension modules. For now you'll need to copy them in from a normal pyoxidizer run, or
248+
see https://github.com/indygreg/PyOxidizer/pull/466
249+
250+
On Windows, the paths will need updating, and the jemalloc features will need to be removed.
251+
252+
If you wish to dig further into how PyOxidizer builds projects, project_building.rs
253+
is a good place to start.

pyoxidizer/src/projectmgmt.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -356,26 +356,26 @@ pub fn init_rust_project(
356356
"A new Rust binary application has been created in {}",
357357
project_path.display()
358358
);
359-
println!();
360-
println!("This application can be built by doing the following:");
361-
println!();
362-
println!(" $ cd {}", project_path.display());
363-
println!(" $ pyoxidizer build");
364-
println!(" $ pyoxidizer run");
365-
println!();
366-
println!("The default configuration is to invoke a Python REPL. You can");
367-
println!("edit the various pyoxidizer.*.bzl config files or the main.rs ");
368-
println!("file to change behavior. The application will need to be rebuilt ");
369-
println!("for configuration changes to take effect.");
370-
println!();
371-
println!("IMPORTANT: use of `cargo` for direct project building and running");
372-
println!("is possible, but likely requires setting environment variables");
373-
println!("like PYOXIDIZER_EXE (the path to the `pyoxidizer` that the build.rs");
374-
println!("build script should use) and PYO3_PYTHON (the path to the");
375-
println!("Python interpreter executable used to configure the Rust crates that");
376-
println!("link against libpython). Search the documentation for references to");
377-
println!("these variables for troubleshooting tips. For best results, use");
378-
println!("the aforementioned `pyoxidizer` commands to build Rust projects.");
359+
print!(
360+
r#"
361+
This application can be built most easily by doing the following:
362+
363+
$ cd {project_path}
364+
$ pyoxidizer run
365+
366+
Note however that this will bypass all the Rust code in the project
367+
folder, and build the project as if you had only created a pyoxidizer.bzl
368+
file. Building from Rust is more involved, and requires multiple steps.
369+
Please see the "PyOxidizer Rust Projects" section of the manual for more
370+
information.
371+
372+
The default configuration is to invoke a Python REPL. You can
373+
edit the various pyoxidizer.*.bzl config files or the main.rs
374+
file to change behavior. The application will need to be rebuilt
375+
for configuration changes to take effect.
376+
"#,
377+
project_path = project_path.display()
378+
);
379379

380380
Ok(())
381381
}

0 commit comments

Comments
 (0)