Skip to content

Commit

Permalink
Use fixed versions and add C++ support
Browse files Browse the repository at this point in the history
  • Loading branch information
olivi-r committed Jul 27, 2023
1 parent 98e9af8 commit 4cc0440
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ python -m pip install .
```

# Usage
Pass the file with any extra arguments to the console scripts:

Simply pass the C or C++ file and any extra arguments straight to the compiler.

### C
```bash
wasmpy-build my_file.c -o my_file.wasm
```
### C++
```bash
wasmpy-build++ my_file.cpp -o my_file.wasm
```
10 changes: 9 additions & 1 deletion patch_headers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import os, shutil, subprocess
import patch

# current tags for versions
vers = {
"3.8": "v3.8.17",
"3.9": "v3.9.17",
"3.10": "v3.10.12",
"3.11": "v3.11.4",
}


for patch_file in os.listdir("patches"):
ver = os.path.splitext(patch_file)[0]
ver_tag = "cp" + "".join(ver.split("."))

subprocess.check_call(["git", "-C", "cpython", "checkout", ver])
subprocess.check_call(["git", "-C", "cpython", "checkout", vers[ver]])

# copy headers and license
shutil.copytree("cpython/Include", f"wasmpy_build/include/{ver_tag}")
Expand Down
9 changes: 7 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def recurse_files(directory):

setuptools.setup(
name="wasmpy-build",
version="0.3.3",
version="0.4.0",
author="Olivia Ryan",
author_email="[email protected]",
description="WebAssembly build tool for CPython C extensions",
Expand All @@ -25,7 +25,12 @@ def recurse_files(directory):
url="https://github.com/olivi-r/wasmpy-build",
packages=["wasmpy_build"],
package_data={"wasmpy_build": recurse_files("wasmpy_build/include")},
entry_points={"console_scripts": ["wasmpy-build=wasmpy_build:build"]},
entry_points={
"console_scripts": [
"wasmpy-build=wasmpy_build:buildc",
"wasmpy-build++=wasmpy_build:buidcpp",
]
},
classifiers=[
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
Expand Down
36 changes: 32 additions & 4 deletions wasmpy_build/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
sdk_dir = os.path.join(appdirs.user_data_dir("wasmpy-build", "wasmpy"))


def download_sdk():
def download_sdk(before=None):
try:
os.makedirs(sdk_dir)

Expand Down Expand Up @@ -48,10 +48,11 @@ def download_sdk():
)

print(f"wasi-sdk installed at: {os.path.join(sdk_dir, 'sdk')}")
build()
if before is not None:
before()


def build():
def buildc():
command = sys.argv[1:]

# find cpython include files
Expand All @@ -75,4 +76,31 @@ def build():

except FileNotFoundError:
print("wasi-sdk not found")
download_sdk()
download_sdk(buildc)


def buildcpp():
command = sys.argv[1:]

# find cpython include files
include_dir = os.path.join(os.path.dirname(__file__), "include", "cp")
version = "".join(str(i) for i in sys.version_info[:2])
include_dir += version

args = [
f"{sdk_dir}/sdk-{platform.system()}/bin/clang++",
f"--sysroot={sdk_dir}/sdk-{platform.system()}/share/wasi-sysroot",
"--target=wasm32-wasi-threads",
"-pthread",
"-nostartfiles",
f"-I{include_dir}",
"-Wl,--no-entry,-export-dynamic,--allow-undefined",
] + command

print(" ".join(args))
try:
subprocess.call(args)

except FileNotFoundError:
print("wasi-sdk not found")
download_sdk(buildcpp)
5 changes: 0 additions & 5 deletions wasmpy_build/__main__.py

This file was deleted.

0 comments on commit 4cc0440

Please sign in to comment.