CMakeLists.txt
8.43 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
cmake_minimum_required(VERSION 3.4)
# Note: this is a header only library. If you have an older CMake than 3.4,
# just add the CLI11/include directory and that's all you need to do.
# Make sure users don't get warnings on a tested (3.4 to 3.14) version
# of CMake. For most of the policies, the new version is better (hence the change).
# We don't use the 3.4...3.14 syntax because of a bug in a version of MSVC
if(${CMAKE_VERSION} VERSION_LESS 3.14)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.14)
endif()
set(VERSION_REGEX "#define CLI11_VERSION[ \t]+\"(.+)\"")
# Read in the line containing the version
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/include/CLI/Version.hpp"
VERSION_STRING REGEX ${VERSION_REGEX})
# Pick out just the version
string(REGEX REPLACE ${VERSION_REGEX} "\\1" VERSION_STRING "${VERSION_STRING}")
# Add the project
project(CLI11 LANGUAGES CXX VERSION ${VERSION_STRING})
# Special target that adds warnings. Is not exported.
add_library(CLI11_warnings INTERFACE)
# Only if built as the main project
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
# User settable
set(CLI11_CXX_STD "11" CACHE STRING "The CMake standard to require")
# Special override for Clang on Linux (useful with an old stdlibc++ and a newer clang)
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
option(CLI11_FORCE_LIBCXX "Force Clang to use libc++ instead of libstdc++ (Linux only)" OFF)
if(CLI11_FORCE_LIBCXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++")
endif()
endif()
set(CUR_PROJ ON)
set(CMAKE_CXX_STANDARD ${CLI11_CXX_STD})
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
option(CLI11_CUDA_TESTS "Build the tests with NVCC to check for warnings there - requires CMake 3.9+")
if(CLI11_CUDA_TESTS)
enable_language(CUDA)
# Print out warning and error numbers
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcudafe --display_error_number")
endif()
option(CLI11_WARNINGS_AS_ERRORS "Turn all warnings into errors (for CI)")
# Be moderately paranoid with flags
if(MSVC)
target_compile_options(CLI11_warnings INTERFACE "/W4")
if(CLI11_WARNINGS_AS_ERRORS)
target_compile_options(CLI11_warnings INTERFACE "/WX")
endif()
else()
target_compile_options(CLI11_warnings INTERFACE -Wall -Wextra -pedantic -Wshadow -Wsign-conversion)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
# GCC 4.8 has a false positive
# Other compilers ignore this flag
target_compile_options(CLI11_warnings INTERFACE -Weffc++)
endif()
endif()
if(CLI11_WARNINGS_AS_ERRORS)
target_compile_options(CLI11_warnings INTERFACE -Werror)
endif()
endif()
if(NOT CMAKE_VERSION VERSION_LESS 3.6)
option(CLI11_CLANG_TIDY "Look for and use Clang-Tidy")
set(CLI11_CLANG_TIDY_OPTIONS "" CACHE STRING "Clang tidy option, such as -fix")
if(CLI11_CLANG_TIDY)
find_program(
CLANG_TIDY_EXE
NAMES "clang-tidy"
DOC "Path to clang-tidy executable"
REQUIRED
)
set(DO_CLANG_TIDY "${CLANG_TIDY_EXE}" ${CLI11_CLANG_TIDY_OPTIONS})
endif()
endif()
if (EXISTS book)
add_subdirectory(book)
endif()
if(NOT CMAKE_VERSION VERSION_LESS 3.9)
find_package(Doxygen)
if(Doxygen_FOUND)
add_subdirectory(docs)
else()
message(STATUS "Doxygen not found, not building docs")
endif()
else()
message(STATUS "Newer CMake adds Doxygen support, update CMake for docs")
endif()
else()
set(CUR_PROJ OFF)
endif()
# Allow dependent options
include(CMakeDependentOption)
# Allow IDE's to group targets into folders
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
file(GLOB CLI11_headers "${CMAKE_CURRENT_SOURCE_DIR}/include/CLI/*")
# To see in IDE, must be listed for target
add_library(CLI11 INTERFACE)
# Duplicated because CMake adds the current source dir if you don't.
target_include_directories(CLI11 INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
# Make add_subdirectory work like find_package
add_library(CLI11::CLI11 ALIAS CLI11)
option(CLI11_INSTALL "Install the CLI11 folder to include during install process" ${CUR_PROJ})
# This folder should be installed
if(CLI11_INSTALL)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/CLI DESTINATION include)
# Make an export target
install(TARGETS CLI11
EXPORT CLI11Targets)
endif()
# Use find_package on the installed package
# Since we have no custom code, we can directly write this
# to Config.cmake (otherwise we'd have a custom config and would
# import Targets.cmake
# Add the version in a CMake readable way
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/CLI11ConfigVersion.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/CLI11ConfigVersion.cmake" @ONLY)
include(GNUInstallDirs)
# These installs only make sense for a local project
if(CUR_PROJ)
# Make version available in the install
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/CLI11ConfigVersion.cmake"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CLI11)
# Install the export target as a file
install(EXPORT CLI11Targets
FILE CLI11Config.cmake
NAMESPACE CLI11::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CLI11)
# Use find_package on the installed package
export(TARGETS CLI11
NAMESPACE CLI11::
FILE CLI11Targets.cmake)
# Register in the user cmake package registry
export(PACKAGE CLI11)
endif()
option(CLI11_SINGLE_FILE "Generate a single header file" OFF)
if(CLI11_SINGLE_FILE)
# Single file test
if(CMAKE_VERSION VERSION_LESS 3.12)
find_package(PythonInterp REQUIRED)
set(Python_VERSION ${PYTHON_VERSION_STRING})
set(Python_EXECUTABLE "${PYTHON_EXECUTABLE}")
else()
find_package(Python REQUIRED)
endif()
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/include")
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp"
COMMAND "${Python_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/scripts/MakeSingleHeader.py" "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/CLI/CLI.hpp" ${CLI11_headers}
)
add_custom_target(generate_cli_single_file ALL
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp")
set_target_properties(generate_cli_single_file
PROPERTIES FOLDER "Scripts")
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp DESTINATION include)
add_library(CLI11_SINGLE INTERFACE)
target_link_libraries(CLI11_SINGLE INTERFACE CLI11)
add_dependencies(CLI11_SINGLE generate_cli_single_file)
target_compile_definitions(CLI11_SINGLE INTERFACE -DCLI11_SINGLE_FILE)
target_include_directories(CLI11_SINGLE INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/include/")
endif()
cmake_dependent_option(CLI11_SINGLE_FILE_TESTS
"Duplicate all the tests for a single file build"
OFF
"CLI11_SINGLE_FILE"
OFF)
if(DEFINED CLI11_TESTING)
set(CLI11_TESTING_INTERNAL "${CLI11_TESTING}")
elseif(CUR_PROJ)
option(BUILD_TESTING "Build the tests" ON)
set(CLI11_TESTING_INTERNAL "${BUILD_TESTING}")
else()
set(CLI11_TESTING_INTERNAL OFF)
endif()
if(CLI11_TESTING_INTERNAL)
enable_testing()
add_subdirectory(tests)
endif()
cmake_dependent_option(CLI11_EXAMPLES "Build the examples" ON "CUR_PROJ" OFF)
if(CLI11_EXAMPLES)
add_subdirectory(examples)
endif()
# Packaging support
set(CPACK_PACKAGE_VENDOR "github.com/CLIUtils/CLI11")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Command line interface")
set(CPACK_PACKAGE_VERSION_MAJOR ${CLI11_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${CLI11_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${CLI11_VERSION_PATCH})
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
set(CPACK_SOURCE_GENERATOR "TGZ;ZIP")
# CPack collects *everything* except what's listed here.
set(CPACK_SOURCE_IGNORE_FILES
/.git
/dist
/.*build.*
/\\\\.DS_Store
/.*\\\\.egg-info
/var
/Pipfile.*$
)
include(CPack)