Skip to content

Commit d3f6452

Browse files
authored
Merge pull request #59 from ewanwm/feature_cmake_tidy
CMake Tidy
2 parents d96240c + f91d3ad commit d3f6452

14 files changed

+190
-158
lines changed

CMakeLists.txt

+31-56
Original file line numberDiff line numberDiff line change
@@ -12,70 +12,49 @@ elseif(NOT DEFINED CMAKE_INSTALL_PREFIX)
1212
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}")
1313
endif()
1414

15+
# user options
16+
option(NT_ENABLE_BENCHMARKING "enable benchmarking using google benchmark" OFF)
17+
option(NT_ENABLE_PYTHON "enable python interface" OFF)
18+
option(NT_COMPILE_TESTS "whether or not to compile unit and integration tests" ON)
19+
option(NT_TEST_COVERAGE "produce code coverage reports when running tests" OFF)
20+
option(NT_BUILD_TIMING "output time to build each target" OFF)
21+
option(NT_USE_PCH "NT_USE_PCH" OFF)
22+
23+
## to build the python library we require to build with the pic flag
24+
if(NT_ENABLE_PYTHON)
25+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
26+
endif()
1527

1628
# Need to add some special compile flags to check the code test coverage
17-
OPTION(NT_TEST_COVERAGE "produce code coverage reports when running tests" OFF)
18-
IF(NT_TEST_COVERAGE)
29+
if(NT_TEST_COVERAGE)
1930
message("Adding flags to check test coverage")
2031
add_compile_options("--coverage")
2132
add_link_options("--coverage")
22-
ELSE()
33+
else()
2334
message("Won't check test coverage")
24-
ENDIF()
35+
endif()
2536

26-
enable_testing()
37+
# enable ctest
38+
if(NT_COMPILE_TESTS)
39+
message("Compiling tests")
40+
enable_testing()
41+
else()
42+
message("Won't compile tests")
43+
endif()
2744

2845
##########################
2946
#### add dependencies ####
3047
##########################
3148

3249
include(cmake/CPM.cmake)
33-
34-
find_package(Protobuf)
35-
if( !Protobuf_FOUND )
36-
message( "didn't find protobuf, will try installing using cpm" )
37-
CPMAddPackage("gh:protocolbuffer/protobuf@27.4")
38-
endif()
39-
40-
find_package(Torch REQUIRED)
41-
message("Torch cxx flags: ${TORCH_CXX_FLAGS}")
42-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
43-
44-
CPMAddPackage("gh:gabime/spdlog@1.8.2")
45-
46-
# If user wants to enable benchmarking we need to set up google benchmark dependency
47-
OPTION(NT_ENABLE_BENCHMARKING "enable benchmarking using google benchmark" OFF)
48-
IF(NT_ENABLE_BENCHMARKING)
49-
message("Enabling benchmarking")
50-
CPMAddPackage(
51-
GITHUB_REPOSITORY "google/benchmark"
52-
VERSION 1.8.5
53-
OPTIONS "BENCHMARK_DOWNLOAD_DEPENDENCIES ON"
54-
)
55-
ELSE()
56-
message("Won't benchmark")
57-
ENDIF()
58-
59-
# If user wants to enable python interface we need to include pybind
60-
OPTION(NT_ENABLE_PYTHON "enable python interface" OFF)
61-
IF(NT_ENABLE_PYTHON)
62-
message("Enabling python")
63-
CPMAddPackage(
64-
GITHUB_REPOSITORY "pybind/pybind11"
65-
VERSION 2.13.5
66-
)
67-
68-
ELSE()
69-
message("Won't enable python interface")
70-
ENDIF()
50+
include(cmake/nuTens-dependencies.cmake)
7151

7252

7353
## check build times
7454
## have this optional as it's not supported on all CMake platforms
75-
OPTION(NT_BUILD_TIMING "output time to build each target" OFF)
76-
IF(NT_BUILD_TIMING)
55+
if(NT_BUILD_TIMING)
7756
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CMAKE_COMMAND} -E time")
78-
ENDIF()
57+
endif()
7958

8059

8160
######################################
@@ -85,17 +64,13 @@ ENDIF()
8564
add_subdirectory(nuTens)
8665
add_subdirectory(tests)
8766

