@@ -17,29 +17,55 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
1717# Enable position independent code for all targets (including subprojects)
1818set (CMAKE_POSITION_INDEPENDENT_CODE ON )
1919
20- # Force avoid MSVC detection on Windows when using MinGW
21- if (WIN32 AND MSVC )
22- message (FATAL_ERROR "MSVC compiler detected. This project requires MinGW-w64. Please use MSYS2/MinGW-w64 environment and specify -G \" MinGW Makefiles\" " )
20+ # Include cmake-js - this must come first to get CMAKE_JS_* variables
21+ if (CMAKE_JS_INC)
22+ include_directories (${CMAKE_JS_INC} )
23+ endif ()
24+
25+ # **关键修复**: 强制清空 cmake-js 通过命令行注入的 MSVC 缓存标志
26+ # cmake-js 会通过 -DCMAKE_SHARED_LINKER_FLAGS="/DELAYLOAD:NODE.EXE" 注入缓存
27+ # 必须使用 CACHE FORCE 来覆盖命令行传入的缓存值
28+ if (WIN32 AND CMAKE_CXX_COMPILER_ID MATCHES "GNU" )
29+ message (STATUS "Detected MinGW-w64 on Windows; FORCE-clear CMAKE_SHARED_LINKER_FLAGS from cache" )
30+ set (CMAKE_SHARED_LINKER_FLAGS "" CACHE STRING "Cleared MSVC-specific linker flags for MinGW compatibility" FORCE)
2331endif ()
2432
25- # Windows MinGW-w64 specific setup
26- if (WIN32 AND CMAKE_COMPILER_IS_GNUCXX)
33+ # Add Node.js include path for node_api.h
34+ execute_process (
35+ COMMAND node -p "require('path').dirname(process.execPath) + '/../include/node'"
36+ OUTPUT_VARIABLE NODE_INCLUDE_DIR
37+ OUTPUT_STRIP_TRAILING_WHITESPACE
38+ )
39+ include_directories (${NODE_INCLUDE_DIR} )
40+
41+ # **关键修复**: Windows + MinGW 环境检测并清除 MSVC 特定标志
42+ if (WIN32 AND CMAKE_CXX_COMPILER_ID MATCHES "GNU" )
2743 message (STATUS "Detected MinGW-w64 environment on Windows" )
2844
29- # Set PKG_CONFIG_PATH for MSYS2
45+ # 设置 PKG_CONFIG_PATH 以便查找 MSYS2 库
3046 if (EXISTS "C:/msys64/mingw64/lib/pkgconfig" )
3147 set (ENV{PKG_CONFIG_PATH} "C:/msys64/mingw64/lib/pkgconfig" )
3248 message (STATUS "Set PKG_CONFIG_PATH to C:/msys64/mingw64/lib/pkgconfig" )
3349 endif ()
3450
35- # Add MSYS2 library and include paths
51+ # 添加 MSYS2 的 include 和 library 路径
3652 if (EXISTS "C:/msys64/mingw64" )
3753 include_directories ("C:/msys64/mingw64/include" )
3854 link_directories ("C:/msys64/mingw64/lib" )
3955 message (STATUS "Added MSYS2 include and library paths" )
4056 endif ()
4157endif ()
4258
59+ # Force avoid MSVC detection on Windows when using MinGW
60+ if (WIN32 AND MSVC )
61+ message (FATAL_ERROR "MSVC compiler detected. This project requires MinGW-w64. Please use MSYS2/MinGW-w64 environment and specify -G \" MinGW Makefiles\" " )
62+ endif ()
63+
64+ # Additional check for cmake-js on Windows - prevent Visual Studio generator
65+ if (WIN32 AND CMAKE_GENERATOR MATCHES "Visual Studio" )
66+ message (FATAL_ERROR "Visual Studio generator detected. This project requires MinGW-w64. Please set CMAKE_GENERATOR=MinGW Makefiles" )
67+ endif ()
68+
4369# Set compiler flags for better compatibility
4470if (UNIX AND NOT APPLE )
4571 # Linux specific flags
@@ -78,19 +104,6 @@ elseif(WIN32)
78104 endif ()
79105endif ()
80106
81- # Include cmake-js
82- if (CMAKE_JS_INC)
83- include_directories (${CMAKE_JS_INC} )
84- endif ()
85-
86- # Add Node.js include path for node_api.h
87- execute_process (
88- COMMAND node -p "require('path').dirname(process.execPath) + '/../include/node'"
89- OUTPUT_VARIABLE NODE_INCLUDE_DIR
90- OUTPUT_STRIP_TRAILING_WHITESPACE
91- )
92- include_directories (${NODE_INCLUDE_DIR} )
93-
94107# Use pkg-config for all platforms (including Windows with MSYS2)
95108find_package (PkgConfig REQUIRED)
96109
@@ -99,9 +112,39 @@ if(POLICY CMP0167)
99112 cmake_policy (SET CMP0167 NEW)
100113endif ()
101114
102- # Find Boost
103- find_package (Boost REQUIRED)
104- set (BOOST_LIBRARIES ${Boost_LIBRARIES} )
115+ # Find Boost - use different methods based on platform
116+ if (WIN32 AND CMAKE_CXX_COMPILER_ID MATCHES "GNU" )
117+ # Windows with MinGW-w64: try both find_package and manual detection
118+ find_package (Boost QUIET )
119+ if (NOT Boost_FOUND)
120+ # Manual detection for MSYS2 Boost
121+ message (STATUS "Boost not found via find_package, trying manual detection..." )
122+
123+ # Look for Boost in MSYS2 locations
124+ find_path (Boost_INCLUDE_DIRS
125+ NAMES boost/version .hpp
126+ PATHS /mingw64/include C:/msys64/mingw64/include
127+ NO_DEFAULT_PATH
128+ )
129+
130+ if (Boost_INCLUDE_DIRS)
131+ message (STATUS "Found Boost headers at: ${Boost_INCLUDE_DIRS} " )
132+ set (Boost_FOUND TRUE )
133+ # For header-only libraries, we don't need to link anything
134+ set (BOOST_LIBRARIES "" )
135+ else ()
136+ message (WARNING "Boost headers not found" )
137+ set (BOOST_LIBRARIES "" )
138+ endif ()
139+ else ()
140+ message (STATUS "Boost found via find_package: ${Boost_VERSION} " )
141+ set (BOOST_LIBRARIES ${Boost_LIBRARIES} )
142+ endif ()
143+ else ()
144+ # Other platforms: use standard find_package
145+ find_package (Boost REQUIRED)
146+ set (BOOST_LIBRARIES ${Boost_LIBRARIES} )
147+ endif ()
105148
106149# Find FFTW3 using pkg-config
107150pkg_check_modules(FFTW3F REQUIRED fftw3f)
@@ -270,11 +313,27 @@ link_directories(${FFTW3F_LIBRARY_DIRS})
270313# Source files for the Node.js addon
271314file (GLOB_RECURSE NATIVE_SOURCES "native/*.cpp" "native/*.h" )
272315
273- # Create the Node.js addon
274- add_library (${PROJECT_NAME} SHARED
275- ${NATIVE_SOURCES}
276- ${CMAKE_JS_SRC}
277- )
316+ # Create the Node.js addon with MinGW-specific handling
317+ if (WIN32 AND CMAKE_CXX_COMPILER_ID MATCHES "GNU" )
318+ # MinGW build: exclude Windows delay load hook
319+ add_library (${PROJECT_NAME} SHARED
320+ ${NATIVE_SOURCES}
321+ )
322+ message (STATUS "MinGW build: excluding CMAKE_JS_SRC (Windows delay load hook)" )
323+ else ()
324+ # Other platforms: include CMAKE_JS_SRC if available
325+ if (CMAKE_JS_SRC)
326+ add_library (${PROJECT_NAME} SHARED
327+ ${NATIVE_SOURCES}
328+ ${CMAKE_JS_SRC}
329+ )
330+ message (STATUS "Non-MinGW build: including CMAKE_JS_SRC for delay-load hook" )
331+ else ()
332+ add_library (${PROJECT_NAME} SHARED
333+ ${NATIVE_SOURCES}
334+ )
335+ endif ()
336+ endif ()
278337
279338# Set properties for Node.js addon
280339set_target_properties (${PROJECT_NAME} PROPERTIES
@@ -298,9 +357,15 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE
298357# Add compile flags
299358target_compile_options (${PROJECT_NAME} PRIVATE ${FFTW3F_CFLAGS_OTHER} )
300359
301- # Link libraries
302- if (CMAKE_JS_LIB)
303- target_link_libraries (${PROJECT_NAME} PRIVATE ${CMAKE_JS_LIB} )
360+ # Link libraries with MinGW-specific handling
361+ if (WIN32 AND CMAKE_CXX_COMPILER_ID MATCHES "GNU" )
362+ # MinGW: don't link CMAKE_JS_LIB as it may cause issues
363+ message (STATUS "MinGW build: skipping CMAKE_JS_LIB linking" )
364+ else ()
365+ # Other platforms: link CMAKE_JS_LIB if available
366+ if (CMAKE_JS_LIB)
367+ target_link_libraries (${PROJECT_NAME} PRIVATE ${CMAKE_JS_LIB} )
368+ endif ()
304369endif ()
305370
306371target_link_libraries (${PROJECT_NAME} PRIVATE
0 commit comments