Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: make nodejs a vcpkg port #1249

Closed
Closed
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
14 changes: 14 additions & 0 deletions overlay_ports/node-addon-api/portfile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO nodejs/node-addon-api
REF v5.0.0
SHA512 5ee14bc325ce66c6f645c3b638efaf69a7dfc90f0d6514f8568dedf34d5b5460b80c4438c4c12264d80c827edf46a82f0ecceb53b2a370ed5ec2ee640fa1ef7f
)

file(COPY ${SOURCE_PATH}/napi.h DESTINATION ${CURRENT_PACKAGES_DIR}/include)
file(COPY ${SOURCE_PATH}/napi-inl.h DESTINATION ${CURRENT_PACKAGES_DIR}/include)
file(COPY ${SOURCE_PATH}/napi-inl.deprecated.h DESTINATION ${CURRENT_PACKAGES_DIR}/include)

file(INSTALL ${SOURCE_PATH}/LICENSE.md DESTINATION ${CURRENT_PACKAGES_DIR}/share/${PORT} RENAME copyright)

file(COPY ${CMAKE_CURRENT_LIST_DIR}/unofficial-node-addon-api-config.cmake DESTINATION ${CURRENT_PACKAGES_DIR}/share/${PORT})
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
find_package(unofficial-node-api CONFIG REQUIRED)

add_library(unofficial::node-addon-api::node-addon-api INTERFACE)
target_link_libraries(unofficial::node-addon-api::node-addon-api INTERFACE unofficial::node-api::node-api)

find_path(node-addon-api_INCLUDE_DIR
NAMES napi.h
PATHS "${CMAKE_CURRENT_LIST_DIR}/../../include/node-addon-api"
NO_DEFAULT_PATH
REQUIRED)
set_target_properties(unofficial::node-addon-api::node-addon-api PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${node-addon-api_INCLUDE_DIR}"
)
19 changes: 19 additions & 0 deletions overlay_ports/node-addon-api/vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "node-addon-api",
"version-semver": "5.0.0",
"description": "Module for using Node-API from C++",
"homepage": "https://github.com/nodejs/node-addon-api",
"license": "MIT",
"supports": "((windows & !arm) | linux | osx) & !uwp",
"dependencies": [
"node-api",
{
"name": "vcpkg-cmake",
"host": true
},
{
"name": "vcpkg-cmake-config",
"host": true
}
]
}
80 changes: 80 additions & 0 deletions overlay_ports/node-api/portfile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# different to the PR version, but only for the VERSION variable handling
set(VERSION 18.12.1)

set(SHA512 0)

if(VCPKG_TARGET_IS_WINDOWS)
set(SHA512 ee66d0c03d2e48046a42616abf7639a3983e7db24c04d8643b9141cb9209a50643e31873c5a4918853a4344e822d653480558510a4db9a2ab481396891d79917)
set(DIST_URL "https://nodejs.org/dist/v${VERSION}/node-v${VERSION}-headers.tar.gz")
if(VCPKG_TARGET_ARCHITECTURE STREQUAL "x64")
vcpkg_download_distfile(
out_win_lib
URLS "https://nodejs.org/dist/v${VERSION}/win-x64/node.lib"
FILENAME "node.lib"
SHA512 95c4b053bf88f758b6124b4a576719901545485613767f1ab996bb019ea7bb0d303c511b357f830e5a14d463dd74c6b412f126103f21d12e31ca00c7de86d853
)
elseif(VCPKG_TARGET_ARCHITECTURE STREQUAL "x86")
vcpkg_download_distfile(
out_win_lib
URLS "https://nodejs.org/dist/v${VERSION}/win-x86/node.lib"
FILENAME "node.lib"
SHA512 0baa54a7870088a3290f817f6362446d304e8710ee36f99075925d110bce5c1eac377aa5c4ed6cf30161f98f39032d848eeb8d459add57b1c6458b8c91c72073
)
endif()
elseif(VCPKG_TARGET_IS_OSX)
if(VCPKG_TARGET_ARCHITECTURE STREQUAL "arm64")
set(SHA512 8eb1713afdce23b0e8408d81aa47b3ea0955de328105d666a57efef8955b286c707c107377cff36164d8455558829ab65567b9cbe5997085afc841d95128fcd5)
set(DIST_URL "https://nodejs.org/dist/v${VERSION}/node-v${VERSION}-darwin-arm64.tar.gz")
elseif(VCPKG_TARGET_ARCHITECTURE STREQUAL "x64")
set(SHA512 ffe7e878fd5e424b0ff0d2e7db5e9c02f283792df2f1a748bd6381226701bcd9f93ae48f88d295412afb10d1c924ca068f70aba9857236c8893a2b812eacf248)
set(DIST_URL "https://nodejs.org/dist/v${VERSION}/node-v${VERSION}-darwin-x64.tar.gz")
endif()
elseif(VCPKG_TARGET_IS_LINUX)
if(VCPKG_TARGET_ARCHITECTURE STREQUAL "x64")
set(SHA512 f137a0dba52ded9f2f6b304c5f41fd2c75ba069ee31cfb89811b14254552c0d5ba10890f7001e64e8a6fee277302cb0ba915e0a417c047577384ac495c4ff447)
set(DIST_URL "https://nodejs.org/dist/v${VERSION}/node-v${VERSION}-linux-x64.tar.gz")
elseif(VCPKG_TARGET_ARCHITECTURE STREQUAL "arm64")
set(SHA512 a0b2f522b0ecdc4b642f3104fd2eb08b8dfa6dc2b116b5a331722b8c6d96b2b6d5df0e691ef2b56e0463e1f30d37c98c686c5d306e1aa8cd927b306c4eef0770)
set(DIST_URL "https://nodejs.org/dist/v${VERSION}/node-v${VERSION}-linux-arm64.tar.gz")
endif()
endif()

