Skip to content

Commit

Permalink
wip: windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Jan 23, 2024
1 parent 55ea434 commit 38ba6c3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 63 deletions.
34 changes: 27 additions & 7 deletions Lib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if (NOT DEFINED CMAKE_JS_VERSIONS OR "${CMAKE_JS_VERSIONS}" STREQUAL "")
message(FATAL_ERROR "Failed to find cmake-js and nodejs versions!")
endif()
string(REGEX MATCH "CMAKEJS_VERSION ([0-9a-zA-Z\.]+)" _ ${CMAKE_JS_VERSIONS})
set(CMAKEJS_VERSION ${CMAKE_MATCH_1})
set(CMAKE_JS_VERSION ${CMAKE_MATCH_1})
string(REGEX MATCH "NODE_RUNTIME ([0-9a-zA-Z\.]+)" _ ${CMAKE_JS_VERSIONS})
set(NODE_RUNTIME ${CMAKE_MATCH_1})
string(REGEX MATCH "NODE_RUNTIMEVERSION ([0-9a-zA-Z\.]+)" _ ${CMAKE_JS_VERSIONS})
Expand Down Expand Up @@ -98,12 +98,32 @@ FUNCTION (cmake_js_add_node_addon PROJECT_NAME)
endif()

# Generate node.lib if needed
set(CMAKE_JS_NODELIB_DEF "") # TODO
if(MSVC AND CMAKE_JS_NODELIB_DEF)
# Generate node.lib
set(CMAKE_JS_NODELIB_TARGET "${CMAKE_BINARY_DIR}/node.lib")
execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_NODELIB_TARGET})
if(MSVC)
# Find node-addon-api
execute_process(COMMAND ${NODE_PATH} -p "require('node-api-headers').def_paths.node_api_def"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE NODE_API_DEF_PATH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (DEFINED NODE_API_DEF_PATH AND NOT "${NODE_API_DEF_PATH}" STREQUAL "")
# Generate node.lib
set(CMAKE_JS_NODELIB_TARGET "${CMAKE_BINARY_DIR}/node.lib")
execute_process(COMMAND ${CMAKE_AR} /def:${NODE_API_DEF_PATH} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_JS_NODELIB_TARGET})
else()
message(FATAL_ERROR "Failed to find node-api-headers node_api_def!")
endif()

endif()
endif()

if (MSVC)
# setup delayload
target_link_options(${PROJECT_NAME} PRIVATE "/DELAYLOAD:NODE.EXE")
target_link_libraries(${PROJECT_NAME} PRIVATE delayimp)

if (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)")
target_link_options(${PROJECT_NAME} PUBLIC "/SAFESEH:NO")
endif()
endif()

Expand Down
6 changes: 0 additions & 6 deletions rewrite/old/buildSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@ class BuildSystem {
getCmakeJsLibString() {
return this._invokeCMake('getCmakeJsLibString')
}
getCmakeJsIncludeString() {
return this._invokeCMake('getCmakeJsIncludeString')
}
getCmakeJsSrcString() {
return this._invokeCMake('getCmakeJsSrcString')
}
configure() {
return this._invokeCMake('configure')
}
Expand Down
42 changes: 2 additions & 40 deletions rewrite/old/cMake.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ const CMLog = require('./cmLog')
const TargetOptions = require('./targetOptions')
const processHelpers = require('./processHelpers')
const locateNAN = require('./locateNAN')
const locateNodeApi = require('./locateNodeApi')
const npmConfigData = require('rc')('npm')
const Toolset = require('./toolset')
const headers = require('node-api-headers')

class CMake {
get path() {
Expand Down Expand Up @@ -111,8 +109,6 @@ class CMake {

const D = []

// CMake.js watermark
D.push({ CMAKE_JS_VERSION: environment.cmakeJsVersion })

// Build configuration:
D.push({ CMAKE_BUILD_TYPE: this.config })
Expand All @@ -132,10 +128,6 @@ class CMake {
const includesString = await this.getCmakeJsIncludeString()
D.push({ CMAKE_JS_INC: includesString })

// Sources:
const srcsString = this.getCmakeJsSrcString()
D.push({ CMAKE_JS_SRC: srcsString })

// Runtime:
D.push({ NODE_RUNTIME: this.targetOptions.runtime })
D.push({ NODE_RUNTIMEVERSION: this.targetOptions.runtimeVersion })
Expand All @@ -160,15 +152,6 @@ class CMake {
const libsString = this.getCmakeJsLibString()
D.push({ CMAKE_JS_LIB: libsString })

if (environment.isWin) {
const nodeLibDefPath = this.getNodeLibDefPath()
if (nodeLibDefPath) {
const nodeLibPath = path.join(this.workDir, 'node.lib')
D.push({ CMAKE_JS_NODELIB_DEF: nodeLibDefPath })
D.push({ CMAKE_JS_NODELIB_TARGET: nodeLibPath })
}
}

if (this.toolset.generator) {
command.push('-G', this.toolset.generator)
}
Expand Down Expand Up @@ -217,7 +200,6 @@ class CMake {
if (environment.isWin) {
const nodeLibDefPath = this.getNodeLibDefPath()
if (nodeLibDefPath) {
libs.push(path.join(this.workDir, 'node.lib'))
} else {
libs.push(...this.dist.winLibs)
}
Expand All @@ -243,32 +225,12 @@ class CMake {
incPaths.push(nanH)
}
} else {
// Base headers
const apiHeaders = require('node-api-headers')
incPaths.push(apiHeaders.include_dir)

// Node-api
const napiH = await locateNodeApi(this.projectRoot)
if (napiH) {
incPaths.push(napiH)
}

}

return incPaths.join(';')
}
getCmakeJsSrcString() {
const srcPaths = []
if (environment.isWin) {
const delayHook = path.normalize(path.join(__dirname, 'cpp', 'win_delay_load_hook.cc'))

srcPaths.push(delayHook.replace(/\\/gm, '/'))
}

return srcPaths.join(';')
}
getNodeLibDefPath() {
return environment.isWin && this.options.isNodeApi ? headers.def_paths.node_api_def : undefined
}

async configure() {
this.verifyIfAvailable()

Expand Down
10 changes: 0 additions & 10 deletions rewrite/old/toolset.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ class Toolset {
this.linkerFlags.push('-undefined dynamic_lookup')
}

this.compilerFlags.push('-DBUILDING_NODE_EXTENSION')

// 4: Build target
if (this.options.target) {
this.log.info('TOOL', 'Building only the ' + this.options.target + ' target, as specified from the command line.')
Expand Down Expand Up @@ -178,14 +176,6 @@ class Toolset {
}
}

this.linkerFlags.push('/DELAYLOAD:NODE.EXE')

if (this.targetOptions.isX86) {
if (install) {
this.log.verbose('TOOL', 'Setting SAFESEH:NO linker flag.')
}
this.linkerFlags.push('/SAFESEH:NO')
}
}
async _getTopSupportedVisualStudioGenerator() {
const CMake = require('./cMake')
Expand Down

0 comments on commit 38ba6c3

Please sign in to comment.