Skip to content

Commit

Permalink
Merge pull request #7647 from niboshi/cleanup-cmake
Browse files Browse the repository at this point in the history
Cleanup `CMakeList.txt`
  • Loading branch information
take-cheeze authored Jul 3, 2019
2 parents 6fef53f 5324c95 commit 98c8b25
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 80 deletions.
179 changes: 99 additions & 80 deletions chainerx_cc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 12,57 @@ SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
include(cmake/clang-tidy.cmake)
include(cmake/third-party.cmake)

# Configure options
option(CHAINERX_BUILD_PYTHON "Build Python binding" OFF)
option(CHAINERX_BUILD_TEST "Build test" OFF)
option(CHAINERX_BUILD_EXAMPLES "Build examples" OFF)
option(CHAINERX_WARNINGS_AS_ERRORS "Make all warnings of compilers into errors" ON)
option(CHAINERX_ENABLE_THREAD_SANITIZER "Enable thread sanitizer." OFF)
#---------
# Options
#---------

# CHAINERX_CUDNN_USE_CUPY
if(DEFINED ENV{CHAINERX_CUDNN_USE_CUPY})
set(DEFAULT_CHAINERX_CUDNN_USE_CUPY $ENV{CHAINERX_CUDNN_USE_CUPY})
else()
set(DEFAULT_CHAINERX_CUDNN_USE_CUPY OFF)
endif()
option(CHAINERX_CUDNN_USE_CUPY "Use existing CuPy installation for cuDNN headers and libraries" ${DEFAULT_CHAINERX_CUDNN_USE_CUPY})

# CHAINERX_ENABLE_COVERAGE
if(DEFINED ENV{CHAINERX_ENABLE_COVERAGE})
set(DEFAULT_CHAINERX_ENABLE_COVERAGE $ENV{CHAINERX_ENABLE_COVERAGE})
else()
set(DEFAULT_CHAINERX_ENABLE_COVERAGE OFF)
endif()
option(CHAINERX_ENABLE_COVERAGE "Enable test coverage with gcov" ${DEFAULT_CHAINERX_ENABLE_COVERAGE})

# CHAINERX_BUILD_CUDA
if(DEFINED ENV{CHAINERX_BUILD_CUDA})
set(DEFAULT_CHAINERX_BUILD_CUDA $ENV{CHAINERX_BUILD_CUDA})
else()
set(DEFAULT_CHAINERX_BUILD_CUDA OFF)
endif()
option(CHAINERX_BUILD_CUDA "Build CUDA backend (if CUDA is available)" ${DEFAULT_CHAINERX_BUILD_CUDA})

# Allow to specify *one* --generate-code option of the nvcc command.
# Supposed usage is to avoid slowness of PTX JIT compilation on development.
set(CHAINERX_NVCC_GENERATE_CODE "$ENV{CHAINERX_NVCC_GENERATE_CODE}" CACHE STRING "nvcc --generate-code option")

# CHAINERX_ENABLE_BLAS
if(DEFINED ENV{CHAINERX_ENABLE_BLAS})
set(DEFAULT_CHAINERX_ENABLE_BLAS $ENV{CHAINERX_ENABLE_BLAS})
else()
set(DEFAULT_CHAINERX_ENABLE_BLAS ON)
endif()

option(CHAINERX_BUILD_PYTHON "Build Python binding" OFF)
option(CHAINERX_BUILD_TEST "Build test" OFF)
option(CHAINERX_BUILD_EXAMPLES "Build examples" OFF)
option(CHAINERX_WARNINGS_AS_ERRORS "Make all warnings of compilers into errors" ON)
option(CHAINERX_ENABLE_THREAD_SANITIZER "Enable thread sanitizer." OFF)
option(CHAINERX_CUDNN_USE_CUPY "Use existing CuPy installation for cuDNN headers and libraries" ${DEFAULT_CHAINERX_CUDNN_USE_CUPY})
option(CHAINERX_ENABLE_COVERAGE "Enable test coverage with gcov" ${DEFAULT_CHAINERX_ENABLE_COVERAGE})
option(CHAINERX_BUILD_CUDA "Build CUDA backend (if CUDA is available)" ${DEFAULT_CHAINERX_BUILD_CUDA})
option(CHAINERX_ENABLE_BLAS "Use BLAS if available" ${DEFAULT_CHAINERX_ENABLE_BLAS})

