Skip to content

Commit 7a5f621

Browse files
committed
add an example of how to compile a Rust project
1 parent 325fc81 commit 7a5f621

2 files changed

Lines changed: 63 additions & 24 deletions

File tree

pyoxidizer/docs/pyoxidizer_rust_projects.rst

Lines changed: 43 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,45 @@ 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+
```
224+
$ PYOXIDIZER_EXECUTABLE=$HOME/.cargo/bin/pyoxidizer \
225+
PYO3_PYTHON=$HOME/python/install/bin/python3.9 \
226+
PYOXIDIZER_CONFIG=$(pwd)/pyoxidizer.bzl \
227+
TARGET=x86_64-apple-darwin \
228+
CARGO_MANIFEST_DIR=. \
229+
OUT_DIR=target/out \
230+
PROFILE=debug \
231+
pyoxidizer run-build-script build.rs
232+
```
233+
234+
That will put the artifacts in target/out.
235+
236+
Then you can run cargo to build your crate:
237+
238+
```
239+
$ PYOXIDIZER_REUSE_ARTIFACTS=1 \
240+
PYOXIDIZER_ARTIFACT_DIR=$(pwd)/target/out \
241+
PYOXIDIZER_EXECUTABLE=$HOME/.cargo/bin/pyoxidizer \
242+
PYOXIDIZER_CONFIG=$(pwd)/pyoxidizer.bzl \
243+
PYO3_CONFIG_FILE=$(pwd)/target/out/pyo3-build-config-file.txt cargo \
244+
build --no-default-features --features \
245+
"build-mode-prebuilt-artifacts global-allocator-jemalloc allocator-jemalloc"
246+
```
247+
248+
After building, you should find an executable in target/debug/.
249+
250+
Note that currently this does not produce any files that have been redirected to the filesystem,
251+
such as extension modules. For now you'll need to copy them in from a normal pyoxidizer run, or
252+
see https://github.com/indygreg/PyOxidizer/pull/466
253+
254+
On Windows, the paths will need updating, and the jemalloc features will need to be removed.
255+
256+
If you wish to dig further into how PyOxidizer builds projects, project_building.rs
257+
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)