Skip to content
Open
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
48 changes: 48 additions & 0 deletions .github/scripts/verify-python-extension.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash
# Import the native extension a rules-python fixture built and run its verifier.
#
# usage: verify-python-extension.sh <fixture-dir>
#
# The interpreter MUST be the one the rule built against: Ubuntu's python3 is
# too old for a cpython-313 extension. The rule records it as a
# `python.executable` fact, which is the first answer. The payload glob is a
# fallback for a cold build-program cache and names no version, so a future
# xim:python release does not require editing this script.
set -eu

repo=$(cd "$(dirname "$0")/../.." && pwd)
fixture=${1:?usage: verify-python-extension.sh <fixture-dir>}
case "$fixture" in
/*) ;;
*) fixture="$repo/$fixture" ;;
esac
cd "$fixture"

py=
if [ -f target/.build-mcpp/build.mcpp.cache ]; then
py=$(sed -n 's/^d fact python\.executable=//p' target/.build-mcpp/build.mcpp.cache \
| tr -d '\r' | head -1)
fi
if [ -z "$py" ]; then
for root in "${XLINGS_HOME:-}" "${MCPP_HOME:-}" "$HOME/.xlings" "$HOME/.mcpp"; do
[ -n "$root" ] || continue
for cand in \
"$root"/registry/data/xpkgs/xim-x-python/*/bin/python3 \
"$root"/registry/data/xpkgs/xim-x-python/*/python.exe \
"$root"/data/xpkgs/xim-x-python/*/bin/python3 \
"$root"/data/xpkgs/xim-x-python/*/python.exe; do
if [ -f "$cand" ]; then py=$cand; break 2; fi
done
done
fi
py=${py:-${MCPP_PYTHON_EXECUTABLE:-$(command -v python3 || command -v python || true)}}
test -n "$py" || { echo "FAIL: no Python interpreter to run verify.py"; exit 1; }
if command -v cygpath >/dev/null 2>&1; then
py=$(cygpath -u "$py")
fi

echo "python: $("$py" -c 'import sys; print(sys.version.split()[0], sys.executable)')"
ext=$(find target \( -name 'fast_ops.cpython-*.so' -o -name 'fast_ops.cp*.pyd' \) -type f \
| { grep -v '/\.build-mcpp/' || true; } | head -1)
test -n "$ext" || { echo "FAIL: no native extension"; find target -name 'fast_ops*' | head; exit 1; }
"$py" verify.py "$(dirname "$ext")"
268 changes: 255 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,139 @@ jobs:
rm -f neg.log restored.log cpu.log cpurun.log
echo "ok: and it builds again once the two agree"

# THE PYTHON FFI, END TO END, WITH NO PYTHON BUILD TOOLCHAIN.
#
# `rules-python`'s ctypes backend writes the `extern "C"` glue and
# `mathlib.py` while the build program plans. The criteria are all
# observable in one run -- the generated texts, the C ABI symbols in the
# shared library, the `.py` placed beside that library, the untouched C++
# consumer and the system `python3` importing the result.
- name: rules-python (ctypes) generates the glue and the wrapper
working-directory: tests/python-consumer
run: |
set -e
"$MCPP" build
d=$(sh "$GITHUB_WORKSPACE/tests/out-dir.sh")/python
test -f "$d/mathlib_pybridge.cpp"
test -f "$d/mathlib.py"
grep -q 'extern "C"' "$d/mathlib_pybridge.cpp"
grep -q 'mcpp_py_mathlib_free_string' "$d/mathlib_pybridge.cpp"
so=$(find target -name 'libmathlib.so' -type f | head -1)
test -n "$so" || { echo "FAIL: no shared library"; exit 1; }
art_dir=$(dirname "$so")
test -f "$art_dir/mathlib.py"
nm -D --defined-only "$so" | grep -q ' mcpp_py_mathlib_add$'
nm -D --defined-only "$so" | grep -q ' mcpp_py_mathlib_greet$'
nm -D --defined-only "$so" | grep -q ' mcpp_py_mathlib_free_string$'
# The source module carries no FFI trace, which is what "zero
# intrusion" means here.
if grep -rnE 'extern "C"|Python\.h|MCPP_PY_EXPORT' src/ \
| grep -vE '^[^:]+:[0-9]+:[[:space:]]*//'; then
echo "FAIL: the C++ module has FFI traces"; exit 1
fi
echo "ok: glue, wrapper, symbols and deployment"

