-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
CMakeLists.txt
168 lines (149 loc) · 4.07 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
cmake_minimum_required(VERSION 3.16...3.21)
# set a default CXX standard used by the external tools like clang-tidy, cppcheck, etc.
# You can later set fine-grained standards for each target using `target_compile_features`
set(CMAKE_CXX_STANDARD 20)
### Add project_options
# include(FetchContent)
# FetchContent_Declare(_project_options URL https://github.com/aminya/project_options/archive/refs/heads/main.zip)
# FetchContent_MakeAvailable(_project_options)
# include(${_project_options_SOURCE_DIR}/Index.cmake)
include(../src/Index.cmake)
run_vcpkg()
project(
myproj
VERSION 0.2.0
LANGUAGES CXX C)
set(PCH_HEADERS
<Eigen/Dense>
<fmt/core.h>
<vector>
<utility>
<string>
<string_view>)
# Enable sanitizers and static analyzers when running the tests
set(ENABLE_SANITIZER_ADDRESS OFF)
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR OFF)
if(NOT
"${CMAKE_SYSTEM_NAME}"
STREQUAL
"Windows")
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
else()
# or it is MSVC and has run vcvarsall
string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir)
if(MSVC AND "${index_of_vs_install_dir}" STREQUAL "-1")
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
endif()
endif()
# Initialize project_options
# uncomment the options to enable them
project_options(
ENABLE_CACHE
ENABLE_CONAN
# WARNINGS_AS_ERRORS
ENABLE_CPPCHECK
ENABLE_CLANG_TIDY
# ENABLE_INCLUDE_WHAT_YOU_USE
ENABLE_COVERAGE
ENABLE_PCH
PCH_HEADERS
${PCH_HEADERS}
ENABLE_DOXYGEN
ENABLE_INTERPROCEDURAL_OPTIMIZATION
ENABLE_NATIVE_OPTIMIZATION
# ENABLE_USER_LINKER
# ENABLE_BUILD_WITH_TIME_TRACE
# ENABLE_UNITY
${ENABLE_SANITIZER_ADDRESS}
# ENABLE_SANITIZER_LEAK
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
# ENABLE_SANITIZER_THREAD
# ENABLE_SANITIZER_MEMORY
# CLANG_WARNINGS "-Weverything"
)
# NOTE: project_options and project_warnings are defined inside project_options
# Executable
add_executable(main ./src/main/main.cpp)
target_link_libraries(main PRIVATE project_options project_warnings)
## dependencies
set(DEPENDENCIES_CONFIGURED fmt Eigen3 docopt)
foreach(DEPENDENCY ${DEPENDENCIES_CONFIGURED})
find_package(${DEPENDENCY} CONFIG REQUIRED)
endforeach()
target_link_system_libraries(
main
PRIVATE
fmt::fmt
Eigen3::Eigen)
add_subdirectory(libs)
## tests
enable_testing()
add_test(NAME main COMMAND main)
# Header-only library
add_library(lib INTERFACE)
set(lib_INCLUDE_DIR "include")
target_include_directories(lib INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${lib_INCLUDE_DIR}>"
"$<INSTALL_INTERFACE:./${CMAKE_INSTALL_INCLUDEDIR}>") # TODO(refactor)
target_link_libraries(lib INTERFACE project_options project_warnings)
target_link_system_libraries(
lib
INTERFACE
fmt::fmt
Eigen3::Eigen)
# Library
add_library(lib2 "./src/mylib2/lib.cpp")
set(lib2_INCLUDE_DIR2 "include")
target_include_directories(lib2 PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${lib2_INCLUDE_DIR2}>"
"$<INSTALL_INTERFACE:./${CMAKE_INSTALL_INCLUDEDIR}>") # TODO(refactor)
target_link_libraries(lib2 PRIVATE project_options project_warnings)
target_link_system_libraries(
lib2
PRIVATE
fmt::fmt
Eigen3::Eigen)
target_link_system_libraries(
lib2
PRIVATE
mythirdpartylib)
# package everything automatically
package_project(
# NAME
# myproj
# TARGETS
# main
# lib
# project_warnings
# project_options
INTERFACE_DEPENDENCIES_CONFIGURED
${DEPENDENCIES_CONFIGURED}
INTERFACE_INCLUDES
${lib_INCLUDE_DIR2}
PUBLIC_INCLUDES
${lib2_INCLUDE_DIR2})
# package separately (for testing)
package_project(
NAME
myproj_header_only_lib
TARGETS
lib
project_options
project_warnings
INTERFACE_DEPENDENCIES_CONFIGURED
${DEPENDENCIES_CONFIGURED}
INTERFACE_INCLUDES
${lib_INCLUDE_DIR})
package_project(
NAME
myproj_lib
TARGETS
lib2
project_options
project_warnings
PUBLIC_INCLUDES
${lib2_INCLUDE_DIR22})
package_project(
NAME
myproj_main
TARGETS
main)