From 61ed9625152c4c16f2c43ccf6177f864502e8b40 Mon Sep 17 00:00:00 2001 From: pmp-p Date: Wed, 20 Nov 2024 03:56:57 +0100 Subject: [PATCH] fix wrapper --- .github/workflows/wasisdk.yml | 1 + python-wasi-sdk.sh | 2 - scripts/cpython-build-wasisdk.sh | 4 +- wasisdk/bin/wasi | 148 +++++++++++++++++++++++-------- 4 files changed, 112 insertions(+), 43 deletions(-) diff --git a/.github/workflows/wasisdk.yml b/.github/workflows/wasisdk.yml index bb2fe95..4657f81 100644 --- a/.github/workflows/wasisdk.yml +++ b/.github/workflows/wasisdk.yml @@ -20,6 +20,7 @@ jobs: env: BUILDS: 3.13 SDKROOT: /opt/python-wasm-sdk + Py_GIL_DISABLED: false steps: - uses: actions/checkout@v3.3.0 diff --git a/python-wasi-sdk.sh b/python-wasi-sdk.sh index c6dc80d..cf11379 100755 --- a/python-wasi-sdk.sh +++ b/python-wasi-sdk.sh @@ -60,8 +60,6 @@ do fi done -read - if [ -d ${SDKROOT} ] then echo "Assuming destination $SDKROOT is ready" diff --git a/scripts/cpython-build-wasisdk.sh b/scripts/cpython-build-wasisdk.sh index 35aa3e4..b77b4a0 100755 --- a/scripts/cpython-build-wasisdk.sh +++ b/scripts/cpython-build-wasisdk.sh @@ -63,8 +63,8 @@ END # sed -i 's| -Wl,--stack-first -Wl,--initial-memory=10485760| --stack-first --initial-memory=10485760|g' $PYSRC/configure LDSHARED="${SDKROOT}/wasisdk/upstream/bin/wasm-ld --no-entry" CONFIG_SITE=$PYSRC/Tools/wasm/config.site-wasm32-wasisdk \ - $PYSRC/configure -C \ - --with-c-locale-coercion --without-pymalloc --disable-ipv6 --disable-gil --with-ensurepip=no \ + $PYSRC/configure -C $GIL \ + --with-c-locale-coercion --without-pymalloc --disable-ipv6 --with-ensurepip=no \ --prefix=${PREFIX} \ --host=wasm32-unknown-wasi --with-suffix=.wasm \ --build=$($PYSRC/config.guess) \ diff --git a/wasisdk/bin/wasi b/wasisdk/bin/wasi index 54ac800..1d4255e 100755 --- a/wasisdk/bin/wasi +++ b/wasisdk/bin/wasi @@ -67,85 +67,155 @@ def dbg(*argv, **kw): kw.setdefault("file", sys.stderr) return print(*argv, **kw) + SDKROOT = Path(os.environ.get("SDKROOT", "/opt/python-wasm-sdk")) WASISDK = Path(os.environ.get("WASISDK", "/opt/python-wasm-sdk/emsdk")) -WASI_SDK_PREFIX=Path(os.environ.get("WASI_SDK_PREFIX", "/opt/python-wasm-sdk/wasisdk/upstream")) +WASI_SDK_PREFIX = Path(os.environ.get("WASI_SDK_PREFIX", "/opt/python-wasm-sdk/wasisdk/upstream")) PREFIX = Path(os.environ.get("PREFIX", "/opt/python-wasm-sdk/devices/wasisdk/usr")) -#dbg(sys.argv) +# dbg(sys.argv) exe = sys.argv.pop(0) -WASI_CFG="--sysroot=${WASI_SDK_PREFIX}/share/wasi-sysroot -iwithsysroot /include/c++/v1" -WASI_PATCH=f"-include {WASISDK}/hotfix/patch.h -isystem {WASISDK}/hotfix" -WASI_DEF= "-D_GNU_SOURCE -D_WASI_EMULATED_MMAN -D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_GETPID" +WASI_CFG = "--sysroot=${WASI_SDK_PREFIX}/share/wasi-sysroot -iwithsysroot /include/c++/v1" +WASI_PATCH = f"-include {WASISDK}/hotfix/patch.h -isystem {WASISDK}/hotfix" +WASI_DEF = "-D_GNU_SOURCE -D_WASI_EMULATED_MMAN -D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_GETPID" -WASI_CFLAGS = os.getenv("WASI_CFLAGS","") +WASI_CFLAGS = os.getenv("WASI_CFLAGS", "") if WASI_CFLAGS: - WASI_DEF=f"{WASI_DEF} {WASI_CFLAGS}" + WASI_DEF = f"{WASI_DEF} {WASI_CFLAGS}" -if exe.endswith('c++'): +if exe.endswith("c++"): mode = "++" - args= ["clang++"] + out = ["clang++"] fixargs = True -elif exe.endswith('cpp'): +elif exe.endswith("cpp"): mode = "-cpp" - args = ["clang-cpp"] - args.extend(WASI_DEF.split(' ')) - args.extend(sys.argv) + out = ["clang-cpp"] + out.extend(WASI_DEF.split(" ")) + out.extend(sys.argv) fixargs = False else: mode = "" - args = ["clang"] + out = ["clang"] fixargs = True if fixargs: + + CMAKE = False + RUSTC = False + LINKING = False + COMPILE = False + MODE = "" + + # fix rust calling + for argc, arg in enumerate(sys.argv): + + # clean up rustc way of passing out. + + if arg in ("-l", "-L", "-I"): + sys.argv[argc] += sys.argv[argc + 1] + sys.argv[argc + 1] = "" + RUSTC = True + + while "" in sys.argv: + sys.argv.remove("") + for arg in sys.argv: if arg in ("-v", "--version"): dbg(*sys.argv) - args=[exe] - args.extend(sys.argv) + out = [exe] + out.extend(sys.argv) break - if arg == '-Wl,--start-group': + if arg.startswith("CMakeFiles/") or arg.startswith("@CMakeFiles/"): + CMAKE = True + + if arg.lower() in ("-fpic", "-latomic"): + continue + + if arg == "-Wl,--start-group": continue - if arg == '-Wl,--end-group': + if arg == "-Wl,--end-group": continue - if arg == '-fno-builtin': - WASI_PATCH="" + if arg == "-pthread": + continue - args.append(arg) + if arg == "-fno-builtin": + WASI_PATCH = "" + + # rustc has an habit of "-l" "c" instead of "-lc" + if arg.startswith("-l"): + if len(arg) > 2: + LINKING = True + # prevent duplicate lib when linking + if arg in out: + continue + + # FAILSAFE + # that is for some very bad known setup.py behaviour regarding cross compiling and some old codebases. + # should not be needed .. + if arg.startswith("-I/"): + if arg.startswith("-I/usr/"): + continue + + elif arg.startswith("-L/") or arg.startswith("-l"): + if not LINKING: + out.append(f"-L{os.environ['PREFIX']}/lib") + LINKING = True + if arg.startswith("-L/usr/"): + continue + + elif arg in ("-o", "-c"): + MODE = arg + MODE_POS = argc + if arg == "-c": + COMPILE = True + elif arg == "-shared": + if not LINKING: + out.append(f"-L{os.environ['PREFIX']}/lib") + LINKING = True + + # duplicates can happen on cmake/rustc but they are expected to be here for a reason so skip them + if not (CMAKE or RUSTC): + # prevent duplicates objects/archives files on cmdline when linking + if LINKING or MODE == "-o": + if arg.endswith(".a") or arg.endswith(".o"): + if arg in out: + continue + + out.append(arg) else: if 0: - dbg("="*80) + dbg("=" * 80) dbg(*sys.argv) - dbg("="*80) - dbg(*args) - dbg("_"*80) + dbg("=" * 80) + dbg(*out) + dbg("_" * 80) - WASI_CFG="--sysroot=${WASI_SDK_PREFIX}/share/wasi-sysroot -iwithsysroot /include/c++/v1" + WASI_CFG = "--sysroot=${WASI_SDK_PREFIX}/share/wasi-sysroot -iwithsysroot /include/c++/v1" if WASI_PATCH: - args.extend(WASI_PATCH.split(' ')) - args.extend("-lwasi-emulated-getpid -lwasi-emulated-mman -lwasi-emulated-signal -lwasi-emulated-process-clocks".split(' ')) - args.extend(WASI_DEF.split(' ')) - - args.extend( "-fPIC -fno-rtti -fno-exceptions -z stack-size=131072".split(' ') ) - args.extend("-Wno-unknown-pragmas -Wno-unused-but-set-variable -Wno-unused-command-line-argument -Wno-unsupported-floating-point-opt -Wno-nullability-completeness".split(' ')) + out.extend(WASI_PATCH.split(" ")) + out.extend("-lwasi-emulated-getpid -lwasi-emulated-mman -lwasi-emulated-signal -lwasi-emulated-process-clocks".split(" ")) + out.extend(WASI_DEF.split(" ")) + + out.extend("-fPIC -fno-rtti -fno-exceptions -z stack-size=131072".split(" ")) + out.extend( + "-Wno-unknown-pragmas -Wno-unused-but-set-variable -Wno-unused-command-line-argument -Wno-unsupported-floating-point-opt -Wno-nullability-completeness".split( + " " + ) + ) # pg - args.append("-Wno-missing-prototypes") + out.append("-Wno-missing-prototypes") # hotpatch - args.append("-Wno-unused-function") - - -exe = f"{WASI_SDK_PREFIX}/bin/clang{mode}" -#dbg(exe,len(args),*args) -os.execv(exe, args) + out.append("-Wno-unused-function") +os.execv(f"{WASI_SDK_PREFIX}/bin/clang{mode}", out)