1 | cmake_minimum_required(VERSION 3.16)
|
2 |
|
3 | macro(set_option_from_env OPTION_NAME)
|
4 | string(TOLOWER ${OPTION_NAME} OPTION_NAME_LOWER)
|
5 |
|
6 | if(DEFINED ENV{npm_config_${OPTION_NAME_LOWER}})
|
7 | if("$ENV{npm_config_${OPTION_NAME_LOWER}}" STREQUAL "true")
|
8 | set("${OPTION_NAME}"
|
9 | ON
|
10 | CACHE BOOL "npm_config_${OPTION_NAME_LOWER}" FORCE)
|
11 | elseif("$ENV{npm_config_${OPTION_NAME_LOWER}}" STREQUAL "false")
|
12 | set("${OPTION_NAME}"
|
13 | OFF
|
14 | CACHE BOOL "npm_config_${OPTION_NAME_LOWER}" FORCE)
|
15 | else()
|
16 | set("${OPTION_NAME}"
|
17 | "$ENV{npm_config_${OPTION_NAME_LOWER}}"
|
18 | CACHE STRING "npm_config_${OPTION_NAME_LOWER}" FORCE)
|
19 | endif()
|
20 | endif()
|
21 |
|
22 | if(${OPTION_NAME})
|
23 | string(REPLACE "zmq_" "" OPTION_NAME_LOWER "${OPTION_NAME_LOWER}")
|
24 | string(REPLACE "_" "-" OPTION_NAME_LOWER "${OPTION_NAME_LOWER}")
|
25 | list(APPEND VCPKG_MANIFEST_FEATURES ${OPTION_NAME_LOWER})
|
26 | endif()
|
27 |
|
28 | message(STATUS "${OPTION_NAME}: ${${OPTION_NAME}}")
|
29 | endmacro()
|
30 |
|
31 | option(ZMQ_DRAFT "Build and install draft APIs (e.g. `server-client`, `radio-dish`, `scatter-gather`)" ON)
|
32 | set_option_from_env(ZMQ_DRAFT)
|
33 |
|
34 | option(ZMQ_CURVE "Enable CURVE security" ON)
|
35 | set_option_from_env(ZMQ_CURVE)
|
36 |
|
37 | option(ZMQ_SODIUM "Using libsodium for CURVE security" ON)
|
38 | set_option_from_env(ZMQ_SODIUM)
|
39 |
|
40 | if(ZMQ_SODIUM AND APPLE)
|
41 | message(STATUS "building libsodium requires running `brew install autoconf automake libtool`")
|
42 | endif()
|
43 |
|
44 | option(ZMQ_WEBSOCKETS "Enable WebSocket transport" OFF)
|
45 | set_option_from_env(ZMQ_WEBSOCKETS)
|
46 |
|
47 | option(ZMQ_WEBSOCKETS_SECURE "Enable WebSocket transport with TSL (wss)" OFF)
|
48 | set_option_from_env(ZMQ_WEBSOCKETS_SECURE)
|
49 |
|
50 | option(ZMQ_NO_SYNC_RESOLVE "send/receive on the socket immediately" OFF)
|
51 | set_option_from_env(ZMQ_NO_SYNC_RESOLVE)
|
52 |
|
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 |
|
59 | option(ZMQ_ENABLE_SANITIZER_UNDEFINED "Enable undefined behavior sanitizer" OFF)
|
60 | set_option_from_env(ZMQ_ENABLE_SANITIZER_UNDEFINED)
|
61 |
|
62 | if(${ZMQ_ENABLE_SANITIZER_UNDEFINED})
|
63 | set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
|
64 | endif()
|
65 |
|
66 | # target system on Windows (for cross-compiling x86) and static linking runtimes
|
67 | if(WIN32)
|
68 | if("$ENV{Platform}" STREQUAL "x86")
|
69 | set(CMAKE_SYSTEM_PROCESSOR "x86")
|
70 | set(VCPKG_TARGET_TRIPLET "x86-windows-static")
|
71 | elseif(NOT "$ENV{PROCESSOR_ARCHITEW6432}" STREQUAL "")
|
72 | set(CMAKE_SYSTEM_PROCESSOR "$ENV{PROCESSOR_ARCHITEW6432}")
|
73 | set(VCPKG_TARGET_TRIPLET "x86-windows-static")
|
74 | else()
|
75 | set(CMAKE_SYSTEM_PROCESSOR "$ENV{PROCESSOR_ARCHITECTURE}")
|
76 | set(VCPKG_TARGET_TRIPLET "x64-windows-static")
|
77 | endif()
|
78 |
|
79 | # Avoid loading of project_optinos/WindowsToolchain
|
80 | set(CMAKE_TOOLCHAIN_FILE ";")
|
81 |
|
82 | # use static runtime library
|
83 | set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
|
84 | endif()
|
85 |
|
86 | include(FetchContent)
|
87 |
|
88 | if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
|
89 | cmake_policy(SET CMP0135 NEW)
|
90 | endif()
|
91 |
|
92 | set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
|
93 |
|
94 | # Add project_options from https://github.com/aminya/project_options Change the
|
95 | # version in the following URL to update the package (watch the releases of the
|
96 | # repository for future updates)
|
97 | set(PROJECT_OPTIONS_VERSION "v0.36.6")
|
98 | FetchContent_Declare(
|
99 | _project_options
|
100 | URL https://github.com/aminya/project_options/archive/refs/tags/${PROJECT_OPTIONS_VERSION}.zip
|
101 | )
|
102 | FetchContent_MakeAvailable(_project_options)
|
103 | include(${_project_options_SOURCE_DIR}/Index.cmake)
|
104 |
|
105 | # MacOS flags that should be set prior to any project calls
|
106 | if(APPLE)
|
107 | set(CMAKE_SHARED_LINKER_FLAGS
|
108 | "${CMAKE_SHARED_LINKER_FLAGS} -undefined dynamic_lookup")
|
109 | endif()
|
110 |
|
111 | run_vcpkg(VCPKG_URL "https://github.com/microsoft/vcpkg.git" VCPKG_REV
|
112 | "ee2d2a100103e0f3613c60655dcf15be7d5157b8")
|
113 |
|
114 | # Name of the project (will be the name of the plugin)
|
115 | project(addon LANGUAGES C CXX)
|
116 |
|
117 | project_options(
|
118 | ENABLE_CACHE
|
119 | ENABLE_COMPILE_COMMANDS_SYMLINK
|
120 | ${ENABLE_SANITIZER_UNDEFINED}
|
121 | )
|
122 |
|
123 | file(GLOB_RECURSE SOURCES "./src/*.cc")
|
124 | add_library(addon SHARED ${SOURCES})
|
125 |
|
126 | if(CMAKE_CXX_COMPILER_ID STREQUAL GNU
|
127 | OR CMAKE_CXX_COMPILER_ID STREQUAL Clang
|
128 | OR CMAKE_CXX_COMPILER_ID STREQUAL AppleClang)
|
129 | target_compile_options(project_warnings INTERFACE "-Wno-shadow")
|
130 | endif()
|
131 |
|
132 | target_link_libraries(addon PRIVATE project_options project_warnings)
|
133 |
|
134 | if(ZMQ_DRAFT)
|
135 | target_compile_definitions(addon PRIVATE ZMQ_BUILD_DRAFT_API)
|
136 | endif()
|
137 |
|
138 | if(ZMQ_NO_SYNC_RESOLVE)
|
139 | target_compile_definitions(addon PRIVATE ZMQ_NO_SYNC_RESOLVE)
|
140 | endif()
|
141 |
|
142 | # ZeroMQ
|
143 | find_package(ZeroMQ CONFIG REQUIRED)
|
144 | target_link_system_libraries(addon PRIVATE libzmq libzmq-static)
|
145 |
|
146 | # Node specific
|
147 | target_include_system_directories(addon PRIVATE ${CMAKE_JS_INC})
|
148 | target_link_system_libraries(addon PRIVATE ${CMAKE_JS_LIB})
|
149 |
|
150 | target_compile_definitions(addon PRIVATE V8_COMPRESS_POINTERS)
|
151 | target_compile_definitions(addon PRIVATE V8_31BIT_SMIS_ON_64BIT_ARCH)
|
152 | target_compile_definitions(addon PRIVATE V8_REVERSE_JSARGS)
|
153 | target_compile_definitions(addon PRIVATE BUILDING_NODE_EXTENSION)
|
154 | target_compile_definitions(addon PRIVATE NAPI_CPP_EXCEPTIONS)
|
155 |
|
156 | if(WIN32)
|
157 | target_compile_definitions(addon PRIVATE "NOMINMAX")
|
158 | target_compile_definitions(addon PRIVATE "NOGDI")
|
159 | target_compile_definitions(addon PRIVATE "WIN32_LEAN_AND_MEAN")
|
160 | endif()
|
161 |
|
162 | # Use `.node` for the library without any "lib" prefix
|
163 | set_target_properties(addon PROPERTIES PREFIX "" SUFFIX ".node")
|
164 |
|
165 | # Windows
|
166 | if(WIN32)
|
167 | set_property(TARGET addon PROPERTY LINK_FLAGS "-Xlinker /DELAYLOAD:NODE.EXE")
|
168 | target_link_libraries(addon PRIVATE "ShLwApi.lib" "delayimp.lib")
|
169 | endif()
|