Skip to content

Commit 09c1368

Browse files
committed
build_quickjs_win.sh
1 parent 3785300 commit 09c1368

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22

33
Runtime/csc.rsp
44
Runtime/csc.rsp.meta
5+
6+
BuildScripts~/_build_tmp/

BuildScripts~/build_quickjs_win.sh

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Build QuickJS-backed puerts.dll for Windows x64 with a custom JS stack size.
4+
#
5+
# Usage:
6+
# ./build_quickjs_win.sh [stack_size_in_bytes]
7+
#
8+
# Examples:
9+
# ./build_quickjs_win.sh # 2MB (default)
10+
# ./build_quickjs_win.sh 4194304 # 4MB
11+
# ./build_quickjs_win.sh 1048576 # 1MB
12+
#
13+
# Prerequisites:
14+
# - Node.js (v16+)
15+
# - CMake (v3.15+)
16+
# - Visual Studio 2019 or 2022 with C++ Desktop workload
17+
#
18+
# The script clones PuerTS, patches the QuickJS default max stack size,
19+
# builds for Windows x64, and copies the resulting puerts.dll into
20+
# Puerts/Plugins/x86_64/.
21+
#
22+
set -euo pipefail
23+
24+
PUERTS_TAG="Unity_v2.2.2_16kb"
25+
STACK_SIZE="${1:-2097152}"
26+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
27+
ONEJS_DIR="$(dirname "$SCRIPT_DIR")"
28+
PLUGINS_DIR="$ONEJS_DIR/Puerts/Plugins/x86_64"
29+
BUILD_DIR="$SCRIPT_DIR/_build_tmp"
30+
31+
# Add VS-bundled CMake to PATH if cmake is not already available
32+
if ! command -v cmake &>/dev/null; then
33+
VS_CMAKE=""
34+
for edition in Community Professional Enterprise BuildTools; do
35+
for year in 2022 2019; do
36+
candidate="/c/Program Files/Microsoft Visual Studio/$year/$edition/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin"
37+
if [ -f "$candidate/cmake.exe" ]; then
38+
VS_CMAKE="$candidate"
39+
break 2
40+
fi
41+
done
42+
done
43+
if [ -n "$VS_CMAKE" ]; then
44+
echo "Found CMake at: $VS_CMAKE"
45+
export PATH="$VS_CMAKE:$PATH"
46+
else
47+
echo "ERROR: CMake not found. Install CMake or Visual Studio with C++ workload." >&2
48+
exit 1
49+
fi
50+
fi
51+
52+
echo "=== Building puerts.dll (QuickJS, Windows x64) ==="
53+
echo " PuerTS tag: $PUERTS_TAG"
54+
echo " Stack size: $STACK_SIZE bytes ($(( STACK_SIZE / 1024 / 1024 ))MB)"
55+
echo ""
56+
57+
# Clean previous build
58+
if [ -d "$BUILD_DIR" ]; then
59+
echo "Removing previous build directory..."
60+
rm -rf "$BUILD_DIR"
61+
fi
62+
mkdir -p "$BUILD_DIR"
63+
64+
# Clone
65+
echo "Cloning PuerTS ($PUERTS_TAG)..."
66+
git clone --branch "$PUERTS_TAG" --depth 1 \
67+
https://github.com/Tencent/puerts.git "$BUILD_DIR/puerts"
68+
69+
# Patch stack size
70+
QJS_HEADER="$BUILD_DIR/puerts/unity/native_src/backend-quickjs/quickjs/quickjs.h"
71+
echo "Patching JS_DEFAULT_STACK_SIZE to $STACK_SIZE..."
72+
sed -i "s/#define JS_DEFAULT_STACK_SIZE (256 \* 1024)/#define JS_DEFAULT_STACK_SIZE ($STACK_SIZE)/" \
73+
"$QJS_HEADER"
74+
75+
# Verify patch applied
76+
if grep -q "JS_DEFAULT_STACK_SIZE ($STACK_SIZE)" "$QJS_HEADER"; then
77+
echo "Patch applied successfully."
78+
else
79+
echo "ERROR: Failed to patch quickjs.h" >&2
80+
exit 1
81+
fi
82+
83+
# Build
84+
echo "Building (this may take a few minutes)..."
85+
cd "$BUILD_DIR/puerts/unity/native_src"
86+
node ../cli make --platform win --arch x64 --backend quickjs --config Release --websocket 0
87+
88+
# Copy result
89+
DLL_PATH="$BUILD_DIR/puerts/unity/native_src/build_win_x64_quickjs/Release/puerts.dll"
90+
if [ ! -f "$DLL_PATH" ]; then
91+
echo "ERROR: Build succeeded but puerts.dll not found at expected path" >&2
92+
echo "Check $BUILD_DIR/puerts/unity/native_src/build_win_x64_quickjs/" >&2
93+
exit 1
94+
fi
95+
96+
echo "Copying puerts.dll to $PLUGINS_DIR/"
97+
cp "$DLL_PATH" "$PLUGINS_DIR/puerts.dll"
98+
99+
# Cleanup
100+
echo "Cleaning up build directory..."
101+
rm -rf "$BUILD_DIR"
102+
103+
echo ""
104+
echo "=== Done! ==="
105+
echo "Updated: $PLUGINS_DIR/puerts.dll"
106+
echo "QuickJS max stack size: $STACK_SIZE bytes"

0 commit comments

Comments
 (0)