2018-03-08 16:08:13 +08:00
# CMake Guide
2018-02-08 09:24:33 +08:00
2018-05-21 17:06:29 +08:00
CMake is an open-source, cross-platform family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generates native makefiles and workspaces that can be used in the compiler environment of your choice.
2018-02-08 09:24:33 +08:00
2018-03-08 16:08:13 +08:00
## Requirement
2018-02-08 09:24:33 +08:00
2018-05-21 17:06:29 +08:00
1. Open your terminal and execute:
```sh
cmake --version
```
2020-12-29 20:55:12 +08:00
if the CMake version is lower than 3.14, please upgrade.
2018-02-08 09:24:33 +08:00
2022-07-08 14:20:41 +08:00
2. You should use **out-of-source** builds, this means you need to create a different directory than **axis** to execute the `cmake` command.
2018-03-08 16:08:13 +08:00
## Step by Step
2018-05-21 17:06:29 +08:00
### Linux
2018-02-08 09:24:33 +08:00
2018-03-08 16:08:13 +08:00
```sh
2022-07-08 14:20:41 +08:00
cd axis
2018-03-08 16:08:13 +08:00
mkdir linux-build & & cd linux-build
cmake ..
make
```
2018-02-08 09:24:33 +08:00
2018-03-08 16:08:13 +08:00
Execute `make help` to see all build targets, `make <target>` build specified target
2018-02-08 09:24:33 +08:00
2018-05-21 17:06:29 +08:00
### Generate Visual Studio projects
2018-02-08 09:24:33 +08:00
2018-03-08 16:08:13 +08:00
```sh
2022-07-08 14:20:41 +08:00
cd axis
2020-12-29 20:55:12 +08:00
mkdir win32-build
cmake -B win32-build -G"Visual Studio 16 2019"
2020-12-29 21:48:02 +08:00
# build
2020-12-29 20:55:12 +08:00
cmake --build win32-build --config Debug
2019-01-23 09:55:55 +08:00
```
2022-07-08 14:20:41 +08:00
or open **axis.sln** in Explorer to use the generated project.
2018-03-08 16:08:13 +08:00
### Generate macOS Project
```sh
2022-07-08 14:20:41 +08:00
cd axis
2020-12-29 20:55:12 +08:00
mkdir mac-build
cmake -B mac-build -GXcode
2022-07-08 14:20:41 +08:00
open mac-build/axis.xcodeproj
2018-03-08 16:08:13 +08:00
```
### Generate iOS Project
```sh
2022-07-08 14:20:41 +08:00
cd axis
2020-12-29 20:55:12 +08:00
mkdir ios-build
cmake -B ios-build -GXcode -DCMAKE_TOOLCHAIN_FILE=cmake/ios.mini.cmake
2022-07-08 14:20:41 +08:00
open ios-build/axis.xcodeproj
2018-03-08 16:08:13 +08:00
```
2019-10-22 13:42:56 +08:00
#### How do I customize the generated Xcode project?
2022-07-08 14:20:41 +08:00
Xcode project settings that you want to affect both the app project and the axis library project should be passed on the command
2019-10-22 13:42:56 +08:00
line when invoking `cmake` .
Xcode project settings that you want to affect the app project only shoudl be put into the its `CMakeLists.txt` file.
Any Xcode Build Setting can be changed by setting `CMAKE_XCODE_ATTRIBUTE_XXX` where `XXX` is the name found within the Xcode Build
Settings page of the target. The following image shows the name of the iOS Deployment Target:
![Find Xcode Build Setting ](images/Xcode_Find_Setting_Name.png )
Cocos also provides the function `set_xcode_property()` to make this easier to set from within a `CMakeLists.txt` file, where only
the `XXX` part needed to be specified:
```
set_xcode_property(${APP_NAME} XXX "Value")
```
##### Deployment Target
2022-07-08 14:20:41 +08:00
As explained above, pass this on the command line so both the app the axis are built using the same version:
2019-10-22 13:42:56 +08:00
For iOS pass `-DCMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET=version` , where `version` is `9.0` , `10.0` , etc.
For macOS pass `-DCMAKE_XCODE_ATTRIBUTE_MACOSX_DEPLOYMENT_TARGET=version` , where `version` is `10.12` , `10.13` , etc.
##### Code Signing Development Team
This should be set in the app `CMakeLists.txt` file. You only need to set the "Development Team" as Xcode will automatically manage the
other settings (certificate type, etc.). However the value you set is the 10-digit serial number following the development team name,
which you can see in the top-right of the [Apple Developer Certificates ](https://developer.apple.com/account/resources/certificates/list ) page.
Set it like this:
```
set_xcode_property(${APP_NAME} DEVELOPMENT_TEAM "GRLXXXX2K9")
```
##### Bundle Identifier, Version numbers and Device Support
All this information is held in the `Info.plist` file that is part of the app's source files and is therefore not generated by `cmake` . Therefore
you can update these values from the `General` page of the Xcode target and the values will be preserved the next time you regenerate the project
from `cmake` :
![Xcode General Page ](images/Xcode_General_Page.png )
2019-01-23 09:55:55 +08:00
2018-04-03 11:18:22 +08:00
2018-05-21 17:06:29 +08:00
### Android Studio
2018-03-08 16:08:13 +08:00
2022-07-08 14:20:41 +08:00
We use the Gradle for Android applications, and Gradle use cmake to build the native code, see [gradle.properties ](https://github.com/c4games/axis/blob/84be684e3858393a6f3efc50e3f95d4e0ac92a20/tests/cpp-empty-test/proj.android/gradle.properties#L38 ): `PROP_NDK_MODE` , it controls how native builds work.
2018-03-08 16:08:13 +08:00
```sh
2018-04-24 13:46:20 +08:00
# android native code build type
# none, native code will never be compiled.
# cmake, native code will be compiled by CMakeLists.txt
# ndk-build, native code will be compiled by Android.mk
2018-07-22 15:28:17 +08:00
PROP_BUILD_TYPE=cmake
2018-03-08 16:08:13 +08:00
```
2022-07-08 14:20:41 +08:00
If you want to add cmake build arguments, please add it at [external Native Build ](https://github.com/c4games/axis/blob/84be684e3858393a6f3efc50e3f95d4e0ac92a20/tests/cpp-empty-test/proj.android/app/build.gradle#L25 ) block of __app/build.gradle__ file.
2018-03-08 16:08:13 +08:00
cmake support win32 ,and support generate&use prebuilt libs (#18683)
* fix win32 prebuilt error, and set cmake as default android native build
* cpp-template support msvc
* add msvc version check
* add -D_USRLUASTATIC, fix lua lib cmake compile error
* fix cpp-empty-test res copy dir, add function needed
* improve msvc res copy
* refactor cmake copy dll logic
* fix cpp-tests compile error
* refactor copy dll temp
* refactor win32 exe using dlls
* js-project compile support cmake using msvc
* improve res mark for win32
* update the way of lua project res mark
* add D_USRLUASTATIC for lua project
* unify RELEASE Release to Release, add CMAKE_CONFIGURATION_TYPES
* improve VS shows targets, add folder
* improve mac/ios res mark
* reduce useless comments
* unify cpp src include variable
* refactor cpp test project package logic
* improve pkg app logic
* start support prebuilt libs for cpp project using cmake
* improve prebuilt libs generate
* expand js/lua support prebuilt libs on mac
* adapt IDE generate libs path
* start do prebuilt for android
* improve the way of using external libs for prebuilt
* prebuilt libs support cpp-empty-test on android studio
* cpp-tests prebuilt libs, and remove useless
* start to improve libs using, to adapt prebuilt
* improve cpp tests prebuilt on android
* prebuilt support lua & js test on android
* prebuilt support cpp&js template
* rename prebuilt variabl, detail androd config
* finish android support prebuilt using cmake
* fix cmake script run in windows error
* reduce variable name length
* improve the way of win32 use dlls to adapt prebuilt
* loosen pick js/lua engine lib condition
* self review and start check linux build
* unify compile option location
* strict copy dlls condition
* improve libs link order
* start redo the way of copy dlls
* unify dlls copy logic
* fix linux res copy error
* update cmake copy file path in linux
* make prebuilt dir if needed, and divide Debug and Release dir
* cmake win32 divide Debug and Release dir well
* comment Lua Template project, for the unfinished lua simulator support
* add simulator cmake file
* start support simulator
* simulator support cmake build on macOS
* fix simulator compile error on win32
* add simulator prebuilt support
* improve mark cocos app macro
* improve cmake template project format
* improve cmake app files format
* fix format improve mistake
* detail cmake readme
* improve readme, useless comments
* add game.rc to template project
* check travis openssl version
* try update travis openssl
* upgrade travis openssl
* change openssl link
* change to upgrade openssl
* re install python with upgrade openssl
* add comment for macro and func, improve variable name
2018-03-08 09:47:57 +08:00
## Build Options
2018-05-21 17:06:29 +08:00
### CMake Common Build Options
cmake support win32 ,and support generate&use prebuilt libs (#18683)
* fix win32 prebuilt error, and set cmake as default android native build
* cpp-template support msvc
* add msvc version check
* add -D_USRLUASTATIC, fix lua lib cmake compile error
* fix cpp-empty-test res copy dir, add function needed
* improve msvc res copy
* refactor cmake copy dll logic
* fix cpp-tests compile error
* refactor copy dll temp
* refactor win32 exe using dlls
* js-project compile support cmake using msvc
* improve res mark for win32
* update the way of lua project res mark
* add D_USRLUASTATIC for lua project
* unify RELEASE Release to Release, add CMAKE_CONFIGURATION_TYPES
* improve VS shows targets, add folder
* improve mac/ios res mark
* reduce useless comments
* unify cpp src include variable
* refactor cpp test project package logic
* improve pkg app logic
* start support prebuilt libs for cpp project using cmake
* improve prebuilt libs generate
* expand js/lua support prebuilt libs on mac
* adapt IDE generate libs path
* start do prebuilt for android
* improve the way of using external libs for prebuilt
* prebuilt libs support cpp-empty-test on android studio
* cpp-tests prebuilt libs, and remove useless
* start to improve libs using, to adapt prebuilt
* improve cpp tests prebuilt on android
* prebuilt support lua & js test on android
* prebuilt support cpp&js template
* rename prebuilt variabl, detail androd config
* finish android support prebuilt using cmake
* fix cmake script run in windows error
* reduce variable name length
* improve the way of win32 use dlls to adapt prebuilt
* loosen pick js/lua engine lib condition
* self review and start check linux build
* unify compile option location
* strict copy dlls condition
* improve libs link order
* start redo the way of copy dlls
* unify dlls copy logic
* fix linux res copy error
* update cmake copy file path in linux
* make prebuilt dir if needed, and divide Debug and Release dir
* cmake win32 divide Debug and Release dir well
* comment Lua Template project, for the unfinished lua simulator support
* add simulator cmake file
* start support simulator
* simulator support cmake build on macOS
* fix simulator compile error on win32
* add simulator prebuilt support
* improve mark cocos app macro
* improve cmake template project format
* improve cmake app files format
* fix format improve mistake
* detail cmake readme
* improve readme, useless comments
* add game.rc to template project
* check travis openssl version
* try update travis openssl
* upgrade travis openssl
* change openssl link
* change to upgrade openssl
* re install python with upgrade openssl
* add comment for macro and func, improve variable name
2018-03-08 09:47:57 +08:00
1. __ `-G` __, generate native build project you specified, for example
* `-GXcode` generate Xcode project files.
2020-10-22 17:58:21 +08:00
* `-GVisual Studio 16 2019` generate Visual Studio 2019 project, the default toolset is v141, add `-T` option to specify toolset, like this `-Tv142`
cmake support win32 ,and support generate&use prebuilt libs (#18683)
* fix win32 prebuilt error, and set cmake as default android native build
* cpp-template support msvc
* add msvc version check
* add -D_USRLUASTATIC, fix lua lib cmake compile error
* fix cpp-empty-test res copy dir, add function needed
* improve msvc res copy
* refactor cmake copy dll logic
* fix cpp-tests compile error
* refactor copy dll temp
* refactor win32 exe using dlls
* js-project compile support cmake using msvc
* improve res mark for win32
* update the way of lua project res mark
* add D_USRLUASTATIC for lua project
* unify RELEASE Release to Release, add CMAKE_CONFIGURATION_TYPES
* improve VS shows targets, add folder
* improve mac/ios res mark
* reduce useless comments
* unify cpp src include variable
* refactor cpp test project package logic
* improve pkg app logic
* start support prebuilt libs for cpp project using cmake
* improve prebuilt libs generate
* expand js/lua support prebuilt libs on mac
* adapt IDE generate libs path
* start do prebuilt for android
* improve the way of using external libs for prebuilt
* prebuilt libs support cpp-empty-test on android studio
* cpp-tests prebuilt libs, and remove useless
* start to improve libs using, to adapt prebuilt
* improve cpp tests prebuilt on android
* prebuilt support lua & js test on android
* prebuilt support cpp&js template
* rename prebuilt variabl, detail androd config
* finish android support prebuilt using cmake
* fix cmake script run in windows error
* reduce variable name length
* improve the way of win32 use dlls to adapt prebuilt
* loosen pick js/lua engine lib condition
* self review and start check linux build
* unify compile option location
* strict copy dlls condition
* improve libs link order
* start redo the way of copy dlls
* unify dlls copy logic
* fix linux res copy error
* update cmake copy file path in linux
* make prebuilt dir if needed, and divide Debug and Release dir
* cmake win32 divide Debug and Release dir well
* comment Lua Template project, for the unfinished lua simulator support
* add simulator cmake file
* start support simulator
* simulator support cmake build on macOS
* fix simulator compile error on win32
* add simulator prebuilt support
* improve mark cocos app macro
* improve cmake template project format
* improve cmake app files format
* fix format improve mistake
* detail cmake readme
* improve readme, useless comments
* add game.rc to template project
* check travis openssl version
* try update travis openssl
* upgrade travis openssl
* change openssl link
* change to upgrade openssl
* re install python with upgrade openssl
* add comment for macro and func, improve variable name
2018-03-08 09:47:57 +08:00
1. __ `CMAKE_BUILD_TYPE` __, specify the build mode, Debug or Release
* `-DCMAKE_BUILD_TYPE=Release` to generate the Release mode project, the default build mode is Debug
1. __ `-H -B` __, `-H` specify the CMake project Home directory, `-B` specify CMake-generated project binary directory. for example
2022-07-08 14:20:41 +08:00
* `-H..\axis -Bmsvc_build` the generated native project's location will be `msvc_build` directory.
cmake support win32 ,and support generate&use prebuilt libs (#18683)
* fix win32 prebuilt error, and set cmake as default android native build
* cpp-template support msvc
* add msvc version check
* add -D_USRLUASTATIC, fix lua lib cmake compile error
* fix cpp-empty-test res copy dir, add function needed
* improve msvc res copy
* refactor cmake copy dll logic
* fix cpp-tests compile error
* refactor copy dll temp
* refactor win32 exe using dlls
* js-project compile support cmake using msvc
* improve res mark for win32
* update the way of lua project res mark
* add D_USRLUASTATIC for lua project
* unify RELEASE Release to Release, add CMAKE_CONFIGURATION_TYPES
* improve VS shows targets, add folder
* improve mac/ios res mark
* reduce useless comments
* unify cpp src include variable
* refactor cpp test project package logic
* improve pkg app logic
* start support prebuilt libs for cpp project using cmake
* improve prebuilt libs generate
* expand js/lua support prebuilt libs on mac
* adapt IDE generate libs path
* start do prebuilt for android
* improve the way of using external libs for prebuilt
* prebuilt libs support cpp-empty-test on android studio
* cpp-tests prebuilt libs, and remove useless
* start to improve libs using, to adapt prebuilt
* improve cpp tests prebuilt on android
* prebuilt support lua & js test on android
* prebuilt support cpp&js template
* rename prebuilt variabl, detail androd config
* finish android support prebuilt using cmake
* fix cmake script run in windows error
* reduce variable name length
* improve the way of win32 use dlls to adapt prebuilt
* loosen pick js/lua engine lib condition
* self review and start check linux build
* unify compile option location
* strict copy dlls condition
* improve libs link order
* start redo the way of copy dlls
* unify dlls copy logic
* fix linux res copy error
* update cmake copy file path in linux
* make prebuilt dir if needed, and divide Debug and Release dir
* cmake win32 divide Debug and Release dir well
* comment Lua Template project, for the unfinished lua simulator support
* add simulator cmake file
* start support simulator
* simulator support cmake build on macOS
* fix simulator compile error on win32
* add simulator prebuilt support
* improve mark cocos app macro
* improve cmake template project format
* improve cmake app files format
* fix format improve mistake
* detail cmake readme
* improve readme, useless comments
* add game.rc to template project
* check travis openssl version
* try update travis openssl
* upgrade travis openssl
* change openssl link
* change to upgrade openssl
* re install python with upgrade openssl
* add comment for macro and func, improve variable name
2018-03-08 09:47:57 +08:00
1. __ `--build <dir>` __, build a CMake-generated project binary tree, for example
* `cmake --build ./msvc_build` , cmake will sellect corresponding build tools.
2019-01-23 09:55:55 +08:00
## 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.
2018-03-08 16:08:13 +08:00
## Useful Links
cmake support win32 ,and support generate&use prebuilt libs (#18683)
* fix win32 prebuilt error, and set cmake as default android native build
* cpp-template support msvc
* add msvc version check
* add -D_USRLUASTATIC, fix lua lib cmake compile error
* fix cpp-empty-test res copy dir, add function needed
* improve msvc res copy
* refactor cmake copy dll logic
* fix cpp-tests compile error
* refactor copy dll temp
* refactor win32 exe using dlls
* js-project compile support cmake using msvc
* improve res mark for win32
* update the way of lua project res mark
* add D_USRLUASTATIC for lua project
* unify RELEASE Release to Release, add CMAKE_CONFIGURATION_TYPES
* improve VS shows targets, add folder
* improve mac/ios res mark
* reduce useless comments
* unify cpp src include variable
* refactor cpp test project package logic
* improve pkg app logic
* start support prebuilt libs for cpp project using cmake
* improve prebuilt libs generate
* expand js/lua support prebuilt libs on mac
* adapt IDE generate libs path
* start do prebuilt for android
* improve the way of using external libs for prebuilt
* prebuilt libs support cpp-empty-test on android studio
* cpp-tests prebuilt libs, and remove useless
* start to improve libs using, to adapt prebuilt
* improve cpp tests prebuilt on android
* prebuilt support lua & js test on android
* prebuilt support cpp&js template
* rename prebuilt variabl, detail androd config
* finish android support prebuilt using cmake
* fix cmake script run in windows error
* reduce variable name length
* improve the way of win32 use dlls to adapt prebuilt
* loosen pick js/lua engine lib condition
* self review and start check linux build
* unify compile option location
* strict copy dlls condition
* improve libs link order
* start redo the way of copy dlls
* unify dlls copy logic
* fix linux res copy error
* update cmake copy file path in linux
* make prebuilt dir if needed, and divide Debug and Release dir
* cmake win32 divide Debug and Release dir well
* comment Lua Template project, for the unfinished lua simulator support
* add simulator cmake file
* start support simulator
* simulator support cmake build on macOS
* fix simulator compile error on win32
* add simulator prebuilt support
* improve mark cocos app macro
* improve cmake template project format
* improve cmake app files format
* fix format improve mistake
* detail cmake readme
* improve readme, useless comments
* add game.rc to template project
* check travis openssl version
* try update travis openssl
* upgrade travis openssl
* change openssl link
* change to upgrade openssl
* re install python with upgrade openssl
* add comment for macro and func, improve variable name
2018-03-08 09:47:57 +08:00
2018-05-21 17:06:29 +08:00
* CMake Official website: [cmake.org ](https://cmake.org/ )
2018-02-08 09:24:33 +08:00
2018-05-21 17:06:29 +08:00
* CMake Documentation: [cmake.org/documentation ](https://cmake.org/documentation/ )
2018-02-08 09:24:33 +08:00
2019-09-16 15:09:01 +08:00
* CMake FAQ: [Wiki/CMake_FAQ ](https://cmake.org/Wiki/CMake_FAQ )