88-
IF(NT_ENABLE_BENCHMARKING)
89-
add_subdirectory(benchmarks)
90-
ENDIF()
91-
92-
IF(NT_ENABLE_PYTHON)
93-
set_property( TARGET tensor PROPERTY POSITION_INDEPENDENT_CODE ON )
94-
set_property( TARGET propagator PROPERTY POSITION_INDEPENDENT_CODE ON )
95-
set_property( TARGET spdlog PROPERTY POSITION_INDEPENDENT_CODE ON )
96-
set_property( TARGET logging PROPERTY POSITION_INDEPENDENT_CODE ON )
67+
if(NT_ENABLE_PYTHON)
9768
add_subdirectory(python)
98-
ENDIF()
69+
endif()
70+
71+
if(NT_ENABLE_BENCHMARKING)
72+
add_subdirectory(benchmarks)
73+
endif()
9974

10075
# Print out a handy message to more easily see the config options
10176
message( STATUS "The following variables have been used to configure the build: " )

benchmarks/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11

22
add_executable(benchmarks benchmarks.cpp)
3+
4+
if(NT_USE_PCH)
5+
target_precompile_headers(benchmarks REUSE_FROM nuTens-pch)
6+
endif()
7+
38
target_link_libraries(benchmarks benchmark::benchmark benchmark::benchmark_main tensor propagator )

cmake/nuTens-dependencies.cmake

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## define dependencies of nutens which will be included using cpm where possible
2+
3+
## ==== Protobuf ====
4+
find_package(Protobuf)
5+
if( !Protobuf_FOUND )
6+
message( "didn't find protobuf, will try installing using cpm" )
7+
CPMAddPackage("gh:protocolbuffer/protobuf@27.4")
8+
endif()
9+
10+
## ==== Pytorch ====
11+
find_package(Torch REQUIRED)
12+
message("Torch cxx flags: ${TORCH_CXX_FLAGS}")
13+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
14+
15+
## ==== spdlog ====
16+
CPMAddPackage("gh:gabime/spdlog@1.8.2")
17+
18+
# ==== google benchmark ====
19+
if(NT_ENABLE_BENCHMARKING)
20+
message("Enabling benchmarking")
21+
CPMAddPackage(
22+
GITHUB_REPOSITORY "google/benchmark"
23+
VERSION 1.8.5
24+
OPTIONS "BENCHMARK_DOWNLOAD_DEPENDENCIES ON"
25+
)
26+
else()
27+
message("Won't benchmark")
28+
endif()
29+
30+
# ==== pybind11 ====
31+
if(NT_ENABLE_PYTHON)
32+
message("Enabling python")
33+
CPMAddPackage(
34+
GITHUB_REPOSITORY "pybind/pybind11"
35+
VERSION 2.13.5
36+
)
37+
38+
else()
39+
message("Won't enable python interface")
40+
endif()

nuTens/CMakeLists.txt

+5-71
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,7 @@
11

2-
########################
3-
#### set up logging ####
4-
########################
5-
add_library(logging logging.hpp)
6-
target_link_libraries(logging spdlog::spdlog)
7-
set_target_properties(logging PROPERTIES LINKER_LANGUAGE CXX)
8-
target_include_directories(logging PUBLIC "${SPDLOG_INCLUDE_DIRS}")
9-
10-
set( NT_LOG_LEVEL "INFO" CACHE STRING "the level of detail to log to the console" )
11-
12-
## Convert NT_LOG_LEVEL to all upper case so that we aren't case sensitive to user input
13-
string( TOUPPER "${NT_LOG_LEVEL}" LOG_LEVEL_UPPER )
14-
15-
## Check the specified log level is valid
16-
set(VALID_LOG_OPTIONS SILENT ERROR WARNING INFO DEBUG TRACE)
17-
list(FIND VALID_LOG_OPTIONS ${LOG_LEVEL_UPPER} index)
18-
if(${index} GREATER -1)
19-
message(STATUS "Setting log level to ${LOG_LEVEL_UPPER}")
20-
else()
21-
message(FATAL_ERROR "Invalid log level specified: ${LOG_LEVEL_UPPER} \n Should be one of: ${VALID_LOG_OPTIONS}")
22-
endif()
23-
24-
## set the log level that will be used inside the logging.hpp file
25-
target_compile_definitions(logging PUBLIC NT_LOG_LEVEL=NT_LOG_LEVEL_${LOG_LEVEL_UPPER})
26-
27-
28-
29-
################################
30-
#### set up instrumentation ####
31-
################################
32-
add_library(instrumentation instrumentation.hpp)
33-
set_target_properties(instrumentation PROPERTIES LINKER_LANGUAGE CXX)
34-
35-
option( NT_PROFILING "enable profiling of the code" OFF )
36-
if( NT_PROFILING )
37-
target_compile_definitions( instrumentation PUBLIC USE_PROFILING )
38-
endif()
39-
40-
41-
## if user wants to use pch then we use the pch
42-
## people, especially if developing, might want to use this as including tensor related things
43-
## can be excruciatingly sloow when building
44-
OPTION(NT_USE_PCH "NT_USE_PCH" OFF)
45-
IF(NT_USE_PCH)
46-
message("Using precompiled header")
47-
48-
add_library(nuTens-pch nuTens-pch.hpp)
49-
50-
SET(PCH_LIBS "${PCH_LIBS};logging;instrumentation")
51-
52-
## the headers included in the PCH will (at some point) depend on which tensor library is being used
53-
IF(TORCH_FOUND)
54-
message( "Including PyTorch includes in the PCH")
55-
message( "TORCH_LIBRARIES = ")
56-
message( "${TORCH_LIBRARIES}")
57-
58-
target_compile_definitions(nuTens-pch PUBLIC USE_PYTORCH)
59-
SET(PCH_LIBS "${PCH_LIBS};${TORCH_LIBRARIES}")
60-
ENDIF()
61-
62-
target_link_libraries(nuTens-pch PUBLIC "${PCH_LIBS}")
63-
target_precompile_headers(nuTens-pch PUBLIC nuTens-pch.hpp)
64-
set_target_properties(nuTens-pch PROPERTIES LINKER_LANGUAGE CXX)
65-
66-
ENDIF() ## end NT_USE_PCH block
67-
68-
69-
70-
71-
2+
add_subdirectory(utils)
723
add_subdirectory(tensors)
73-
add_subdirectory(propagator)
4+
add_subdirectory(propagator)
5+
6+
add_library(nuTens INTERFACE)
7+
target_link_libraries(nuTens INTERFACE tensor propagator)

