Skip to content
42 changes: 20 additions & 22 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 copy_if_different ${cc_file} "${opt_COPY_TO}/${file_name}"
COMMENT "copy file: ${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 copy_if_different ${res_file} "${opt_COPY_TO}/${res_file_relat_path}"
COMMENT "copy file: ${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 @@ -91,18 +98,17 @@ endfunction()

# copy the `cocos_target` needed dlls into `COPY_TO` folder
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 copy_if_different ${cc_dll_file} "$<TARGET_FILE_DIR:${cocos_target}>/${cc_dll_name}"
COMMENT "copy file: ${cc_dll_name}"
)
endforeach()
endfunction()

Expand Down Expand Up @@ -172,27 +178,19 @@ endfunction()
function(setup_cocos_app_config app_name)
# set target PROPERTIES, depend different platforms
if(APPLE)
set(APP_BIN_DIR "${CMAKE_BINARY_DIR}/bin")
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")
endif()
set_target_properties(${app_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${APP_BIN_DIR}")

set_target_properties(${app_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/${app_name}")
# 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)
set(APP_RES_DIR "$<TARGET_FILE_DIR:${app_name}>/Resources" 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
7 changes: 6 additions & 1 deletion cmake/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ 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.

### Generate macOS Project

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
4 changes: 2 additions & 2 deletions templates/cpp-template-default/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ if(APPLE)
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})
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR} FOLDERS ${GAME_RES_FOLDER})
endif()
10 changes: 5 additions & 5 deletions templates/js-template-default/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ if(APPLE)
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})
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()

8 changes: 4 additions & 4 deletions templates/lua-template-default/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ if(APPLE)
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})
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()
4 changes: 2 additions & 2 deletions tests/cpp-empty-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ if(APPLE)
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})
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR} FOLDERS ${GAME_RES_FOLDER})
endif()
4 changes: 2 additions & 2 deletions tests/cpp-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,11 @@ if(APPLE)
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})
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR} FOLDERS ${GAME_RES_FOLDER})
endif()

if(WIN32)
Expand Down
10 changes: 5 additions & 5 deletions tests/js-tests/project/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ if(APPLE)
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})
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()

8 changes: 4 additions & 4 deletions tests/lua-empty-test/project/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ if(APPLE)
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}/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})
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()
8 changes: 4 additions & 4 deletions tests/lua-tests/project/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ if(APPLE)
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}/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})
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()