Skip to content

Commit 2718b80

Browse files
drelaptophuangwei1024
authored andcommitted
refine cmake, allow developer switch Debug/Release in IDE (cocos2d#19329)
* refine cmake, support multi configuration when generate Xcode/VS * docs update * cmake, update multi config condition * remove custom target property: CC_DEPEND_DLLS * cmake, adapt res copy to multi config types * move "APP_RES_DIR" up, and improve setup_cocos_app_config * move repeat comments into docs * notify user the copy file process * target_link_options isn't supported until cmake 3.13 * cmake readme update
1 parent 28d7a75 commit 2718b80

File tree

14 files changed

+105
-109
lines changed

14 files changed

+105
-109
lines changed

cmake/Modules/CocosBuildHelpers.cmake

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
include(CMakeParseArguments)
22

3-
# copy resource `FILES` and `FOLDERS` to `COPY_TO` folder
4-
function(cocos_copy_res)
3+
# copy resource `FILES` and `FOLDERS` to TARGET_FILE_DIR/Resources
4+
function(cocos_copy_target_res cocos_target)
55
set(oneValueArgs COPY_TO)
66
set(multiValueArgs FILES FOLDERS)
77
cmake_parse_arguments(opt "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
88
# copy files
99
foreach(cc_file ${opt_FILES})
1010
get_filename_component(file_name ${cc_file} NAME)
11-
configure_file(${cc_file} "${opt_COPY_TO}/${file_name}" COPYONLY)
11+
add_custom_command(TARGET ${cocos_target} POST_BUILD
12+
COMMAND ${CMAKE_COMMAND} -E echo "copy file into Resources: ${file_name} ..."
13+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${cc_file} "${opt_COPY_TO}/${file_name}"
14+
)
1215
endforeach()
1316
# copy folders files
1417
foreach(cc_folder ${opt_FOLDERS})
@@ -17,7 +20,10 @@ function(cocos_copy_res)
1720
foreach(res_file ${folder_files})
1821
get_filename_component(res_file_abs_path ${res_file} ABSOLUTE)
1922
file(RELATIVE_PATH res_file_relat_path ${folder_abs_path} ${res_file_abs_path})
20-
configure_file(${res_file} "${opt_COPY_TO}/${res_file_relat_path}" COPYONLY)
23+
add_custom_command(TARGET ${cocos_target} POST_BUILD
24+
COMMAND ${CMAKE_COMMAND} -E echo "copy file into Resources: ${res_file_relat_path} ..."
25+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${res_file} "${opt_COPY_TO}/${res_file_relat_path}"
26+
)
2127
endforeach()
2228
endforeach()
2329
endfunction()
@@ -79,8 +85,9 @@ function(get_target_depends_ext_dlls cocos_target all_depend_dlls_out)
7985
search_depend_libs_recursive(${cocos_target} depend_libs)
8086
foreach(depend_lib ${depend_libs})
8187
if(TARGET ${depend_lib})
82-
get_target_property(tmp_dlls ${depend_lib} CC_DEPEND_DLLS)
83-
if(tmp_dlls)
88+
get_target_property(found_shared_lib ${depend_lib} IMPORTED_IMPLIB)
89+
if(found_shared_lib)
90+
get_target_property(tmp_dlls ${depend_lib} IMPORTED_LOCATION)
8491
list(APPEND all_depend_ext_dlls ${tmp_dlls})
8592
endif()
8693
endif()
@@ -89,20 +96,19 @@ function(get_target_depends_ext_dlls cocos_target all_depend_dlls_out)
8996
set(${all_depend_dlls_out} ${all_depend_ext_dlls} PARENT_SCOPE)
9097
endfunction()
9198

92-
# copy the `cocos_target` needed dlls into `COPY_TO` folder
99+
# copy the `cocos_target` needed dlls into TARGET_FILE_DIR
93100
function(cocos_copy_target_dll cocos_target)
94-
set(oneValueArgs COPY_TO)
95-
cmake_parse_arguments(opt "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
96101
get_target_depends_ext_dlls(${cocos_target} all_depend_dlls)
97102
# remove repeat items
98103
if(all_depend_dlls)
99104
list(REMOVE_DUPLICATES all_depend_dlls)
100105
endif()
101-
# todo, add a option to enable/disable debug print
102-
message(STATUS "prepare to copy external dlls for ${cocos_target}:${all_depend_dlls}")
103106
foreach(cc_dll_file ${all_depend_dlls})
104107
get_filename_component(cc_dll_name ${cc_dll_file} NAME)
105-
configure_file(${cc_dll_file} "${opt_COPY_TO}/${cc_dll_name}" COPYONLY)
108+
add_custom_command(TARGET ${cocos_target} POST_BUILD
109+
COMMAND ${CMAKE_COMMAND} -E echo "copy dll into target file dir: ${cc_dll_name} ..."
110+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${cc_dll_file} "$<TARGET_FILE_DIR:${cocos_target}>/${cc_dll_name}"
111+
)
106112
endforeach()
107113
endfunction()
108114

@@ -168,31 +174,21 @@ function(source_group_single_file single_file)
168174
source_group("${ide_file_group}" FILES ${single_file})
169175
endfunction()
170176

171-
# setup a cocos application, include "APP_BIN_DIR", "APP_RES_DIR" config
177+
# setup a cocos application
172178
function(setup_cocos_app_config app_name)
173-
# set target PROPERTIES, depend different platforms
179+
# put all output app into bin/${app_name}
180+
set_target_properties(${app_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/${app_name}")
174181
if(APPLE)
175-
set(APP_BIN_DIR "${CMAKE_BINARY_DIR}/bin")
176-
set_target_properties(${app_name} PROPERTIES MACOSX_BUNDLE 1
177-
)
182+
# output macOS/iOS .app
183+
set_target_properties(${app_name} PROPERTIES MACOSX_BUNDLE 1)
178184
elseif(MSVC)
179-
# only Debug and Release mode was supported when using MSVC.
180-
set(APP_BIN_DIR "${CMAKE_BINARY_DIR}/bin/${APP_NAME}/$<CONFIG>")
181-
set(APP_RES_DIR "${CMAKE_BINARY_DIR}/bin/${APP_NAME}/${CMAKE_BUILD_TYPE}/Resources")
182-
#Visual Studio Defaults to wrong type
183-
set_target_properties(${app_name} PROPERTIES LINK_FLAGS "/SUBSYSTEM:WINDOWS")
184-
else(LINUX)
185-
set(APP_BIN_DIR "${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}/${APP_NAME}")
186-
set(APP_RES_DIR "${APP_BIN_DIR}/Resources")
185+
# visual studio default is Console app, but we need Windows app
186+
set_property(TARGET ${app_name} APPEND PROPERTY LINK_FLAGS "/SUBSYSTEM:WINDOWS")
187187
endif()
188-
set_target_properties(${app_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${APP_BIN_DIR}")
189-
190188
# auto mark code files for IDE when mark app
191189
if(XCODE OR VS)
192-
cocos_mark_code_files(${APP_NAME})
190+
cocos_mark_code_files(${app_name})
193191
endif()
194-
195-
set(APP_RES_DIR ${APP_RES_DIR} PARENT_SCOPE)
196192
endfunction()
197193

198194
# if cc_variable not set, then set it cc_value

cmake/Modules/CocosConfigDefine.cmake

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,35 @@
2525
set(MACOSX TRUE)
2626
set(PLATFORM_FOLDER mac)
2727
endif()
28-
else()
28+
else()
2929
message(FATAL_ERROR "Unsupported platform, CMake will exit")
3030
return()
3131
endif()
3232

33-
# build mode, Debug is default value
34-
if(NOT CMAKE_BUILD_TYPE)
35-
if(DEBUG_MODE)
36-
set(CMAKE_BUILD_TYPE Debug)
37-
else()
38-
set(CMAKE_BUILD_TYPE Release)
39-
endif()
40-
endif()
33+
# generators that are capable of organizing into a hierarchy of folders
34+
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
35+
# simplify generator condition, please use them everywhere
36+
if(CMAKE_GENERATOR STREQUAL Xcode)
37+
set(XCODE TRUE)
38+
elseif(CMAKE_GENERATOR MATCHES Visual)
39+
set(VS TRUE)
40+
endif()
41+
message(STATUS "CMAKE_GENERATOR: ${CMAKE_GENERATOR}")
4142

42-
if(CMAKE_GENERATOR)
43-
# generators that are capable of organizing into a hierarchy of folders
44-
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
45-
# simplify generator condition judgement
46-
if(CMAKE_GENERATOR STREQUAL Xcode)
47-
set(XCODE TRUE)
48-
elseif(CMAKE_GENERATOR MATCHES Visual)
49-
set(VS TRUE)
43+
if(CMAKE_CONFIGURATION_TYPES)
44+
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Reset the configurations to what we need" FORCE)
45+
message(STATUS "CMAKE_CONFIGURATION_TYPES: ${CMAKE_CONFIGURATION_TYPES}")
46+
else()
47+
if(NOT CMAKE_BUILD_TYPE)
48+
if(DEBUG_MODE) # build mode, Debug is default value
49+
set(CMAKE_BUILD_TYPE Debug)
50+
else()
51+
set(CMAKE_BUILD_TYPE Release)
52+
endif()
5053
endif()
51-
# make configurations type keep same to cmake build type.
52-
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_BUILD_TYPE}" CACHE STRING "Reset the configurations to what we need" FORCE)
53-
endif()
54-
message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
55-
54+
message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
55+
endif()
5656

57-
# custom target property for dll collect
58-
define_property(TARGET
59-
PROPERTY CC_DEPEND_DLLS
60-
BRIEF_DOCS "depend dlls of a target"
61-
FULL_DOCS "use to save depend dlls of a target"
62-
)
6357
# custom target property for lua/js link
6458
define_property(TARGET
6559
PROPERTY CC_JS_DEPEND

cmake/README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ CMake is an open-source, cross-platform family of tools designed to build, test
88
```sh
99
cmake --version
1010
```
11-
if the CMake version is lower than 3.1, please upgrade.
11+
if the CMake version is lower than 3.6, please upgrade.
1212

1313
2. You should use __out-of-source__ builds, this means you need to create a different directory than __cocos2d-x__ to execute the `cmake` command.
1414

@@ -33,7 +33,14 @@ mkdir win32-build && cd win32-build
3333
cmake .. -G"Visual Studio 15 2017" -Tv141
3434
```
3535

36-
Execute `cmake --build .` to compile, or open __Cocos2d-x.sln__ in Explorer to use the generated project.
36+
Execute `cmake --build .` to compile,
37+
```
38+
cmake --build . --config Debug
39+
cmake --build . --config Release
40+
```
41+
or open __Cocos2d-x.sln__ in Explorer to use the generated project.
42+
43+
If can't found `MSVCR110.dll` issue occurs to you, please install this [Visual C++ Runtime Libraries](https://www.microsoft.com/en-us/download/details.aspx?id=30679), when runing the cpp-tests project
3744

3845
### Generate macOS Project
3946

@@ -53,7 +60,9 @@ cmake .. -GXcode -DCMAKE_TOOLCHAIN_FILE=../cmake/ios.toolchain.cmake
5360
open Cocos2d-x.xcodeproj
5461
```
5562

56-
The default build is for running on actual iOS hardware, if you want to run in the simulator, please add `-DIOS_PLATFORM=SIMULATOR` for architecture i386 or `-DIOS_PLATFORM=SIMULATOR64` for x86_64.
63+
The default build is for running on iOS device, if you want to run in the simulator, please add `-DIOS_PLATFORM=SIMULATOR` for architecture i386 or `-DIOS_PLATFORM=SIMULATOR64` for x86_64, but remember you can't run metal-support app in simulator because Apple limitation.
64+
65+
if you want to sign iOS app in CMake, you will need to fill development team ID into `set_xcode_property(${APP_NAME} DEVELOPMENT_TEAM "")`, or select to sign in Xcode after project files generated.
5766

5867
### Android Studio
5968

@@ -91,6 +100,11 @@ If you want to add cmake build arguments, please add it at [external Native Buil
91100

92101
* `cmake --build ./msvc_build`, cmake will sellect corresponding build tools.
93102

103+
## Tips
104+
105+
1. Use `cmake ..` to refersh resources and code files, after you modify `Resources` or `CMakeLists.txt`.
106+
1. Don't need `CMAKE_BUILD_TYPE` options when `-G` Xcode or Visual Studio, CMake scripts will generate both configurations, so you can switch `Debug` and `Release` in IDE.
107+
94108
## Useful Links
95109

96110
* CMake Official website: [cmake.org](https://cmake.org/)

cocos/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ if(XCODE OR VS)
162162
cocos_mark_code_files("cocos2d")
163163
endif()
164164

165-
if(WIN32)
165+
if(WINDOWS)
166166
# precompiled header. Compilation time speedup ~4x.
167167
target_sources(cocos2d PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/precheader.cpp")
168168
set_target_properties(cocos2d PROPERTIES COMPILE_FLAGS "/Yuprecheader.h /FIprecheader.h")

cocos/scripting/js-bindings/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ if(XCODE OR VS)
180180
cocos_mark_code_files("jscocos2d")
181181
endif()
182182

183-
if(WIN32)
183+
if(WINDOWS)
184184
# precompiled header
185185
target_sources(jscocos2d PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/precheader.cpp")
186186
set_target_properties(jscocos2d PROPERTIES COMPILE_FLAGS "/Yuprecheader.h /FIprecheader.h")

external/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "v3-deps-156",
2+
"version": "v3-deps-157",
33
"zip_file_size": "141265643",
44
"repo_name": "cocos2d-x-3rd-party-libs-bin",
55
"repo_parent": "https://github.com/cocos2d/",

templates/cpp-template-default/CMakeLists.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,14 @@ if(APPLE)
138138
elseif(IOS)
139139
cocos_pak_xcode(${APP_NAME} INFO_PLIST "iOSBundleInfo.plist.in")
140140
set_xcode_property(${APP_NAME} ASSETCATALOG_COMPILER_APPICON_NAME "AppIcon")
141-
# A way to find your own DEVELOPMENT_TEAM value:
142-
# https://github.com/cocos2d/cocos2d-x/issues/19319#issuecomment-455931410
143141
set_xcode_property(${APP_NAME} DEVELOPMENT_TEAM "")
144142
set_xcode_property(${APP_NAME} CODE_SIGN_IDENTITY "iPhone Developer")
145143
endif()
146144
elseif(WINDOWS)
147-
cocos_copy_target_dll(${APP_NAME} COPY_TO ${APP_RES_DIR}/..)
145+
cocos_copy_target_dll(${APP_NAME})
148146
endif()
149147

150148
if(LINUX OR WINDOWS)
151-
cocos_copy_res(COPY_TO ${APP_RES_DIR} FOLDERS ${GAME_RES_FOLDER})
149+
set(APP_RES_DIR "$<TARGET_FILE_DIR:${APP_NAME}>/Resources")
150+
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR} FOLDERS ${GAME_RES_FOLDER})
152151
endif()

templates/js-template-default/CMakeLists.txt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,18 @@ if(APPLE)
137137
elseif(IOS)
138138
cocos_pak_xcode(${APP_NAME} INFO_PLIST "iOSBundleInfo.plist.in")
139139
set_xcode_property(${APP_NAME} ASSETCATALOG_COMPILER_APPICON_NAME "AppIcon")
140-
# A way to find your own DEVELOPMENT_TEAM value:
141-
# https://github.com/cocos2d/cocos2d-x/issues/19319#issuecomment-455931410
142140
set_xcode_property(${APP_NAME} DEVELOPMENT_TEAM "")
143141
set_xcode_property(${APP_NAME} CODE_SIGN_IDENTITY "iPhone Developer")
144142
endif()
145143
elseif(WINDOWS)
146-
cocos_copy_target_dll(${APP_NAME} COPY_TO ${APP_RES_DIR}/..)
144+
cocos_copy_target_dll(${APP_NAME})
147145
endif()
148146
# copy resource on linux or WINDOWS
149147
if(LINUX OR WINDOWS)
150-
cocos_copy_res(COPY_TO ${APP_RES_DIR} FILES ${res_main_files})
151-
cocos_copy_res(COPY_TO ${APP_RES_DIR}/res FOLDERS ${res_res_folders})
152-
cocos_copy_res(COPY_TO ${APP_RES_DIR}/src FOLDERS ${res_src_folders})
153-
cocos_copy_res(COPY_TO ${APP_RES_DIR}/script FOLDERS ${res_script_folders})
148+
set(APP_RES_DIR "$<TARGET_FILE_DIR:${APP_NAME}>/Resources")
149+
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR} FILES ${res_main_files})
150+
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR}/res FOLDERS ${res_res_folders})
151+
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR}/src FOLDERS ${res_src_folders})
152+
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR}/script FOLDERS ${res_script_folders})
154153
endif()
155154

templates/lua-template-default/CMakeLists.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,16 @@ if(APPLE)
160160
elseif(IOS)
161161
cocos_pak_xcode(${APP_NAME} INFO_PLIST "iOSBundleInfo.plist.in")
162162
set_xcode_property(${APP_NAME} ASSETCATALOG_COMPILER_APPICON_NAME "AppIcon")
163-
# A way to find your own DEVELOPMENT_TEAM value:
164-
# https://github.com/cocos2d/cocos2d-x/issues/19319#issuecomment-455931410
165163
set_xcode_property(${APP_NAME} DEVELOPMENT_TEAM "")
166164
set_xcode_property(${APP_NAME} CODE_SIGN_IDENTITY "iPhone Developer")
167165
endif()
168166
elseif(WINDOWS)
169-
cocos_copy_target_dll(${APP_NAME} COPY_TO ${APP_RES_DIR}/..)
167+
cocos_copy_target_dll(${APP_NAME})
170168
endif()
171169
# copy resource on linux or WINDOWS
172170
if(LINUX OR WINDOWS)
173-
cocos_copy_res(COPY_TO ${APP_RES_DIR}/res FOLDERS ${res_res_folders})
174-
cocos_copy_res(COPY_TO ${APP_RES_DIR}/src FOLDERS ${res_src_folders})
175-
cocos_copy_res(COPY_TO ${APP_RES_DIR}/src/cocos FOLDERS ${res_script_folders})
171+
set(APP_RES_DIR "$<TARGET_FILE_DIR:${APP_NAME}>/Resources")
172+
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR}/res FOLDERS ${res_res_folders})
173+
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR}/src FOLDERS ${res_src_folders})
174+
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR}/src/cocos FOLDERS ${res_script_folders})
176175
endif()

