0%

CMake notes

Why “file(GLOB xxx)” is evil

About find_package

  • Check local modules: cmake –help-module-list

  • Manual for a specific module: cmake –help-module FindCUDA

  • find_package(CUDA 10.1 REQUIRED)

    • It is no longer necessary to use this module or call find_package(CUDA) for compiling CUDA code. Instead, list CUDA among the languages named in the top-level call to the project() command, or call the
      enable_language() command with CUDA.
    • However, if you want to some functions defined in FindCUDA module like cuda_select_nvcc_arch_flags(ARCH_FLAGS), you need explicitly use this module.
    • The following code will automatically pass nvidia graphic’s compute capability to nvcc.
    1
    2
    3
    4
    5
    6
    find_package(CUDA ${MIN_CUDA_VERSION} REQUIRED)

    set(CUDA_SEPARABLE_COMPILATION ON)
    cuda_select_nvcc_arch_flags(ARCH_FLAGS)
    string(REGEX MATCH "[0-9][0-9]" ARCH "${ARCH_FLAGS}")
    set(CMAKE_CUDA_ARCHITECTURES ${ARCH})
  • find_package(CUDATookit 10.1 REQUIRED)

    • This module will define CUDAToolkit_INCLUDE_DIRS (normally /usr/local/cuda/include), CUDAToolkit_LIBRARY_DIR and etc.
  • set_source_files_properties(${CUDA_SOURCE_FILES} PROPERTIES LANGUAGE CUDA)

    • This command should in the same CMakeLists.txt file with the add_library(xxx) command.

CMake Command

  • shell command: cmake . -B build -G “Unix Makefiles”
  • shell command: cmake –build build
  • shell command: cmake –build build –target test
  • shell command: cmake –build build –target install

Template for future referance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(projectname LANGUAGES CXX CUDA)

option(ENABLE_DEBUG_SYMBOLS "whether to build with debug symbols" OFF)
option(ENABLE_SANITY_CHECK "whether to build with sanity cheks" OFF)

if(ENABLE_DEBUG_SYMBOLS)
set(CMAKE_CXX_FLAGS "{CMAKE_CXX_FLAGS} -g")
endif(ENABLE_DEBUG_SYMBOLS)

if(ENABLE_SANITY_CHECK)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -DNDEBUG -fsanitize=address -fno-omit-frame-pointer -Wno-unused-parameter")
else(ENABLE_SANITY_CHECK)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
endif(ENABLE_SANITY_CHECK)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(MIN_CUDA_VERSION "10.0")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")

add_subdirectory(projectname)