get_filename_component(DIST_FILENAME "${DIST_URL}" NAME)

if(out_win_lib)
# nodejs requires the same node.lib to be used for both debug and release builds
file(COPY "${out_win_lib}" DESTINATION "${CURRENT_PACKAGES_DIR}/lib")
file(COPY "${out_win_lib}" DESTINATION "${CURRENT_PACKAGES_DIR}/debug/lib")
endif()

# download dist
vcpkg_download_distfile(
out_dist
URLS "${DIST_URL}"
FILENAME "${DIST_FILENAME}"
SHA512 "${SHA512}"
)

# extract dist
vcpkg_extract_source_archive(
OUT_SOURCE_PATH
ARCHIVE "${out_dist}"
)

# copy headers
set(suffix "include/node")
set(source_path "${OUT_SOURCE_PATH}/${suffix}")
file(COPY "${source_path}" DESTINATION "${CURRENT_PACKAGES_DIR}/include" FILES_MATCHING PATTERN "*.h")

# we do not take the license from the dist file because for windows it is not included as we download the headers only
set(license_url "https://raw.githubusercontent.com/nodejs/node/v18.12.1/LICENSE")
vcpkg_download_distfile(
out_license
URLS "${license_url}"
FILENAME "LICENSE"
SHA512 2d79b49a12178a078cf1246ef7589d127189914403cd6f4dfe277ced2b3ef441a6e6ee131f1c75f996d1c1528b7e1ae332e83c1dc44580b2b51a933ed0c50c48
)
file(INSTALL "${out_license}" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}" RENAME copyright)

# copy ./unofficial-node-api-config.cmake to ${CURRENT_PACKAGES_DIR}/share/node-api
file(COPY "${CMAKE_CURRENT_LIST_DIR}/unofficial-node-api-config.cmake" DESTINATION "${CURRENT_PACKAGES_DIR}/share/unofficial-${PORT}")
23 changes: 23 additions & 0 deletions overlay_ports/node-api/unofficial-node-api-config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
if(WIN32)
add_library(unofficial::node-api::node-api UNKNOWN IMPORTED)
else()
add_library(unofficial::node-api::node-api INTERFACE IMPORTED)
endif()

find_path(node-api_INCLUDE_DIR
NAMES node.h
PATHS "${CMAKE_CURRENT_LIST_DIR}/../../include/node"
NO_DEFAULT_PATH
REQUIRED)
set_target_properties(unofficial::node-api::node-api PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${node-api_INCLUDE_DIR}"
)

if(WIN32)
find_library(node-api_LIBRARY_RELEASE NAMES node PATHS "${CMAKE_CURRENT_LIST_DIR}/../../lib" NO_DEFAULT_PATH REQUIRED)
find_library(node-api_LIBRARY_DEBUG NAMES node PATHS "${CMAKE_CURRENT_LIST_DIR}/../../debug/lib" NO_DEFAULT_PATH REQUIRED)
set_target_properties(unofficial::node-api::node-api PROPERTIES
IMPORTED_LOCATION_DEBUG "${node-api_LIBRARY_DEBUG}"
IMPORTED_LOCATION_RELEASE "${node-api_LIBRARY_RELEASE}"
)
endif()
18 changes: 18 additions & 0 deletions overlay_ports/node-api/vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "node-api",
"version-semver": "18.12.1",
"description": "NodeJS API for writing modules in C++",
"homepage": "https://nodejs.org/api/addons.html",
"license": null,
"supports": "((windows & !arm) | linux | osx) & !uwp",
"dependencies": [
{
"name": "vcpkg-cmake",
"host": true
},
{
"name": "vcpkg-cmake-config",
"host": true
}
]
}
38 changes: 4 additions & 34 deletions skymp5-server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,10 @@ add_subdirectory(cpp)
# Add TypeScript part of the server
add_subdirectory(ts)

