diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 1f696eb..9bbf501 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -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 diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index a428b29..745c840 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -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 diff --git a/Makefile b/Makefile index ebf1b29..bcfd525 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/README.md b/README.md index 1023aa2..de45a79 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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`: @@ -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" ``` diff --git a/build-python.sh b/build-python.sh new file mode 100755 index 0000000..e17e63b --- /dev/null +++ b/build-python.sh @@ -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