cmake-11-cmakeSyntax

macro

add_executable(cpp_test test.cpp)
target_link_libraries(cpp_test sum_integers)

macro(add_catch_test _name _cost)
  math(EXPR num_macro_calls "${num_macro_calls} + 1")
  message(STATUS "add_catch_test called with ${ARGC} arguments: ${ARGV}")

  set(_argn "${ARGN}")
  if(_argn)
    message(STATUS "oops - macro received argument(s) we did not expect: ${ARGN}")
  endif()

  add_test(
    NAME
      ${_name}
    COMMAND
      $<TARGET_FILE:cpp_test>
      [${_name}] --success --out
      ${PROJECT_BINARY_DIR}/tests/${_name}.log --durations yes
    WORKING_DIRECTORY
      ${CMAKE_CURRENT_BINARY_DIR}
    )

  set_tests_properties(
    ${_name}
    PROPERTIES
      COST ${_cost}
    )
endmacro()

set(num_macro_calls 0)

add_catch_test(short 1.5)
add_catch_test(long 2.5 extra_argument)

message(STATUS "in total there were ${num_macro_calls} calls to add_catch_test")

$ cmake .
	-- The CXX compiler identification is GNU 9.3.0
	-- Check for working CXX compiler: /usr/bin/c++
	-- Check for working CXX compiler: /usr/bin/c++ -- works
	-- Detecting CXX compiler ABI info
	-- Detecting CXX compiler ABI info - done
	-- Detecting CXX compile features
	-- Detecting CXX compile features - done
	-- add_catch_test called with 2 arguments: short;1.5
	-- add_catch_test called with 3 arguments: long;2.5;extra_argument
	-- oops - macro received argument(s) we did not expect: extra_argument
	-- in total there were 2 calls to add_catch_test
	-- Configuring done
	-- Generating done
	-- Build files have been written to: /home/jianleya/trainning/cmake/cmake/cmake-cookbook/cmake-cookbook/chapter-07/recipe-01/cxx-example

$ make && make test
	Running tests...
	Test project /home/jianleya/trainning/cmake/cmake/cmake-cookbook/cmake-cookbook/chapter-07/recipe-01/cxx-example
	    Start 2: long
	1/2 Test #2: long .............................   Passed    0.00 sec
	    Start 1: short
	2/2 Test #1: short ............................   Passed    0.01 sec
	
	100% tests passed, 0 tests failed out of 2
	
	Total Test time (real) =   0.01 sec

color

# CMakeLists.txt

	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	project(recipe-02 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}")

# cmake/colors.cmake
	# colorize CMake output
	
	# code adapted from stackoverflow: http://stackoverflow.com/a/19578320
	# from post authored by https://stackoverflow.com/users/2556117/fraser
	
	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()

$ cmake .
# as below pic

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值