CMake: Splitting Source Code into Modules

CMake: Splitting Source Code into Modules
CMake: Splitting Source Code into Modules

Introduction:

Projects usually start with a single CMakeLists.txt file, which grows over time. In this article, we will demonstrate a mechanism to split the CMakeLists.txt into smaller units. The motivation for splitting CMakeLists.txt into modules:

  • The main CMakeLists.txt is easier to read;

  • CMake modules can be reused in other projects

  • Combined with functions, modules can help us limit the scope of variables.

In this article, we will demonstrate how to define and include a macro that allows us to obtain CMake colored output (for important status messages or warnings).

CMake: Splitting Source Code into Modules

Project Structure

.
├── cmake
│     └── colors.cmake
└── CMakeLists.txt

https://gitee.com/jiangli01/tutorials/tree/master/cmake-tutorial/chapter7/02

Related Source Code

cmake/color.cmake

# colorize CMake output
macro(define_colors)
  if(WIN32)
    # has no effect on WIN32
    set(ColourReset "")
    set(ColourBold "")
    set(Red "")
    set(Green "")
    set(Yellow "")
    set(Blue "")
    set(Magenta "")
    set(Cyan "")
    set(White "")
    set(BoldRed "")
    set(BoldGreen "")
    set(BoldYellow "")
    set(BoldBlue "")
    set(BoldMagenta "")
    set(BoldCyan "")
    set(BoldWhite "")
  else()
    string(ASCII 27 Esc)
    set(ColourReset "${Esc}[m")
    set(ColourBold "${Esc}[1m")
    set(Red "${Esc}[31m")
    set(Green "${Esc}[32m")
    set(Yellow "${Esc}[33m")
    set(Blue "${Esc}[34m")
    set(Magenta "${Esc}[35m")
    set(Cyan "${Esc}[36m")
    set(White "${Esc}[37m")
    set(BoldRed "${Esc}[1;31m")
    set(BoldGreen "${Esc}[1;32m")
    set(BoldYellow "${Esc}[1;33m")
    set(BoldBlue "${Esc}[1;34m")
    set(BoldMagenta "${Esc}[1;35m")
    set(BoldCyan "${Esc}[1;36m")
    set(BoldWhite "${Esc}[1;37m")
  endif()
endmacro()

CMakeLists.txt

cmake_minimum_required(VERSION 3.10 FATAL_ERROR)

project(example LANGUAGES NONE)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

include(colors)
define_colors()

message(STATUS "This is a normal message")
message(STATUS "${Red}This is a red${ColourReset}")
message(STATUS "${BoldRed}This is a bold red${ColourReset}")
message(STATUS "${Green}This is a green${ColourReset}")
message(STATUS "${BoldMagenta}This is bold${ColourReset}")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

Add the cmake subdirectory to the list of paths searched for CMake modules.

include(colors)
define_colors()

Include the colors.cmake module and call the macro defined within.

message(STATUS "This is a normal message")
message(STATUS "${Red}This is a red${ColourReset}")
message(STATUS "${BoldRed}This is a bold red${ColourReset}")
message(STATUS "${Green}This is a green${ColourReset}")
message(STATUS "${BoldMagenta}This is bold${ColourReset}")

Print messages in different colors.

Result Display

mkdir build
cd build
cmake ..

CMake: Splitting Source Code into ModulesFinally, I wish everyone to become stronger!!!

CMake: Splitting Source Code into Modules
CMake: Splitting Source Code into Modules

Leave a Comment