Skip to content

Commit a80d50b

Browse files
committed
build: detect system processor on Windows via project_options
1 parent 384b3d8 commit a80d50b

File tree

1 file changed

+38
-41
lines changed

1 file changed

+38
-41
lines changed

CMakeLists.txt

+38-41
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
cmake_minimum_required(VERSION 3.16)
22

3+
include(FetchContent)
4+
5+
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
6+
cmake_policy(SET CMP0135 NEW)
7+
endif()
8+
9+
# Add project_options from https://github.com/aminya/project_options
10+
set(PROJECT_OPTIONS_VERSION "v0.41.0")
11+
FetchContent_Declare(
12+
_project_options
13+
URL https://github.com/aminya/project_options/archive/refs/tags/${PROJECT_OPTIONS_VERSION}.zip
14+
)
15+
FetchContent_MakeAvailable(_project_options)
16+
include(${_project_options_SOURCE_DIR}/Index.cmake)
17+
18+
# Detect ZeroMQ options from environment variables
319
macro(set_option_from_env OPTION_NAME)
420
string(TOLOWER ${OPTION_NAME} OPTION_NAME_LOWER)
521

@@ -50,33 +66,34 @@ set_option_from_env(ZMQ_WEBSOCKETS_SECURE)
5066
option(ZMQ_NO_SYNC_RESOLVE "send/receive on the socket immediately" OFF)
5167
set_option_from_env(ZMQ_NO_SYNC_RESOLVE)
5268

53-
if(APPLE)
54-
option(MACOSX_DEPLOYMENT_TARGET "MacOS deployment target" "10.15")
55-
set_option_from_env(MACOSX_DEPLOYMENT_TARGET)
56-
set(CMAKE_OSX_DEPLOYMENT_TARGET ${MACOSX_DEPLOYMENT_TARGET})
57-
endif()
58-
69+
# Add undefined behavior sanitizer if requested
5970
option(ZMQ_ENABLE_SANITIZER_UNDEFINED "Enable undefined behavior sanitizer" OFF)
6071
set_option_from_env(ZMQ_ENABLE_SANITIZER_UNDEFINED)
6172

6273
if(${ZMQ_ENABLE_SANITIZER_UNDEFINED})
6374
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
6475
endif()
6576

66-
# target system on Windows (for cross-compiling x86) and static linking runtimes
77+
# Set MacOS deployment target
78+
if(APPLE)
79+
option(MACOSX_DEPLOYMENT_TARGET "MacOS deployment target" "10.15")
80+
set_option_from_env(MACOSX_DEPLOYMENT_TARGET)
81+
set(CMAKE_OSX_DEPLOYMENT_TARGET ${MACOSX_DEPLOYMENT_TARGET})
82+
endif()
83+
84+
# Set position independent code on all platforms
85+
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
86+
87+
# Set static runtime on Windows
6788
if(WIN32)
6889
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "")
69-
if("$ENV{Platform}" STREQUAL "x86")
70-
set(CMAKE_SYSTEM_PROCESSOR "x86")
71-
elseif(NOT "$ENV{PROCESSOR_ARCHITEW6432}" STREQUAL "")
72-
set(CMAKE_SYSTEM_PROCESSOR "$ENV{PROCESSOR_ARCHITEW6432}")
73-
else()
74-
set(CMAKE_SYSTEM_PROCESSOR "$ENV{PROCESSOR_ARCHITECTURE}")
75-
endif()
90+
detect_compiler()
91+
string(TOLOWER "${DETECTED_CMAKE_HOST_SYSTEM_PROCESSOR}" CMAKE_SYSTEM_PROCESSOR_LOWER)
92+
else()
93+
string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" CMAKE_SYSTEM_PROCESSOR_LOWER)
7694
endif()
7795