- name: the C++ consumer imports the same module unchanged
working-directory: tests/python-consumer/cpp-app
run: |
set -e
"$MCPP" build
"$MCPP" run | tee run.log
grep -q '^cpp-app ok' run.log

- name: python3 imports the generated wrapper and every base type round-trips
working-directory: tests/python-consumer
run: |
set -e
so=$(find target -name 'libmathlib.so' -type f | head -1)
art_dir=$(dirname "$so")
PYTHONPATH="$art_dir" python3 -c '
import inspect, mathlib

assert mathlib.add(17, 25) == 42
assert mathlib.add.__doc__ == "Return the \"sum\""
assert mathlib.mul_i64(10**12, 3) == 3 * 10**12
assert abs(mathlib.hypotenuse(3.0, 4.0) - 5.0) < 1e-12
assert mathlib.is_even(42) is True
assert mathlib.is_even(41) is False
assert mathlib.toggle(True) is False
assert mathlib.toggle(False) is True

assert mathlib.greet("mcpp") == "Hello, mcpp!"
assert mathlib.greet("世界🚀") == "Hello, 世界🚀!"
assert mathlib.greet("") == "Hello, !"
assert mathlib.join("a", "b") == "ab"
assert mathlib.noop() is None
# A C++ exception at the extern "C" boundary returns the zero sentinel.
assert mathlib.checked_div(10, 2) == 5
assert mathlib.checked_div(1, 0) == 0

for i in range(1000):
assert mathlib.greet(f"user-{i}") == f"Hello, user-{i}!"

sig = inspect.signature(mathlib.greet)
assert sig.parameters["name"].annotation in ("str", str)
assert sig.return_annotation in ("str", str)
print("python-consumer ok")
'

# THE PRECEDENCE CONTRACT, WITH BOTH SIDES PRESENT.
#
# The shared environment names one headers pair and the build program
# states a different one. `rules-python` must take the explicit pair,
# and a failing stated interpreter must not fall back to the shared one.
# `MCPP_NO_AUTO_INSTALL=1` proves the rule requested no payload: it reads
# `xim:python` only when the graph installed it.
- name: rules-python takes a stated SDK over the shared environment, without a payload request
working-directory: tests/all-rules-compile
run: |
set -e
env_dir="$RUNNER_TEMP/python-env/include"
stated_dir="$RUNNER_TEMP/python-stated/include"
mkdir -p "$env_dir" "$stated_dir"
: > "$env_dir/Python.h"
: > "$stated_dir/Python.h"
rm -rf target
MCPP_NO_AUTO_INSTALL=1 \
MCPP_TEST_PYTHON_PRECEDENCE=1 \
MCPP_TEST_PYTHON_INCLUDE_DIR="$stated_dir" \
MCPP_PYTHON_INCLUDE_DIR="$env_dir" \
MCPP_PYTHON_EXT_SUFFIX=.cpython-313-x86_64-linux-gnu.so \
"$MCPP" build --configure-only
echo "ok: the stated pair won over the shared environment"

# And a failing MCPP_PYTHON_EXECUTABLE is strict: it is refused, not
# replaced by xim:python, uv or PATH.
rm -rf target
MCPP_NO_AUTO_INSTALL=1 \
MCPP_TEST_PYTHON_STRICT_ENV=1 \
MCPP_PYTHON_EXECUTABLE=/definitely/not/a/python \
"$MCPP" build --configure-only
echo "ok: a stated environment interpreter is strict"

