Skip to content

Commit

Permalink
Setting compiler flags as in the other R&D projects.
Browse files Browse the repository at this point in the history
  • Loading branch information
krasznaa committed Oct 9, 2023
1 parent fa181e9 commit 38d6ae6
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 7 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is part of covfie, a part of the ACTS project
#
# Copyright (c) 2022 CERN
# Copyright (c) 2022-2023 CERN
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
Expand Down Expand Up @@ -50,6 +50,14 @@ option(
"Disable warnings about missing C++ features. Enabling this is strongly discouraged."
)

option(
COVFIE_FAIL_ON_WARNINGS
"Treat compiler warnings as errors."
)

# Make the CMake modules in the cmake/ directory visible to the project.
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

# The core library should always be built.
add_subdirectory(lib)

Expand Down
5 changes: 4 additions & 1 deletion benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is part of covfie, a part of the ACTS project
#
# Copyright (c) 2022 CERN
# Copyright (c) 2022-2023 CERN
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
Expand All @@ -16,6 +16,9 @@ find_package(
REQUIRED
)

# Set up the C++ compiler flags for the benchmarks.
include(covfie-compiler-options-cpp)

# Common benchmarking components must be build.
add_subdirectory(common)

Expand Down
5 changes: 4 additions & 1 deletion benchmarks/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# This file is part of covfie, a part of the ACTS project
#
# Copyright (c) 2022 CERN
# Copyright (c) 2022-2023 CERN
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.

enable_language(CUDA)

# Set up the CUDA compiler flags for the benchmarks.
include(covfie-compiler-options-cuda)