78-
string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" CMAKE_SYSTEM_PROCESSOR_LOWER)
79-
96+
# set vcpkg triplet to static runtime
8097
if("${CMAKE_SYSTEM_PROCESSOR_LOWER}" STREQUAL "amd64" OR "${CMAKE_SYSTEM_PROCESSOR_LOWER}" STREQUAL "x64")
8198
set(VCPKG_TARGET_TRIPLET "x64-windows-static")
8299
elseif("${CMAKE_SYSTEM_PROCESSOR_LOWER}" STREQUAL "arm64" OR "${CMAKE_SYSTEM_PROCESSOR_LOWER}" STREQUAL "aarch64")
@@ -87,33 +104,11 @@ if(WIN32)
87104
message(STATUS "Not setting VCPKG_TARGET_TRIPLET for ${CMAKE_SYSTEM_PROCESSOR}")
88105
endif()
89106

90-
# Avoid loading of project_optinos/WindowsToolchain
91-
set(CMAKE_TOOLCHAIN_FILE ";")
92-
93107
# use static runtime library
94108
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
95109
endif()
96110

97-
include(FetchContent)
98-
99-
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
100-
cmake_policy(SET CMP0135 NEW)
101-
endif()
102-
103-
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
104-
105-
# Add project_options from https://github.com/aminya/project_options Change the
106-
# version in the following URL to update the package (watch the releases of the
107-
# repository for future updates)
108-
set(PROJECT_OPTIONS_VERSION "v0.36.6")
109-
FetchContent_Declare(
110-
_project_options
111-
URL https://github.com/aminya/project_options/archive/refs/tags/${PROJECT_OPTIONS_VERSION}.zip
112-
)
113-
FetchContent_MakeAvailable(_project_options)
114-
include(${_project_options_SOURCE_DIR}/Index.cmake)
115-
116-
# MacOS flags that should be set prior to any project calls
111+
# Defer undefined symbol resolution to runtime on MacOS
117112
if(APPLE)
118113
set(CMAKE_SHARED_LINKER_FLAGS
119114
"${CMAKE_SHARED_LINKER_FLAGS} -undefined dynamic_lookup")
@@ -122,7 +117,7 @@ endif()
122117
run_vcpkg(VCPKG_URL "https://github.com/microsoft/vcpkg.git" VCPKG_REV
123118
"608d1dbcd6969679f82b1ca6b89d58939c9b228e")
124119

125-
# Name of the project (will be the name of the plugin)
120+
# Name of the addon
126121
project(addon LANGUAGES C CXX)
127122

128123
project_options(
@@ -158,12 +153,14 @@ target_link_system_libraries(addon PRIVATE libzmq libzmq-static)
158153
target_include_system_directories(addon PRIVATE ${CMAKE_JS_INC})
159154
target_link_system_libraries(addon PRIVATE ${CMAKE_JS_LIB})
160155

156+
# Node flags
161157
target_compile_definitions(addon PRIVATE V8_COMPRESS_POINTERS)
162158
target_compile_definitions(addon PRIVATE V8_31BIT_SMIS_ON_64BIT_ARCH)
163159
target_compile_definitions(addon PRIVATE V8_REVERSE_JSARGS)
164160
target_compile_definitions(addon PRIVATE BUILDING_NODE_EXTENSION)
165161
target_compile_definitions(addon PRIVATE NAPI_CPP_EXCEPTIONS)
166162

163+
# Windows definitions
167164
if(WIN32)
168165
target_compile_definitions(addon PRIVATE "NOMINMAX")
169166
target_compile_definitions(addon PRIVATE "NOGDI")
@@ -173,7 +170,7 @@ endif()
173170
# Use `.node` for the library without any "lib" prefix
174171
set_target_properties(addon PROPERTIES PREFIX "" SUFFIX ".node")
175172

176-
# Windows
173+
# Windows delay load node.exe
177174
if(WIN32)
178175
set_property(TARGET addon PROPERTY LINK_FLAGS "-Xlinker /DELAYLOAD:NODE.EXE")
179176
target_link_libraries(addon PRIVATE "ShLwApi.lib" "delayimp.lib")

0 commit comments

Comments
 (0)