Files
pangolin/client/windows/CMakeLists.txt
wangjia b12519be0a fix(client/windows): runner 加 /utf-8 编译选项,根治 C4819 中文注释编译失败
win32_window.cpp 含中文注释(UTF-8 无 BOM),在 zh-CN 机器(代码页 936)+ 新版
MSVC 上报 C4819「字符无法用当前代码页表示」,叠加 runner 的 /WX 直接 error
C2220 编译失败。APPLY_STANDARD_SETTINGS 加 /utf-8,让 MSVC 按 UTF-8 解析
所有 runner 源码,永久根治。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-27 20:49:36 +08:00

136 lines
5.8 KiB
CMake

# Project-level configuration.
cmake_minimum_required(VERSION 3.14)
project(pangolin_vpn LANGUAGES CXX)
# The name of the executable created for the application. Change this to change
# the on-disk name of your application.
set(BINARY_NAME "pangolin_vpn")
# Explicitly opt in to modern CMake behaviors to avoid warnings with recent
# versions of CMake.
cmake_policy(VERSION 3.14...3.25)
# Define build configuration option.
get_property(IS_MULTICONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(IS_MULTICONFIG)
set(CMAKE_CONFIGURATION_TYPES "Debug;Profile;Release"
CACHE STRING "" FORCE)
else()
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE "Debug" CACHE
STRING "Flutter build mode" FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
"Debug" "Profile" "Release")
endif()
endif()
# Define settings for the Profile build mode.
set(CMAKE_EXE_LINKER_FLAGS_PROFILE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
set(CMAKE_SHARED_LINKER_FLAGS_PROFILE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE}")
set(CMAKE_C_FLAGS_PROFILE "${CMAKE_C_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS_PROFILE "${CMAKE_CXX_FLAGS_RELEASE}")
# Use Unicode for all projects.
add_definitions(-DUNICODE -D_UNICODE)
# Compilation settings that should be applied to most targets.
#
# Be cautious about adding new options here, as plugins use this function by
# default. In most cases, you should add new options to specific targets instead
# of modifying this function.
function(APPLY_STANDARD_SETTINGS TARGET)
target_compile_features(${TARGET} PUBLIC cxx_std_17)
# /utf-8: 按 UTF-8 解析源码,否则 zh-CN 机器(代码页 936)对含中文注释的 runner
# 源码(如 win32_window.cpp)报 C4819,叠加 /WX 直接编译失败。
target_compile_options(${TARGET} PRIVATE /W4 /WX /wd"4100" /utf-8)
target_compile_options(${TARGET} PRIVATE /EHsc)
target_compile_definitions(${TARGET} PRIVATE "_HAS_EXCEPTIONS=0")
target_compile_definitions(${TARGET} PRIVATE "$<$<CONFIG:Debug>:_DEBUG>")
endfunction()
# Flutter library and tool build rules.
set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter")
add_subdirectory(${FLUTTER_MANAGED_DIR})
# Application build; see runner/CMakeLists.txt.
add_subdirectory("runner")
# Generated plugin build rules, which manage building the plugins and adding
# them to the application.
include(flutter/generated_plugins.cmake)
# === Installation ===
# Support files are copied into place next to the executable, so that it can
# run in place. This is done instead of making a separate bundle (as on Linux)
# so that building and running from within Visual Studio will work.
set(BUILD_BUNDLE_DIR "$<TARGET_FILE_DIR:${BINARY_NAME}>")
# Make the "install" step default, as it's required to run.
set(CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD 1)
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE)
endif()
set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data")
set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}")
install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}"
COMPONENT Runtime)
install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}"
COMPONENT Runtime)
install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
COMPONENT Runtime)
if(PLUGIN_BUNDLED_LIBRARIES)
install(FILES "${PLUGIN_BUNDLED_LIBRARIES}"
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
COMPONENT Runtime)
endif()
# ── Pangolin sing-box kernel ─────────────────────────────────────────────────
# Bundle sing-box.exe + wintun.dll NEXT TO the app exe so kernel_process.dart's
# exeDir lookup (_resolveBinaryPath) finds them at runtime; wintun.dll must sit
# beside sing-box.exe (sing-box loads it to create the TUN adapter). Built by
# app/kernel/fetch-desktop-bin.sh → app/kernel/dist/desktop/windows-<arch>/.
# Only needed for packaged/installed builds — a `flutter run` from the repo is
# already covered by _resolveBinaryPath walking up to the dist dir. If absent,
# warn (mirrors android build.gradle's placeholder) so a dev build still compiles.
file(GLOB PANGOLIN_KERNEL_DIRS "${CMAKE_SOURCE_DIR}/../../app/kernel/dist/desktop/windows-*")
set(PANGOLIN_KERNEL_FOUND FALSE)
foreach(kernel_dir ${PANGOLIN_KERNEL_DIRS})
if(EXISTS "${kernel_dir}/sing-box.exe")
set(PANGOLIN_KERNEL_FOUND TRUE)
install(FILES "${kernel_dir}/sing-box.exe" DESTINATION "${CMAKE_INSTALL_PREFIX}" COMPONENT Runtime)
if(EXISTS "${kernel_dir}/wintun.dll")
install(FILES "${kernel_dir}/wintun.dll" DESTINATION "${CMAKE_INSTALL_PREFIX}" COMPONENT Runtime)
else()
message(WARNING "pangolin: wintun.dll missing in ${kernel_dir} — TUN will fail to start")
endif()
endif()
endforeach()
if(NOT PANGOLIN_KERNEL_FOUND)
message(WARNING "pangolin: sing-box.exe not found under app/kernel/dist/desktop/windows-* — run 'app/kernel/fetch-desktop-bin.sh windows amd64' before packaging")
endif()
# Copy the native assets provided by the build.dart from all packages.
set(NATIVE_ASSETS_DIR "${PROJECT_BUILD_DIR}native_assets/windows/")
install(DIRECTORY "${NATIVE_ASSETS_DIR}"
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
COMPONENT Runtime)
# Fully re-copy the assets directory on each build to avoid having stale files
# from a previous install.
set(FLUTTER_ASSET_DIR_NAME "flutter_assets")
install(CODE "
file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\")
" COMPONENT Runtime)
install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}"
DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime)
# Install the AOT library on non-Debug builds only.
install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}"
CONFIGURATIONS Profile;Release
COMPONENT Runtime)