From 61dc13567e23054f1ef355a1274eebf4220bfc1d Mon Sep 17 00:00:00 2001 From: Leonid Pospelov Date: Fri, 20 Sep 2024 01:42:37 +0500 Subject: [PATCH] internal: add node-embedder-api port (#2165) --- .github/actions/pr_base/action.yml | 1 + CMakeLists.txt | 9 +- .../node-embedder-api/portfile.cmake | 169 ++++++++++++++++++ overlay_ports/node-embedder-api/vcpkg.json | 20 +++ vcpkg.json | 9 + 5 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 overlay_ports/node-embedder-api/portfile.cmake create mode 100644 overlay_ports/node-embedder-api/vcpkg.json diff --git a/.github/actions/pr_base/action.yml b/.github/actions/pr_base/action.yml index d0bd2d1e4b..82e0af2975 100644 --- a/.github/actions/pr_base/action.yml +++ b/.github/actions/pr_base/action.yml @@ -157,6 +157,7 @@ runs: $cmake_command_full += "$vcpkg_triplet_arg " $cmake_command_full += "$vcpkg_root_arg " $cmake_command_full += "-DUNIT_DATA_DIR=skyrim_data_files " + $cmake_command_full += "-DBUILD_NODEJS_PLUGIN=ON " $cmake_command_full += "$prepare_nexus_archives_arg " $cmake_command_full += "-DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} " $cmake_command_full += "-DSKYRIM_SE=${{ inputs.SKYRIM_SE_FLAG }} " diff --git a/CMakeLists.txt b/CMakeLists.txt index 8478d8dbe0..caadee3b6f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,7 +55,9 @@ if(NOT "${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}/build") message(FATAL_ERROR "${msg}") endif() +# Bool options (Part 1) option(SKYRIM_SE "Legacy Skyrim SE (1.5) build" OFF) +option(BUILD_NODEJS_PLUGIN "Build a NodeJS plugin for Skyrim if ON, use a pre-built from ./client-deps otherwise" OFF) if(SKYRIM_SE) list(APPEND VCPKG_MANIFEST_FEATURES "skyrim-se") @@ -63,6 +65,10 @@ else() list(APPEND VCPKG_MANIFEST_FEATURES "skyrim-ae") endif() +if(BUILD_NODEJS_PLUGIN) + list(APPEND VCPKG_MANIFEST_FEATURES "build-nodejs") +endif() + cmake_minimum_required(VERSION 3.19) project(skymp) enable_testing() @@ -74,7 +80,7 @@ if(MSVC) endif() endif() -# Bool options +# Bool options (Part 2) option(JS_ENGINE_TRACING_ENABLED "Useful for JsEngine debugging. See JsEngine.h" OFF) option(PREPARE_NEXUS_ARCHIVES "Prepare SP and other archives during build or not" OFF) option(BUILD_UNIT_TESTS "Build unit tests (excluded from build when off - workaround for #1182)" ON) @@ -89,6 +95,7 @@ message(STATUS BUILD_UNIT_TESTS=${BUILD_UNIT_TESTS}) message(STATUS INSTALL_CLIENT_DIST=${INSTALL_CLIENT_DIST}) message(STATUS BUILD_GAMEMODE=${BUILD_GAMEMODE}) message(STATUS OFFLINE_MODE=${OFFLINE_MODE}) +message(STATUS BUILD_NODEJS_PLUGIN=${BUILD_NODEJS_PLUGIN}) if(PREPARE_NEXUS_ARCHIVES AND EMSCRIPTEN) message(FATAL_ERROR "PREPARE_NEXUS_ARCHIVES is not supported on Emscripten") diff --git a/overlay_ports/node-embedder-api/portfile.cmake b/overlay_ports/node-embedder-api/portfile.cmake new file mode 100644 index 0000000000..b740ab13d0 --- /dev/null +++ b/overlay_ports/node-embedder-api/portfile.cmake @@ -0,0 +1,169 @@ +vcpkg_from_github( + OUT_SOURCE_PATH SOURCE_PATH + REPO nodejs/node + REF "v${VERSION}" + SHA512 cb88b3576ac810ceace7d5824d5a6e1c4181a9327f1420d6eb1546a03a22f5f80bdddcbef6bc478ce4cb62dcd724b7a04eda707845a4003b96a5d6aed5463b37 + HEAD_REF main +) + +# Fixes arm64-windows host building x64-windows target +vcpkg_replace_string("${SOURCE_PATH}/configure.py" "'ARM64' : 'arm64'" "'ARM64' : 'x64'") + +vcpkg_find_acquire_program(PYTHON3) +get_filename_component(PYTHON3_EXE_PATH ${PYTHON3} DIRECTORY) +vcpkg_add_to_path(PREPEND "${PYTHON3_EXE_PATH}") + +if(VCPKG_TARGET_IS_WINDOWS) + set(nodejs_options openssl-no-asm static ${VCPKG_TARGET_ARCHITECTURE}) + + if(NOT "${VCPKG_BUILD_TYPE}" STREQUAL "release") + message(STATUS "Building nodejs Debug") + + execute_process( + COMMAND "${SOURCE_PATH}/vcbuild.bat" debug ${nodejs_options} + WORKING_DIRECTORY "${SOURCE_PATH}" + + OUTPUT_VARIABLE NODE_BUILD_SH_OUT + ERROR_VARIABLE NODE_BUILD_SH_ERR + RESULT_VARIABLE NODE_BUILD_SH_RES + ECHO_OUTPUT_VARIABLE + ECHO_ERROR_VARIABLE + ) + if(NOT NODE_BUILD_SH_RES EQUAL 0) + message(FATAL_ERROR "Failed to build nodejs Debug (code ${NODE_BUILD_SH_RES})") + endif() + endif() + + message(STATUS "Building nodejs Release") + + execute_process( + COMMAND "${SOURCE_PATH}/vcbuild.bat" release ${nodejs_options} + WORKING_DIRECTORY "${SOURCE_PATH}" + + OUTPUT_VARIABLE NODE_BUILD_SH_OUT + ERROR_VARIABLE NODE_BUILD_SH_ERR + RESULT_VARIABLE NODE_BUILD_SH_RES + ECHO_OUTPUT_VARIABLE + ECHO_ERROR_VARIABLE + ) + + if(NOT NODE_BUILD_SH_RES EQUAL 0) + message(FATAL_ERROR "Failed to build nodejs Release (code ${NODE_BUILD_SH_RES})") + endif() + + file(GLOB libs_debug "${SOURCE_PATH}/Debug/*.lib") + foreach(path ${libs_debug}) + file(MAKE_DIRECTORY "${CURRENT_PACKAGES_DIR}/debug/lib/node-embedder-api") + file(COPY "${path}" DESTINATION "${CURRENT_PACKAGES_DIR}/debug/lib/node-embedder-api") + endforeach() + + file(GLOB libs "${SOURCE_PATH}/Release/*.lib") + foreach(path ${libs}) + if(NOT "${path}" MATCHES ".*cctest\.lib|.*embedtest\.lib") + file(MAKE_DIRECTORY "${CURRENT_PACKAGES_DIR}/lib/node-embedder-api") + file(COPY "${path}" DESTINATION "${CURRENT_PACKAGES_DIR}/lib/node-embedder-api") + endif() + endforeach() +else() + find_program(MAKE make REQUIRED) + + if(NOT "${VCPKG_BUILD_TYPE}" STREQUAL "release") + message(STATUS "Configuring nodejs Debug") + + execute_process( + COMMAND "${SOURCE_PATH}/configure" "--debug" + WORKING_DIRECTORY "${SOURCE_PATH}" + + OUTPUT_VARIABLE NODE_BUILD_SH_OUT + ERROR_VARIABLE NODE_BUILD_SH_ERR + RESULT_VARIABLE NODE_BUILD_SH_RES + ECHO_OUTPUT_VARIABLE + ECHO_ERROR_VARIABLE + ) + + if(NOT NODE_BUILD_SH_RES EQUAL 0) + message(FATAL_ERROR "Failed to configure nodejs debug (code ${NODE_BUILD_SH_RES})") + endif() + + message(STATUS "Building nodejs Debug") + + vcpkg_execute_build_process( + COMMAND "${MAKE}" "-j${VCPKG_CONCURRENCY}" ${MAKE_OPTIONS} + NO_PARALLEL_COMMAND "${MAKE}" "-j1" ${MAKE_OPTIONS} + WORKING_DIRECTORY "${SOURCE_PATH}" + LOGNAME "build-${TARGET_TRIPLET}-dbg" + ) + + file(GLOB libs "${SOURCE_PATH}/Debug/*.a") + foreach(path ${libs}) + file(MAKE_DIRECTORY "${CURRENT_PACKAGES_DIR}/debug/lib/node-embedder-api") + file(COPY "${path}" DESTINATION "${CURRENT_PACKAGES_DIR}/debug/lib/node-embedder-api") + endforeach() + endif() + + message(STATUS "Configuring nodejs Release") + + execute_process( + COMMAND "${SOURCE_PATH}/configure" + WORKING_DIRECTORY "${SOURCE_PATH}" + + OUTPUT_VARIABLE NODE_BUILD_SH_OUT + ERROR_VARIABLE NODE_BUILD_SH_ERR + RESULT_VARIABLE NODE_BUILD_SH_RES + ECHO_OUTPUT_VARIABLE + ECHO_ERROR_VARIABLE + ) + + if(NOT NODE_BUILD_SH_RES EQUAL 0) + message(FATAL_ERROR "Failed to configure nodejs release (code ${NODE_BUILD_SH_RES})") + endif() + + message(STATUS "Building nodejs Release") + + vcpkg_execute_build_process( + COMMAND "${MAKE}" "-j${VCPKG_CONCURRENCY}" ${MAKE_OPTIONS} + NO_PARALLEL_COMMAND "${MAKE}" "-j1" ${MAKE_OPTIONS} + WORKING_DIRECTORY "${SOURCE_PATH}" + LOGNAME "build-${TARGET_TRIPLET}-rel" + ) + + file(GLOB libs "${SOURCE_PATH}/Release/*.a") + foreach(path ${libs}) + file(MAKE_DIRECTORY "${CURRENT_PACKAGES_DIR}/lib/node-embedder-api") + file(COPY "${path}" DESTINATION "${CURRENT_PACKAGES_DIR}/lib/node-embedder-api") + endforeach() + +endif() + +file(MAKE_DIRECTORY "${CURRENT_PACKAGES_DIR}/include/node-embedder-api") + +# main header +file(COPY "${SOURCE_PATH}/src/node.h" DESTINATION "${CURRENT_PACKAGES_DIR}/include/node-embedder-api") + +# node.h requirements +file(COPY "${SOURCE_PATH}/src/node_api.h" DESTINATION "${CURRENT_PACKAGES_DIR}/include/node-embedder-api") +file(COPY "${SOURCE_PATH}/src/node_version.h" DESTINATION "${CURRENT_PACKAGES_DIR}/include/node-embedder-api") + +file(GLOB v8_headers "${SOURCE_PATH}/src/deps/v8/include/*.h") +foreach(v8_header ${v8_headers}) + file(COPY "${v8_header}" DESTINATION "${CURRENT_PACKAGES_DIR}/include/node-embedder-api") +endforeach() + +file(GLOB v8_headers "${SOURCE_PATH}/src/deps/v8/include/cppgc/*.h") +foreach(v8_header ${v8_headers}) + file(COPY "${v8_header}" DESTINATION "${CURRENT_PACKAGES_DIR}/include/node-embedder-api/cppgc") +endforeach() + +file(GLOB v8_headers "${SOURCE_PATH}/src/deps/v8/include/libplatform/*.h") +foreach(v8_header ${v8_headers}) + file(COPY "${v8_header}" DESTINATION "${CURRENT_PACKAGES_DIR}/include/node-embedder-api/libplatform") +endforeach() + +# node_api.h requirements +file(COPY "${SOURCE_PATH}/src/js_native_api.h" DESTINATION "${CURRENT_PACKAGES_DIR}/include/node-embedder-api") +file(COPY "${SOURCE_PATH}/src/node_api_types.h" DESTINATION "${CURRENT_PACKAGES_DIR}/include/node-embedder-api") + +# js_native_api.h requirements +file(COPY "${SOURCE_PATH}/src/js_native_api_types.h" DESTINATION "${CURRENT_PACKAGES_DIR}/include/node-embedder-api") + +file(INSTALL "${SOURCE_PATH}/LICENSE" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}" RENAME copyright) diff --git a/overlay_ports/node-embedder-api/vcpkg.json b/overlay_ports/node-embedder-api/vcpkg.json new file mode 100644 index 0000000000..2581b14906 --- /dev/null +++ b/overlay_ports/node-embedder-api/vcpkg.json @@ -0,0 +1,20 @@ +{ + "name": "node-embedder-api", + "version": "22.0.0", + "description": "NodeJS API for embedding NodeJS in your C++ applications", + "homepage": "https://nodejs.org/api/embedding.html", + "license": "MIT", + "supports": "(windows & x64 & !uwp) | linux | osx", + "dependencies": [ + { + "name": "vcpkg-cmake", + "host": true, + "platform": "windows" + }, + { + "name": "vcpkg-cmake-config", + "host": true, + "platform": "windows" + } + ] +} diff --git a/vcpkg.json b/vcpkg.json index 44c8a3575b..4d8056f2ef 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -84,6 +84,15 @@ }, "skyrim-vr": { "description": "Build platform for Skyrim VR." + }, + "build-nodejs": { + "description": "", + "dependencies": [ + { + "name": "node-embedder-api", + "platform": "windows" + } + ] } } }