Skip to content

Commit df292e6

Browse files
committed
Windows CI fix
1 parent 07e64d2 commit df292e6

File tree

3 files changed

+132
-84
lines changed

3 files changed

+132
-84
lines changed

.github/workflows/build.yml

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
node_arch: arm64
3131
cmake_arch: arm64
3232

33-
# Windows builds
33+
# Windows builds with MSVC
3434
- os: windows-latest
3535
arch: x64
3636
node_arch: x64
@@ -106,7 +106,7 @@ jobs:
106106
echo "Checking gfortran installation..."
107107
ls -la ${BREW_PREFIX}/bin/gfortran* || echo "gfortran not found"
108108
109-
# 找到正确的gfortran路径
109+
# 找到正确的gfortran路径并设置为环境变量
110110
GFORTRAN_PATH=$(find ${BREW_PREFIX}/bin -name "gfortran*" | head -1)
111111
if [ -n "$GFORTRAN_PATH" ] && [ -x "$GFORTRAN_PATH" ]; then
112112
echo "Found gfortran at: $GFORTRAN_PATH"
@@ -124,42 +124,31 @@ jobs:
124124
echo "LIBRARY_PATH=${BREW_PREFIX}/lib:$LIBRARY_PATH" >> $GITHUB_ENV
125125
echo "LD_LIBRARY_PATH=${BREW_PREFIX}/lib:$LD_LIBRARY_PATH" >> $GITHUB_ENV
126126
127-
# 3. 安装系统依赖 - Windows
128-
- name: Setup MSYS2 for Windows build
127+
# 3. Windows MSVC 环境设置
128+
- name: Setup Visual Studio Build Tools
129129
if: runner.os == 'Windows'
130-
uses: msys2/setup-msys2@v2
131-
with:
132-
msystem: MINGW64
133-
update: true
134-
path-type: inherit
135-
install: >-
136-
mingw-w64-x86_64-gcc
137-
mingw-w64-x86_64-gcc-fortran
138-
mingw-w64-x86_64-cmake
139-
mingw-w64-x86_64-ninja
140-
mingw-w64-x86_64-pkg-config
141-
mingw-w64-x86_64-fftw
142-
mingw-w64-x86_64-boost
130+
uses: microsoft/setup-msbuild@v1
143131

144-
- name: Setup Windows environment
132+
- name: Setup vcpkg & install FFTW3
145133
if: runner.os == 'Windows'
146-
shell: msys2 {0}
147134
run: |
148-
echo "/c/msys64/mingw64/bin" >> $GITHUB_PATH
149-
echo "Verifying pkg-config installation..."
150-
which pkg-config || (echo "pkg-config not found, reinstalling..." && pacman -S --noconfirm mingw-w64-x86_64-pkg-config)
151-
pkg-config --version || (echo "Unable to run pkg-config, reinstalling..." && pacman -S --noconfirm mingw-w64-x86_64-pkg-config)
135+
git clone https://github.com/Microsoft/vcpkg.git
136+
.\vcpkg\bootstrap-vcpkg.bat
137+
138+
# 使用经典模式安装依赖包
139+
.\vcpkg\vcpkg install fftw3f:x64-windows fftw3:x64-windows boost:x64-windows
152140
153-
echo "CC=gcc" >> $GITHUB_ENV
154-
echo "CXX=g++" >> $GITHUB_ENV
155-
echo "FC=gfortran" >> $GITHUB_ENV
156-
echo "CMAKE_Fortran_COMPILER=gfortran" >> $GITHUB_ENV
157-
echo "CMAKE_GENERATOR=MinGW Makefiles" >> $GITHUB_ENV
158-
159-
gcc --version
160-
g++ --version
161-
gfortran --version
162-
cmake --version
141+
# 设置vcpkg环境变量
142+
echo "VCPKG_ROOT=${{ github.workspace }}\vcpkg" >> $env:GITHUB_ENV
143+
echo "CMAKE_TOOLCHAIN_FILE=${{ github.workspace }}\vcpkg\scripts\buildsystems\vcpkg.cmake" >> $env:GITHUB_ENV
144+
shell: powershell
145+
146+
- name: Setup Intel Fortran
147+
if: runner.os == 'Windows'
148+
uses: fortran-lang/setup-fortran@v1
149+
with:
150+
compiler: intel-classic
151+
version: '2021.10'
163152