nuTens/propagator/CMakeLists.txt

+9-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ add_library(
99
const-density-solver.hpp const-density-solver.cpp
1010
)
1111

12-
IF(USE_PCH)
13-
target_precompile_headers(propagator REUSE_FROM tensor)
14-
ENDIF()
12+
target_link_libraries(
13+
propagator PUBLIC
14+
tensor
15+
constants
16+
utils
17+
)
1518

16-
target_link_libraries(propagator PUBLIC tensor constants instrumentation)
19+
if(NT_USE_PCH)
20+
target_precompile_headers(propagator REUSE_FROM nuTens-pch)
21+
endif()
1722

1823
target_include_directories(propagator PUBLIC "${CMAKE_SOURCE_DIR}")
1924
set_target_properties(propagator PROPERTIES LINKER_LANGUAGE CXX)

nuTens/propagator/base-matter-solver.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

3-
#include <nuTens/instrumentation.hpp>
43
#include <nuTens/tensors/tensor.hpp>
4+
#include <nuTens/utils/instrumentation.hpp>
55

66
/// @file base-matter-solver.hpp
77

nuTens/tensors/CMakeLists.txt

+7-18
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11

2-
3-
IF(TORCH_FOUND)
2+
if(TORCH_FOUND)
43
add_library(tensor STATIC tensor.hpp torch-tensor.cpp)
54
target_compile_definitions(tensor PUBLIC USE_PYTORCH)
6-
ENDIF()
7-
8-
IF(NT_USE_PCH)
9-
target_link_libraries(tensor PUBLIC nuTens-pch)
10-
11-
ELSE()
12-
target_link_libraries(tensor PUBLIC logging)
13-
14-
IF(TORCH_FOUND)
5+
endif()
156

16-
target_link_libraries(tensor PUBLIC "${TORCH_LIBRARIES}")
7+
## when new tensor backends are added will add coresponding <library>-tensor.cpp and compile accordingly
178

18-
ELSE()
19-
message( FATAL_ERROR "No library found to deal with tensors. Currently only pytorch is available, please install this and try again." )
20-
ENDIF()
21-
ENDIF()
9+
if(NT_USE_PCH)
10+
target_precompile_headers(tensor REUSE_FROM nuTens-pch)
11+
endif()
2212

23-
target_link_libraries(tensor PUBLIC instrumentation)
24-
target_include_directories(tensor PUBLIC "${CMAKE_SOURCE_DIR}")
13+
target_link_libraries(tensor PUBLIC utils)
2514
set_target_properties(tensor PROPERTIES LINKER_LANGUAGE CXX)

nuTens/tensors/tensor.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
#include <complex>
66
#include <iostream>
77
#include <map>
8-
#include <nuTens/instrumentation.hpp>
9-
#include <nuTens/logging.hpp>
108
#include <nuTens/tensors/dtypes.hpp>
9+
#include <nuTens/utils/instrumentation.hpp>
10+
#include <nuTens/utils/logging.hpp>
1111
#include <variant>
1212
#include <vector>
1313

0 commit comments

Comments
 (0)