Skip to content
Merged
56 changes: 26 additions & 30 deletions cmake/Modules/CocosBuildHelpers.cmake
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
include(CMakeParseArguments)

# copy resource `FILES` and `FOLDERS` to `COPY_TO` folder
function(cocos_copy_res)
# copy resource `FILES` and `FOLDERS` to TARGET_FILE_DIR/Resources
function(cocos_copy_target_res cocos_target)
set(oneValueArgs COPY_TO)
set(multiValueArgs FILES FOLDERS)
cmake_parse_arguments(opt "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
# copy files
foreach(cc_file ${opt_FILES})
get_filename_component(file_name ${cc_file} NAME)
configure_file(${cc_file} "${opt_COPY_TO}/${file_name}" COPYONLY)
add_custom_command(TARGET ${cocos_target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "copy file into Resources: ${file_name} ..."
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${cc_file} "${opt_COPY_TO}/${file_name}"
)
endforeach()
# copy folders files
foreach(cc_folder ${opt_FOLDERS})
Expand All @@ -17,7 +20,10 @@ function(cocos_copy_res)
foreach(res_file ${folder_files})
get_filename_component(res_file_abs_path ${res_file} ABSOLUTE)
file(RELATIVE_PATH res_file_relat_path ${folder_abs_path} ${res_file_abs_path})
configure_file(${res_file} "${opt_COPY_TO}/${res_file_relat_path}" COPYONLY)
add_custom_command(TARGET ${cocos_target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "copy file into Resources: ${res_file_relat_path} ..."
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${res_file} "${opt_COPY_TO}/${res_file_relat_path}"
)
endforeach()
endforeach()
endfunction()
Expand Down Expand Up @@ -79,8 +85,9 @@ function(get_target_depends_ext_dlls cocos_target all_depend_dlls_out)
search_depend_libs_recursive(${cocos_target} depend_libs)
foreach(depend_lib ${depend_libs})
if(TARGET ${depend_lib})
get_target_property(tmp_dlls ${depend_lib} CC_DEPEND_DLLS)
if(tmp_dlls)
get_target_property(found_shared_lib ${depend_lib} IMPORTED_IMPLIB)
if(found_shared_lib)
get_target_property(tmp_dlls ${depend_lib} IMPORTED_LOCATION)
list(APPEND all_depend_ext_dlls ${tmp_dlls})
endif()
endif()
Expand All @@ -89,20 +96,19 @@ function(get_target_depends_ext_dlls cocos_target all_depend_dlls_out)
set(${all_depend_dlls_out} ${all_depend_ext_dlls} PARENT_SCOPE)
endfunction()

# copy the `cocos_target` needed dlls into `COPY_TO` folder
# copy the `cocos_target` needed dlls into TARGET_FILE_DIR
function(cocos_copy_target_dll cocos_target)
set(oneValueArgs COPY_TO)
cmake_parse_arguments(opt "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
get_target_depends_ext_dlls(${cocos_target} all_depend_dlls)
# remove repeat items
if(all_depend_dlls)
list(REMOVE_DUPLICATES all_depend_dlls)
endif()
# todo, add a option to enable/disable debug print
message(STATUS "prepare to copy external dlls for ${cocos_target}:${all_depend_dlls}")
foreach(cc_dll_file ${all_depend_dlls})
get_filename_component(cc_dll_name ${cc_dll_file} NAME)
configure_file(${cc_dll_file} "${opt_COPY_TO}/${cc_dll_name}" COPYONLY)
add_custom_command(TARGET ${cocos_target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "copy dll into target file dir: ${cc_dll_name} ..."
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${cc_dll_file} "$<TARGET_FILE_DIR:${cocos_target}>/${cc_dll_name}"
)
endforeach()
endfunction()

Expand Down Expand Up @@ -168,31 +174,21 @@ function(source_group_single_file single_file)
source_group("${ide_file_group}" FILES ${single_file})
endfunction()

# setup a cocos application, include "APP_BIN_DIR", "APP_RES_DIR" config
# setup a cocos application
function(setup_cocos_app_config app_name)
# set target PROPERTIES, depend different platforms
# put all output app into bin/${app_name}
set_target_properties(${app_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/${app_name}")
if(APPLE)
set(APP_BIN_DIR "${CMAKE_BINARY_DIR}/bin")
set_target_properties(${app_name} PROPERTIES MACOSX_BUNDLE 1
)
# output macOS/iOS .app
set_target_properties(${app_name} PROPERTIES MACOSX_BUNDLE 1)
elseif(MSVC)
# only Debug and Release mode was supported when using MSVC.
set(APP_BIN_DIR "${CMAKE_BINARY_DIR}/bin/${APP_NAME}/$<CONFIG>")
set(APP_RES_DIR "${CMAKE_BINARY_DIR}/bin/${APP_NAME}/${CMAKE_BUILD_TYPE}/Resources")
#Visual Studio Defaults to wrong type
set_target_properties(${app_name} PROPERTIES LINK_FLAGS "/SUBSYSTEM:WINDOWS")
else(LINUX)
set(APP_BIN_DIR "${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}/${APP_NAME}")
set(APP_RES_DIR "${APP_BIN_DIR}/Resources")
# visual studio default is Console app, but we need Windows app
set_property(TARGET ${app_name} APPEND PROPERTY LINK_FLAGS "/SUBSYSTEM:WINDOWS")
endif()
set_target_properties(${app_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${APP_BIN_DIR}")

# auto mark code files for IDE when mark app
if(XCODE OR VS)
cocos_mark_code_files(${APP_NAME})
cocos_mark_code_files(${app_name})
endif()

set(APP_RES_DIR ${APP_RES_DIR} PARENT_SCOPE)
endfunction()

# if cc_variable not set, then set it cc_value
Expand Down
50 changes: 22 additions & 28 deletions cmake/Modules/CocosConfigDefine.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,35 @@
set(MACOSX TRUE)
set(PLATFORM_FOLDER mac)
endif()
else()
else()
message(FATAL_ERROR "Unsupported platform, CMake will exit")
return()
endif()

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

if(CMAKE_GENERATOR)
# generators that are capable of organizing into a hierarchy of folders
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# simplify generator condition judgement
if(CMAKE_GENERATOR STREQUAL Xcode)
set(XCODE TRUE)
elseif(CMAKE_GENERATOR MATCHES Visual)
set(VS TRUE)
if(CMAKE_CONFIGURATION_TYPES)
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Reset the configurations to what we need" FORCE)
message(STATUS "CMAKE_CONFIGURATION_TYPES: ${CMAKE_CONFIGURATION_TYPES}")
else()
if(NOT CMAKE_BUILD_TYPE)
if(DEBUG_MODE) # build mode, Debug is default value
set(CMAKE_BUILD_TYPE Debug)
else()
set(CMAKE_BUILD_TYPE Release)
endif()
endif()
# make configurations type keep same to cmake build type.
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_BUILD_TYPE}" CACHE STRING "Reset the configurations to what we need" FORCE)
endif()
message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")

message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
endif()

# custom target property for dll collect
define_property(TARGET
PROPERTY CC_DEPEND_DLLS
BRIEF_DOCS "depend dlls of a target"
FULL_DOCS "use to save depend dlls of a target"
)
# custom target property for lua/js link
define_property(TARGET
PROPERTY CC_JS_DEPEND
Expand Down
20 changes: 17 additions & 3 deletions cmake/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CMake is an open-source, cross-platform family of tools designed to build, test
```sh
cmake --version
```
if the CMake version is lower than 3.1, please upgrade.
if the CMake version is lower than 3.6, please upgrade.

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.

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

Execute `cmake --build .` to compile, or open __Cocos2d-x.sln__ in Explorer to use the generated project.
Execute `cmake --build .` to compile,
```
cmake --build . --config Debug
cmake --build . --config Release
```
or open __Cocos2d-x.sln__ in Explorer to use the generated project.

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

### Generate macOS Project

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

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.
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.

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.

### Android Studio

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

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

## Tips

1. Use `cmake ..` to refersh resources and code files, after you modify `Resources` or `CMakeLists.txt`.
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.

## Useful Links

* CMake Official website: [cmake.org](https://cmake.org/)
Expand Down
2 changes: 1 addition & 1 deletion cocos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ if(XCODE OR VS)
cocos_mark_code_files("cocos2d")
endif()

if(WIN32)
if(WINDOWS)
# precompiled header. Compilation time speedup ~4x.
target_sources(cocos2d PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/precheader.cpp")
set_target_properties(cocos2d PROPERTIES COMPILE_FLAGS "/Yuprecheader.h /FIprecheader.h")
Expand Down
2 changes: 1 addition & 1 deletion cocos/scripting/js-bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ if(XCODE OR VS)
cocos_mark_code_files("jscocos2d")
endif()

if(WIN32)
if(WINDOWS)
# precompiled header
target_sources(jscocos2d PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/precheader.cpp")
set_target_properties(jscocos2d PROPERTIES COMPILE_FLAGS "/Yuprecheader.h /FIprecheader.h")
Expand Down
2 changes: 1 addition & 1 deletion external/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v3-deps-156",
"version": "v3-deps-157",
"zip_file_size": "141265643",
"repo_name": "cocos2d-x-3rd-party-libs-bin",
"repo_parent": "https://github.com/cocos2d/",
Expand Down
7 changes: 3 additions & 4 deletions templates/cpp-template-default/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,14 @@ if(APPLE)
elseif(IOS)
cocos_pak_xcode(${APP_NAME} INFO_PLIST "iOSBundleInfo.plist.in")
set_xcode_property(${APP_NAME} ASSETCATALOG_COMPILER_APPICON_NAME "AppIcon")
# A way to find your own DEVELOPMENT_TEAM value:
# https://github.com/cocos2d/cocos2d-x/issues/19319#issuecomment-455931410
set_xcode_property(${APP_NAME} DEVELOPMENT_TEAM "")
set_xcode_property(${APP_NAME} CODE_SIGN_IDENTITY "iPhone Developer")
endif()
elseif(WINDOWS)
cocos_copy_target_dll(${APP_NAME} COPY_TO ${APP_RES_DIR}/..)
cocos_copy_target_dll(${APP_NAME})
endif()

if(LINUX OR WINDOWS)
cocos_copy_res(COPY_TO ${APP_RES_DIR} FOLDERS ${GAME_RES_FOLDER})
set(APP_RES_DIR "$<TARGET_FILE_DIR:${APP_NAME}>/Resources")
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR} FOLDERS ${GAME_RES_FOLDER})
endif()
13 changes: 6 additions & 7 deletions templates/js-template-default/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,18 @@ if(APPLE)
elseif(IOS)
cocos_pak_xcode(${APP_NAME} INFO_PLIST "iOSBundleInfo.plist.in")
set_xcode_property(${APP_NAME} ASSETCATALOG_COMPILER_APPICON_NAME "AppIcon")
# A way to find your own DEVELOPMENT_TEAM value:
# https://github.com/cocos2d/cocos2d-x/issues/19319#issuecomment-455931410
set_xcode_property(${APP_NAME} DEVELOPMENT_TEAM "")
set_xcode_property(${APP_NAME} CODE_SIGN_IDENTITY "iPhone Developer")
endif()
elseif(WINDOWS)
cocos_copy_target_dll(${APP_NAME} COPY_TO ${APP_RES_DIR}/..)
cocos_copy_target_dll(${APP_NAME})
endif()
# copy resource on linux or WINDOWS
if(LINUX OR WINDOWS)
cocos_copy_res(COPY_TO ${APP_RES_DIR} FILES ${res_main_files})
cocos_copy_res(COPY_TO ${APP_RES_DIR}/res FOLDERS ${res_res_folders})
cocos_copy_res(COPY_TO ${APP_RES_DIR}/src FOLDERS ${res_src_folders})
cocos_copy_res(COPY_TO ${APP_RES_DIR}/script FOLDERS ${res_script_folders})
set(APP_RES_DIR "$<TARGET_FILE_DIR:${APP_NAME}>/Resources")
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR} FILES ${res_main_files})
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR}/res FOLDERS ${res_res_folders})
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR}/src FOLDERS ${res_src_folders})
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR}/script FOLDERS ${res_script_folders})
endif()

11 changes: 5 additions & 6 deletions templates/lua-template-default/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,16 @@ if(APPLE)
elseif(IOS)
cocos_pak_xcode(${APP_NAME} INFO_PLIST "iOSBundleInfo.plist.in")
set_xcode_property(${APP_NAME} ASSETCATALOG_COMPILER_APPICON_NAME "AppIcon")
# A way to find your own DEVELOPMENT_TEAM value:
# https://github.com/cocos2d/cocos2d-x/issues/19319#issuecomment-455931410
set_xcode_property(${APP_NAME} DEVELOPMENT_TEAM "")
set_xcode_property(${APP_NAME} CODE_SIGN_IDENTITY "iPhone Developer")
endif()
elseif(WINDOWS)
cocos_copy_target_dll(${APP_NAME} COPY_TO ${APP_RES_DIR}/..)
cocos_copy_target_dll(${APP_NAME})
endif()
# copy resource on linux or WINDOWS
if(LINUX OR WINDOWS)
cocos_copy_res(COPY_TO ${APP_RES_DIR}/res FOLDERS ${res_res_folders})
cocos_copy_res(COPY_TO ${APP_RES_DIR}/src FOLDERS ${res_src_folders})
cocos_copy_res(COPY_TO ${APP_RES_DIR}/src/cocos FOLDERS ${res_script_folders})
set(APP_RES_DIR "$<TARGET_FILE_DIR:${APP_NAME}>/Resources")
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR}/res FOLDERS ${res_res_folders})
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR}/src FOLDERS ${res_src_folders})
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR}/src/cocos FOLDERS ${res_script_folders})
endif()
7 changes: 3 additions & 4 deletions tests/cpp-empty-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,14 @@ if(APPLE)
cocos_pak_xcode(${APP_NAME} INFO_PLIST "iOSBundleInfo.plist.in")
set_xcode_property(${APP_NAME} ASSETCATALOG_COMPILER_APPICON_NAME "AppIcon-${APP_NAME}")
set_xcode_property(${APP_NAME} CODE_SIGN_IDENTITY "iPhone Developer")
# A way to find your own DEVELOPMENT_TEAM value:
# https://github.com/cocos2d/cocos2d-x/issues/19319#issuecomment-455931410
set_xcode_property(${APP_NAME} DEVELOPMENT_TEAM "")
set_xcode_property(${APP_NAME} CODE_SIGN_IDENTITY "iPhone Developer")
endif()
elseif(WINDOWS)
cocos_copy_target_dll(${APP_NAME} COPY_TO ${APP_RES_DIR}/..)
cocos_copy_target_dll(${APP_NAME})
endif()

