Skip to content

Commit

Permalink
Merge pull request #1638 from kuzudb/nodejs-win-fix
Browse files Browse the repository at this point in the history
Node.js build for Windows
  • Loading branch information
mewim committed Jun 6, 2023
2 parents 57a97d7 + 2de465b commit 0248f05
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 12 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/windows-nodejs-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Build-Windows-NodeJS-Module

on:
workflow_dispatch:

jobs:
build-windows-nodejs:
runs-on: self-hosted-windows
steps:
- uses: actions/checkout@v3

- name: Create Node.js source distribution
working-directory: tools/nodejs_api
run: |
node clean
node package
- name: Extract tarball
working-directory: tools/nodejs_api
run: tar -xvzf kuzu-source.tar.gz

- name: Build Node.js native module
working-directory: tools/nodejs_api/package
shell: cmd
run: |
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
npm i
- name: Move Node.js native module
working-directory: tools/nodejs_api/package
run: ren kuzujs.node kuzujs-win32-x64.node

- uses: actions/upload-artifact@v3
with:
name: windows-nodejs-module
path: tools/nodejs_api/package/kuzujs-win32-x64.node
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea/
.vscode
.vs
bazel-*
.clwb
.cache
Expand Down
19 changes: 13 additions & 6 deletions tools/nodejs_api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ add_definitions(-DNAPI_VERSION=5)
add_definitions(-DNODE_RUNTIME=node)
add_definitions(-DBUILDING_NODE_EXTENSION)

# Print CMAKE_CURRENT_SOURCE_DIR
# If on Windows use npx.cmd instead of npx
if(WIN32)
set(NPX_CMD npx.cmd)
else()
set(NPX_CMD npx)
endif()

execute_process(
COMMAND npx cmake-js print-cmakejs-include
COMMAND ${NPX_CMD} cmake-js print-cmakejs-include
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE CMAKE_JS_INC
)
execute_process(
COMMAND npx cmake-js print-cmakejs-lib
COMMAND ${NPX_CMD} cmake-js print-cmakejs-lib
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE CMAKE_JS_LIB
)
execute_process(
COMMAND npx cmake-js print-cmakejs-src
COMMAND ${NPX_CMD} cmake-js print-cmakejs-src
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE CMAKE_JS_SRC
)
Expand All @@ -38,12 +44,13 @@ include_directories(${NODE_ADDON_API_INCLUDE_PATH})

file(GLOB CPP_SOURCE_FILES ./src_cpp/*)
file(GLOB JS_SOURCE_FILES ./src_js/*.js)
file(COPY ${JS_SOURCE_FILES} ${CMAKE_JS_SRC} DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}/build")
file(COPY ${JS_SOURCE_FILES} DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}/build")

add_library(${PROJECT_NAME} SHARED ${CPP_SOURCE_FILES})
add_library(${PROJECT_NAME} SHARED ${CPP_SOURCE_FILES} ${CMAKE_JS_SRC})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
set_target_properties(${PROJECT_NAME}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/build"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/build"
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/build")
if(APPLE)
Expand Down
14 changes: 14 additions & 0 deletions tools/nodejs_api/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");

const SRC_PATH = path.resolve(__dirname, "../..");
const THREADS = require("os").cpus().length;

console.log(`Using ${THREADS} threads to build Kuzu.`);

execSync("npm run clean", { stdio: "inherit" });
execSync(`make nodejs NUM_THREADS=${THREADS}`, {
cwd: SRC_PATH,
stdio: "inherit",
});
18 changes: 18 additions & 0 deletions tools/nodejs_api/clean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require("fs");
const path = require("path");

const IS_CLEAN_ALL = process.argv[2] === "all";

const BUILD_DIR = path.resolve(__dirname, "build");
fs.rmSync(BUILD_DIR, { recursive: true, force: true });
console.log("build dir removed");

if (IS_CLEAN_ALL) {
const NODE_MODULES_DIR = path.resolve(__dirname, "node_modules");
fs.rmSync(NODE_MODULES_DIR, { recursive: true, force: true });
console.log("node_modules dir removed");

const PACKAGE_LOCK_JSON = path.resolve(__dirname, "package-lock.json");
fs.rmSync(PACKAGE_LOCK_JSON, { force: true });
console.log("package-lock.json removed");
}
11 changes: 8 additions & 3 deletions tools/nodejs_api/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,16 @@ if (process.platform === "darwin") {
}
}

childProcess.execSync("make nodejs NUM_THREADS=" + THREADS, {
env,
const execArgs = {
cwd: path.join(__dirname, "kuzu-source"),
stdio: "inherit",
});
};

execArgs.env = process.platform === "win32" ? process.env : env;

console.log(env);

childProcess.execSync("make nodejs NUM_THREADS=" + THREADS, execArgs);

// Copy the built files to the package directory
const BUILT_DIR = path.join(
Expand Down
4 changes: 4 additions & 0 deletions tools/nodejs_api/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ const KUZU_VERSION_TEXT = "Kuzu VERSION";
}

packageJson.scripts.install = "node install.js";
// npm modifies environment variables during install, which causes build
// errors on Windows. This is a workaround.
// packageJson.scripts.preinstall = "npm config set ignore-scripts true";
// packageJson.scripts.postinstall = "npm config set ignore-scripts false";

await fs.writeFile(
path.join(ARCHIVE_DIR_PATH, "package.json"),
Expand Down
6 changes: 3 additions & 3 deletions tools/nodejs_api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
},
"scripts": {
"test": "mocha test",
"clean": "rm -rf build",
"clean-all": "rm -rf build node_modules package-lock.json",
"build": "npm run clean && cd ../.. && make nodejs NUM_THREADS=$(node -e \"console.log(require('os').cpus().length)\")"
"clean": "node clean.js",
"clean-all": "node clean.js all",
"build": "node build.js"
},
"author": "Kùzu Team",
"license": "MIT",
Expand Down

0 comments on commit 0248f05

Please sign in to comment.