# Add "cpp/addon" in a few steps:

# "cmake-js" is tool from JS world allowing us to fetch NodeJS headers/libs/sources
# We need them to our build native NodeJS addon

# cmake-js doesn't allow to simply include something into existing CMake-based build system
# it expects top-level CMakeLists.txt. We can't meet this requirement.
# We want to generate project files with `cmake ..`, not `yarn configure` or something
# But we still want to build native NodeJS addon

# Solution:
# 1. Give cmake-js a special CMakeLists.txt from the `cmake-js-fetch` directory.
# Look into package.json and `cmake-js-fetch/CMakeLists.txt` to understand mechanism
yarn_execute_command(
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
COMMAND cmake-js-fetch
)

# 2. `cmake-js-fetch/CMakeLists.txt` creates `node.cmake` while generating

# 3. Simply include `node.cmake`.
include(${CMAKE_CURRENT_LIST_DIR}/cmake-js-fetch-build/node.cmake)

# 4. Include `node-addon-api`, a wrapper over NAPI.
execute_process(COMMAND node -p "require('node-addon-api').include"
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
OUTPUT_VARIABLE NODE_ADDON_API_DIR
)
string(REPLACE "\n" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
string(REPLACE "\"" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
target_include_directories(node INTERFACE ${NODE_ADDON_API_DIR})

# Great. Now we have CMake target called "node".
# We can link our target against it.
# Create target called "node" to be linked into addon
add_library(node INTERFACE)
find_package(unofficial-node-addon-api CONFIG REQUIRED)
target_link_libraries(node INTERFACE unofficial::node-addon-api::node-addon-api)

file(GLOB_RECURSE sources "${SKYMP5_SERVER_SOURCE_DIR}/cpp/addon/*")
if(MSVC)
Expand Down
21 changes: 0 additions & 21 deletions skymp5-server/cmake-js-fetch/CMakeLists.txt

This file was deleted.

4 changes: 1 addition & 3 deletions skymp5-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
"description": "",
"license": "AGPL-3.0",
"scripts": {
"build-ts": "parcel build ts/index.ts --bundle-node-modules --out-dir ../build/dist/server/dist_back --out-file skymp5-server.js --no-cache --target node",
"cmake-js-fetch": "cmake-js configure --directory cmake-js-fetch --out cmake-js-fetch-build"
"build-ts": "parcel build ts/index.ts --bundle-node-modules --out-dir ../build/dist/server/dist_back --out-file skymp5-server.js --no-cache --target node"
},
"author": "Leonid Pospelov <[email protected]>",
"dependencies": {
"argparse": "^2.0.1",
"axios": "^0.26.1",
"chokidar": "^3.5.3",
"cmake-js": "^6.3.0",
"crc-32": "^1.2.2",
"koa": "^2.13.4",
"koa-proxy": "^1.0.0-alpha.3",
Expand Down
21 changes: 0 additions & 21 deletions skymp5-server/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1732,27 +1732,6 @@ clone@^2.1.1:
resolved "https://registry.yarnpkg.com/clone/-/clone-2.1.2.tgz#1b7f4b9f591f1e8f83670401600345a02887435f"
integrity sha1-G39Ln1kfHo+DZwQBYANFoCiHQ18=

cmake-js@^6.3.0:
version "6.3.0"
resolved "https://registry.yarnpkg.com/cmake-js/-/cmake-js-6.3.0.tgz#ea9b5fd6789bf18dd02bdfec900bccc3fbe72af6"
integrity "sha1-6ptf1nib8Y3QK9/skAvMw/vnKvY= sha512-1uqTOmFt6BIqKlrX+39/aewU/JVhyZWDqwAL+6psToUwxj3yWPJiwxiZFmV0XdcoWmqGs7peZTxTbJtAcH8hxw=="
dependencies:
axios "^0.21.1"
debug "^4"
fs-extra "^5.0.0"
is-iojs "^1.0.1"
lodash "^4"
memory-stream "0"
npmlog "^1.2.0"
rc "^1.2.7"
semver "^5.0.3"
splitargs "0"
tar "^4"
unzipper "^0.8.13"
url-join "0"
which "^1.0.9"
yargs "^3.6.0"

co@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
Expand Down
1 change: 1 addition & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"simdjson",
"robin-hood-hashing",
"simpleini",
"node-addon-api",
{
"name": "directxtk",
"platform": "windows"
Expand Down