if(LINUX OR WINDOWS)
cocos_copy_res(COPY_TO ${APP_RES_DIR} FOLDERS ${GAME_RES_FOLDER})
set(APP_RES_DIR "$<TARGET_FILE_DIR:${APP_NAME}>/Resources")
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR} FOLDERS ${GAME_RES_FOLDER})
endif()
9 changes: 4 additions & 5 deletions tests/cpp-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -473,22 +473,21 @@ if(APPLE)
elseif(IOS)
cocos_pak_xcode(${APP_NAME} INFO_PLIST "iOSBundleInfo.plist.in")
set_xcode_property(${APP_NAME} ASSETCATALOG_COMPILER_APPICON_NAME "AppIcon-${APP_NAME}")
# A way to find your own DEVELOPMENT_TEAM value:
# https://github.com/cocos2d/cocos2d-x/issues/19319#issuecomment-455931410
set_xcode_property(${APP_NAME} DEVELOPMENT_TEAM "")
set_xcode_property(${APP_NAME} CODE_SIGN_IDENTITY "iPhone Developer")
endif()
elseif(WINDOWS)
# "too large PDB" error often occurs in cpp-tests when using default "/Zi"
target_compile_options(${APP_NAME} PRIVATE /Z7)
cocos_copy_target_dll(${APP_NAME} COPY_TO ${APP_RES_DIR}/..)
cocos_copy_target_dll(${APP_NAME})
endif()

