Skip to content

Commit

Permalink
Dynamically compile rust modules instead of committing binaries in repo
Browse files Browse the repository at this point in the history
  • Loading branch information
lanedirt committed Jan 8, 2025
1 parent 7d2bab6 commit 692bbc9
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ yarn-error.log

# Rust compilation dirs
rust/*/target
# Compiled rust apps (rust modules are recompiled during container start)
storage/ffi-libs/*.so
3 changes: 3 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ elif [[ "$role" == "app" ]]; then
php artisan cache:clear && php artisan config:cache && php artisan route:cache && php artisan view:cache
fi

# Compile rust modules
./rust/test_ffi/compile.sh

exec php-fpm
else
echo "Could not match the container role \"$role\""
Expand Down
13 changes: 5 additions & 8 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ take up a lot of memory and the BattleEngine in PHP can e.g. take up gigabytes o
The rust source code is stored in the `./rust/` folder.

## Compile rust version
After making change to any rust modules, you should manually compile them and copy it over to
the `storage/ffi-libs` folder.
The Rust modules are automatically compiled during container startup. However, during development
or testing, you may want to manually compile the modules to test your changes immediately.

To compile rust, go to the rust module directory, e.g.
To compile rust, go to the rust module directory inside the container, e.g.

```bash
# Navigate to the rust package you want to compile
Expand All @@ -28,9 +28,6 @@ cp target/release/libtest_ffi.so ../../storage/ffi-libs
Alternatively, you can also run the `compile.sh` script inside the module's folder which executes the statements above:

```bash
# Navigate to the rust package you want to compile
cd rust/test_ffi

# Execute compile.sh
./compile.sh
# Execute compile.sh (from app root)
./rust/test_ffi/compile.sh
```
4 changes: 2 additions & 2 deletions rust/test_ffi/compile.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Compile the rust package
cargo build --release
cargo build --manifest-path="rust/test_ffi/Cargo.toml" --release

# Copy the compiled rust package to the ffi-libs storage directory.
# The .so files are called by Laravel.
cp target/release/libtest_ffi.so ../../storage/ffi-libs
cp rust/test_ffi/target/release/libtest_ffi.so storage/ffi-libs
1 change: 1 addition & 0 deletions storage/ffi-libs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rust modules will be compiled during container startup and will be placed here automatically.
Binary file removed storage/ffi-libs/libtest_ffi.so
Binary file not shown.

0 comments on commit 692bbc9

Please sign in to comment.