Skip to content

Commit

Permalink
add nim-wasi initial support
Browse files Browse the repository at this point in the history
  • Loading branch information
pmp-p committed Feb 9, 2024
1 parent 7f6d1dd commit 8736fbb
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 33 deletions.
39 changes: 37 additions & 2 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ else
fi
fi

# ============ emscripten ==============

# stable
# export EMFLAVOUR=3.1.34
Expand All @@ -53,8 +54,26 @@ fi
#export EMFLAVOUR=${EMFLAVOUR:latest}
export EMFLAVOUR=${EMFLAVOUR:-tot}

# ============== wasi sdk ===============

# stable
WASI_SDK=${WASI_SDK:-20}
WASI_URL=https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_SDK}/wasi-sdk-${WASI_SDK}.0-linux.tar.gz

WASISDK="${SDKROOT}/wasisdk"


# =============== Nimrod ====================

# stable
NIM_VERSION=nim-2.0.2
NIM_URL=https://nim-lang.org/download/${NIM_VERSION}-linux_x64.tar.xz


NIM_VERSION=nim-git
NIM_URL=https://github.com/nim-lang/Nim

NIMSDK=${NIMSDK:-"${SDKROOT}/nimsdk"}


# base wasm features pure is "mvp" , "bi" is bigint
Expand Down Expand Up @@ -85,7 +104,22 @@ then
echo -n
else
OLDPATH=${PATH}
PATH=/usr/local/bin:/usr/bin:/opt/bin:/bin

PATH=/usr/bin:/bin
for extrabin in /usr/local/bin /opt/bin
do
if echo $PATH|grep -q ":$extrabin"
then
continue
fi

if [ -d $extrabin ]
then
PATH=$extrabin:$PATH
fi
done


# this is python used for emsdk : ${SYS_PYTHON} -> ${EMSDK_PYTHON}
for py in 10 9 8 7
do
Expand All @@ -99,6 +133,7 @@ else
fi
done

export GITGET="git clone --recursive --no-tags --depth 1 --single-branch --branch"
export LD_LIBRARY_PATH="${HOST_PREFIX}/lib:$LD_LIBRARY_PATH"
export PATH="${HOST_PREFIX}/bin:$OLDPATH"
export PATH="${HOST_PREFIX}/bin:$PATH:$OLDPATH"
fi
185 changes: 185 additions & 0 deletions python-nim-sdk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#!/bin/bash

SDKROOT=${SDKROOT:-/opt/python-wasm-sdk}
CONFIG=$(realpath ${CONFIG:-config})
. $CONFIG

if [ -d ${NIMSDK} ]
then
echo "
will not overwrite existing working directory
NIMSDK=${NIMSDK}
"
else
mkdir ${NIMSDK}
fi


pushd ${NIMSDK}
if [ -d $NIM_VERSION/bin ]
then
echo "
will not overwrite existing Nim build found in
${NIMSDK}/$NIM_VERSION/bin
"
else
if echo $NIM_URL|grep -q nim-lang\.org
then
echo ERROR download/install release from $NIM_URL
else
$GITGET devel $NIM_URL $NIM_VERSION
pushd $NIM_VERSION
chmod +x *sh
CC=clang CXX=clang++ ./build_all.sh
popd
fi
fi

# --usenimcache

mkdir -p ${NIMSDK}/nim