tests/cpp-empty-test/CMakeLists.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,14 @@ if(APPLE)
136136
cocos_pak_xcode(${APP_NAME} INFO_PLIST "iOSBundleInfo.plist.in")
137137
set_xcode_property(${APP_NAME} ASSETCATALOG_COMPILER_APPICON_NAME "AppIcon-${APP_NAME}")
138138
set_xcode_property(${APP_NAME} CODE_SIGN_IDENTITY "iPhone Developer")
139-
# A way to find your own DEVELOPMENT_TEAM value:
140-
# https://github.com/cocos2d/cocos2d-x/issues/19319#issuecomment-455931410
141139
set_xcode_property(${APP_NAME} DEVELOPMENT_TEAM "")
142140
set_xcode_property(${APP_NAME} CODE_SIGN_IDENTITY "iPhone Developer")
143141
endif()
144142
elseif(WINDOWS)
145-
cocos_copy_target_dll(${APP_NAME} COPY_TO ${APP_RES_DIR}/..)
143+
cocos_copy_target_dll(${APP_NAME})
146144
endif()
147145

148146
if(LINUX OR WINDOWS)
149-
cocos_copy_res(COPY_TO ${APP_RES_DIR} FOLDERS ${GAME_RES_FOLDER})
147+
set(APP_RES_DIR "$<TARGET_FILE_DIR:${APP_NAME}>/Resources")
148+
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR} FOLDERS ${GAME_RES_FOLDER})
150149
endif()

0 commit comments

Comments
 (0)