How to get different build type for c++ via cmake at once?

Viewed 664

I have a cpp project and using external(git submodule) directory for sanitizer. Here is how my project looks like.

├── CMakeLists.txt
├── Dockerfile
├── externals
│   └── sanitizers-cmake
├── jenkins
│   └── submodule
├── Jenkinsfile
├── README.md
├── scripts
│   └── run.sh
└── src
    └── main.cpp

Sanitizer came from externals directory. Here is my cmake file

   cmake_minimum_required(VERSION 3.10)
project(testproject)


set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -std=c++17 -Wall -Wextra -Wpedantic")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -std=c++17 -Wall -Wextra -Wpedantic")

add_executable(intro src/main.cpp)

ADD_CUSTOM_TARGET(debug
  COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug ${CMAKE_SOURCE_DIR}
  COMMAND ${CMAKE_COMMAND}  
  COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
  COMMENT "Switch CMAKE_BUILD_TYPE to Debug"
  )

ADD_CUSTOM_TARGET(release
  COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Release ${CMAKE_SOURCE_DIR}
  COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
  COMMENT "Switch CMAKE_BUILD_TYPE to Release"
  )


ADD_CUSTOM_TARGET(asanitizer
  COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=ASAN ${CMAKE_SOURCE_DIR}
  COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
  COMMENT "Switch CMAKE_BUILD_TYPE to Address Sanitizer"
  )

include("cmake/cppcheck.cmake")
include("cmake/clang-dev-tools.cmake")

include("cmake/sanitizer.cmake")

I edited my CMakeLists.txt and I defined 3 build types debug, release and address sanitizer. Currently, after invoking CMake, I can run the make with one of the following params. My aim is to build them parallel and get all 3 binaries at the same time. Currently, I am overriding them. And I cannot avoid calling make. It should be simply handled by Cmake

$ make debug | release | asanitizer


Host= Debian 10
Compiler= clang-7
cmake= 3.13 
generator= ninja
0 Answers
Related