cat > ${NIMSDK}/nim/config.nims <<END
echo " ==== python-nim-sdk ======"
import std/strformat
var ARCH="$(arch)"
var SDKROOT = getEnv("SDKROOT","${SDKROOT}")
--colors:on
--threads:off
echo fmt" ==== Panda3D: generic config {ARCH=} from {SDKROOT=} ===="
--cc:clang
--os:linux
--define:usemalloc
--noCppExceptions
--define:noCppExceptions
--exceptions:quirky
# gc : bohem => need libgc.so.1
--gc:orc
--define:noSignalHandler
#
--define:static
# better debug but optionnal/tweakable
--parallelBuild:1
--opt:speed
--define:debug
when defined(wasi):
echo " ===== Panda3D: wasi build ======"
# overwrite
ARCH="wasisdk"
# needed for compiler
--cc:clang
--os:linux
--define:emscripten
--define:static
# component model aka reactor
--noMain
--cpu:wasm32
switch("clibdir", fmt"{SDKROOT}/devices/{ARCH}/usr/lib/wasm32-wasi")
switch("passC", fmt"-m32 -Djmp_buf=int")
switch("passC", "-Os -g3 -mllvm -inline-threshold=1")
switch("passL","-lstdc++ ")
# more compat but in the long term workaround Panda3D instead
# wasi emulations
switch("passC", "-D_GNU_SOURCE")
switch("passC", "-D_WASI_EMULATED_MMAN -D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_GETPID")
switch("passL", "-lwasi-emulated-getpid -lwasi-emulated-mman -lwasi-emulated-signal -lwasi-emulated-process-clocks")
# don't use _start/main but _initialize instead
switch("passL", "-Wl,--export-all -mexec-model=reactor")
# better debug but optionnal/tweakable
--parallelBuild:1
--opt:none
else:
echo fmt" ===== Panda3D: native {ARCH} build ======"
switch("passL", "-lfreetype -lharfbuzz")
# -lfftw3 -lassimp")
# static
switch("passL", "-lp3tinydisplay")
# common
switch("passL", "-lc -lz")
# only for script gen which does not pass cincludes
# --passC:-I/opt/python-wasm-sdk/devices/${ARCH}/usr/include/panda3d
# --passL:-L${SDKROOT}/devices/${ARCH}/usr/lib
switch("nimcache", fmt"{SDKROOT}/nimsdk/cache.{ARCH}")
switch("clibdir", fmt"{SDKROOT}/devices/{ARCH}/usr/lib")
switch("cincludes", fmt"{SDKROOT}/devices/{ARCH}/usr/include/panda3d")
switch("out", fmt"out.{ARCH}")
END

cat > nimsdk_env.sh <<END
if [[ -z \${NIMSDK_ENV+z} ]]
then
export NIMSDK_ENV=true
export SDKROOT=$SDKROOT
. $CONFIG
. $WASISDK/wasisdk_env.sh
export XDG_CONFIG_HOME=${NIMSDK}
export NIMBLE_DIR=${NIMSDK}/pkg
export PATH=${NIMSDK}/${NIM_VERSION}/bin:$PATH
echo "
* using nimsdk from \$(realpath \${NIMSDK}/\${NIM_VERSION}/bin)
with sys python \$SYS_PYTHON and host python \$HPY
toolchain file CMAKE_TOOLCHAIN_FILE=\${CMAKE_TOOLCHAIN_FILE}
install prefix PREFIX=\${PREFIX}
Nim from $NIMSDK/$NIM_VERSION/bin
SDKROOT=$SDKROOT
NIMSDK=$NIMSDK
NIMBLE_DIR=\$NIMBLE_DIR
PATH=$PATH
clang=\$(which clang)
clang++=\$(which clang++)
"
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="[PyDK:nim] \[\e[32m\]\w \[\e[91m\]\$(parse_git_branch)\[\e[00m\]\$ "
else
echo "nimsdk: config already set !" 1>&2
fi
END

popd

39 changes: 30 additions & 9 deletions python-wasi-sdk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ if echo $0|grep -q python-wasm-sdk
then
echo " * adding emsdk to wasm-sdk"
emsdk=true
wasisdk=false
nimsdk=false
else
emsdk=false
BUILDS=3.12
echo " * adding wasi-sdk to wasm-sdk"
wasisdk=true
nimsdk=true
fi

echo " * adding wasi-sdk to wasm-sdk"
wasisdk=true

if [ -d ${SDKROOT} ]
then
Expand All @@ -40,18 +43,18 @@ ORIGIN=$(pwd)

# 3.12 3.11 3.10

BUILDS=${BUILDS:-3.11 3.12 3.13}
BUILDS=${BUILDS:-3.11 3.13 3.12}

