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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,16 @@ Alternatively, if you already have the project checked out, you can initialize t
```git
git submodule update --init
```
To build the JNI shared library, please follow these steps:

1. Install [JDK](https://docs.aws.amazon.com/corretto/latest/corretto-7-ug/downloads-list.html) and [CMake](https://cmake.org/download/).

2. Set the **JAVA_HOME** system environment variable, and add **CMake** to your **PATH**.
- For Windows users, install [mingw-w64](https://github.com/skeeto/w64devkit) and add its **bin** directory to your **PATH**.

3. Run the build script:
- **Windows:** [build-jni.bat](./build-jni.bat)
- **MacOS/Linux:** [build-jni.sh](./build-jni.sh)
## Usage

### Initialization
Expand Down
32 changes: 32 additions & 0 deletions build-jni.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@echo off
setlocal

cd wrapper-java\src\main
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: 批处理脚本中的相对 cd 和未加引号路径在某些环境下会导致失败

这些命令假定脚本从仓库根目录运行,并且目录路径中没有空格。为了提高健壮性,建议基于脚本所在路径构造路径(例如 cd /d "%~dp0wrapper-java\src\main"),并为所有路径参数加上引号。

Original comment in English

suggestion: Relative cd and unquoted paths in the batch script can cause failures in some environments

These commands assume the script is run from the repo root and that no directories contain spaces. To make it more robust, base paths on the script location (e.g. cd /d "%~dp0wrapper-java\src\main") and quote all path arguments.


set args=
set build_args=

where ninja >nul 2>nul
if errorlevel 1 (
set args=%args% -G "MinGW Makefiles"
) else (
set args=%args% -G Ninja -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++
set build_args=%build_args% -j %NUMBER_OF_PROCESSORS%
)

if exist build (
echo Deleting old build folder...
rmdir /s /q build
)

call :run cmake -B build -S . %args% -DCMAKE_BUILD_TYPE=Release || exit /b 1
call :run cmake --build build %build_args% || exit /b 1
call :run copy build\libquickjs-java-wrapper.dll ..\..\.. || exit /b 1

echo Build success
exit /b

:run
echo ^> %*
%*
exit /b %errorlevel%
37 changes: 37 additions & 0 deletions build-jni.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash
set -euo pipefail


run() {
echo "> $*"
"$@"
}

cd wrapper-java/src/main

if [ -d build ]; then
echo "Deleting old build folder..."
rm -rf build
fi

if command -v ninja >/dev/null 2>&1; then
GENERATOR_ARGS="-G Ninja"
BUILD_ARGS="-j $(nproc 2>/dev/null || sysctl -n hw.ncpu)"
else
GENERATOR_ARGS=""
BUILD_ARGS=""
fi

run cmake -B build -S . $GENERATOR_ARGS -DCMAKE_BUILD_TYPE=Release
run cmake --build build $BUILD_ARGS

OS_NAME=$(uname)
if [[ "$OS_NAME" == "Darwin" ]]; then
DYLIB_EXT="dylib"
else
DYLIB_EXT="so"
fi

run cp "build/libquickjs-java-wrapper.$DYLIB_EXT" ../../..

echo "Build success"
5 changes: 4 additions & 1 deletion native/cpp/quickjs_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ static void jsFuncCallbackFinalizer(JSRuntime *rt, JSValue val) {

static JSClassDef js_func_callback_class = {
"JSFuncCallback",
.finalizer = jsFuncCallbackFinalizer,
jsFuncCallbackFinalizer,
nullptr,
nullptr,
nullptr
};

static JSValue jsFnCallback(JSContext *ctx,
Expand Down
1 change: 1 addition & 0 deletions wrapper-java/src/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ add_library( # Sets the name of the library.


include_directories($ENV{JAVA_HOME}/include/)
include_directories($ENV{JAVA_HOME}/include/win32)
include_directories($ENV{JAVA_HOME}/include/darwin)
include_directories($ENV{JAVA_HOME}/include/linux)