Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul C/CPP cmake configuration #14

Merged
merged 19 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#=============================================================================
# Copyright (c) 2020, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(NVTX-C VERSION 0.1.0 LANGUAGES C)

###################################################################################################
# - build type ------------------------------------------------------------------------------------

# Set a default build type if none was specified
set(DEFAULT_BUILD_TYPE "Release")

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to '${DEFAULT_BUILD_TYPE}' since none specified.")
set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}" CACHE
STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
"Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()

###################################################################################################
# - nvtx target -----------------------------------------------------------------------------------
add_library(nvtx3-c INTERFACE)
target_include_directories(nvtx3-c INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include>")
target_link_libraries(nvtx3-c INTERFACE ${CMAKE_DL_LIBS})


57 changes: 14 additions & 43 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
#=============================================================================
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
jrhemstad marked this conversation as resolved.
Show resolved Hide resolved

project(NVTX VERSION 0.1.0 LANGUAGES C CXX CUDA)
project(NVTX-CPP VERSION 0.1.0 LANGUAGES CXX)

find_package(CUDA REQUIRED)
include(cmake/CPM.cmake)

###################################################################################################
# - build type ------------------------------------------------------------------------------------
Expand All @@ -34,60 +34,31 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
"Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()

###################################################################################################
# - compiler options ------------------------------------------------------------------------------
# Include the nvtx-c directory to access the nvtx-c CMake target
add_subdirectory(nvtx-c)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
###################################################################################################
# - nvtx target -----------------------------------------------------------------------------------
add_library(nvtx3-cpp INTERFACE)
target_include_directories(nvtx3-cpp INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include>")

if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
endif(CMAKE_COMPILER_IS_GNUCXX)
target_compile_features(nvtx3-cpp INTERFACE cxx_std_11)
jcohen-nvidia marked this conversation as resolved.
Show resolved Hide resolved
target_link_libraries(nvtx3-cpp INTERFACE nvtx3-c)

# Build options
option(BUILD_TESTS "Configure CMake to build tests" ON)
option(BUILD_BENCHMARKS "Configure CMake to build (google) benchmarks" ON)

###################################################################################################
# - cmake modules ---------------------------------------------------------------------------------

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/" ${CMAKE_MODULE_PATH})

include(FeatureSummary)
include(CheckIncludeFiles)
include(CheckLibraryExists)

###################################################################################################
# - add gtest -------------------------------------------------------------------------------------

include(CTest)
include(ConfigureGoogleTest)

if(BUILD_TESTS)
if(GTEST_FOUND)
message(STATUS "Google C++ Testing Framework (Google Test) found in ${GTEST_ROOT}")
include_directories(${GTEST_INCLUDE_DIR})
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tests)
else()
message(AUTHOR_WARNING "Google C++ Testing Framework (Google Test) not found: automated tests are disabled.")
endif(GTEST_FOUND)
add_subdirectory(tests)
endif(BUILD_TESTS)

###################################################################################################
# - add google benchmark --------------------------------------------------------------------------

if(BUILD_BENCHMARKS)
include(ConfigureGoogleBenchmark)
if(GBENCH_FOUND)
message(STATUS "Google C++ Benchmarking Framework (Google Benchmark) found in ${GBENCH_ROOT}")
include_directories(${GBENCH_INCLUDE_DIR})
add_subdirectory(${CMAKE_SOURCE_DIR}/benchmarks)
else()
message(AUTHOR_WARNING "Google C++ Benchmarking Framework (Google Benchmark) not found: automated tests are disabled.")
endif(GBENCH_FOUND)
add_subdirectory(benchmarks)
endif(BUILD_BENCHMARKS)


###################################################################################################
# - build doxygen ---------------------------------------------------------------------------------
add_custom_command(OUTPUT BUILD_DOXYGEN
Expand Down
46 changes: 19 additions & 27 deletions cpp/benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,24 @@
#=============================================================================
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
jrhemstad marked this conversation as resolved.
Show resolved Hide resolved

project(NVTX_BENCHS LANGUAGES C CXX CUDA)

###################################################################################################
# - compiler options ------------------------------------------------------------------------------
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_CUDA_STANDARD 14)
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
CPMAddPackage(
NAME benchmark
GITHUB_REPOSITORY google/benchmark
VERSION 1.5.2
GIT_SHALLOW TRUE
OPTIONS
"BENCHMARK_ENABLE_TESTING Off"
"BENCHMARK_ENABLE_INSTALL Off"
# The REGEX feature test fails when gbench's cmake is run under CPM w/ gcc5.4 because it doesn't assume C++11
# Additionally, attempting to set the CMAKE_CXX_VERSION here doesn't propogate to the feature test build
# Therefore, we just disable the feature test and assume platforms we care about have a regex impl available
"RUN_HAVE_STD_REGEX 0" #
)

if (benchmark_ADDED)
# patch google benchmark target
set_target_properties(benchmark PROPERTIES CXX_STANDARD 11)
endif()

###################################################################################################
# - compiler function -----------------------------------------------------------------------------
Expand All @@ -32,28 +41,11 @@ function(ConfigureBench CMAKE_BENCH_NAME CMAKE_BENCH_SRC)
add_executable(${CMAKE_BENCH_NAME}
${CMAKE_BENCH_SRC})
set_target_properties(${CMAKE_BENCH_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(${CMAKE_BENCH_NAME} benchmark benchmark_main pthread dl)
target_link_libraries(${CMAKE_BENCH_NAME} benchmark benchmark_main pthread nvtx3-cpp)
set_target_properties(${CMAKE_BENCH_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/gbenchmarks")
endfunction(ConfigureBench)

###################################################################################################
# - include paths ---------------------------------------------------------------------------------

include_directories("${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}"
"${CMAKE_SOURCE_DIR}/include"
"${CMAKE_SOURCE_DIR}"
"${GBENCH_INCLUDE_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}")

###################################################################################################
# - library paths ---------------------------------------------------------------------------------

link_directories("${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES}" # CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES is an undocumented/unsupported variable containing the link directories for nvcc
"${CMAKE_BINARY_DIR}/lib"
"${CMAKE_BINARY_DIR}"
"${GBENCH_LIBRARY_DIR}")

###################################################################################################
### test sources ##################################################################################
###################################################################################################
Expand Down
2 changes: 1 addition & 1 deletion cpp/benchmarks/nvtx/nvtx_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#include <benchmark/benchmark.h>

#include <nvtx3.hpp>
#include <nvtx3/nvtx3.hpp>

/**
* Measure cost of not reusing the same `event_attributes` object
Expand Down
19 changes: 19 additions & 0 deletions cpp/cmake/CPM.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
set(CPM_DOWNLOAD_VERSION 0.27.4)
jrhemstad marked this conversation as resolved.
Show resolved Hide resolved

if(CPM_SOURCE_CACHE)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()

if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
file(DOWNLOAD
https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION}
)
endif()

include(${CPM_DOWNLOAD_LOCATION})
File renamed without changes.
1 change: 1 addition & 0 deletions cpp/nvtx-c
35 changes: 11 additions & 24 deletions cpp/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
#=============================================================================
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
jrhemstad marked this conversation as resolved.
Show resolved Hide resolved

project(NVTX_TESTS LANGUAGES C CXX CUDA)
include(CTest)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_CUDA_STANDARD 14)
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
CPMAddPackage(
NAME GTest
GITHUB_REPOSITORY google/googletest
GIT_TAG release-1.10.0
VERSION 1.10.0
GIT_SHALLOW TRUE
OPTIONS "INSTALL_GTEST OFF"
# googletest >= 1.10.0 provides a cmake config file -- use it if it exists
FIND_PACKAGE_ARGUMENTS "CONFIG")

###################################################################################################
# - compiler function -----------------------------------------------------------------------------
Expand All @@ -30,29 +34,12 @@ function(ConfigureTest CMAKE_TEST_NAME CMAKE_TEST_SRC)
add_executable(${CMAKE_TEST_NAME} ${CMAKE_TEST_SRC})
set_target_properties(${CMAKE_TEST_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(${CMAKE_TEST_NAME} gmock gtest gmock_main
gtest_main pthread dl ${CUPTI_LIBRARY})
gtest_main pthread nvtx3-cpp )
set_target_properties(${CMAKE_TEST_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/gtests")
add_test(NAME ${CMAKE_TEST_NAME} COMMAND ${CMAKE_TEST_NAME})
endfunction(ConfigureTest)

###################################################################################################
# - include paths ---------------------------------------------------------------------------------

include_directories("${GTEST_INCLUDE_DIR}"
"${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}"
"${CMAKE_SOURCE_DIR}")

###################################################################################################
# - library paths ---------------------------------------------------------------------------------

link_directories("${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES}" # CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES is an undocumented/unsupported variable containing the link directories for nvcc
"${CMAKE_BINARY_DIR}/lib"
"${GTEST_LIBRARY_DIR}")

###################################################################################################
### test sources ##################################################################################
###################################################################################################

###################################################################################################
# - nvtx tests -------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/nvtx_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <gtest/gtest.h>

#include <nvtx3.hpp>
#include <nvtx3/nvtx3.hpp>

struct NVTX_Test : public ::testing::Test {
};
Expand Down