if(MSVC)
option(CUDA_USE_STATIC_CUDA_RUNTIME "Use the static version of the CUDA runtime library if available" OFF)
set(CUDA_USE_STATIC_CUDA_RUNTIME OFF CACHE INTERNAL "")
endif()

#-------------
# Build setup
#-------------

# Set CMAKE_BUILD_TYPE (defaults to Release).
# CMake's specification is case-insensitive, but we only accept capitalized ones.
if(NOT CMAKE_BUILD_TYPE)
Expand All @@ -65,15 73,67 @@ if(NOT ((CMAKE_BUILD_TYPE STREQUAL "Release") OR (CMAKE_BUILD_TYPE STREQUAL "Deb
message(FATAL_ERROR "Invalid CMAKE_BUILD_TYPE: " ${CMAKE_BUILD_TYPE})
endif()

# CUDA
if(${CHAINERX_BUILD_CUDA})
#-----------
# C setup
#-----------
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE 1) # ref. https://texus.me/2015/09/06/cmake-and-gcov/

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU|Clang|Intel")
if(MSVC)
option(CUDA_USE_STATIC_CUDA_RUNTIME "Use the static version of the CUDA runtime library if available" OFF)
set(CUDA_USE_STATIC_CUDA_RUNTIME OFF CACHE INTERNAL "")
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
else()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -Wextra -fPIC -pthread")
endif()
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG") # cmake -DCMAKE_BUILD_TYPE=Release
set(CMAKE_CXX_FLAGS_DEBUG "-O0") # cmake -DCMAKE_BUILD_TYPE=Debug

# cmake -DCHAINERX_ENABLE_COVERAGE=ON|OFF
if(CHAINERX_ENABLE_COVERAGE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
endif()

# cmake -DCHAINERX_WARNINGS_AS_ERRORS=ON|OFF
if (CHAINERX_WARNINGS_AS_ERRORS)
if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
endif()
endif()

# cmake -DCHAINERX_ENABLE_THREAD_SANITIZER=ON|OFF
if(CHAINERX_ENABLE_THREAD_SANITIZER)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
endif()
endif()

# Compiler-specific workarounds

if(CMAKE_CXX_COMPILER_ID MATCHES "^(Clang|AppleClang)$" AND NOT MSVC)
EXECUTE_PROCESS(COMMAND ${CMAKE_CXX_COMPILER} --version OUTPUT_VARIABLE clang_full_version_string)
string(REGEX REPLACE ".*clang version ([0-9] \\.[0-9] ).*" "\\1" CLANG_VERSION_STRING ${clang_full_version_string})

if(CLANG_VERSION_STRING VERSION_LESS 6.0)
# clang<6.0 suggests superfluous braces for std::array initialization.
# https://bugs.llvm.org/show_bug.cgi?id=21629
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-missing-braces")
endif()
endif()


#--------------
# Dependencies
#--------------

# CUDA
if(${CHAINERX_BUILD_CUDA})
find_package(CUDA QUIET)
if(${CUDA_FOUND})
list(APPEND CUDA_NVCC_FLAGS "-std=c ${CMAKE_CXX_STANDARD}")

# CUDA_cublas_device_LIBRARY is required for CUDA > 9.2 with cmake < to 3.12.2.
# This is because cublas_device was deprecated in CUDA 9.2, but FindCUDA supported it in 3.12.2.
set(CUDA_cublas_device_LIBRARY ${CUDA_LIBRARIES})
Expand Down Expand Up @@ -105,6 165,10 @@ if(${CHAINERX_BUILD_CUDA})
endif()
endif()

# Allow to specify *one* --generate-code option of the nvcc command.
# Supposed usage is to avoid slowness of PTX JIT compilation on development.
set(CHAINERX_NVCC_GENERATE_CODE "$ENV{CHAINERX_NVCC_GENERATE_CODE}" CACHE STRING "nvcc --generate-code option")

find_package(CuDNN 7 REQUIRED)
include_directories(${CUDNN_INCLUDE_DIRS})
if(NOT CHAINERX_NVCC_GENERATE_CODE STREQUAL "")
Expand Down Expand Up @@ -137,60 201,6 @@ if(${CHAINERX_ENABLE_BLAS})
endif()
endif()

# C setup
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE 1) # ref. https://texus.me/2015/09/06/cmake-and-gcov/

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU|Clang|Intel")
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
else()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -Wextra -fPIC -pthread")
endif()
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG") # cmake -DCMAKE_BUILD_TYPE=Release
set(CMAKE_CXX_FLAGS_DEBUG "-O0") # cmake -DCMAKE_BUILD_TYPE=Debug
if (${CUDA_FOUND})
list(APPEND CUDA_NVCC_FLAGS -std=c 14)
endif()

# cmake -DCHAINERX_ENABLE_COVERAGE=ON|OFF
if(CHAINERX_ENABLE_COVERAGE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
endif()

# cmake -DCHAINERX_WARNINGS_AS_ERRORS=ON|OFF
if (CHAINERX_WARNINGS_AS_ERRORS)
if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
endif()
endif()

# cmake -DCHAINERX_ENABLE_THREAD_SANITIZER=ON|OFF
if(CHAINERX_ENABLE_THREAD_SANITIZER)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
endif()
endif()

# Compiler-specific workarounds

if(CMAKE_CXX_COMPILER_ID MATCHES "^(Clang|AppleClang)$" AND NOT MSVC)
EXECUTE_PROCESS(COMMAND ${CMAKE_CXX_COMPILER} --version OUTPUT_VARIABLE clang_full_version_string)
string(REGEX REPLACE ".*clang version ([0-9] \\.[0-9] ).*" "\\1" CLANG_VERSION_STRING ${clang_full_version_string})

if(CLANG_VERSION_STRING VERSION_LESS 6.0)
# clang<6.0 suggests superfluous braces for std::array initialization.
# https://bugs.llvm.org/show_bug.cgi?id=21629
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-missing-braces")
endif()
endif()


include_directories("${PROJECT_SOURCE_DIR}")

# dl libs
if(DEFINED CMAKE_DL_LIBS)
else()
Expand Down Expand Up @@ -221,12 231,7 @@ get_third_party(abseil)
include_directories("${CMAKE_CURRENT_BINARY_DIR}/abseil")
add_subdirectory("${CMAKE_CURRENT_BINARY_DIR}/abseil" "${CMAKE_CURRENT_BINARY_DIR}/abseil-build")

# Test

# ChainerX is linked with MultiThreaded DLL, including gtest
if(NOT(CMAKE_CXX_COMPILER_ID MATCHES "GCC|Clang") AND MSVC)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
endif()
# gtest

# TODO(niboshi): Remove gtest dependency from testing
get_third_party(gtest)
Expand All @@ -235,18 240,32 @@ add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
EXCLUDE_FROM_ALL)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/googletest-src/googletest/include)

# TODO(durswd): Remove it. It is hack for MSVC LLVM
if(CMAKE_CXX_COMPILER_ID MATCHES "GCC|Clang" AND MSVC)
target_compile_options(gtest INTERFACE -Wno-global-constructors PRIVATE -w)
if(MSVC)
if(CMAKE_CXX_COMPILER_ID MATCHES "GCC|Clang")
# TODO(durswd): Remove it. It is hack for MSVC LLVM
target_compile_options(gtest INTERFACE -Wno-global-constructors PRIVATE -w)
else()
# ChainerX is linked with MultiThreaded DLL, including gtest
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
endif()
endif()


#------
# Test
#------
if(${CHAINERX_BUILD_TEST})
enable_testing()
endif()

#----------
# Examples
#----------
if(${CHAINERX_BUILD_EXAMPLES})
add_subdirectory(examples)
endif()

#----------
# ChainerX
#----------
add_subdirectory(chainerx)
2 changes: 2 additions & 0 deletions chainerx_cc/chainerx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 1,5 @@
include_directories("${PROJECT_SOURCE_DIR}")

add_subdirectory(kernels)
add_subdirectory(routines)
add_subdirectory(native)
Expand Down
2 changes: 2 additions & 0 deletions chainerx_cc/examples/mnist/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 1,5 @@
include_directories("${PROJECT_SOURCE_DIR}")

add_executable(train_mnist
mnist.cc
train_mnist.cc
Expand Down

0 comments on commit 98c8b25

Please sign in to comment.