if(LINUX OR WINDOWS)
cocos_copy_res(COPY_TO ${APP_RES_DIR} FOLDERS ${GAME_RES_FOLDER})
set(APP_RES_DIR "$<TARGET_FILE_DIR:${APP_NAME}>/Resources")
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR} FOLDERS ${GAME_RES_FOLDER})
endif()

if(WIN32)
if(WINDOWS)
# precompiled header. Compilation time speedup ~4x.
target_sources(${APP_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/Classes/precheader.cpp")
set_target_properties(${APP_NAME} PROPERTIES COMPILE_FLAGS "/Yuprecheader.h /FIprecheader.h")
Expand Down
13 changes: 6 additions & 7 deletions tests/js-tests/project/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,18 @@ if(APPLE)
elseif(IOS)
cocos_pak_xcode(${APP_NAME} INFO_PLIST "iOSBundleInfo.plist.in")
set_xcode_property(${APP_NAME} ASSETCATALOG_COMPILER_APPICON_NAME "AppIcon-${APP_NAME}")
# A way to find your own DEVELOPMENT_TEAM value:
# https://github.com/cocos2d/cocos2d-x/issues/19319#issuecomment-455931410
set_xcode_property(${APP_NAME} DEVELOPMENT_TEAM "")
set_xcode_property(${APP_NAME} CODE_SIGN_IDENTITY "iPhone Developer")
endif()
elseif(WINDOWS)
cocos_copy_target_dll(${APP_NAME} COPY_TO ${APP_RES_DIR}/..)
cocos_copy_target_dll(${APP_NAME})
endif()

if(LINUX OR WINDOWS)
cocos_copy_res(COPY_TO ${APP_RES_DIR} FILES ${res_main_files})
cocos_copy_res(COPY_TO ${APP_RES_DIR}/res FOLDERS ${res_res_folders})
cocos_copy_res(COPY_TO ${APP_RES_DIR}/src FOLDERS ${res_src_folders})
cocos_copy_res(COPY_TO ${APP_RES_DIR}/script FOLDERS ${res_script_folders})
set(APP_RES_DIR "$<TARGET_FILE_DIR:${APP_NAME}>/Resources")
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR} FILES ${res_main_files})
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR}/res FOLDERS ${res_res_folders})
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR}/src FOLDERS ${res_src_folders})
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR}/script FOLDERS ${res_script_folders})
endif()

Loading