https://github.com/fenglielie/cmakezero
https://github.com/fenglielie/cmakezero
cmake cpp
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/fenglielie/cmakezero
- Owner: fenglielie
- Created: 2023-11-12T16:50:07.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-12T09:03:28.000Z (over 1 year ago)
- Last Synced: 2025-03-12T10:20:23.501Z (over 1 year ago)
- Topics: cmake, cpp
- Language: CMake
- Homepage:
- Size: 54.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CMakeZero
CMakeZero is a CMake script that provides macros and functions to simplify the writing of `CMakeLists.txt`.
## Usage
```
-- macro usage:
- zero_usage(): print usage
- zero_init(): print usage, then init the project (call after project)
- zero_init_quiet(): init the project (call after project)
- zero_info(): show infomation
- zero_check_update(): check for updates to zero.cmake
-- function usage:
- zero_add_subdirs(src): go to src/CMakeLists.txt and src/*/CMakeLists.txt
- zero_add_subdirs_rec(src): go to src/CMakeLists.txt and src/*/*/CMakeLists.txt (recurse)
- zero_get_files(tmp test): search source files in test/ => tmp
- zero_get_files_rec(tmp test): search source files in test/ and test/*/ => tmp (recurse)
-- target function usage:
- zero_target_preset_definitions(targetname): add some compile definitions
* ZERO_TARGET_NAME=targetname
* ZERO_PROJECT_SOURCE_DIR=PROJECT_SOURCE_DIR
* ZERO_CURRENT_SOURCE_DIR=CMAKE_CURRENT_SOURCE_DIR
- zero_target_info(targetname): show target properties
```
## Example
Manually download the [zero.cmake](cmake/zero.cmake) file and place it in the `cmake/` subdirectory of your project root. You can also download it using the following command:
```
wget https://raw.githubusercontent.com/fenglielie/cmakezero/main/cmake/zero.cmake
```
Then, import it in the `CMakeLists.txt` of your project:
```cmake
cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
project(Demo VERSION 1.0)
include(cmake/zero.cmake)
zero_init()
zero_info()
zero_add_subdirs_rec(src)
zero_check_update()
```
After that, you can use the functions and macros provided by `zero.cmake` to simplify the writing of `CMakeLists.txt`.
For example, add an executable target using all source files in the current directory (and recursive subdirectories):
```cmake
zero_get_files_rec(SRCS .)
add_executable(test ${SRCS})
```
Add some compile definitions and show properties
```cmake
zero_get_files_rec(SRCS .)
add_executable(test ${SRCS})
zero_target_preset_definitions(test)
zero_target_info(test)
```
The output of `zero_target_info` is like this:
```
[cmake] -- ---------- ----------
[cmake] -- name: test1
[cmake] -- type: executable
[cmake] -- location: /path/to/cmakezero/src/test1
[cmake] -- sources:
[cmake] -- * /path/to/cmakezero/src/test1/test1.cpp
[cmake] -- compile_definitions:
[cmake] -- * ZERO_TARGET_NAME="test1"
[cmake] -- * ZERO_PROJECT_SOURCE_DIR="/path/to/cmakezero"
[cmake] -- * ZERO_CURRENT_SOURCE_DIR="/path/to/cmakezero/src/test1"
```