-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
140 lines (122 loc) · 5.29 KB
/
CMakeLists.txt
File metadata and controls
140 lines (122 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
cmake_minimum_required(VERSION 3.28)
find_program(CCACHE_PROGRAM ccache)
if (CCACHE_PROGRAM)
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
message(STATUS "Ccache found: ${CCACHE_PROGRAM}")
endif ()
add_compile_definitions(
$<$<CONFIG:Debug>:DEBUG>
$<$<CONFIG:Debug>:_DEBUG> # 对于 MSVC 兼容性
$<$<NOT:$<CONFIG:Debug>>:NDEBUG>
)
# Detect Android build: Gradle may set CMAKE_SYSTEM_NAME=Android or VCPKG_TARGET_ANDROID=ON
# before project() is called; the ANDROID variable is only set after project().
if(VCPKG_TARGET_ANDROID OR CMAKE_SYSTEM_NAME STREQUAL "Android")
set(VCPKG_TARGET_ANDROID ON)
# Gradle passes ANDROID_NDK as a CMake -D variable, propagate it to env
if(DEFINED ANDROID_NDK AND NOT DEFINED ENV{ANDROID_NDK_HOME})
set(ENV{ANDROID_NDK_HOME} "${ANDROID_NDK}")
elseif(DEFINED CMAKE_ANDROID_NDK AND NOT DEFINED ENV{ANDROID_NDK_HOME})
set(ENV{ANDROID_NDK_HOME} "${CMAKE_ANDROID_NDK}")
elseif(DEFINED ENV{ANDROID_NDK})
set(ENV{ANDROID_NDK_HOME} "$ENV{ANDROID_NDK}")
endif()
# If Gradle already set CMAKE_TOOLCHAIN_FILE to vcpkg and VCPKG_CHAINLOAD_TOOLCHAIN_FILE
# to the Android NDK toolchain, we only need to set VCPKG_TARGET_TRIPLET here.
# Otherwise, fall back to the vcpkg_android.cmake helper.
if(DEFINED VCPKG_CHAINLOAD_TOOLCHAIN_FILE)
# Gradle already configured the toolchain chain; just set the triplet
if(DEFINED VCPKG_ROOT AND NOT DEFINED ENV{VCPKG_ROOT})
set(ENV{VCPKG_ROOT} "${VCPKG_ROOT}")
endif()
if(ANDROID_ABI MATCHES "arm64-v8a")
set(VCPKG_TARGET_TRIPLET "arm64-android" CACHE STRING "" FORCE)
elseif(ANDROID_ABI MATCHES "armeabi-v7a")
set(VCPKG_TARGET_TRIPLET "arm-android" CACHE STRING "" FORCE)
elseif(ANDROID_ABI MATCHES "x86_64")
set(VCPKG_TARGET_TRIPLET "x64-android" CACHE STRING "" FORCE)
elseif(ANDROID_ABI MATCHES "x86")
set(VCPKG_TARGET_TRIPLET "x86-android" CACHE STRING "" FORCE)
endif()
message(STATUS "Android build with Gradle-managed toolchain: triplet=${VCPKG_TARGET_TRIPLET}")
else()
include(cmake/vcpkg_android.cmake)
endif()
else()
set(CMAKE_TOOLCHAIN_FILE $ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake)
endif()
set(APP_NAME krkr2)
project(${APP_NAME})
if(ANDROID)
add_compile_options(-Wno-inconsistent-missing-override)
endif()
option(ENABLE_TESTS "enable tests execute build(exclude android)" ON)
option(BUILD_TOOLS "build tools execute build(exclude android ios)" ON)
option(BUILD_ENGINE_API "build engine_api bridge shared library" ON)
set(CMAKE_C_STANDARD 17)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(KRKR2CORE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cpp/core)
set(KRKR2PLUGIN_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cpp/plugins)
if(MSVC)
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:/EHsc>" "/MP" "/utf-8")
add_link_options("/ignore:4099" "/INCREMENTAL" "/DEBUG:FASTLINK")
# set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
if(ANDROID)
if(BUILD_ENGINE_API)
# When BUILD_ENGINE_API is ON (Flutter build), krkr2_android.cpp is compiled
# directly into libengine_api.so, so we don't need a separate libkrkr2.so.
# Just skip building the standalone krkr2 target.
else()
add_library(${PROJECT_NAME} SHARED ${CMAKE_CURRENT_SOURCE_DIR}/platforms/android/cpp/krkr2_android.cpp)
find_package(unofficial-breakpad CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC unofficial::breakpad::libbreakpad_client)
endif()
elseif(LINUX)
add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/platforms/linux/main.cpp)
elseif(WINDOWS)
add_executable(${PROJECT_NAME}
${CMAKE_CURRENT_SOURCE_DIR}/platforms/windows/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/platforms/windows/game.rc
)
elseif(IOS)
# iOS uses Flutter as the app shell; only build engine libraries.
# engine_api is built as a static library and linked into the Flutter Runner.
elseif(MACOS)
# macOS uses Flutter as the app shell; only build engine libraries.
# The standalone krkr2 executable (cocos2dx) is no longer produced.
endif()
# external lib
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/cpp/external)
# build library
add_subdirectory(${KRKR2CORE_PATH})
add_subdirectory(${KRKR2PLUGIN_PATH})
if(BUILD_ENGINE_API)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/bridge/engine_api)
endif()
# Link the standalone executable target (not needed on macOS where Flutter is the app shell)
if(TARGET ${PROJECT_NAME})
target_link_libraries(${PROJECT_NAME} PUBLIC
krkr2plugin krkr2core
)
## app config
if(APPLE)
set_target_properties(${APP_NAME} PROPERTIES RESOURCE "${APP_UI_RES}")
if(MACOSX)
set_target_properties(${APP_NAME} PROPERTIES
MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/platforms/apple/macos/Info.plist"
)
endif()
endif()
endif()
if(ENABLE_TESTS AND NOT (IOS OR ANDROID))
enable_testing()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tests/CMakeLists.txt")
add_subdirectory(tests)
endif()
endif()
if(BUILD_TOOLS AND NOT (IOS OR ANDROID))
add_subdirectory(tools)
endif()