# Create the benchmark executable from the individual files.
add_executable(
benchmark_cuda
Expand Down
43 changes: 43 additions & 0 deletions cmake/covfie-compiler-options-cpp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This file is part of covfie, a part of the ACTS project
#
# Copyright (c) 2023 CERN
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.

# Include the helper function(s).
include( covfie-functions )

# Turn on a number of warnings for the "known compilers".
if( ( "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU" ) OR
( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" ) )

# Basic flags for all build modes.
covfie_add_flag( CMAKE_CXX_FLAGS "-Wall" )
covfie_add_flag( CMAKE_CXX_FLAGS "-Wextra" )
covfie_add_flag( CMAKE_CXX_FLAGS "-Wshadow" )
covfie_add_flag( CMAKE_CXX_FLAGS "-Wunused-local-typedefs" )
covfie_add_flag( CMAKE_CXX_FLAGS "-pedantic" )

# Fail on warnings, if asked for that behaviour.
if( COVFIE_FAIL_ON_WARNINGS )
covfie_add_flag( CMAKE_CXX_FLAGS "-Werror" )
endif()

elseif( "${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC" )

# Basic flags for all build modes.
string( REGEX REPLACE "/W[0-9]" "" CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS}" )
covfie_add_flag( CMAKE_CXX_FLAGS "/W4" )

# Fail on warnings, if asked for that behaviour.
if( COVFIE_FAIL_ON_WARNINGS )
covfie_add_flag( CMAKE_CXX_FLAGS "/WX" )
endif()

# Turn on the correct setting for the __cplusplus macro with MSVC.
covfie_add_flag( CMAKE_CXX_FLAGS "/Zc:__cplusplus" )

endif()
44 changes: 44 additions & 0 deletions cmake/covfie-compiler-options-cuda.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This file is part of covfie, a part of the ACTS project
#
# Copyright (c) 2023 CERN
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.

# FindCUDAToolkit needs at least CMake 3.17.
cmake_minimum_required( VERSION 3.17 )

# Include the helper function(s).
include( covfie-functions )

# Figure out the properties of CUDA being used.
find_package( CUDAToolkit REQUIRED )

# Set the architecture to build code for.
set( CMAKE_CUDA_ARCHITECTURES "52" CACHE STRING
"CUDA architectures to build device code for" )

# Turn on the correct setting for the __cplusplus macro with MSVC.
if( "${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC" )
covfie_add_flag( CMAKE_CUDA_FLAGS "-Xcompiler /Zc:__cplusplus" )
endif()

if( "${CMAKE_CUDA_COMPILER_ID}" MATCHES "NVIDIA" )
# Make CUDA generate debug symbols for the device code as well in a debug
# build.
covfie_add_flag( CMAKE_CUDA_FLAGS_DEBUG "-G" )
# Allow to use functions in device code that are constexpr, even if they are
# not marked with __device__.
covfie_add_flag( CMAKE_CUDA_FLAGS "--expt-relaxed-constexpr" )
endif()

# Fail on warnings, if asked for that behaviour.
if( COVFIE_FAIL_ON_WARNINGS )
if( ( "${CUDAToolkit_VERSION}" VERSION_GREATER_EQUAL "10.2" ) AND
( "${CMAKE_CUDA_COMPILER_ID}" MATCHES "NVIDIA" ) )
covfie_add_flag( CMAKE_CUDA_FLAGS "-Werror all-warnings" )
elseif( "${CMAKE_CUDA_COMPILER_ID}" MATCHES "Clang" )
covfie_add_flag( CMAKE_CUDA_FLAGS "-Werror" )
endif()
endif()
29 changes: 29 additions & 0 deletions cmake/covfie-functions.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This file is part of covfie, a part of the ACTS project
#
# Copyright (c) 2023 CERN
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.

# Helper function for adding individual flags to "flag variables".
#
# Usage: covfie_add_flag( CMAKE_CXX_FLAGS "-Wall" )
#
function( covfie_add_flag name value )

# Escape special characters in the value:
set( matchedValue "${value}" )
foreach( c "*" "." "^" "$" "+" "?" )
string( REPLACE "${c}" "\\${c}" matchedValue "${matchedValue}" )
endforeach()

# Check if the variable already has this value in it:
if( "${${name}}" MATCHES "${matchedValue}" )
return()
endif()

# If not, then let's add it now:
set( ${name} "${${name}} ${value}" PARENT_SCOPE )

endfunction( covfie_add_flag )
5 changes: 4 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# This file is part of covfie, a part of the ACTS project
#
# Copyright (c) 2022 CERN
# Copyright (c) 2022-2023 CERN
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.

# Set up the C++ compiler flags for the examples.
include(covfie-compiler-options-cpp)

# The common tools should be available for examples on all platforms.
add_subdirectory(common)

Expand Down
5 changes: 4 additions & 1 deletion examples/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is part of covfie, a part of the ACTS project
#
# Copyright (c) 2022 CERN
# Copyright (c) 2022-2023 CERN
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
Expand All @@ -10,6 +10,9 @@
# necessary tooling is set up.
enable_language(CUDA)

# Set up the CUDA compiler flags for the examples.
include(covfie-compiler-options-cuda)

# Add the 3D field slice rendering tool based on CUDA.
add_executable(
render_slice_cuda
Expand Down
5 changes: 4 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is part of covfie, a part of the ACTS project
#
# Copyright (c) 2022 CERN
# Copyright (c) 2022-2023 CERN
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
Expand All @@ -9,6 +9,9 @@
# All the tests here will require Google Test, so we will go ahead and find it.
find_package(GTest CONFIG REQUIRED)

# Set up the C++ compiler flags for the tests.
include(covfie-compiler-options-cpp)

# The testing utils are always built.
add_subdirectory(utils)

Expand Down
5 changes: 4 additions & 1 deletion tests/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is part of covfie, a part of the ACTS project
#
# Copyright (c) 2022 CERN
# Copyright (c) 2022-2023 CERN
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
Expand All @@ -9,6 +9,9 @@
# Enable the CUDA language!
enable_language(CUDA)

# Set up the CUDA compiler flags for the tests.
include(covfie-compiler-options-cuda)

# Create the test executable from the individual test groups.
add_executable(
test_cuda
Expand Down

0 comments on commit 38d6ae6

Please sign in to comment.