Skip to content

Commit

Permalink
fix wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
pmp-p committed Nov 20, 2024
1 parent 44be097 commit 61ed962
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 43 deletions.
1 change: 1 addition & 0 deletions .github/workflows/wasisdk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions python-wasi-sdk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ do
fi
done

read

if [ -d ${SDKROOT} ]
then
echo "Assuming destination $SDKROOT is ready"
Expand Down
4 changes: 2 additions & 2 deletions scripts/cpython-build-wasisdk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
148 changes: 109 additions & 39 deletions wasisdk/bin/wasi
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 61ed962

Please sign in to comment.