for PYBUILD in $BUILDS
do
cd "$ORIGIN"

if echo $PYBUILD|grep -q 12$
then
wasisdk=true
else
wasisdk=false
fi
# if echo $PYBUILD|grep -q 12$
# then
# wasisdk=true
# else
# wasisdk=false
# fi

if [ -f ${SDKROOT}/dev ]
then
Expand Down Expand Up @@ -94,6 +97,19 @@ do

fi

cat > /opt/python-wasm-sdk/devices/$(arch)/usr/bin/py <<END
#!/bin/bash
export XDG_SESSION_TYPE=x11
export SDKROOT=${SDKROOT}
export PYTHONPATH=/data/git/pygbag/src:/data/git/platform_wasm:${PYTHONPATH}
export PYTHONPYCACHEPREFIX=$PYTHONPYCACHEPREFIX
export HOME=${SDKROOT}
export PATH=${SDKROOT}/devices/$(arch)/usr/bin:\$PATH
export LD_LIBRARY_PATH=${SDKROOT}/devices/$(arch)/usr/lib:${SDKROOT}/devices/$(arch)/usr/lib64:$LD_LIBRARY_PATH
${SDKROOT}/devices/$(arch)/usr/bin/python\${PYBUILD:-$PYBUILD} $@
END
chmod + /opt/python-wasm-sdk/devices/$(arch)/usr/bin/py


if $emsdk
then
Expand Down Expand Up @@ -173,6 +189,11 @@ END

fi

if $nimsdk
then
${SDKROOT}/python-nim-sdk.sh
fi

. ${SDKROOT}/scripts/pack-sdk.sh

else
Expand Down
30 changes: 15 additions & 15 deletions scripts/cpython-build-wasisdk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,10 @@ pushd ${SDKROOT}
. ${CONFIG:-config}


export PREFIX=${PREFIX:-${SDKROOT}/devices/wasisdk/usr}
#export PREFIX=${PREFIX:-${SDKROOT}/devices/wasisdk/usr}
export PYTHON_FOR_BUILD=${PYTHON_FOR_BUILD:-${HPY}}


echo "
* building cpython-wasi ${PREFIX}/bin/python${PYBUILD}.wasm
with PYTHON_FOR_BUILD=$PYTHON_FOR_BUILD
" 1>&2



if [ -f ${PYTHON_FOR_BUILD} ]
then
echo found PYTHON_FOR_BUILD=${PYTHON_FOR_BUILD}
Expand All @@ -32,8 +23,6 @@ else
popd
fi

. ${SDKROOT}/wasisdk/wasisdk_env.sh


export LD_LIBRARY_PATH=${SDKROOT}/devices/x86_64/usr/lib:$LD_LIBRARY_PATH

Expand All @@ -42,9 +31,20 @@ then

PYSRC=${SDKROOT}/src/cpython${PYBUILD}

. ${SDKROOT}/wasisdk/wasisdk_env.sh


echo "
* building python wasi from ${PYSRC}
"
* building cpython-wasi ${PREFIX}/bin/python${PYBUILD}.wasm
from ${PYSRC}
with PYTHON_FOR_BUILD=$PYTHON_FOR_BUILD
CC=$CC
CXX=$CXX
CPP=$CPP
" 1>&2


mkdir -p ${SDKROOT}/build/cpython-wasi

Expand All @@ -62,7 +62,7 @@ END
sed -i 's| -Wl,--stack-first -Wl,--initial-memory=10485760||g' $PYSRC/configure.ac
sed -i 's| -Wl,--stack-first -Wl,--initial-memory=10485760||g' $PYSRC/configure

CC=clang CXX=clang++ LDSHARED="${SDKROOT}/wasisdk/upstream/bin/wasm-ld --no-entry" CONFIG_SITE=$PYSRC/Tools/wasm/config.site-wasm32-wasisdk \
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 --with-ensurepip=no \
--prefix=${PREFIX} \
Expand Down
Loading

0 comments on commit 8736fbb

Please sign in to comment.