cmake_minimum_required(VERSION 3.5 FATAL_ERROR)# project name, in this case no language requiredproject(recipe-01 LANGUAGES NONE)# print custom message depending on the operating systemif(CMAKE_SYSTEM_NAME STREQUAL "Linux")message(STATUS "Configuring on/for Linux")elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")message(STATUS "Configuring on/for macOS")elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")message(STATUS "Configuring on/for Windows")elseif(CMAKE_SYSTEM_NAME STREQUAL "AIX")message(STATUS "Configuring on/for IBM AIX")else()message(STATUS "Configuring on/for ${CMAKE_SYSTEM_NAME}")endif()#log
$ cmake .-- Configuring on/for Linux
-- Configuring done
-- Generating done
-- Build files have been written to:/home/jianleya/trainning/cmake/cmake/cmake-cookbook/cmake-cookbook/chapter-02/recipe-01/example
CMAKE_SYSTEM_NAME (platform independent code)
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(recipe-02 LANGUAGES CXX)add_executable(hello-world hello-world.cpp)# let the preprocessor know about the system nameif(CMAKE_SYSTEM_NAME STREQUAL "Linux")target_compile_definitions(hello-world PUBLIC "IS_LINUX")endif()if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")target_compile_definitions(hello-world PUBLIC "IS_MACOS")endif()if(CMAKE_SYSTEM_NAME STREQUAL "Windows")target_compile_definitions(hello-world PUBLIC "IS_WINDOWS")endif()
#include<cstdlib>#include<iostream>#include<string>
std::string say_hello(){#ifdef IS_WINDOWSreturn std::string("Hello from Windows!");#elif IS_LINUXreturn std::string("Hello from Linux!");#elif IS_MACOSreturn std::string("Hello from macOS!");#elsereturn std::string("Hello from an unknown system!");#endif}intmain(){
std::cout <<say_hello()<< std::endl;return EXIT_SUCCESS;}
CMAKE_CXX_COMPILER_ID (compiler ID)
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)# project name and languageproject(recipe-03 LANGUAGES CXX)add_executable(hello-world hello-world.cpp)# compiler nametarget_compile_definitions(hello-world PUBLIC "COMPILER_NAME=\"${CMAKE_CXX_COMPILER_ID}\"")# let the preprocessor know about the compiler vendorif(CMAKE_CXX_COMPILER_ID MATCHES Intel)target_compile_definitions(hello-world PUBLIC "IS_INTEL_CXX_COMPILER")endif()if(CMAKE_CXX_COMPILER_ID MATCHES GNU)target_compile_definitions(hello-world PUBLIC "IS_GNU_CXX_COMPILER")endif()if(CMAKE_CXX_COMPILER_ID MATCHES PGI)target_compile_definitions(hello-world PUBLIC "IS_PGI_CXX_COMPILER")endif()if(CMAKE_CXX_COMPILER_ID MATCHES XL)target_compile_definitions(hello-world PUBLIC "IS_XL_CXX_COMPILER")endif()# etc ...
#include<cstdlib>#include<iostream>#include<string>
std::string say_hello(){#ifdef IS_INTEL_CXX_COMPILER// only compiled when Intel compiler is selected// such compiler will not compile the other branchesreturn std::string("Hello Intel compiler!");#elif IS_GNU_CXX_COMPILER// only compiled when GNU compiler is selected// such compiler will not compile the other branchesreturn std::string("Hello GNU compiler!");#elif IS_PGI_CXX_COMPILER// etc.return std::string("Hello PGI compiler!");#elif IS_XL_CXX_COMPILERreturn std::string("Hello XL compiler!");#elsereturn std::string("Hello unknown compiler - have we met before?");#endif}intmain(){
std::cout <<say_hello()<< std::endl;
std::cout <<"compiler name is " COMPILER_NAME << std::endl;return EXIT_SUCCESS;}
CMAKE_HOST_SYSTEM_PROCESSOR(cpu arch)
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(recipe-04 LANGUAGES CXX)# define executable and its source fileadd_executable(arch-dependent arch-dependent.cpp)# let the preprocessor know about the size of void *if(CMAKE_SIZEOF_VOID_P EQUAL 8)target_compile_definitions(arch-dependent PUBLIC "IS_64_BIT_ARCH")message(STATUS "Target is 64 bits")else()target_compile_definitions(arch-dependent PUBLIC "IS_32_BIT_ARCH")message(STATUS "Target is 32 bits")endif()# host cpu architectureif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "i386")message(STATUS "i386 architecture detected")elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "i686")message(STATUS "i686 architecture detected")elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")message(STATUS "x86_64 architecture detected")else()message(STATUS "host processor architecture is unknown")endif()target_compile_definitions(arch-dependent
PUBLIC "ARCHITECTURE=${CMAKE_HOST_SYSTEM_PROCESSOR}")
#include<cstdlib>#include<iostream>#include<string>#define STRINGIFY(x) #x#define TOSTRING(x) STRINGIFY(x)
std::string say_hello(){
std::string arch_info(TOSTRING(ARCHITECTURE));
arch_info += std::string(" architecture. ");#ifdef IS_32_BIT_ARCHreturn arch_info + std::string("Compiled on a 32 bit host processor.");#elif IS_64_BIT_ARCHreturn arch_info + std::string("Compiled on a 64 bit host processor.");#elsereturn arch_info + std::string("Neither 32 nor 64 bit, puzzling ...");#endif}intmain(){
std::cout <<say_hello()<< std::endl;return EXIT_SUCCESS;}
cpu instruction set
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)project(recipe-05 LANGUAGES CXX)# define executableadd_executable(processor-info "")# and its source filetarget_sources(processor-info
PRIVATE
processor-info.cpp
)# and its include directoriestarget_include_directories(processor-info
PRIVATE
${PROJECT_BINARY_DIR})foreach(key
IN ITEMS
NUMBER_OF_LOGICAL_CORES
NUMBER_OF_PHYSICAL_CORES
TOTAL_VIRTUAL_MEMORY
AVAILABLE_VIRTUAL_MEMORY
TOTAL_PHYSICAL_MEMORY
AVAILABLE_PHYSICAL_MEMORY
IS_64BIT
HAS_FPU
HAS_MMX
HAS_MMX_PLUS
HAS_SSE
HAS_SSE2
HAS_SSE_FP
HAS_SSE_MMX
HAS_AMD_3DNOW
HAS_AMD_3DNOW_PLUS
HAS_IA64
OS_NAME
OS_RELEASE
OS_VERSION
OS_PLATFORM
)cmake_host_system_information(RESULT _${key} QUERY ${key})endforeach()configure_file(config.h.in config.h @ONLY)