Last active
November 23, 2022 14:36
-
-
Save Natsu-Akatsuki/f5de1111901da2efdb420c65dc3d8f26 to your computer and use it in GitHub Desktop.
CMake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ref https://stackoverflow.com/questions/18968979/how-to-get-colorized-output-with-cmake | |
if (NOT WIN32) | |
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 () | |
message("This is normal") | |
message("${Red}This is Red${ColourReset}") | |
message("${Green}This is Green${ColourReset}") | |
message("${Yellow}This is Yellow${ColourReset}") | |
message("${Blue}This is Blue${ColourReset}") | |
message("${Magenta}This is Magenta${ColourReset}") | |
message("${Cyan}This is Cyan${ColourReset}") | |
message("${White}This is White${ColourReset}") | |
message("${BoldRed}This is BoldRed${ColourReset}") | |
message("${BoldGreen}This is BoldGreen${ColourReset}") | |
message("${BoldYellow}This is BoldYellow${ColourReset}") | |
message("${BoldBlue}This is BoldBlue${ColourReset}") | |
message("${BoldMagenta}This is BoldMagenta${ColourReset}") | |
message("${BoldCyan}This is BoldCyan${ColourReset}") | |
message("${BoldWhite}This is BoldWhite\n\n${ColourReset}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# >>> evaluate compile and link time >>> | |
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CMAKE_COMMAND} -E time") | |
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK "${CMAKE_COMMAND} -E time") | |
# <<< evaluate compile and link time | |
# >>> save compile product >>> | |
target_compile_options(<target_name> PUBLIC "-save-temps") | |
# >>> common configuration >>> | |
set(CMAKE_CXX_STANDARD 14) | |
set(CMAKE_BUILD_TYPE DEBUG) | |
set(CMAKE_VERBOSE_MAKEFILE ON) # 查看具体的编译项 | |
# >>> show warning >>> | |
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wdeprecated-declarations -Wall -Wwrite-strings -Wconversion") | |
# -Wconversion: e.g double -> float (narrowing conversion) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
find_package(OpenMP REQUIRED) | |
if (OPENMP_FOUND) | |
message("OPENMP FOUND") | |
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") | |
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") # -fopenmp | |
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}") | |
endif () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# >>> pybind11 >>> | |
# get python version | |
execute_process( | |
COMMAND python3 --version | |
COMMAND cut -c 8- | |
OUTPUT_VARIABLE PYTHON_VERSION) | |
string(STRIP ${PYTHON_VERSION} PYTHON_VERSION) | |
set(PYBIND11_PYTHON_VERSION ${PYTHON_VERSION}) | |
message(STATUS "Python version is ${PYBIND11_PYTHON_VERSION}") | |
find_package(pybind11) | |
# <<< pybind11 <<< |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## usage: | |
## include(cmake/TensorRT.cmake) | |
## CUDA_LIBRARIES, CUDNN_LIBRARY, TENSORRT_LIBRARIES | |
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=deprecated-declarations -Wno-deprecated-declarations -Wno-deprecated -Wno-cpp") | |
# suppress eigen warning: "-Wcpp Please use cuda_runtime_api.h or cuda_runtime.h instead" | |
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-cpp") | |
if (NOT WIN32) | |
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 () | |
# >>> CUDA >>> | |
option(CUDA_AVAIL "CUDA available" OFF) | |
find_package(CUDA) | |
if (CUDA_FOUND) | |
find_library(CUBLAS_LIBRARIES cublas HINTS | |
${CUDA_TOOLKIT_ROOT_DIR}/lib64 | |
${CUDA_TOOLKIT_ROOT_DIR}/lib | |
) | |
include_directories(${CUDA_INCLUDE_DIRS}) | |
message(STATUS "${Green}CUDA is available!${ColourReset}") | |
set(CUDA_AVAIL ON) | |
else () | |
message(SEND_ERROR "${Red}CUDA NOT FOUND${ColourReset}") | |
set(CUDA_AVAIL OFF) | |
endif (CUDA_FOUND) | |
message(STATUS "${Blue}CUDA Libs: ${CUDA_LIBRARIES}${ColourReset}") | |
message(STATUS "${Blue}CUDA Headers: ${CUDA_INCLUDE_DIRS}${ColourReset}") | |
# >>> CUDA >>> | |
# >>> CUDNN >>> | |
# set flags for CUDNN availability | |
option(CUDNN_AVAIL "CUDNN available" OFF) | |
# try to find the CUDNN module | |
find_library(CUDNN_LIBRARY_PATH cudnn | |
HINTS ${CUDA_TOOLKIT_ROOT_DIR}/lib64/ | |
DOC "CUDNN library." | |
) | |
if (CUDNN_LIBRARY_PATH) | |
message(STATUS "${Green}[INFO] CUDNN is available!${ColourReset}") | |
set(CUDNN_LIBRARY ${CUDNN_LIBRARY_PATH}) | |
set(CUDNN_AVAIL ON) | |
else () | |
message(SEND_ERROR "${Red}[ERROR] CUDNN is NOT Available${ColourReset}") | |
set(CUDNN_AVAIL OFF) | |
endif () | |
message(STATUS "${Blue}CUDNN_LIBRARY: ${CUDNN_LIBRARY_PATH}${ColourReset}") | |
# >>> CUDNN >>> | |
# >>> TensorRT >>> | |
option(TRT_AVAIL "TensorRT available" OFF) | |
# 检查tensorrt的安装方式 | |
execute_process( | |
COMMAND dpkg -l | |
COMMAND grep nv-tensorrt | |
OUTPUT_VARIABLE TRT_IS_DEB) | |
if (NOT TRT_IS_DEB) | |
message(STATUS "${Red}[WARNING] Tensorrt is not installed through DEB package, should specify the LIBRARY_PATH and INCLUDE_PATH explicitly${ColourReset}") | |
set(TENSORRT_DIR $ENV{HOME}/Application/TensorRT-8.4.1.5) | |
include_directories(${TENSORRT_DIR}/include) | |
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "${TENSORRT_DIR}/lib") | |
endif () | |
find_library(NVINFER NAMES nvinfer) | |
find_library(NVPARSERS NAMES nvparsers) | |
find_library(NVINFER_PLUGIN NAMES nvinfer_plugin) | |
find_library(NVONNX_PARSER NAMES nvonnxparser) | |
find_library(NVCAFFE_PARSER NAMES nvcaffe_parser) | |
set(TENSORRT_LIBRARIES ${NVINFER} ${NVPARSERS} ${NVINFER_PLUGIN} ${NVONNX_PARSER} ${NVCAFFE_PARSER}) | |
set(TRT_AVAIL ON) | |
message(STATUS "${Blue}NVINFER: ${NVINFER}${ColourReset}") | |
message(STATUS "${Blue}NVPARSERS: ${NVPARSERS}${ColourReset}") | |
message(STATUS "${Blue}NVINFER_PLUGIN: ${NVINFER_PLUGIN}${ColourReset}") | |
message(STATUS "${Blue}NVONNX_PARSER: ${NVONNX_PARSER}${ColourReset}") | |
if (NVINFER AND NVPARSERS AND NVINFER_PLUGIN AND NVONNX_PARSER) | |
message(STATUS "${Green}[INFO] TensorRT is available!${ColourReset}") | |
set(TRT_AVAIL ON) | |
else () | |
message(SEND_ERROR "${Red}[ERROR] TensorRT is NOT Available${ColourReset}") | |
set(TRT_AVAIL OFF) | |
endif () | |
# >>> TensorRT >>> | |
if (NOT (TRT_AVAIL AND CUDA_AVAIL AND CUDNN_AVAIL)) | |
message(FATAL_ERROR "[ERROR] Tensorrt model won't be built, CUDA and/or CUDNN and/or TensorRT were not found.") | |
endif () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# >>> EIGEN >>> | |
find_package(Eigen3 REQUIRED) | |
include_directories(${EIGEN3_INCLUDE_DIRS}) | |
# 使用自定义版本的EIGEN | |
set(Eigen3_DIR "../share/eigen3/cmake/") | |
# 令PCL点云库使用指定的EIGEN库(PCL库自己写了一个Eigen.cmake,默认用系统的Eigen) | |
set(EIGEN_ROOT "../eigen/include/") | |
# >>> EIGEN >>> | |
# >>> PCL >>> | |
set(PCL_DIR "$ENV{HOME}/pcl-1.10/share/pcl-1.10") | |
find_package(PCL QUIET) | |
include_directories(${PCL_INCLUDE_DIRS}) | |
target_link_libraries(<target_name> ${PCL_LIBRARIES}) | |
# >>> PCL >>> | |
# >>> SOPHUS >>> | |
find_package(Sophus REQUIRED) | |
include_directories(${Sophus_INCLUDE_DIRS}) | |
# SOPHUS默认使用fmt,若不要使用fmt则编译时需要-DUSE_BASIC_LOGGING=ON | |
target_link_libraries(${Sophus_LIBRARIES} fmt::fmt) | |
# >>> SOPHUS >>> | |
# >>> FMT >>> | |
find_package(FMT REQUIRED) | |
target_link_libraries(<target_name> fmt::fmt) | |
# 或者 | |
target_link_libraries(<target_name> fmt) | |
# >>> FMT >>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment