-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
120 lines (105 loc) · 4.84 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#===================================================================================================== PROJECT SETUP ===
cmake_minimum_required(VERSION 3.14)
project(tardigrade_vector_tools VERSION 0.2.0)
# Set common project paths relative to project root directory
set(CPP_SRC_PATH "src/cpp")
set(CMAKE_SRC_PATH "src/cmake")
# Add the cmake folder to locate project CMake module(s)
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/${CMAKE_SRC_PATH}" ${CMAKE_MODULE_PATH})
# Set build type checks
string(TOLOWER "${CMAKE_BUILD_TYPE}" cmake_build_type_lower)
set(not_conda_test "true")
if(cmake_build_type_lower STREQUAL "conda-test")
set(not_conda_test "false")
endif()
# Get version number from Git
set(VERSION_UPDATE_FROM_GIT True)
if(${not_conda_test} STREQUAL "true")
find_package(Python COMPONENTS Interpreter REQUIRED)
execute_process(COMMAND ${Python_EXECUTABLE} -m setuptools_scm
OUTPUT_VARIABLE ${PROJECT_NAME}_VERSION_STRING_FULL
OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REGEX MATCH "[0-9]+\.[0-9]+\.[0-9]+" ${PROJECT_NAME}_VERSION ${${PROJECT_NAME}_VERSION_STRING_FULL})
if(${${PROJECT_NAME}_VERSION} STREQUAL "")
set(${PROJECT_NAME}_VERSION 0.0.0)
endif()
project(${PROJECT_NAME} VERSION ${${PROJECT_NAME}_VERSION})
endif()
# Add installation directory variables
include(GNUInstallDirs)
# Set the c++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic")
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lrt")
endif()
# Enable CTest
enable_testing()
#================================================================================================= FIND DEPENDENCIES ===
# Find eigen
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
include_directories(${EIGEN3_INCLUDE_DIR})
if(EIGEN3_FOUND)
message(STATUS "Found Eigen3: ${EIGEN3_INCLUDE_DIR}")
endif()
# Set build type checks
string(TOLOWER "${CMAKE_BUILD_TYPE}" cmake_build_type_lower)
# Find related, but external, projects in installed environment
include(FetchContent)
set(upstream_packages "tardigrade_error_tools")
foreach(package ${upstream_packages})
string(TOUPPER "${package}" package_upper)
find_package(${package} ${upstream_required} CONFIG)
if(${package}_FOUND)
message(STATUS "Found ${package}: ${${package}_DIR}")
else()
# Find related, but external, projects using FetchContent and building locally
message(WARNING "Did not find an installed ${package} package. Attempting local build with FetchContent.")
# TODO: Decide how to handle main/dev build differences. bash env variables for GIT TAG?
FetchContent_Declare(
${package}
GIT_REPOSITORY ssh://[email protected]:10022/aea/material-models/${package}.git
GIT_TAG origin/dev
)
FetchContent_MakeAvailable(${package})
endif()
endforeach(package)
#=============================================================================================== ADD PROJECT TARGETS ===
# MUST COME AFTER DEPENDENCY LOCATING
# Add project source directories
if(${not_conda_test} STREQUAL "true")
include_directories("${CPP_SRC_PATH}")
add_subdirectory("${CPP_SRC_PATH}")
endif()
# Only add tests and documentation for current project builds. Protects downstream project builds.
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
# Find Boost. Required for tests
find_package(Boost 1.53.0 REQUIRED COMPONENTS unit_test_framework)
# Add c++ tests and docs
add_subdirectory("${CPP_SRC_PATH}/tests")
if(${not_conda_test} STREQUAL "true")
add_subdirectory("docs")
endif()
endif()
#==================================================================================== SETUP INSTALLATION CMAKE FILES ===
if(${not_conda_test} STREQUAL "true")
include(CMakePackageConfigHelpers)
write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
configure_package_config_file(
"${PROJECT_SOURCE_DIR}/${CMAKE_SRC_PATH}/${PROJECT_NAME}Config.cmake.in"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION
${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
# CMake won't build the targets for local builds of upstream projects
if(cmake_build_type_lower STREQUAL release)
install(EXPORT ${PROJECT_NAME}_Targets
FILE ${PROJECT_NAME}Targets.cmake
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
endif()
install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
endif()