164153
# 4. 安装 npm 依赖
165154
- name: Install npm dependencies
@@ -173,6 +162,9 @@ jobs:
173162
- name: Build native module (Linux/macOS)
174163
if: runner.os != 'Windows'
175164
run: |
165+
# 清理可能存在的CMake缓存
166+
rm -rf build/
167+
176168
if [ "${{ matrix.arch }}" = "arm64" ] && [ "${{ runner.os }}" = "Linux" ]; then
177169
# Linux ARM64 交叉编译
178170
npx cmake-js compile --arch=${{ matrix.cmake_arch }} \
@@ -186,35 +178,20 @@ jobs:
186178
npx cmake-js compile --arch=${{ matrix.cmake_arch }}
187179
fi
188180
189-
# 6. 构建原生模块 - Windows
190-
- name: Build native module (Windows)
181+
# 6. 构建原生模块 - Windows MSVC
182+
- name: Build native module (Windows MSVC)
191183
if: runner.os == 'Windows'
192-
shell: msys2 {0}
193184
run: |
194-
echo "--- Checking Node.js and npx in MSYS2 (with path-type: inherit) ---"
195-
echo "PATH: $PATH"
196-
197-
echo "which node:"
198-
which node || echo "node not found"
199-
echo "node --version:"
200-
node --version || echo "node not runnable"
185+
echo "--- Building with MSVC toolchain ---"
186+
if (Test-Path "build") { Remove-Item -Recurse -Force "build" }
201187
202-
echo "which npx:"
203-
which npx || echo "npx not found"
204-
echo "npx --version:"
205-
npx --version || echo "npx not runnable"
206-
207-
echo "Attempting to run npx cmake-js compile..."
208-
npx cmake-js compile --arch=${{ matrix.cmake_arch }} \
209-
-G "MinGW Makefiles" \
210-
--CDCMAKE_C_COMPILER=gcc \
211-
--CDCMAKE_CXX_COMPILER=g++ \
212-
--CDCMAKE_Fortran_COMPILER=gfortran \
213-
--CDCMAKE_SHARED_LINKER_FLAGS='' \
214-
--CDCMAKE_MODULE_LINKER_FLAGS='' \
215-
--CDCMAKE_EXE_LINKER_FLAGS='' \
216-
--CDCMAKE_JS_SRC='' \
188+
npx cmake-js compile --arch=${{ matrix.cmake_arch }} `
189+
-G "Visual Studio 17 2022" -A x64 `
190+
--CDCMAKE_TOOLCHAIN_FILE="$env:CMAKE_TOOLCHAIN_FILE" `
191+
--CDVCPKG_MANIFEST_MODE=OFF `
192+
--CDVCPKG_MANIFEST_INSTALL=OFF `
217193
--verbose
194+
shell: powershell
218195

219196
# 7. 运行测试
220197
- name: Run tests
@@ -241,6 +218,7 @@ jobs:
241218
echo "❌ Basic test file not found!"
242219
exit 1
243220
fi
221+
shell: bash
244222

245223
# 8. 创建预构建二进制文件
246224
- name: Create prebuilt binaries

CMakeLists.txt

Lines changed: 95 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
cmake_minimum_required(VERSION 3.15)
2-
project(wsjtx_lib_nodejs)
2+
3+
# 禁用 vcpkg Manifest 模式,使用经典模式
4+
if(DEFINED CMAKE_TOOLCHAIN_FILE AND CMAKE_TOOLCHAIN_FILE MATCHES "vcpkg")
5+
set(VCPKG_MANIFEST_MODE OFF CACHE BOOL "Disable vcpkg manifest mode" FORCE)
6+
set(VCPKG_MANIFEST_INSTALL OFF CACHE BOOL "Disable vcpkg manifest install" FORCE)
7+
message(STATUS "Detected vcpkg toolchain, disabling manifest mode")
8+
endif()
9+
10+
project(wsjtx_lib_nodejs LANGUAGES C CXX Fortran)
11+
12+
# Use MSVC on Windows by default, else keep existing
13+
if(MSVC)
14+
message(STATUS "Using MSVC toolchain on Windows")
15+
# Enable Fortran support via Intel oneAPI if available
16+
# vcpkg toolchain file should be passed to CMake invocation
17+
endif()
318

419
# Set C++ standard
520
set(CMAKE_CXX_STANDARD 17)
@@ -38,7 +53,9 @@ elseif(APPLE)
3853
endif()
3954

4055
# Include cmake-js
41-
include_directories(${CMAKE_JS_INC})
56+
if(CMAKE_JS_INC)
57+
include_directories(${CMAKE_JS_INC})
58+
endif()
4259

4360
# Add Node.js include path for node_api.h
4461
execute_process(
@@ -49,7 +66,30 @@ execute_process(
4966
include_directories(${NODE_INCLUDE_DIR})
5067

5168
# Platform-specific package finding
52-
if(WIN32)
69+
if(WIN32 AND MSVC)
70+
# Windows with MSVC: Use vcpkg for FFTW3
71+
find_package(FFTW3F CONFIG REQUIRED)
72+
find_package(FFTW3 CONFIG REQUIRED)
73+
74+
# Set CMake policy for newer Boost versions
75+
if(POLICY CMP0167)
76+
cmake_policy(SET CMP0167 NEW)
77+
endif()
78+
79+
# Find Boost
80+
find_package(Boost QUIET)
81+
if(Boost_FOUND)
82+
message(STATUS "Boost found: ${Boost_VERSION}")
83+
set(BOOST_LIBRARIES ${Boost_LIBRARIES})
84+
else()
85+
message(WARNING "Boost not found via find_package, continuing...")
86+
set(BOOST_LIBRARIES "")
87+
endif()
88+
89+
# MSVC doesn't need separate threads library for FFTW
90+
set(FFTW_HAS_THREADS FALSE)
91+
set(FFTW_THREADS_LIBRARIES "")
92+
elseif(WIN32)
5393
# Windows with MSYS2: Use pkg-config like Unix systems
5494
find_package(PkgConfig REQUIRED)
5595

@@ -111,19 +151,34 @@ else()
111151
endif()
112152

113153
# Include directories
114-
include_directories(
115-
${CMAKE_SOURCE_DIR}/wsjtx_lib
116-
${CMAKE_SOURCE_DIR}/native
117-
${CMAKE_SOURCE_DIR}/node_modules/node-addon-api
118-
${FFTW3F_INCLUDE_DIRS}
119-
${Boost_INCLUDE_DIRS}
120-
)
154+
if(WIN32 AND MSVC)
155+
include_directories(
156+
${CMAKE_SOURCE_DIR}/wsjtx_lib
157+
${CMAKE_SOURCE_DIR}/native
158+
${CMAKE_SOURCE_DIR}/node_modules/node-addon-api
159+
)
160+
else()
161+
include_directories(
162+
${CMAKE_SOURCE_DIR}/wsjtx_lib
163+
${CMAKE_SOURCE_DIR}/native
164+
${CMAKE_SOURCE_DIR}/node_modules/node-addon-api
165+
${FFTW3F_INCLUDE_DIRS}
166+
${Boost_INCLUDE_DIRS}
167+
)
168+
endif()
121169

122170
# Define LIBRARIES_FROM_REFERENCES for wsjtx_lib submodule
123-
set(LIBRARIES_FROM_REFERENCES
124-
${FFTW3F_LIBRARIES}
125-
${FFTW_THREADS_LIBRARIES}
126-
)
171+
if(WIN32 AND MSVC)
172+
set(LIBRARIES_FROM_REFERENCES
173+
FFTW3F::fftw3f
174+
FFTW3::fftw3
175+
)
176+
else()
177+
set(LIBRARIES_FROM_REFERENCES
178+
${FFTW3F_LIBRARIES}
179+
${FFTW_THREADS_LIBRARIES}
180+
)
181+
endif()
127182

128183
# Platform-specific library setup
129184
if(APPLE)
@@ -150,10 +205,7 @@ if(APPLE)
150205
if(GFORTRAN_EXECUTABLE)
151206
message(STATUS "Found gfortran: ${GFORTRAN_EXECUTABLE}")
152207

153-
# Set the Fortran compiler for the project
154-
set(CMAKE_Fortran_COMPILER ${GFORTRAN_EXECUTABLE})
155-
156-
# Get library path
208+
# Get library path (but don't set CMAKE_Fortran_COMPILER here to avoid cache conflicts)
157209
execute_process(
158210
COMMAND ${GFORTRAN_EXECUTABLE} --print-file-name=libgfortran.dylib
159211
OUTPUT_VARIABLE GFORTRAN_LIB_PATH
@@ -203,7 +255,7 @@ elseif(UNIX)
203255
if(FFTW_HAS_THREADS)
204256
list(APPEND LIBRARIES_FROM_REFERENCES fftw3f_threads)
205257
endif()
206-
elseif(WIN32)
258+
elseif(WIN32 AND NOT MSVC)
207259
# Windows specific libraries - MSYS2/MinGW environment
208260
list(APPEND LIBRARIES_FROM_REFERENCES
209261
gfortran
@@ -216,8 +268,10 @@ endif()
216268
add_subdirectory(wsjtx_lib)
217269

218270
# Link directories (must be before creating target)
219-
if(NOT WIN32)
220-
link_directories(${FFTW3F_LIBRARY_DIRS})
271+
if(NOT WIN32 OR NOT MSVC)
272+
if(NOT WIN32)
273+
link_directories(${FFTW3F_LIBRARY_DIRS})
274+
endif()
221275
endif()
222276

223277
# Source files for the Node.js addon
@@ -231,7 +285,7 @@ if(MINGW)
231285
# Exclude CMAKE_JS_SRC for MinGW
232286
)
233287
else()
234-
# For other platforms (or if not MINGW, though cmake-js might handle this differently)
288+
# For other platforms (MSVC, Linux, macOS)
235289
add_library(${PROJECT_NAME} SHARED
236290
${NATIVE_SOURCES}
237291
${CMAKE_JS_SRC}
@@ -247,7 +301,9 @@ set_target_properties(${PROJECT_NAME} PROPERTIES
247301
)
248302

249303
# Set Node.js addon include directories
250-
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC})
304+
if(CMAKE_JS_INC)
305+
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC})
306+
endif()
251307

252308
# Compiler-specific options
253309
target_compile_definitions(${PROJECT_NAME} PRIVATE
@@ -256,8 +312,10 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE
256312
)
257313

258314
# Add compile flags
259-
if(NOT WIN32)
260-
target_compile_options(${PROJECT_NAME} PRIVATE ${FFTW3F_CFLAGS_OTHER})
315+
if(NOT WIN32 OR NOT MSVC)
316+
if(NOT WIN32)
317+
target_compile_options(${PROJECT_NAME} PRIVATE ${FFTW3F_CFLAGS_OTHER})
318+
endif()
261319
endif()
262320

263321
# Link libraries
@@ -267,6 +325,17 @@ if(MINGW)
267325
wsjtx_lib
268326
# Potentially add -lnode or the path to libnode.dll.a if needed
269327
)
328+
elseif(WIN32 AND MSVC)
329+
# MSVC linking
330+
if(CMAKE_JS_LIB)
331+
target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_JS_LIB})
332+
endif()
333+
target_link_libraries(${PROJECT_NAME} PRIVATE
334+
wsjtx_lib
335+
FFTW3F::fftw3f
336+
FFTW3::fftw3
337+
${BOOST_LIBRARIES}
338+
)
270339
else()
271340
target_link_libraries(${PROJECT_NAME}
272341
${CMAKE_JS_LIB}
@@ -304,7 +373,7 @@ elseif(UNIX)
304373
target_link_options(${PROJECT_NAME} PRIVATE
305374
-Wl,--as-needed
306375
)
307-
elseif(WIN32)
376+
elseif(WIN32 AND NOT MSVC)
308377
# Windows linking - MSYS2/MinGW environment
309378
target_link_libraries(${PROJECT_NAME}
310379
${FFTW3F_LIBRARIES}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"scripts": {
2424
"build": "npm run build:native && npm run build:ts",
2525
"build:native": "cmake-js compile",
26+
"build:native:win": "cmake-js compile -G \"Visual Studio 17 2022\" -A x64 --CDCMAKE_TOOLCHAIN_FILE=%VCPKG_ROOT%/scripts/buildsystems/vcpkg.cmake --CDVCPKG_MANIFEST_MODE=OFF --CDVCPKG_MANIFEST_INSTALL=OFF",
2627
"build:ts": "tsc",
2728
"clean": "cmake-js clean && rimraf dist",
2829
"test": "npm run build && node --test dist/test/wsjtx.basic.test.js",

0 commit comments

Comments
 (0)