# THE NANOBIND BACKEND, AGAINST THE PUBLISHED INDEX.
#
# `mcpplibs.mwrap.nanobind` 3.1.0 is in mcpplibs/mcpp-index, so this
# fixture resolves it from the real index, like every other consumer.
# The interpreter is NOT the host's python3: Ubuntu 24.04 ships 3.12,
# and a `cpython-313` extension cannot be imported by it. The rule
# records the interpreter it built against as `python.executable`, so
# that fact is the first answer, and the other paths are fallbacks for a
# cold or partial build-program cache.
- name: rules-python (nanobind explicit_list) builds and imports a native extension
working-directory: tests/python-nanobind-consumer
run: |
set -e
"$MCPP" build
bash "$GITHUB_WORKSPACE/.github/scripts/verify-python-extension.sh" tests/python-nanobind-consumer

- name: rules-python (nanobind reflect) binds the exported namespace on GCC 16
working-directory: tests/python-reflect-consumer
run: |
set -e
"$MCPP" build
bash "$GITHUB_WORKSPACE/.github/scripts/verify-python-extension.sh" tests/python-reflect-consumer

# NO BUILD PROGRAM AT ALL, WHICH IS WHAT THE TWO KEYS BUY.
#
# `spirv-consumer` and `spirv-module-consumer` both write a `build.mcpp`,
Expand Down Expand Up @@ -1004,24 +1137,38 @@ jobs:
# EVERY RULE PASSES A DEPFILE, AND THE DENOMINATOR IS THE RULE COUNT.
#
# The step above proves the channel works for the one lane CI can run.
# The other four compile for accelerators no runner has, so what can be
# The other rules compile for accelerators no runner has, so what can be
# asserted about them is that each one declares the field -- read from the
# code rather than from a list written here, so a seventh rule is counted
# the day it is added and not the day someone remembers this step.
- name: every rule declares a depfile
# code rather than from a list written here, so a new rule is counted the
# day it is added and not the day someone remembers this step.
#
# `rules-python` is the exception: it drives no compiler of its own. Its
# generated translation unit is compiled by the engine, whose compile edge
# already carries the scanner's depfile, and its only action is the
# artifact copy after the link. A rule that adds a compiler action must
# still declare `depfile`; this member claims no source extension and
# therefore has none to declare.
- name: every rule that compiles a translation unit declares a depfile
run: |
set -u
cd "$GITHUB_WORKSPACE"
rules=$(ls rules/*.cppm | wc -l)
withdep=$(grep -l 'depfile' rules/*.cppm | wc -l)
rules=0
withdep=0
missing=0
for f in rules/*.cppm; do
# rules-python compiles no translation unit of its own: its only
# action is the artifact copy after the engine's link.
[ "${f##*/}" = "python.cppm" ] && continue
rules=$((rules + 1))
if grep -q 'depfile' "$f"; then
withdep=$((withdep + 1))
else
echo "FAIL: $f passes no depfile"
missing=1
fi
done
echo "rules with a depfile: $withdep of $rules"
if [ "$withdep" -ne "$rules" ]; then
echo "FAIL: these rules pass no depfile:"
for f in rules/*.cppm; do
grep -q 'depfile' "$f" || echo " $f"
done
exit 1
fi
[ "$missing" -eq 0 ] || exit 1

# SLANG: A DIFFERENT LANGUAGE, THE SAME SURFACE.
#
Expand Down Expand Up @@ -2235,6 +2382,23 @@ jobs:
"$MCPP" run | tee run.log
grep -q '^all-rules-compile ok' run.log

# THE NATIVE EXTENSION ON THE TWO HOSTS THE LINUX JOB DOES NOT COVER.
#
# The fixture pins `xim:python` 3.13.12, so the extension is
# `fast_ops.cpython-313-*.so` on macOS and `fast_ops.cp313-*.pyd` on
# Windows. Both are located the same way; `stage` is the directory the
# engine placed the linked library in, which is also where the fixture's
# dependent library sits. The interpreter comes from the rule's
# `python.executable` fact first, because it is the one the extension was
# built against; the payload paths are the fallback, never the host's
# `python3` when that is a different minor version.
- name: rules-python (nanobind explicit_list) builds and imports a native extension on this host
working-directory: tests/python-nanobind-consumer
run: |
set -e
"$MCPP" build
bash "$GITHUB_WORKSPACE/.github/scripts/verify-python-extension.sh" tests/python-nanobind-consumer

- name: rules-spirv through a consumer
working-directory: tests/spirv-consumer
run: |
Expand Down Expand Up @@ -2279,6 +2443,84 @@ jobs:
fi
echo "ok: the boundary is generated and namespaced on this host"

# THE PYTHON FFI, ON THE TWO HOSTS WHERE ITS PLATFORM BRANCHES RUN.
#
# The `consumers` job closes the Linux half with `nm -D` and
# `libmathlib.so` for `rules-python`'s ctypes backend. What only this
# matrix can answer is the rest of the generated wrapper: `__declspec(dllexport)` and the `.dll` candidate
# list, `os.add_dll_directory` and the handle it must keep alive, the
# 16-byte `_McppBytes` returned by value under the MSVC ABI, and macOS's
# `.dylib` spelling. The fixture is ordinary C++23 and needs no payload.
#
# `sys.path` is seeded from the argument rather than `PYTHONPATH`: Git
# Bash translates a POSIX `PYTHONPATH` entry before Python sees it, and
# the imported module's own directory is the only one that matters. The
# UTF-8 sample is written as escapes so a non-UTF-8 console locale
# cannot corrupt the source of this step.
- name: rules-python (ctypes) generates the glue, shared library and wrapper on this host
working-directory: tests/python-consumer
run: |
set -e
"$MCPP" build
d=$(sh "$GITHUB_WORKSPACE/tests/out-dir.sh")/python
test -f "$d/mathlib_pybridge.cpp"
test -f "$d/mathlib.py"
if [ "$RUNNER_OS" = "macOS" ]; then
lib=$(find target -name 'libmathlib.dylib' -type f | head -1)
elif [ "$RUNNER_OS" = "Windows" ]; then
lib=$(find target \( -name 'mathlib.dll' -o -name 'libmathlib.dll' \) -type f | head -1)
else
lib=$(find target -name 'libmathlib.so' -type f | head -1)
fi
test -n "$lib" || { echo "FAIL: no shared library"; exit 1; }
art_dir=$(dirname "$lib")
test -f "$art_dir/mathlib.py"
py=$(command -v python3 || command -v python)
"$py" - "$art_dir" <<'PY'
import inspect
import sys
sys.path.insert(0, sys.argv[1])
import mathlib

assert mathlib.add(17, 25) == 42
assert mathlib.add.__doc__ == "Return the \"sum\""
assert mathlib.mul_i64(10**12, 3) == 3 * 10**12
assert abs(mathlib.hypotenuse(3.0, 4.0) - 5.0) < 1e-12
assert mathlib.is_even(42) is True
assert mathlib.is_even(41) is False
assert mathlib.toggle(True) is False
assert mathlib.toggle(False) is True

assert mathlib.greet("mcpp") == "Hello, mcpp!"
assert mathlib.greet("\u4e16\u754c\U0001f680") == "Hello, \u4e16\u754c\U0001f680!"
assert mathlib.greet("") == "Hello, !"
assert mathlib.join("a", "b") == "ab"
assert mathlib.join("a\x00b", "c") == "a\x00bc"
assert mathlib.noop() is None
# A C++ exception at the extern "C" boundary returns the zero sentinel.
assert mathlib.checked_div(10, 2) == 5
assert mathlib.checked_div(1, 0) == 0

if sys.platform == "win32":
assert len(mathlib._DLL_DIR_HANDLES) >= 1

for i in range(1000):
assert mathlib.greet(f"user-{i}") == f"Hello, user-{i}!"

sig = inspect.signature(mathlib.greet)
assert sig.parameters["name"].annotation in ("str", str)
assert sig.return_annotation in ("str", str)
print("python-consumer ok")
PY

- name: the C++ consumer imports the same module unchanged on this host
working-directory: tests/python-consumer/cpp-app
run: |
set -e
"$MCPP" build
"$MCPP" run | tee run.log
grep -q '^cpp-app ok' run.log

# ── dist, on the platform each plugin serves ────────────────────────
#
# A PLAN-LEVEL ASSERTION IS NOT A MEASUREMENT. `plan_for()` returning
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ runs comes from is in [docs/02](docs/02-tool-sources.md).
| `dist-apk` | Packs the native closure into a signed APK or App Bundle, with Java, Kotlin and Maven libraries. | [docs](docs/32-dist-apk.md) |

<details>
<summary><b>All 23 plugins</b> — feature, behaviour, doc; the modules and engine floors are in the <a href="docs/README.md#the-plugins">documentation index</a></summary>
<summary><b>All 24 plugins</b> — feature, behaviour, doc; the modules and engine floors are in the <a href="docs/README.md#the-plugins">documentation index</a></summary>

| feature | what it does | doc |
|---|---|---|
Expand All @@ -73,6 +73,7 @@ runs comes from is in [docs/02](docs/02-tool-sources.md).
| `rules-metal` | Compiles `.metal` shaders into Metal libraries with the host's Xcode and deploys them beside the program. | [docs](docs/10-rules.md#rules-metal) |
| `rules-qt` | Runs `moc`, `uic`, `rcc` and Qt's Linguist tools as actions, links the Qt modules and places their runtime. | [docs](docs/11-rules-qt.md) |
| `rules-qml` | Builds a QML application: each module's `moc`, `qmltyperegistrar`, `qmlcachegen`, `rcc` and `qsb` chain as actions, Qt's QML modules the program imports placed beside it, and a `qt.conf`. Implies `rules-qt`. | [docs](docs/12-rules-qml.md) |
| `rules-python` | Binds a C++23 module's surface to Python as a native CPython extension with nanobind, or as `extern "C"` glue and a `ctypes` wrapper with no Python build dependency. | [docs](docs/13-rules-python.md) |
| `rules-slang` | Compiles Slang (`*.slang`) and embeds or places the result. | [docs](docs/10-rules.md#rules-slang) |
| `rules-spirv` | Compiles GLSL and HLSL shader stages to SPIR-V and embeds or places the result. | [docs](docs/10-rules.md#rules-spirv) |
| `rules-swift` | Compiles a package's `.swift` sources into one module the C and C++ sources call. | [docs](docs/10-rules.md#rules-swift) |
Expand Down
3 changes: 2 additions & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ int main() {
| `dist-apk` | 把原生闭包打成已签名的 APK 或 App Bundle,带 Java、Kotlin 与 Maven 库。 | [docs](docs/32-dist-apk.md) |

<details>
<summary><b>全部 23 个插件成员</b>——feature、行为、文档;模块与引擎下限见<a href="docs/README.md#the-plugins">文档索引</a></summary>
<summary><b>全部 24 个插件成员</b>——feature、行为、文档;模块与引擎下限见<a href="docs/README.md#the-plugins">文档索引</a></summary>

| feature | 行为 | 文档 |
|---|---|---|
Expand All @@ -70,6 +70,7 @@ int main() {
| `rules-metal` | 用宿主的 Xcode 把 `.metal` 着色器编成 Metal 库,并部署在程序旁。 | [docs](docs/10-rules.md#rules-metal) |
| `rules-qt` | 以 action 运行 `moc`、`uic`、`rcc` 与 Qt 的翻译工具,链接 Qt 模块并放置其运行时。 | [docs](docs/11-rules-qt.md) |
| `rules-qml` | 构建 QML 应用:各模块的 `moc`、`qmltyperegistrar`、`qmlcachegen`、`rcc`、`qsb` 链为 action,程序导入的 Qt QML 模块放在程序旁,并写 `qt.conf`。隐含 `rules-qt`。 | [docs](docs/12-rules-qml.md) |
| `rules-python` | 把 C++23 模块的表面绑定到 Python:经 nanobind 产出原生 CPython 扩展,或以 `extern "C"` 胶水加 `ctypes` 包装,构建期无需 Python 工具链。 | [docs](docs/13-rules-python.md) |
| `rules-slang` | 编译 Slang(`*.slang`),结果可内嵌或放置。 | [docs](docs/10-rules.md#rules-slang) |
| `rules-spirv` | 把 GLSL 与 HLSL 着色器阶段编译为 SPIR-V,结果可内嵌或放置。 | [docs](docs/10-rules.md#rules-spirv) |
| `rules-swift` | 把包的 `.swift` 源码编成一个模块,供 C 与 C++ 源码调用。 | [docs](docs/10-rules.md#rules-swift) |
Expand Down
Loading
Loading