Skip to content

Commit 3af743f

Browse files
authored
Simplify project CMakeLists.txt by moving code sections to cmake modules (#2426)
* Simplify project CMakeLists.txt by moving code sections to cmake modules * More platform setup sections into one file * Reference new module * [skip ci] Add comment regarding the order of cmake module include statements * Ensure base path is correct for App.xaml when ax_setup_winrt_sources is called from a directory other than the root of the project
1 parent 69752f2 commit 3af743f

File tree

12 files changed

+299
-340
lines changed

12 files changed

+299
-340
lines changed

cmake/Modules/AXBuildHelpers.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ macro(ax_setup_winrt_sources )
629629
${_AX_ROOT}/core/platform/winrt/xaml/AxmolRenderer.cpp
630630
)
631631

632-
file(TO_NATIVE_PATH "${CMAKE_CURRENT_LIST_DIR}/proj.winrt/App.xaml" APP_XAML_FULL_PATH)
632+
file(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/proj.winrt/App.xaml" APP_XAML_FULL_PATH)
633633
set_property(
634634
SOURCE proj.winrt/App.h proj.winrt/App.cpp proj.winrt/App.idl
635635
PROPERTY VS_SETTINGS
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
if (IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/axmol")
2+
set(_AX_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/axmol")
3+
set(_is_axmol_embed TRUE)
4+
message(STATUS "Building isolated project: ${APP_NAME}")
5+
else()
6+
set(_AX_ROOT "$ENV{AX_ROOT}")
7+
if(NOT (_AX_ROOT STREQUAL ""))
8+
file(TO_CMAKE_PATH ${_AX_ROOT} _AX_ROOT)
9+
message(STATUS "Using system env var _AX_ROOT=${_AX_ROOT}")
10+
else()
11+
message(FATAL_ERROR "Please run setup.ps1 add system env var 'AX_ROOT' to specific the engine root")
12+
endif()
13+
endif()
14+
15+
list(APPEND CMAKE_MODULE_PATH
16+
${_AX_ROOT}/cmake/Modules/
17+
)
18+
19+
include(AXBuildSet)
20+
21+
if(NOT _is_axmol_embed)
22+
if ((WIN32 OR LINUX) AND DEFINED AX_PREBUILT_DIR AND IS_DIRECTORY ${_AX_ROOT}/${AX_PREBUILT_DIR})
23+
set(_AX_USE_PREBUILT TRUE)
24+
endif()
25+
endif()
26+
27+
if (NOT _AX_USE_PREBUILT)
28+
add_subdirectory(${_AX_ROOT}/core ${ENGINE_BINARY_PATH}/axmol/core)
29+
endif()
30+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
if ((NOT APPLE) AND (NOT WINRT))
2+
ax_get_resource_path(APP_RES_DIR ${APP_NAME})
3+
ax_sync_target_res(${APP_NAME} LINK_TO ${APP_RES_DIR} FOLDERS ${content_folder} SYM_LINK 1)
4+
5+
if((WINDOWS AND (NOT (CMAKE_GENERATOR MATCHES "Ninja"))))
6+
set_property(TARGET ${APP_NAME} PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${content_folder}")
7+
if(NOT DEFINED BUILD_ENGINE_DONE)
8+
set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT ${APP_NAME})
9+
endif()
10+
endif()
11+
elseif(WINRT)
12+
if(NOT DEFINED BUILD_ENGINE_DONE)
13+
set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT ${APP_NAME})
14+
endif()
15+
endif()
16+
17+
if (_AX_USE_PREBUILT) # support windows and linux
18+
use_ax_compile_define(${APP_NAME})
19+
20+
include(AXLinkHelpers)
21+
ax_link_cxx_prebuilt(${APP_NAME} ${_AX_ROOT} ${AX_PREBUILT_DIR})
22+
endif()
23+
24+
ax_setup_app_props(${APP_NAME})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
if(APPLE)
2+
set_target_properties(${APP_NAME} PROPERTIES RESOURCE "${APP_UI_RES}")
3+
set_xcode_property(${APP_NAME} INSTALL_PATH "\$(LOCAL_APPS_DIR)")
4+
set_xcode_property(${APP_NAME} PRODUCT_BUNDLE_IDENTIFIER "dev.axmol.dummy")
5+
6+
if(MACOSX)
7+
set_target_properties(${APP_NAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/proj.ios_mac/mac/Info.plist")
8+
elseif(TVOS)
9+
set_target_properties(${APP_NAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/proj.ios_mac/ios/targets/tvos/Info.plist")
10+
set_xcode_property(${APP_NAME} ASSETCATALOG_COMPILER_APPICON_NAME "Brand Assets")
11+
elseif(IOS)
12+
set_target_properties(${APP_NAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/proj.ios_mac/ios/targets/ios/Info.plist")
13+
set_xcode_property(${APP_NAME} ASSETCATALOG_COMPILER_APPICON_NAME "AppIcon")
14+
set_xcode_property(${APP_NAME} TARGETED_DEVICE_FAMILY "1,2")
15+
endif()
16+
17+
# For code-signing, set the DEVELOPMENT_TEAM:
18+
#set_xcode_property(${APP_NAME} DEVELOPMENT_TEAM "GRLXXXX2K9")
19+
elseif(WINDOWS)
20+
# config quick starter batch script run.bat for windows
21+
if(WIN32)
22+
file(RELATIVE_PATH CMAKE_BUILD_RELATIVE_DIR "${CMAKE_CURRENT_SOURCE_DIR}" "${PROJECT_BINARY_DIR}")
23+
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/run.bat.in" "${CMAKE_CURRENT_SOURCE_DIR}/run.bat" @ONLY)
24+
endif()
25+
26+
if(NOT _AX_USE_PREBUILT)
27+
ax_sync_target_dlls(${APP_NAME})
28+
endif()
29+
endif()

templates/cpp/CMakeLists.txt

Lines changed: 17 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -33,209 +33,57 @@ if(XCODE)
3333
set(CMAKE_XCODE_GENERATE_TOP_LEVEL_PROJECT_ONLY TRUE)
3434
endif()
3535

36-
# config quick starter batch script run.bat for windows
37-
if(WIN32)
38-
file(RELATIVE_PATH CMAKE_BUILD_RELATIVE_DIR "${CMAKE_CURRENT_SOURCE_DIR}" "${PROJECT_BINARY_DIR}")
39-
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/run.bat.in" "${CMAKE_CURRENT_SOURCE_DIR}/run.bat" @ONLY)
40-
endif()
41-
4236
set(_is_axmol_embed FALSE)
43-
if (IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/axmol")
44-
set(_AX_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/axmol")
45-
set(_is_axmol_embed TRUE)
46-
message(STATUS "Building isolated project: ${APP_NAME}")
47-
else()
48-
set(_AX_ROOT "$ENV{AX_ROOT}")
49-
if(NOT (_AX_ROOT STREQUAL ""))
50-
file(TO_CMAKE_PATH ${_AX_ROOT} _AX_ROOT)
51-
message(STATUS "Using system env var _AX_ROOT=${_AX_ROOT}")
52-
else()
53-
message(FATAL_ERROR "Please run setup.ps1 add system env var 'AX_ROOT' to specific the engine root")
54-
endif()
55-
endif()
56-
57-
set(CMAKE_MODULE_PATH ${_AX_ROOT}/cmake/Modules/)
58-
59-
include(AXBuildSet)
60-
6137
set(_AX_USE_PREBUILT FALSE)
6238

63-
if(NOT _is_axmol_embed)
64-
if ((WIN32 OR LINUX) AND DEFINED AX_PREBUILT_DIR AND IS_DIRECTORY ${_AX_ROOT}/${AX_PREBUILT_DIR})
65-
set(_AX_USE_PREBUILT TRUE)
66-
endif()
67-
endif()
39+
set(CMAKE_MODULE_PATH
40+
${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules
41+
)
6842

69-
if (NOT _AX_USE_PREBUILT)
70-
add_subdirectory(${_AX_ROOT}/core ${ENGINE_BINARY_PATH}/axmol/core)
71-
endif()
43+
# NOTE: The order of the cmake module "include(AXGame...)" statements matters
44+
include(AXGameEngineSetup)
7245

7346
# The common cross-platforms source files and header files
7447
file(GLOB_RECURSE GAME_HEADER
7548
Source/*.h Source/*.hpp
76-
)
49+
)
50+
7751
file(GLOB_RECURSE GAME_SOURCE
7852
Source/*.cpp Source/*.c
79-
)
53+
)
8054

8155
set(GAME_INC_DIRS
82-
"${CMAKE_CURRENT_SOURCE_DIR}/Source"
56+
"${CMAKE_CURRENT_SOURCE_DIR}/Source"
8357
)
8458

8559
set(content_folder
8660
"${CMAKE_CURRENT_SOURCE_DIR}/Content"
87-
)
61+
)
8862

8963
if(APPLE)
9064
ax_mark_multi_resources(common_content_files RES_TO "Resources" FOLDERS ${content_folder})
9165
elseif(WINDOWS)
9266
ax_mark_multi_resources(common_content_files RES_TO "Content" FOLDERS ${content_folder})
9367
endif()
9468

95-
if(ANDROID)
96-
# the APP_NAME should match on AndroidManifest.xml
97-
list(APPEND GAME_SOURCE
98-
proj.android/app/jni/main.cpp
99-
)
100-
elseif(LINUX)
101-
list(APPEND GAME_SOURCE
102-
proj.linux/main.cpp
103-
)
104-
list(APPEND GAME_SOURCE ${common_content_files})
105-
elseif(WASM)
106-
list(APPEND GAME_SOURCE
107-
proj.wasm/main.cpp
108-
)
109-
list(APPEND GAME_SOURCE ${common_content_files})
110-
elseif(WINDOWS)
111-
if(NOT WINRT)
112-
list(APPEND GAME_HEADER
113-
proj.win32/main.h
114-
proj.win32/resource.h
115-
)
116-
list(APPEND GAME_SOURCE
117-
proj.win32/main.cpp
118-
proj.win32/game.rc
119-
)
120-
else()
121-
ax_setup_winrt_sources()
122-
endif()
123-
list(APPEND GAME_SOURCE ${common_content_files})
124-
elseif(APPLE)
125-
if(IOS)
126-
list(APPEND GAME_HEADER
127-
proj.ios_mac/ios/AppController.h
128-
proj.ios_mac/ios/RootViewController.h
129-
)
130-
131-
if (TVOS)
132-
set(APP_UI_RES
133-
proj.ios_mac/ios/targets/tvos/LaunchScreenBackground.png
134-
proj.ios_mac/ios/targets/tvos/LaunchScreen.storyboard
135-
proj.ios_mac/ios/targets/tvos/Images.xcassets
136-
)
137-
else()
138-
set(APP_UI_RES
139-
proj.ios_mac/ios/targets/ios/LaunchScreenBackground.png
140-
proj.ios_mac/ios/targets/ios/LaunchScreen.storyboard
141-
proj.ios_mac/ios/targets/ios/Images.xcassets
142-
)
143-
endif()
144-
145-
list(APPEND GAME_SOURCE
146-
proj.ios_mac/ios/main.m
147-
proj.ios_mac/ios/AppController.mm
148-
proj.ios_mac/ios/RootViewController.mm
149-
proj.ios_mac/ios/Prefix.pch
150-
${APP_UI_RES}
151-
)
152-
elseif(MACOSX)
153-
set(APP_UI_RES
154-
proj.ios_mac/mac/Icon.icns
155-
)
156-
list(APPEND GAME_SOURCE
157-
proj.ios_mac/mac/main.cpp
158-
proj.ios_mac/mac/Prefix.pch
159-
${APP_UI_RES}
160-
)
161-
endif()
162-
list(APPEND GAME_SOURCE ${common_content_files})
163-
endif()
69+
include(AXGameSourceSetup)
16470

16571
# mark app complie info and libs info
16672
set(APP_SOURCES
16773
${GAME_HEADER}
16874
${GAME_SOURCE}
16975
)
170-
if(NOT ANDROID)
171-
add_executable(${APP_NAME} ${APP_SOURCES})
172-
else()
173-
add_library(${APP_NAME} SHARED ${APP_SOURCES})
174-
# whole archive for jni
175-
target_link_libraries(${APP_NAME} -Wl,--whole-archive cpp_android_spec -Wl,--no-whole-archive)
176-
config_android_shared_libs("dev.axmol.lib" "${CMAKE_CURRENT_SOURCE_DIR}/proj.android/app/src")
177-
endif()
178-
179-
if (NOT _AX_USE_PREBUILT)
180-
target_link_libraries(${APP_NAME} ${_AX_CORE_LIB})
181-
endif()
182-
183-
# The optional thirdparties(not dependent by engine)
184-
if (AX_WITH_YAML_CPP)
185-
list(APPEND GAME_INC_DIRS "${_AX_ROOT}/3rdparty/yaml-cpp/include")
186-
endif()
187-
188-
target_include_directories(${APP_NAME} PRIVATE ${GAME_INC_DIRS})
18976

77+
include(AXGameTargetSetup)
19078

19179
# mark app resources, resource will be copy auto after mark
19280
ax_setup_app_config(${APP_NAME})
19381

194-
if(APPLE)
195-
set_target_properties(${APP_NAME} PROPERTIES RESOURCE "${APP_UI_RES}")
196-
set_xcode_property(${APP_NAME} INSTALL_PATH "\$(LOCAL_APPS_DIR)")
197-
set_xcode_property(${APP_NAME} PRODUCT_BUNDLE_IDENTIFIER "dev.axmol.dummy")
198-
199-
if(MACOSX)
200-
set_target_properties(${APP_NAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/proj.ios_mac/mac/Info.plist")
201-
elseif(TVOS)
202-
set_target_properties(${APP_NAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/proj.ios_mac/ios/targets/tvos/Info.plist")
203-
set_xcode_property(${APP_NAME} ASSETCATALOG_COMPILER_APPICON_NAME "Brand Assets")
204-
elseif(IOS)
205-
set_target_properties(${APP_NAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/proj.ios_mac/ios/targets/ios/Info.plist")
206-
set_xcode_property(${APP_NAME} ASSETCATALOG_COMPILER_APPICON_NAME "AppIcon")
207-
set_xcode_property(${APP_NAME} TARGETED_DEVICE_FAMILY "1,2")
208-
endif()
209-
210-
# For code-signing, set the DEVELOPMENT_TEAM:
211-
#set_xcode_property(${APP_NAME} DEVELOPMENT_TEAM "GRLXXXX2K9")
212-
elseif(WINDOWS)
213-
if(NOT _AX_USE_PREBUILT)
214-
ax_sync_target_dlls(${APP_NAME})
215-
endif()
216-
endif()
82+
# Add any libraries you need to link to the project after this point
21783

218-
if ((NOT APPLE) AND (NOT WINRT))
219-
ax_get_resource_path(APP_RES_DIR ${APP_NAME})
220-
ax_sync_target_res(${APP_NAME} LINK_TO ${APP_RES_DIR} FOLDERS ${content_folder} SYM_LINK 1)
221-
222-
if((WINDOWS AND (NOT (CMAKE_GENERATOR MATCHES "Ninja"))))
223-
set_property(TARGET ${APP_NAME} PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${content_folder}")
224-
if(NOT DEFINED BUILD_ENGINE_DONE)
225-
set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT ${APP_NAME})
226-
endif()
227-
endif()
228-
elseif(WINRT)
229-
if(NOT DEFINED BUILD_ENGINE_DONE)
230-
set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT ${APP_NAME})
231-
endif()
232-
endif()
233-
234-
if (_AX_USE_PREBUILT) # support windows and linux
235-
use_ax_compile_define(${APP_NAME})
23684

237-
include(AXLinkHelpers)
238-
ax_link_cxx_prebuilt(${APP_NAME} ${_AX_ROOT} ${AX_PREBUILT_DIR})
239-
endif()
85+
# Default Platform-specific setup
86+
include(AXGamePlatformSetup)
24087

241-
ax_setup_app_props(${APP_NAME})
88+
# Make sure the AXGameFinalSetup is included at the end of this file
89+
include(AXGameFinalSetup)

templates/cpp/axproj-template.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"to": "${packageName}",
3737
"files": [
3838
"proj.android/app/build.gradle",
39+
"cmake/modules/AXGamePlatformSetup.cmake",
3940
"CMakeLists.txt"
4041
]
4142
},
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
if(ANDROID)
2+
# the APP_NAME should match on AndroidManifest.xml
3+
list(APPEND GAME_SOURCE
4+
proj.android/app/jni/main.cpp
5+
)
6+
elseif(LINUX)
7+
list(APPEND GAME_SOURCE
8+
proj.linux/main.cpp
9+
)
10+
list(APPEND GAME_SOURCE ${common_content_files})
11+
elseif(WASM)
12+
list(APPEND GAME_SOURCE
13+
proj.wasm/main.cpp
14+
)
15+
list(APPEND GAME_SOURCE ${common_content_files})
16+
elseif(WINDOWS)
17+
if(NOT WINRT)
18+
list(APPEND GAME_HEADER
19+
proj.win32/main.h
20+
proj.win32/resource.h
21+
)
22+
list(APPEND GAME_SOURCE
23+
proj.win32/main.cpp
24+
proj.win32/game.rc
25+
)
26+
else()
27+
ax_setup_winrt_sources()
28+
endif()
29+
list(APPEND GAME_SOURCE ${common_content_files})
30+
elseif(APPLE)
31+
if(IOS)
32+
list(APPEND GAME_HEADER
33+
proj.ios_mac/ios/AppController.h
34+
proj.ios_mac/ios/RootViewController.h
35+
)
36+
37+
if (TVOS)
38+
set(APP_UI_RES
39+
proj.ios_mac/ios/targets/tvos/LaunchScreenBackground.png
40+
proj.ios_mac/ios/targets/tvos/LaunchScreen.storyboard
41+
proj.ios_mac/ios/targets/tvos/Images.xcassets
42+
)
43+
else()
44+
set(APP_UI_RES
45+
proj.ios_mac/ios/targets/ios/LaunchScreenBackground.png
46+
proj.ios_mac/ios/targets/ios/LaunchScreen.storyboard
47+
proj.ios_mac/ios/targets/ios/Images.xcassets
48+
)
49+
endif()
50+
51+
list(APPEND GAME_SOURCE
52+
proj.ios_mac/ios/main.m
53+
proj.ios_mac/ios/AppController.mm
54+
proj.ios_mac/ios/RootViewController.mm
55+
proj.ios_mac/ios/Prefix.pch
56+
${APP_UI_RES}
57+
)
58+
elseif(MACOSX)
59+
set(APP_UI_RES
60+
proj.ios_mac/mac/Icon.icns
61+
)
62+
list(APPEND GAME_SOURCE
63+
proj.ios_mac/mac/main.cpp
64+
proj.ios_mac/mac/Prefix.pch
65+
${APP_UI_RES}
66+
)
67+
endif()
68+
list(APPEND GAME_SOURCE ${common_content_files})
69+
endif()

0 commit comments

Comments
 (0)