Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,7 @@ jobs:
shell: bash
run: |
git submodule update --init --recursive
mkdir -p cpython/builddir/wasi
mkdir -p cpython/builddir/build
cd cpython/builddir/build
../../configure --prefix=$(pwd)/install --enable-optimizations
make
cd ../wasi
CONFIG_SITE=../../Tools/wasm/config.site-wasm32-wasi ../../Tools/wasm/wasi-env \
../../configure -C --host=wasm32-unknown-wasi --build=$(../../config.guess) \
--with-build-python=$(pwd)/../build/python --prefix=$(pwd)/install --disable-test-modules
make
make install
cd ../../..
./build-python.sh

- name: Publish CPython
uses: actions/upload-artifact@v3
Expand Down
13 changes: 1 addition & 12 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,7 @@ jobs:
shell: bash
run: |
git submodule update --init --recursive
mkdir -p cpython/builddir/wasi
mkdir -p cpython/builddir/build
cd cpython/builddir/build
../../configure --prefix=$(pwd)/install --enable-optimizations
make
cd ../wasi
CONFIG_SITE=../../Tools/wasm/config.site-wasm32-wasi ../../Tools/wasm/wasi-env \
../../configure -C --host=wasm32-unknown-wasi --build=$(../../config.guess) \
--with-build-python=$(pwd)/../build/python --prefix=$(pwd)/install --disable-test-modules
make
make install
cd ../../..
./build-python.sh

- name: Lint
shell: bash
Expand Down
19 changes: 10 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
WASI_SDK_PATH ?= /opt/wasi-sdk
CARGO_TARGET_DIR ?= $(shell pwd)/target

target/release/spin-python: \
target/wasm32-wasi/release/spin_python_engine.wasm \
$(CARGO_TARGET_DIR)/release/spin-python: \
$(CARGO_TARGET_DIR)/wasm32-wasi/release/spin_python_engine.wasm \
crates/spin-python-cli/build.rs \
crates/spin-python-cli/src/main.rs
cd crates/spin-python-cli && \
SPIN_PYTHON_ENGINE_PATH=../../$< \
SPIN_PYTHON_ENGINE_PATH=$< \
SPIN_PYTHON_CORE_LIBRARY_PATH=$$(pwd)/../../cpython/builddir/wasi/install/lib/python3.11 \
cargo build --release $(BUILD_TARGET)

target/wasm32-wasi/release/spin_python_engine.wasm: \
$(CARGO_TARGET_DIR)/wasm32-wasi/release/spin_python_engine.wasm: \
crates/spin-python-engine/src/lib.rs \
crates/spin-python-engine/build.rs \
target/pyo3-config.txt
$(CARGO_TARGET_DIR)/pyo3-config.txt
cd crates/spin-python-engine && \
PYO3_CONFIG_FILE=$$(pwd)/../../target/pyo3-config.txt \
PYO3_CONFIG_FILE=$(CARGO_TARGET_DIR)/pyo3-config.txt \
cargo build --release --target=wasm32-wasi

target/pyo3-config.txt: crates/spin-python-engine/pyo3-config.txt
mkdir -p target
cp $< target
$(CARGO_TARGET_DIR)/pyo3-config.txt: crates/spin-python-engine/pyo3-config.txt
mkdir -p $(CARGO_TARGET_DIR)
cp $< $(CARGO_TARGET_DIR)
if which cygpath > /dev/null; then \
echo "lib_dir=$$(cygpath -w $$(pwd)/cpython/builddir/wasi)" >> $@; \
else \
Expand Down
44 changes: 17 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@

This is an experiment to build a Spin Python SDK using CPython, Wizer, and PyO3.

## Prerequisites

- [WASI SDK](https://github.com/WebAssembly/wasi-sdk) v16 or later, installed in /opt/wasi-sdk
- [CPython](https://github.com/python/cpython) build prereqs (e.g. Make, Clang, etc.)
- [Rust](https://rustup.rs/) (including `wasm32-wasi` target)
- [Spin](https://github.com/fermyon/spin)
- [pipenv](https://pypi.org/project/pipenv/) for installing Python project dependencies

## Installing the Plugin and Running Examples

Use the following command to install the `py2wasm` plugin and then build the spin app:
Use the following command to install the `py2wasm` plugin and then build the example Spin app:

```
spin plugins update
Expand All @@ -24,22 +16,20 @@ spin up

## Building and Running

First, build CPython for wasm32-wasi:
### Prerequisites

```
git submodule update --init --recursive
mkdir -p cpython/builddir/wasi
mkdir -p cpython/builddir/build
cd cpython/builddir/build
../../configure --prefix=$(pwd)/install --enable-optimizations
make
cd ../wasi
CONFIG_SITE=../../Tools/wasm/config.site-wasm32-wasi ../../Tools/wasm/wasi-env \
../../configure -C --host=wasm32-unknown-wasi --build=$(../../config.guess) \
--with-build-python=$(pwd)/../build/python --prefix=$(pwd)/install --disable-test-modules
make
make install
cd ../../..
- [WASI SDK](https://github.com/WebAssembly/wasi-sdk) v16 or later, installed in /opt/wasi-sdk
- [CPython](https://github.com/python/cpython) build prereqs (e.g. Make, Clang, etc.)
- [Rust](https://rustup.rs/) (including `wasm32-wasi` target)
- [Spin](https://github.com/fermyon/spin)
- [pipenv](https://pypi.org/project/pipenv/) for installing Python project dependencies

### Instructions

First, build CPython for wasm32-wasi.

```bash
./build-python.sh
```

Then, build the `spin-python-cli`:
Expand All @@ -52,13 +42,13 @@ Finally, build and run the example app:

```
cd examples/hello_world
../../target/release/spin-python app -o app.wasm
$CARGO_TARGET_DIR/release/spin-python app -o app.wasm
spin up
```

**Note:* `spin-python` and `py2wasm` are just different names for the same command. `spin-python` is used in the context of running the binary in a standalone context while `py2wasm` is used when the command is run via Spin. In the samples provided in the `examples` directory, the `spin build` command depends on the plugin `py2wasm` being installed. Therefore, to test the locally built `spin-python` binary, replace the build command in the `spin.toml` to invoke it using spin build.
**Note:* `spin-python` and `py2wasm` are just different names for the same command. `spin-python` is used in the context of running the binary in a standalone context while `py2wasm` is used when the command is run via Spin. In the samples provided in the `examples` directory, the `spin build` command depends on the plugin `py2wasm` being installed. Therefore, to test the locally built `spin-python` binary, replace the build command in the `spin.toml` to invoke it using `spin build`.

```
[component.build]
command = "../../target/release/spin-python app -o app.wasm"
command = "$CARGO_TARGET_DIR/release/spin-python app -o app.wasm"
```
25 changes: 25 additions & 0 deletions build-python.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

set -euo pipefail

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
EXE=python
elif [[ "$OSTYPE" == "darwin"* ]]; then
EXE=python.exe
else
echo "This script does not support $OSTYPE"
fi

mkdir -p cpython/builddir/wasi cpython/builddir/build
pushd cpython/builddir/build
../../configure --prefix=$(pwd)/install --enable-optimizations
make
popd

pushd cpython/builddir/wasi
CONFIG_SITE=../../Tools/wasm/config.site-wasm32-wasi ../../Tools/wasm/wasi-env \
../../configure -C --host=wasm32-unknown-wasi --build=$(../../config.guess) \
--with-build-python=$(pwd)/../build/$EXE --prefix=$(pwd)/install --disable-test-modules
make
make install
popd