diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index 1e6c2f9..b2364fe 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -1,15 +1,14 @@ name: benchmarks -# The benchmarks (benchmarks/, build_local.sh bench) for every architecture x +# The benchmarks (benchmarks/, pixi run bench) for every architecture x # browser, main thread + worker. The results are shown on the summary page of # each job (benchmarks/report.py) and uploaded as json. CI runners are shared # and noisy: compare the rows of one run, not absolute numbers across runs. +# Manual only (Actions -> benchmarks -> Run workflow): nothing uses the +# results automatically. on: workflow_dispatch: - push: - branches: [main] - pull_request: # a new run (push / PR update) cancels the older run of the same workflow on # the same branch / PR (release tags: each tag its own group, never cancelled) @@ -17,10 +16,6 @@ concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} cancel-in-progress: true -defaults: - run: - shell: bash -l {0} - jobs: bench: name: bench ${{ matrix.wasm }} / ${{ matrix.browser }} @@ -33,31 +28,26 @@ jobs: wasm: [wasm32, wasm64] browser: [chromium, firefox, webkit] - env: - BROWSERS: ${{ matrix.browser }} - PLAYWRIGHT_INSTALL_ARGS: --with-deps - steps: - uses: actions/checkout@v4 - - name: Install micromamba and the dev env - uses: mamba-org/setup-micromamba@v2 + - uses: prefix-dev/setup-pixi@v0.11.3 with: - environment-file: environment-dev.yml - environment-name: pyjs-build - cache-environment: true - init-shell: bash + pixi-version: v0.84.2 + cache: true + + - name: Install the browser + run: pixi run playwright install --with-deps ${{ matrix.browser }} - name: Run the benchmarks (main thread + worker) - run: | - export BUILD_ENV_PREFIX="$MAMBA_ROOT_PREFIX/envs/pyjs-build" - ./build_local.sh bench ${{ matrix.wasm }} + run: pixi run bench ${{ matrix.wasm }} ${{ matrix.browser }} + # (a fresh runner: the json files of this run only) - name: Report if: always() run: | shopt -s nullglob - results=(build_root/benchmarks/${{ matrix.wasm }}/latest_*.json) + results=(build_root/benchmarks/${{ matrix.wasm }}/*.json) if [ ${#results[@]} -eq 0 ]; then echo "no benchmark results" >> "$GITHUB_STEP_SUMMARY" exit 0 diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index f2ae4ed..17d7fd7 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -15,11 +15,6 @@ concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} cancel-in-progress: true -defaults: - run: - shell: bash -l {0} - - jobs: build_pyjs_docs: @@ -30,23 +25,15 @@ jobs: steps: - uses: actions/checkout@v4 - # creates (and caches) the dev env (incl. sphinx), - # build_local.sh uses it via BUILD_ENV_PREFIX - - name: Install micromamba and the dev env - uses: mamba-org/setup-micromamba@v2 + - uses: prefix-dev/setup-pixi@v0.11.3 with: - environment-file: environment-dev.yml - environment-name: pyjs-build - cache-environment: true - init-shell: bash + pixi-version: v0.84.2 + cache: true # builds the npm package (wasm32 + wasm64, deployed next to the html for # the playground) and the sphinx docs - name: Build the docs - run: | - export BUILD_ENV_PREFIX="$MAMBA_ROOT_PREFIX/envs/pyjs-build" - export DOCS_OUT_DIR="$GITHUB_WORKSPACE/docs_build/html" - ./build_local.sh docs + run: pixi run docs docs_build/html ################################################################ # upload to github pages diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 33faf5d..9646b20 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,11 +12,6 @@ concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} cancel-in-progress: true -defaults: - run: - shell: bash -l {0} - - jobs: test-browser: @@ -32,45 +27,29 @@ jobs: wasm: [wasm32, wasm64] browser: [chromium, firefox, webkit] - env: - BROWSERS: ${{ matrix.browser }} - # install the system libraries the browser needs (via apt) - PLAYWRIGHT_INSTALL_ARGS: --with-deps - steps: - uses: actions/checkout@v4 - # creates (and caches) the dev env (incl. playwright), build_local.sh - # uses it via BUILD_ENV_PREFIX and creates the host / test envs itself - - name: Install micromamba and the dev env - uses: mamba-org/setup-micromamba@v2 + # pixi + the default (dev) environment from pixi.lock, cached; the + # emscripten environments are installed by the tasks + - uses: prefix-dev/setup-pixi@v0.11.3 with: - # one dev env for wasm32 and wasm64 (build_local.sh sets -m64) - environment-file: environment-dev.yml - environment-name: pyjs-build - cache-environment: true - init-shell: bash + pixi-version: v0.84.2 + cache: true + + # the browser + the system libraries it needs (apt) + - name: Install the browser + run: pixi run playwright install --with-deps ${{ matrix.browser }} # one build for all browsers: JSPI (pyjs.run_sync) is used when the browser # supports it (needs python with the wasm-gc call trampoline, # emscripten-forge python >= build 6) - name: Build and test (browser main thread + worker) - run: | - export BUILD_ENV_PREFIX="$MAMBA_ROOT_PREFIX/envs/pyjs-build" - ./build_local.sh test ${{ matrix.wasm }} + run: pixi run test ${{ matrix.wasm }} ${{ matrix.browser }} # the same build, JSPI switched off at runtime (like a browser without JSPI) - name: Test without JSPI (browser main thread + worker) - env: - DISABLE_JSPI: "1" - run: | - export BUILD_ENV_PREFIX="$MAMBA_ROOT_PREFIX/envs/pyjs-build" - ./build_local.sh test ${{ matrix.wasm }} + run: pixi run test ${{ matrix.wasm }} ${{ matrix.browser }} "main worker" numpy no-jspi - name: Test without numpy (browser main thread) - env: - TEST_ENV: no-numpy - MODES: main - run: | - export BUILD_ENV_PREFIX="$MAMBA_ROOT_PREFIX/envs/pyjs-build" - ./build_local.sh test ${{ matrix.wasm }} + run: pixi run test ${{ matrix.wasm }} ${{ matrix.browser }} main no-numpy diff --git a/.github/workflows/npm.yml b/.github/workflows/npm.yml index 3ea199a..5acaca5 100644 --- a/.github/workflows/npm.yml +++ b/.github/workflows/npm.yml @@ -1,6 +1,6 @@ name: npm -# The npm package (npm/, build_local.sh npm): built + tested on every push / +# The npm package (npm/, pixi run test-npm): built + tested on every push / # PR, published on release tags (e.g. `4.1.0`, must equal the version in # CMakeLists.txt) or manually (workflow_dispatch with publish=true). # @@ -28,10 +28,6 @@ concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} cancel-in-progress: true -defaults: - run: - shell: bash -l {0} - env: NPM_PACKAGE_NAME: ${{ vars.NPM_PACKAGE_NAME || '@derthorsten/pyjs' }} @@ -42,33 +38,25 @@ jobs: # a failed JSPI suspension can hang the browser instead of failing timeout-minutes: 120 - env: - BROWSERS: chromium firefox webkit - PLAYWRIGHT_INSTALL_ARGS: --with-deps - steps: - uses: actions/checkout@v4 - # one dev env (cached) for both architectures - - name: Install micromamba and the dev env - uses: mamba-org/setup-micromamba@v2 + - uses: prefix-dev/setup-pixi@v0.11.3 with: - environment-file: environment-dev.yml - environment-name: pyjs-build - cache-environment: true - init-shell: bash + pixi-version: v0.84.2 + cache: true + + - name: Install the browsers + run: pixi run playwright install --with-deps chromium firefox webkit - # wasm32 + wasm64, main thread + module worker, offline + network - name: Build and test the npm package - run: | - export BUILD_ENV_PREFIX="$MAMBA_ROOT_PREFIX/envs/pyjs-build" - ./build_local.sh npm --test + run: pixi run test-npm - name: Upload the package uses: actions/upload-artifact@v4 with: name: npm-package - path: build_root/npm/*.tgz + path: build_root/npm/tgz/*.tgz if-no-files-found: error publish: diff --git a/.gitignore b/.gitignore index f33956a..1a5f104 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,8 @@ emsdk_* docs_build/ empack_config.yaml build_root/ +# pixi environments (pixi.toml) +.pixi/ docs/_generated/ # jupyterlite build state (written by the docs build) .jupyterlite.doit.db diff --git a/CLAUDE.md b/CLAUDE.md index e35f55e..006cec3 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -9,47 +9,54 @@ Targets: Python 3.14, emscripten 6.0.8, emscripten-forge `6x` channel Packaged by the emscripten-forge recipe `recipes/recipes_wasm/pyjs` (packages `pyjs` = python module, `libpyjs-static` = lib + runtime js/wasm, `pyjs-dev` = both). -## Build / test locally: always via `build_local.sh` +## Build / test locally: always via pixi tasks (`pixi.toml`) -The CI uses the same script, so local == CI. Only micromamba is needed. +The CI runs the same tasks, so local == CI. Only pixi >= 0.84 is needed +(older ones do not know `emscripten-wasm64`; `pixi self-update`). ```bash -./build_local.sh build wasm32 # build + install pyjs -./build_local.sh test wasm32 # + browser tests (main thread + worker) -BROWSERS="chromium firefox webkit" ./build_local.sh test wasm64 -DISABLE_JSPI=1 ./build_local.sh test wasm32 # JSPI switched off at runtime -TEST_ENV=no-numpy ./build_local.sh test wasm32 -./build_local.sh bench wasm32 # benchmarks -BENCH_FILTER=async ./build_local.sh bench wasm32 -./build_local.sh docs # npm package + sphinx docs -./build_local.sh npm [--test] # npm package (wasm32 + wasm64) -./build_local.sh test wasm32 --fresh # recreate envs / build dirs first +pixi run build wasm32 # build pyjs (archs: wasm32 | wasm64 | "wasm32 wasm64") +pixi run test wasm32 # npm package of the arch + browser tests (main + worker) +pixi run test wasm64 "chromium firefox webkit" +# test [browsers] [modes] [numpy|no-numpy] [jspi|no-jspi] +pixi run test wasm32 chromium "main worker" numpy no-jspi # JSPI switched off at runtime +pixi run test wasm32 chromium main no-numpy +pixi run bench wasm32 [browsers] [modes] [filter] # benchmarks +pixi run docs [out dir] # npm package + sphinx docs +pixi run npm-package [archs] # npm package (default wasm32 + wasm64) +pixi run test-npm [browsers] # npm package + both archs, lock CLI, bundlers +pixi run clean # rm -rf build_root (envs stay) ``` -Useful env vars (full list at the top of the script): `ARCHS` (archs of -docs / npm, default both), `BROWSERS` (chromium firefox webkit; webkit = Safari engine), -`MODES` (main worker), `BENCH_FILTER`, `BUILD_ENV_PREFIX` -(use an existing dev env, never deleted; CI), `TEST_ENV` (`default` | -`no-numpy`: `TEST_PACKAGES`), -`PLAYWRIGHT_INSTALL_ARGS` (`--with-deps` on CI), `DOCS_OUT_DIR`, `DISABLE_JSPI=1`, -`EXTRA_CHANNELS` (channels searched first when creating envs). - -Everything lives in `build_root/` (gitignored, never commit): - -| dir | env file | | -|---|---|---| -| `dev_prefix` | `environment-dev.yml` | native, **one for wasm32 + wasm64**: `emscripten-core`, cmake, empack, node, playwright (+ playwright-python), sphinx | -| `host_prefix_` | `environment-host.yml` | emscripten env pyjs is built against + installed into | -| `test_env__` | none: `PYTHON_PACKAGES` + `TEST_PACKAGES` in build_local.sh | env of the vanilla test page (`` = `TEST_ENV`: `default` / `no_numpy`) | -| `npm_env_` | none: `PYTHON_PACKAGES` | `npm`: the env shipped in the package (python only) | -| `build_` | | cmake build dir | -| `pages/` | | test pages (`pyjs_test_driver build-page`), `pages/bundler_projects/` the bundler apps | -| `benchmarks//` | | benchmark logs + json | -| `docs_html/` | | built docs | +Task chain (tasks with args loop over `archs` via MiniJinja `| split`): +`envs` (`pixi install -e host- / npm-env- --platform +emscripten-`) -> `configure` (emcmake) -> `build` -> `pack` (empack +-> `build_root/npm/stage//`) -> `npm-package` (+ `npm-ci`) -> +`test-page` -> `_run-tests` (`pyjs_test_driver run` loops browsers x modes +itself and prints the summary). `test-npm` = alias of `npm-package`, +`_run-tests` per arch, `test-lock`, `test-bundlers`. + +Environments (`pixi.lock`, `.pixi/envs/`, gitignored): + +| env | | +|---|---| +| `default` (feature `dev`) | native, **one for wasm32 + wasm64**: `emscripten-core`, cmake, empack, node, playwright (+ playwright-python), sphinx | +| `host-` | emscripten env pyjs is built against (incl. `libarchive-static` for `src/extract.cpp`); nothing is installed into it | +| `npm-env-` | the python env shipped in the npm package (python, pip, ca-certificates) | + +Build output in `build_root/` (gitignored, never commit): + +| dir | | +|---|---| +| `build_` | cmake build dir | +| `npm/stage/`, `npm/package`, `npm/tgz/*.tgz` | npm package | +| `pages/` | test pages (`pyjs_test_driver build-page`), `pages/bundler_projects/` the bundler apps | +| `benchmarks//` | benchmark json | +| `docs_html/` | built docs | The tests / benchmarks mount `module/pyjs` from the source tree and take the runtime (`pyjs_runtime_browser.{js,wasm}`) from the build dir, so Python-only -changes need no rebuild (the script still runs cmake/make, which is a no-op). +changes need no rebuild (the tasks still run cmake, which is a no-op). WebGPU: `--use-port=emdawnwebgpu` is an `INTERFACE` link option of the `pyjs` target (cmake option `PYJS_WEBGPU`, default ON; emscripten downloads the port @@ -79,19 +86,28 @@ for wasm64). `tests/sync/test_memory.py` allocates beyond 2 GiB / 4 GiB and passes a pointer above that to JS (on wasm32 one object must be < 2 GiB: `Py_ssize_t` is 32 bit, so it allocates chunks). -### Gotchas in build_local.sh -- Structure (top to bottom): command line, settings, helpers, dev env - (activated once), one architecture (`use_arch`, `build_pyjs`, - `pack_runtime`, `build_vanilla_page`, `run_tests`), npm - (`build_npm_package`, `test_npm_package`), the `cmd_*` commands. `npm` / - `docs` loop over `ARCHS` in one process. Must stay readable by humans. +### Gotchas of the pixi setup +- Channels: workspace `6x, conda-forge` (emscripten builds of 6x win, strict + priority); the `dev` feature prepends `conda-forge, 6x` (native tools + from conda-forge, `emscripten-core` only exists in 6x; no osx-64 build of + it, so the dev env is osx-arm64 + linux-64 only). A local recipe build + (rattler-build output dir) goes first in the workspace channels. +- The emscripten envs cannot be installed by `pixi run` / setup-pixi + ("unsupported-platform"): only `pixi install -e --platform + emscripten-` (task `envs`, nested pixi call: pixi must be on PATH). +- Task args are positional, in the order of `args`; several words in one arg + (`"chromium firefox"`) are split by MiniJinja `| split` / argparse + `nargs="+"`. Arg names: no dashes. Multi-line `cmd`s start with `set -e` + (deno_task_shell does not stop on errors otherwise); backslash line + continuations need TOML literal strings (`'''`). - One dev env for both architectures: it has `emscripten-core`, not `emscripten_emscripten-wasm32` / `-wasm64` (those only depend on emscripten-core and, for wasm64, add `-m64` in their activation). - `use_arch` adds `-m64` to `EMCC_CFLAGS` (reaches every emcc call) for - wasm64 only. The `CMAKE_ARGS` of the activation are not used (they bake in - `PREFIX` at activation time), `build_pyjs` passes everything explicitly. - The emscripten library cache keeps `wasm32-emscripten` / + `configure` / `build` add `-m64` to `EMCC_CFLAGS` (reaches every emcc call, + e.g. the ports) for wasm64 only. The `CMAKE_ARGS` of the activation are not + used, `configure` passes everything explicitly; the host libraries are + linked from `CMAKE_INSTALL_PREFIX` (= the host env, never installed into). + The emscripten library cache (in the dev env) keeps `wasm32-emscripten` / `wasm64-emscripten` apart. - cmake + wasm64: `-m64` must be in `CMAKE_C_FLAGS` on the command line: `Emscripten.cmake` decides `CMAKE_SIZEOF_VOID_P` from `CMAKE_C_FLAGS` @@ -102,15 +118,13 @@ passes a pointer above that to JS (on wasm32 one object must be < 2 GiB: problem: 64 bit binaries, but their `*ConfigVersion.cmake` say 32 bit (and reject 64 bit projects). Not used by pyjs anymore (the docs do not build xeus-python), matters for projects building against them. Never edit env - files in place: micromamba hardlinks them to its package cache. -- Activation scripts are not `set -u` safe (wrapped in `set +u`). + files in place: they are hardlinked to the package cache. - `CMakeLists.txt` runs `$ENV{CONDA_PREFIX}/bin/python` (merge_js.py), so - `CONDA_PREFIX` must be the build env. -- empack finds its default config via `sys.prefix.startswith(CONDA_PREFIX)` (plain - string prefix): a stale `CONDA_PREFIX=.../build_prefix` breaks envs named - `.../build_prefix_wasm32`. Run things with the right env activated. -- Do not edit `build_local.sh` while it runs (bash reads scripts lazily, you get - bogus syntax errors). + `CONDA_PREFIX` must be the dev env (it is, in every task). +- empack finds its default config via `sys.prefix.startswith(CONDA_PREFIX)`. +- A build dir configured with another toolchain path (e.g. an old env) gets + its cmake cache reset on the next configure ("You have changed variables + that require your cache to be deleted"): harmless, it reconfigures. ## Tests @@ -135,10 +149,11 @@ passes a pointer above that to JS (on wasm32 one object must be < 2 GiB: sets `globalThis.pyjs`, awaits `main(options)`), sets `window.__pyjs_result`; worker mode: `worker.js`, console + result relayed via postMessage. -- Page kinds: `vanilla` (raw `pyjs_runtime_browser.js` via indirect eval + - `bootstrap_from_empack_packed_environment` with the empack-packed - test env: the classic API xeus-python uses), `npm` (the package, - `TEST_PACKAGES` via `extraPackages` / mambajs, network), `vite` / `webpack` +- Only npm-based pages (pyjs 5: the vanilla page is gone; `test ` + builds the npm package of that arch). Page kinds: `npm` (the package, + `TEST_PACKAGES` via `packages` (solved), a test-channel package via `urls` + (pinned), `archives`, `files`, `onProgress` recorded in + `globalThis.__pyjs_progress`; network), `vite` / `webpack` / `rollup` / `esbuild` / `parcel` (bundled app around `web/bundler_main.js`, runtime files via `copy-assets` + `cdnUrl`, main thread only, built with `CI=true`). @@ -230,7 +245,7 @@ like synchronous JS errors; pyodide compatible: `.name` / `.message`, e.g. CPython thread state LIFO); `can_run_sync()` is False in sync JS callbacks, while another `run_sync` waits, and for promising calls nested in running wasm (`depth`). `run_sync` leaves / re-enters the current asyncio task. -- Tests: `DISABLE_JSPI=1 ./build_local.sh test ` switches JSPI off at +- Tests: `pixi run test "main worker" numpy no-jspi` switches JSPI off at runtime (`run --disable-jspi` -> option `disable_jspi` -> `pyjs.core._disable_jspi()` in `tests/main.py`), CI runs both. A browser without the APIs at all can be simulated with playwright: `delete WebAssembly.Suspending` in an init script. @@ -241,70 +256,101 @@ like synchronous JS errors; pyodide compatible: `.name` / `.message`, e.g. Needs libpython from the emscripten-forge python recipe with the trampoline patches (0016 / 0017 + wasm64 `emscripten_trampoline_inner_wasm64.c`, build >= 6). -- Test a local recipe build: `EXTRA_CHANNELS=/output ./build_local.sh ...` - with a fresh host env (`rm -rf build_root/host_prefix_`); rattler-build - may leave a stale `repodata.json` when it overwrites a package with the same - file name, and micromamba then rejects the "wrong size" package. +- Test a local recipe build: put `/output` first in the workspace + `channels` of `pixi.toml` (re-solves `pixi.lock`; do not commit that); + rattler-build may leave a stale `repodata.json` when it overwrites a + package with the same file name, and the installer then rejects the "wrong + size" package. - With JSPI, urllib3 uses its `fetch` path (`pyodide.ffi.run_sync` polyfill); it slices `Uint8Array.to_py()` results, so `TypedArrayBuffer` supports memoryview-style indexing (`module/pyjs/convert.py`). -## npm package (`npm/`, `build_local.sh npm`) +## npm package (`npm/`, `pixi run npm-package`) - One package with both builds: `.` (= wasm32), `./wasm32`, `./wasm64`; name via `NPM_PACKAGE_NAME` (now `@derthorsten/pyjs`, later `@emscripten-forge/pyjs`), - version = `project(pyjs VERSION ...)` in CMakeLists.txt. TypeScript in + version = `project(pyjs VERSION ...)` in CMakeLists.txt (both read by + build.mjs itself, `--name` / `--version` override). TypeScript in `npm/src/` (`runtime.ts` = the init), assembled by `npm/build.mjs` (esbuild: self-contained ESM, all files incl. chunks in `dist/`; the solver mambajs + rattler, ~6 MB, is a lazy chunk). The published `package.json` is written by build.mjs (`npm/package.json` is only the build tooling). -- `./build_local.sh npm [--test]`: per arch (`ARCHS`): normal pyjs build, env from - `PYTHON_PACKAGES` (python only) packed by empack with +- `pixi run npm-package [archs]`: per arch: normal pyjs build, the env + `npm-env-` packed by empack (task `pack`) with `npm/empack_config.yaml` (drops CPython's `test/` = 75% of the python package), + `module/pyjs` as mount, `build_utils/npm_env_meta.py` adds channels + a pinned python spec (mambajs needs `channels`; the pin keeps the solver from replacing the running python) -> `build_root/npm/stage//`; - then `build_root/npm/package/` + `npm pack` -> `build_root/npm/*.tgz`. - `ARCHS=wasm32` (local only), `NPM_TEST_BUNDLERS=0`. -- Init (`runtime.ts`) = the jupyterlite-xeus path via mambajs-core: - `createModule` (`assets//pyjs_runtime_browser.mjs` = the classic - MODULARIZE js + `export default createModule`, located via - `import.meta.url`) -> `bootstrapEmpackPackedEnvironment` (untarjs into - `Module.FS` *before* python starts) -> extra packages (mambajs `install`, - lock platform `emscripten-`) -> `bootstrapPython` (= - `init_phase_1/2`) -> `loadSharedLibs` (main thread only). untarjs is - created by us with its wasm at `dist/untarjs.wasm` (mambajs-core's own - copy would look for `./unpack-*.wasm` relative to the page). The packages - `python` / `pyjs` are never replaced by `installPackages`. -- Returns the existing Module (`exec`, `eval`, `exec_eval`, - `async_exec_eval`, `to_js`, ...) + `installPackages(specs)`; the - `bootstrap_from_empack_packed_environment` / `init_phase_*` API stays as is. + then `build_root/npm/package/` + `npm pack` -> `build_root/npm/tgz/*.tgz`. + `pixi run test-npm` tests it fully (bundlers: `pixi run test-bundlers` alone). +- pyjs 5 init: the core (libpyjs + pre/post js) never fills the file system + or preloads shared libs; whoever fills it calls `Module.startPython()` (synchronous) + (`pre_js/init.js`: python version from C++ `_python_version`, error without + `/lib/pythonX.Y`, idempotent). The classic API (`bootstrap_from_empack_*`, + `init_phase_*`, `_untar`, `_install_conda_file`, untar/inflate C++, + `load_pkg.js`, `dynload.js`, `fetch.js`) is gone. The runtime target + (not libpyjs) has `src/extract.cpp`: `Module._extract_archive(bytes, dest, + {conda, meta, rename})` on libarchive (tar / zip, gzip / bzip2 / zstd, no + xz; .conda = zip of pkg-/info-*.tar.zst), own write loop (no `..` / + absolute paths / writes through the archive's symlinks; hard links -> + copies), returns `{files, sharedLibs, meta}`. Tests: `tests/sync/test_extract.py`. +- npm init (`runtime.ts`), all in parallel: `createModule` (or `wasmModule` + via `instantiateWasm`), solve (`solve.ts`: mambajs, lazy chunk, also + `empackLockToMambajsLock` / `formatChannels` / `computePackageUrl` from it: + the main bundle has no mambajs / untarjs), downloads (`fetch.ts`: byte + progress, sha256 only with `verifySha256`, max 6 parallel); package cache + (Cache Storage `pyjs-packages-v2`, by package URL): the installed layout + as uncompressed tar (`_extract_archive` option `tar`) + relocation + placeholders in a header, a warm install only untars (bypassed with + `verifySha256` + sha256); bytecode cache (`bytecode.ts`, + `pyjs-packages-v2-bytecode`, by package / env tarball URL): the `.pyc` + files of each package's `.py` files (`Module._tar_files`), restored with + the package, saved when `__pycache__` entry counts grow (3 s interval, + page hidden, `Module.saveBytecode()`). Valid because the extraction keeps + the archive mtimes (`set_mtime`) and `relocate` restores them (python + checks a `.pyc` against source mtime + size); then extraction in order + env tarballs -> packages (`install.ts`: conda layout, `site-packages/` -> + `lib/pythonX.Y/site-packages/`, `info/paths.json` text placeholders -> "", + per-package file lists, old files removed before a replacement) -> + `archives` -> `files` -> `startPython` -> `sharedlibs.ts` (dlopen promise, + libs RTLD_GLOBAL first, `.cpython-*.so` / lib-dynload RTLD_LOCAL; main + thread only; *after* python started: extension modules' static + initializers use the C API, e.g. matplotlib's ft2font crashes otherwise). + `Module.initTimings`: phase times (ms) of the init, `Module.installTimings` + of the last `installPackages` (`state.timings` points to the running one). Options: `packages` (specs), `urls` (pinned + `{url, sha256?}`), `archives`, `files`, `cache`, `verifySha256`, + `onProgress`, `wasmModule`, `env`. `python` / `pyjs` are never replaced. +- Returns the Module + `installPackages(specs | pinned)`, `unpackArchive`, + `addFiles` (serialized in a queue). Entries also export `compileRuntime()`. + CLI: `npx lock ` (`src/lock.ts`, bundled for node into + `bin/lock.mjs`) -> `{wasm32: [{url, sha256}], ...}`; the docs build pins the + playground's numpy with it (`pyjs-lock.json`). - Tests: the full test suite on npm pages (both archs x main / worker) and on the 5 bundler pages (see Tests). npm-only tests: `tests/npm/test_*.py` (async), run by `tests/main.py` only with option `npm_package` (the harness - sets it when the Module has `installPackages`; the recipe never runs them). + sets it when the Module has `installPackages`); `test_install.py`: pinned at + init / runtime, `.conda`, relocation, replacing, sha256, cache, bytecode + cache, progress, `compileRuntime`. `tests/npm/test_channel.py` installs from a self-hosted channel (static files, like GitHub Pages next to docs) that the driver generates at `/test-channel/` (`pyjs_test_driver/test_channel.py`: noarch python package - + arch package, rattler-build output layout). The rattler gateway first + + arch package, rattler-build output layout; unindexed for pinned tests: a + `.conda` package, arch package v2.0.0). The rattler gateway first probes `repodata_shards.msgpack.zst` / `repodata.json.zst` / `.bz2` (404s are fine), then reads `repodata.json`. - Channel order = priority. mambajs' `install` appends new channels *after* the lock's (the shipped env: emscripten-forge, conda-forge), so `solve` in - `runtime.ts` puts the requested channels first (`formatChannels` from + `solve.ts` puts the requested channels first (`formatChannels` from mambajs-core): a self-hosted CI build must win over a published package (`test_self_hosted_channel_has_priority`: a fake `tabulate` 0.0.1 beats conda-forge's 0.9.x). -- Archives: option `archives: [{url, path}]` (unpacked with untarjs before - python starts) / `Module.unpackArchive({url, path})` (later, then - `importlib.invalidate_caches()`); files written with absolute paths - (cwd-independent), directory entries skipped, paths outside `path` rejected. +- Archives: option `archives: [{url, path}]` (unpacked by `_extract_archive` + before python starts) / `Module.unpackArchive({url, path})` (later, then + `importlib.invalidate_caches()`); paths resolved against `path` (never the + cwd), directory entries not listed, paths outside `path` rejected. Tests: `tests/npm/test_archives.py` with archives the driver generates (`pyjs_test_driver/test_archives.py`); the npm / bundler pages pass one in `archives`. -- mambajs-core creates the dirs of relative package paths relative to the - *cwd* but writes the files below the prefix `/`: `applyLock` in - `runtime.ts` runs with cwd `/` (Python may have changed it). - CI: `.github/workflows/npm.yml` builds + tests (all browsers), publishes on release tags (`4.1.0`, must equal the package version) / manual dispatch; npm trusted publishing (OIDC, configured on npmjs.com for this repo + @@ -312,7 +358,7 @@ like synchronous JS errors; pyodide compatible: `.name` / `.message`, e.g. `cd build_root/npm/package && npm publish --access public`. - On jsDelivr import `dist/index.js` directly, not `/+esm` (re-bundling breaks the `import.meta.url` asset paths). -- Assets (`assets/untarjs.wasm`, `assets//`: runtime + packed env) are +- Assets (`assets//`: runtime + packed env) are fetched at runtime, bundlers do not copy them. Lookup (`resolveAssets` in `runtime.ts`): `assetsUrl` -> `assets/` next to the module (probe of `/empack_env_meta.json`, must parse as env meta: dev servers answer @@ -324,12 +370,10 @@ like synchronous JS errors; pyodide compatible: `.name` / `.message`, e.g. lives in the unminified `dist/load-runtime.js` (esbuild's minifier drops the `webpackIgnore` / `@vite-ignore` comments), `__filename` / `__dirname` defined as `undefined` (webpack rewrites `typeof __filename` - in the untarjs glue). -- `build_utils/npm_bundler_test.py` (part of `npm --test`, - `NPM_TEST_BUNDLERS=0` skips it): vite, webpack, rollup, esbuild, parcel, - each self-hosted (`copy-assets`) and with the CDN fallback (`cdnUrl` -> - a local route, the package is not published yet); projects in - `build_root/npm/bundler_test//` install the built `.tgz`. + in the untarjs glue bundled by mambajs-core, lazy solver chunk). +- `pixi run test-bundlers` (part of `test-npm`): pages built with vite, + webpack, rollup, esbuild, parcel around the packed package + (`build_root/npm/tgz/`), projects in `build_root/pages/bundler_projects/`. ## Benchmarks (`benchmarks/`) @@ -337,7 +381,7 @@ pytest-benchmark cannot await in the browser, so `benchmarks/harness.py` is a small async-aware replacement (`benchmark(fn, *args)`, `await benchmark.aio(coro_fn, *args)`, `ops=` for loops inside JS). `benchmarks/main.py` discovers `bench_*` functions in `bench_*.py` and prints a -`PYJS_BENCH_RESULT_JSON:` line which build_local.sh extracts (files written in +`PYJS_BENCH_RESULT_JSON:` line which the driver extracts (files written in the browser FS do not reach the host). Compare runs via the stored json files; pure-JS benchmarks (`raw_js_*`) show the run-to-run noise. @@ -354,7 +398,7 @@ pure-JS benchmarks (`raw_js_*`) show the run-to-run noise. `docs/conf.py` (version from CMakeLists.txt). - Playground: `docs/_extra/playground/index.html` (monokai, Monaco from cdnjs, `workerMain.js` without `.min`), loads the npm package built by - `build_local.sh docs` and deployed as `/pyjs/`. Python helpers + `pixi run docs` and deployed as `/pyjs/`. Python helpers injected by the page: `install(specs, verbose)`, `stopped()`, `on(event, handler)` (canvas listeners, removed on run / example switch). @@ -363,16 +407,19 @@ pure-JS benchmarks (`raw_js_*`) show the run-to-run noise. - All workflows: `concurrency` per workflow + PR number / ref with `cancel-in-progress`: a new push / PR update cancels the older run (each release tag is its own group). +- All use `prefix-dev/setup-pixi` (pixi version pinned, `pixi.lock`, default + env cached; the emscripten envs come from the task `envs`) and + `pixi run playwright install --with-deps ` first. - `.github/workflows/main.yml`: matrix wasm32/wasm64 x chromium/firefox/webkit, - dev env (`environment-dev.yml`) from `setup-micromamba` (cached) -> - `BUILD_ENV_PREFIX` -> - `./build_local.sh test `, again with `DISABLE_JSPI=1`, plus a - `TEST_ENV=no-numpy` run. Jobs have a timeout (a failed suspension hangs). -- `.github/workflows/bench.yml`: `./build_local.sh bench ` for wasm32 / - wasm64 x chromium / firefox / webkit (main thread + worker), on pushes to - `main`, PRs and manually; `benchmarks/report.py` writes the results as + `pixi run test `, again with JSPI off, plus a no-numpy + run (main thread). Jobs have a timeout (a failed suspension hangs). +- `.github/workflows/npm.yml`: `pixi run test-npm`, uploads + `build_root/npm/tgz/*.tgz`, publishes it on release tags. +- `.github/workflows/bench.yml`: `pixi run bench ` for wasm32 / + wasm64 x chromium / firefox / webkit (main thread + worker), manual only + (workflow_dispatch); `benchmarks/report.py` writes the results as markdown tables to the job summary, the json files are artifacts. -- `.github/workflows/docs.yml`: `./build_local.sh docs`, +- `.github/workflows/docs.yml`: `pixi run docs docs_build/html`, deploys `docs_build/html` to GitHub Pages from `main`. diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c76179..8f0bdfa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 4.0) -project(pyjs VERSION 4.4.1 DESCRIPTION "pyjs") +project(pyjs VERSION 5.0.0 DESCRIPTION "pyjs") # cmake module directory @@ -53,13 +53,9 @@ set(PREJS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/pre_js/shortcuts.js ${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/pre_js/operators.js ${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/pre_js/platform.js - ${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/pre_js/wait_for_dependencies.js ${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/pre_js/init.js ${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/pre_js/cleanup.js ${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/pre_js/make_proxy.js - ${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/pre_js/dynload/dynload.js - ${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/pre_js/fetch.js - ${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/pre_js/load_pkg.js ${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/pre_js/literal_map.js ${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/pre_js/canvas.js ) @@ -97,9 +93,6 @@ set(PYJS_HEADERS include/pyjs/export_js_proxy.hpp include/pyjs/export_py_object.hpp include/pyjs/export_pyjs_module.hpp - include/pyjs/untar.hpp - include/pyjs/install_conda_file.hpp - include/pyjs/inflate.hpp ${CMAKE_CURRENT_BINARY_DIR}/pyjs_pre.js ${CMAKE_CURRENT_BINARY_DIR}/pyjs_post.js ) @@ -114,9 +107,6 @@ add_library(pyjs STATIC src/export_py_object.cpp src/convert.cpp src/export_pyjs_module.cpp - src/inflate.cpp - src/untar.cpp - src/install_conda_file.cpp ) if(WASM64) target_compile_options(pyjs PUBLIC -m64) @@ -189,7 +179,9 @@ find_package(pybind11 CONFIG) -add_executable(pyjs_runtime_browser src/runtime.cpp ) +# the runtime; src/extract.cpp (libarchive) only here, not in libpyjs: +# embedders like xeus-python fill the file system themselves +add_executable(pyjs_runtime_browser src/runtime.cpp src/extract.cpp) SET(ENVIRONMENT "web,worker") target_compile_definitions(pyjs_runtime_browser PUBLIC -DPYTEST_DRIVER_WEB) @@ -212,7 +204,11 @@ target_compile_options(pyjs_runtime_browser PRIVATE -fPIC) # include the dir where pybind11 is installed, so that we can find the pybind11Config.cmake file include_directories(${pybind11_INCLUDE_DIRS}) -target_link_libraries(pyjs_runtime_browser PRIVATE ${PYTHON_LIBRARIES} pyjs) +# libarchive first: it needs bz2 / z / zstd from PYTHON_LIBRARIES +target_link_libraries(pyjs_runtime_browser PRIVATE + ${CMAKE_INSTALL_PREFIX}/lib/libarchive.a + ${PYTHON_LIBRARIES} + pyjs) diff --git a/README.md b/README.md index 88f1b4f..222f419 100644 --- a/README.md +++ b/README.md @@ -59,16 +59,16 @@ console.log(py_list.get(0)) // same as py_list[0] on the python side ## Development Building, testing, benchmarking and building the docs all go through -`build_local.sh` (only [micromamba](https://mamba.readthedocs.io) is needed), -the CI uses the same script: +[pixi](https://pixi.sh) tasks (only pixi >= 0.84 is needed), the CI runs the +same tasks: ```bash -./build_local.sh build wasm32 # build pyjs -./build_local.sh test wasm64 # build + run the tests in the browser -BROWSERS="chromium firefox webkit" ./build_local.sh test wasm32 -./build_local.sh bench wasm32 # FFI / asyncio benchmarks -./build_local.sh docs # sphinx docs + jupyterlite -./build_local.sh npm # npm package +pixi run build wasm32 # build pyjs +pixi run test wasm64 # build + run the tests in the browser +pixi run test wasm32 "chromium firefox webkit" +pixi run bench wasm32 # FFI / asyncio benchmarks +pixi run docs # sphinx docs + playground +pixi run npm-package # npm package ``` See [development](https://emscripten-forge.github.io/pyjs/development.html) diff --git a/benchmarks/main.py b/benchmarks/main.py index e81698d..4b1716d 100644 --- a/benchmarks/main.py +++ b/benchmarks/main.py @@ -1,7 +1,7 @@ """The pyjs benchmarks, run inside the browser. The test driver (`pyjs_test_driver run ... --entry benchmarks`, see -`build_local.sh bench `) writes this directory into the emscripten file +`pixi run bench `) writes this directory into the emscripten file system and awaits `main(options)`. Discovers ``bench_*`` functions (sync or async) in ``bench_*.py`` modules next to this file, runs them with the async-aware harness, prints a table and returns the results (the driver diff --git a/benchmarks/report.py b/benchmarks/report.py index 9538667..6d177af 100644 --- a/benchmarks/report.py +++ b/benchmarks/report.py @@ -1,4 +1,4 @@ -"""Benchmark results (json, see `build_local.sh bench`) as a markdown report. +"""Benchmark results (json, see `pixi run bench`) as a markdown report. python benchmarks/report.py ... [--out report.md] diff --git a/build_local.sh b/build_local.sh deleted file mode 100755 index 13f4ed1..0000000 --- a/build_local.sh +++ /dev/null @@ -1,413 +0,0 @@ -#!/bin/bash -# Build, test and benchmark pyjs, build the npm package and the docs. -# The same script runs locally and on CI. It only needs micromamba (or mamba): -# all tools come from conda environments, created on the first run. -# -# ./build_local.sh build wasm32|wasm64 build pyjs -# ./build_local.sh test wasm32|wasm64 build pyjs, run the tests in the browser -# ./build_local.sh bench wasm32|wasm64 build pyjs, run the benchmarks in the browser -# ./build_local.sh npm [--test] build the npm package (+ run the tests on it) -# ./build_local.sh docs build the npm package + the docs -# -# --fresh delete the environments and build directories first -# -# Environment variables (all optional): -# BROWSERS browsers for tests / benchmarks: chromium firefox webkit -# (default: chromium) -# MODES run in the browser main thread and / or a web worker: -# main worker (default: both) -# ARCHS architectures of `npm` / `docs` (default: wasm32 wasm64) -# TEST_ENV test packages: default | no-numpy (default: default) -# DISABLE_JSPI=1 test like in a browser without JSPI -# BENCH_FILTER only run benchmarks whose name contains this text -# NPM_TEST_BUNDLERS=0 `npm --test`: skip the apps built with bundlers -# NPM_PACKAGE_NAME default: @derthorsten/pyjs -# DOCS_OUT_DIR default: build_root/docs_html -# BUILD_ENV_PREFIX use this existing dev environment (CI) -# EXTRA_CHANNELS conda channels searched first, e.g. a local recipe build -# PLAYWRIGHT_INSTALL_ARGS e.g. --with-deps (CI) -# -# Everything is created in build_root/ (not in git): -# dev_prefix/ native tools: emscripten, cmake, node, playwright, ... -# host_prefix_/ what pyjs is built against (python, pybind11, ...) -# build_/ cmake build directory of pyjs -# test_env__/ python + test packages for the tests in the browser -# npm_env_/ python + stdlib, shipped in the npm package -# pages/ web pages the tests run on (pyjs_test_driver) -# npm/ the npm package (npm/package/, npm/*.tgz) -set -euo pipefail - -usage() { - sed -n '2,13p' "$0" | sed 's/^# \{0,1\}//' - exit 1 -} - -######################################## -# command line -######################################## -COMMAND="${1:-}" -shift || true -ARCH="" -FRESH=0 -WITH_TESTS=0 -for arg in "$@"; do - case "$arg" in - wasm32|wasm64) ARCH="$arg" ;; - --fresh) FRESH=1 ;; - --test) WITH_TESTS=1 ;; - *) usage ;; - esac -done -case "$COMMAND" in - build|test|bench) [ -n "$ARCH" ] || usage ;; - npm|docs) [ -z "$ARCH" ] || usage ;; # always all of ARCHS - *) usage ;; -esac - -######################################## -# settings -######################################## -SRC_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -BUILD_ROOT="$SRC_DIR/build_root" -BROWSERS="${BROWSERS:-chromium}" -MODES="${MODES:-main worker}" -ARCHS="${ARCHS:-wasm32 wasm64}" -TEST_ENV="${TEST_ENV:-default}" -NPM_PACKAGE_NAME="${NPM_PACKAGE_NAME:-@derthorsten/pyjs}" -DOCS_OUT_DIR="${DOCS_OUT_DIR:-$BUILD_ROOT/docs_html}" -DEV_PREFIX="${BUILD_ENV_PREFIX:-$BUILD_ROOT/dev_prefix}" -NUM_JOBS="$(sysctl -n hw.ncpu 2>/dev/null || nproc)" -CHANNELS=(https://repo.prefix.dev/emscripten-forge/6x conda-forge) - -# the packages the tests need (tests/sync/test_http.py: the http stack of -# e.g. scipy.datasets). Test pages with the raw runtime get them packed into -# the test env, npm pages install them at runtime (mambajs). -case "$TEST_ENV" in - default) TEST_PACKAGES=(numpy pytest urllib3 requests pyodide-http pooch) ;; - no-numpy) TEST_PACKAGES=(pytest) ;; # pyjs must work without (optional) numpy - *) echo "unknown TEST_ENV=$TEST_ENV (default | no-numpy)"; exit 1 ;; -esac -PYTHON_PACKAGES=(python "emscripten-abi==6.0.8") - -MAMBA_EXE="${MAMBA_EXE:-$(type -P micromamba || type -P mamba || true)}" -[ -x "$MAMBA_EXE" ] || { echo "micromamba / mamba not found (set MAMBA_EXE)"; exit 1; } - -######################################## -# helpers -######################################## -# a visible header in the (long) output -section() { - printf '========================================\n%s\n========================================\n' "$*" -} - -# conda channel arguments: EXTRA_CHANNELS first -channel_args() { - local channel - for channel in ${EXTRA_CHANNELS:-} "$@"; do - printf -- '-c\n%s\n' "$channel" - done -} - -# create_env_from_file [--platform ] -# (only if the environment does not exist yet) -create_env_from_file() { - local prefix="$1" file="$2" - shift 2 - [ -d "$prefix" ] && return 0 - echo "creating $prefix from $(basename "$file")" - # shellcheck disable=SC2046 - "$MAMBA_EXE" create --yes -p "$prefix" -f "$file" $(channel_args) "$@" -} - -# create_env_from_packages ... -# (only if the environment does not exist yet) -create_env_from_packages() { - local prefix="$1" platform="$2" - shift 2 - [ -d "$prefix" ] && return 0 - echo "creating $prefix: $*" - # shellcheck disable=SC2046 - "$MAMBA_EXE" create --yes -p "$prefix" --platform "$platform" $(channel_args "${CHANNELS[@]}") "$@" -} - -# install_browsers: the playwright browsers in BROWSERS (no-op if present) -install_browsers() { - # shellcheck disable=SC2086 - playwright install ${PLAYWRIGHT_INSTALL_ARGS:-} $BROWSERS -} - -# test_driver ...: creates and runs the test pages (pyjs_test_driver/) -test_driver() { - (cd "$SRC_DIR" && python -m pyjs_test_driver "$@") -} - -######################################## -# the dev environment: created and activated once, for both architectures -######################################## -if [ "$FRESH" = "1" ]; then - echo "removing the environments and build directories" - [ -n "${BUILD_ENV_PREFIX:-}" ] || rm -rf "$DEV_PREFIX" - rm -rf "$BUILD_ROOT"/host_prefix_* "$BUILD_ROOT"/build_wasm* "$BUILD_ROOT"/test_env_* \ - "$BUILD_ROOT"/npm_env_* "$BUILD_ROOT"/pages -fi -if [ -n "${BUILD_ENV_PREFIX:-}" ]; then - [ -d "$BUILD_ENV_PREFIX" ] || { echo "BUILD_ENV_PREFIX=$BUILD_ENV_PREFIX does not exist"; exit 1; } -else - create_env_from_file "$DEV_PREFIX" "$SRC_DIR/environment-dev.yml" -fi - -# The activation of emscripten-core sets the compiler flags. Flags of an -# earlier activation (e.g. in the calling shell, maybe with -m64) must not -# leak in. Activation scripts are not `set -u` safe. -unset CONDA_FORGE_EMSCRIPTEN_ACTIVATED EMCC_CFLAGS CFLAGS CXXFLAGS LDFLAGS 2>/dev/null || true -set +u -eval "$("$MAMBA_EXE" shell hook -s bash)" -micromamba activate "$DEV_PREFIX" -set -u -# CMakeLists.txt runs $CONDA_PREFIX/bin/python (merge_js.py), empack finds -# its config via CONDA_PREFIX -export CONDA_PREFIX="$DEV_PREFIX" -EMCC_CFLAGS_WASM32="$EMCC_CFLAGS" - -######################################## -# one architecture -######################################## -# use_arch : the paths and compiler flags of one architecture, -# creates its host environment. -# -# The dev env only has emscripten-core, whose flags are the wasm32 flags. For -# wasm64 every emcc call needs -m64 (EMCC_CFLAGS); that is all the -# emscripten_emscripten-wasm64 package would add. -use_arch() { - ARCH="$1" - PLATFORM="emscripten-$ARCH" - export PREFIX="$BUILD_ROOT/host_prefix_$ARCH" - BUILD_DIR="$BUILD_ROOT/build_$ARCH" - TEST_ENV_PREFIX="$BUILD_ROOT/test_env_${TEST_ENV//-/_}_$ARCH" - VANILLA_PAGE="$BUILD_ROOT/pages/vanilla_$ARCH" - if [ "$ARCH" = "wasm64" ]; then - export EMCC_CFLAGS="$EMCC_CFLAGS_WASM32 -m64" - else - export EMCC_CFLAGS="$EMCC_CFLAGS_WASM32" - fi - export LDFLAGS="-L$PREFIX/lib" - section "$ARCH (EMCC_CFLAGS=$EMCC_CFLAGS)" - - create_env_from_file "$PREFIX" "$SRC_DIR/environment-host.yml" --platform "$PLATFORM" - PY_VER="$(basename "$PREFIX"/include/python3.* | sed 's/^python//')" # e.g. 3.14 -} - -# build_pyjs: build pyjs for ARCH and install it into its host env -build_pyjs() { - local wasm64=FALSE cmake_m64=() - if [ "$ARCH" = "wasm64" ]; then - wasm64=TRUE - # the emscripten toolchain file decides 32 / 64 bit from CMAKE_C_FLAGS - cmake_m64=(-DCMAKE_C_FLAGS=-m64 -DCMAKE_CXX_FLAGS=-m64) - fi - mkdir -p "$BUILD_DIR" - ( - cd "$BUILD_DIR" - emcmake cmake "$SRC_DIR" \ - -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_PREFIX_PATH="$PREFIX" \ - -DCMAKE_FIND_ROOT_PATH="$PREFIX" \ - -DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \ - -DCMAKE_INSTALL_PREFIX="$PREFIX" \ - ${cmake_m64[@]+"${cmake_m64[@]}"} \ - -DWASM64="$wasm64" \ - -DBUILD_RUNTIME_BROWSER=ON \ - -DBUILD_RUNTIME_NODE=OFF \ - -DLINK_LIBLZMA=ON \ - -DPY_VERSION="$PY_VER" \ - -DPYTHON_SITE_PACKAGES="$PREFIX/lib/python$PY_VER/site-packages" \ - -DPython_INCLUDE_DIRS="$PREFIX/include/python$PY_VER" \ - -DPython_LIBRARY="$PREFIX/lib/libpython$PY_VER.a" \ - -DPython_LIBRARIES="$PREFIX/lib/libpython$PY_VER.a" \ - -DPython_EXECUTABLE="$DEV_PREFIX/bin/python$PY_VER" \ - -DPython_Interpreter_FOUND=TRUE \ - -DPython_FOUND=TRUE \ - -DPYTHON_MODULE_EXTENSION=.so \ - -DPYTHON_MODULE_EXT_SUFFIX=.so \ - -DPYTHON_MODULE_DEBUG_POSTFIX="" - emmake make -j"$NUM_JOBS" - emmake make install - ) - echo "pyjs ($ARCH): $BUILD_DIR/pyjs_runtime_browser.{js,wasm}" -} - -# pack_runtime [empack args...]: the runtime of this -# build + the env packed by empack + the pyjs python module of this repository -pack_runtime() { - local env="$1" out="$2" - shift 2 - rm -rf "$out" - mkdir -p "$out" - cp "$BUILD_DIR/pyjs_runtime_browser.js" "$BUILD_DIR/pyjs_runtime_browser.wasm" "$out/" - empack pack env --env-prefix "$env" --relocate-prefix / --outdir "$out" "$@" - empack pack dir --host-dir "$SRC_DIR/module/pyjs" \ - --mount-dir "/lib/python$PY_VER/site-packages/pyjs" \ - --outname pyjs_module.tar.gz --outdir "$out" - empack pack append --env-meta "$out/empack_env_meta.json" --tarfile "$out/pyjs_module.tar.gz" -} - -# build_vanilla_page: the test page with the raw runtime of this build and the -# test packages packed by empack (VANILLA_PAGE) -build_vanilla_page() { - create_env_from_packages "$TEST_ENV_PREFIX" "$PLATFORM" "${PYTHON_PACKAGES[@]}" "${TEST_PACKAGES[@]}" - pack_runtime "$TEST_ENV_PREFIX" "$BUILD_ROOT/pages/vanilla_assets_$ARCH" - test_driver build-page vanilla --assets "$BUILD_ROOT/pages/vanilla_assets_$ARCH" --out "$VANILLA_PAGE" -} - -# run_tests