Skip to content

Commit faea921

Browse files
authored
chore: cmake reorganization (#768)
update conan and azure-pipelines to fix gcc 4.8 issue make the CLI11 target sources only for newer CMake Change the cmake to use subdirectory and move the library generation and warnings to separate files.
1 parent c7aff0e commit faea921

File tree

8 files changed

+167
-132
lines changed

8 files changed

+167
-132
lines changed

.ci/azure-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
steps:
22
# Needed on GCC 4.8 docker image for some reason
33
- script: mkdir build
4-
displalyName: "Make build directory"
4+
displayName: "Make build directory"
55

66
- task: CMake@1
77
inputs:

CMakeLists.txt

Lines changed: 4 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ project(
2727
LANGUAGES CXX
2828
VERSION ${VERSION_STRING})
2929

30+
list(APPEND CMAKE_MODULE_PATH "${CLI11_SOURCE_DIR}/cmake")
31+
3032
# Print the version number of CMake if this is the main project
3133
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
3234
message(STATUS "CMake ${CMAKE_VERSION}")
@@ -128,89 +130,9 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
128130
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
129131
endif()
130132

131-
# Special target that adds warnings. Is not exported.
132-
add_library(CLI11_warnings INTERFACE)
133-
134-
set(unix-warnings -Wall -Wextra -pedantic -Wshadow -Wsign-conversion -Wswitch-enum)
135-
136-
# Clang warnings
137-
# -Wfloat-equal could be added with Catch::literals and _a usage
138-
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
139-
list(
140-
APPEND
141-
unix-warnings
142-
-Wcast-align
143-
-Wimplicit-atomic-properties
144-
-Wmissing-declarations
145-
-Woverlength-strings
146-
-Wshadow
147-
-Wstrict-selector-match
148-
-Wundeclared-selector)
149-
# -Wunreachable-code Doesn't work on Clang 3.4
150-
endif()
151-
152-
# Buggy in GCC 4.8
153-
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
154-
list(APPEND unix-warnings -Weffc++)
155-
endif()
156-
157-
target_compile_options(
158-
CLI11_warnings
159-
INTERFACE $<$<BOOL:${CLI11_FORCE_LIBCXX}>:-stdlib=libc++>
160-
$<$<CXX_COMPILER_ID:MSVC>:/W4
161-
$<$<BOOL:${CLI11_WARNINGS_AS_ERRORS}>:/WX>>
162-
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:${unix-warnings}
163-
$<$<BOOL:${CLI11_WARNINGS_AS_ERRORS}>:-Werror>>)
164-
165-
if(NOT CMAKE_VERSION VERSION_LESS 3.13)
166-
target_link_options(CLI11_warnings INTERFACE $<$<BOOL:${CLI11_FORCE_LIBCXX}>:-stdlib=libc++>)
167-
endif()
168-
169-
# To see in IDE, headers must be listed for target
170-
set(MAYBE_CONFIGURE_DEPENDS "")
171-
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND NOT CMAKE_VERSION VERSION_LESS 3.12)
172-
list(INSERT MAYBE_CONFIGURE_DEPENDS 0 CONFIGURE_DEPENDS)
173-
endif()
174-
175-
file(GLOB CLI11_headers ${MAYBE_CONFIGURE_DEPENDS} "${PROJECT_SOURCE_DIR}/include/CLI/*.hpp")
176-
file(GLOB CLI11_impl_headers ${MAYBE_CONFIGURE_DEPENDS}
177-
"${PROJECT_SOURCE_DIR}/include/CLI/impl/*.hpp")
133+
include(CLI11Warnings)
178134

179-
if(CLI11_PRECOMPILED)
180-
# Create static lib
181-
file(GLOB CLI11_precompile_sources "${PROJECT_SOURCE_DIR}/src/*.cpp")
182-
add_library(CLI11 STATIC ${CLI11_headers} ${CLI11_impl_headers} ${CLI11_precompile_sources})
183-
target_compile_definitions(CLI11 PUBLIC -DCLI11_COMPILE)
184-
185-
set(PUBLIC_OR_INTERFACE PUBLIC)
186-
else()
187-
add_library(CLI11 INTERFACE)
188-
set(PUBLIC_OR_INTERFACE INTERFACE)
189-
endif()
190-
191-
# Allow IDE's to group targets into folders
192-
add_library(CLI11::CLI11 ALIAS CLI11) # for add_subdirectory calls
193-
194-
# Duplicated because CMake adds the current source dir if you don't.
195-
target_include_directories(
196-
CLI11 ${PUBLIC_OR_INTERFACE} $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
197-
$<INSTALL_INTERFACE:include>)
198-
199-
if(CMAKE_VERSION VERSION_LESS 3.8)
200-
# This might not be a complete list
201-
target_compile_features(
202-
CLI11
203-
INTERFACE cxx_lambdas
204-
cxx_nullptr
205-
cxx_override
206-
cxx_range_for
207-
cxx_right_angle_brackets
208-
cxx_strong_enums
209-
cxx_constexpr
210-
cxx_auto_type)
211-
else()
212-
target_compile_features(CLI11 INTERFACE cxx_std_11)
213-
endif()
135+
add_subdirectory(src)
214136

215137
# Allow tests to be run on CUDA
216138
if(CLI11_CUDA_TESTS)
@@ -222,13 +144,6 @@ endif()
222144

223145
# This folder should be installed
224146
if(CLI11_INSTALL)
225-
install(FILES ${CLI11_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/CLI")
226-
if(NOT CLI11_COMPILE)
227-
install(FILES ${CLI11_impl_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/CLI/impl")
228-
endif()
229-
230-
# Make an export target
231-
install(TARGETS CLI11 EXPORT CLI11Targets)
232147

233148
# Use find_package on the installed package
234149
# Since we have no custom code, we can directly write this
@@ -261,42 +176,6 @@ if(CLI11_INSTALL)
261176
export(PACKAGE CLI11)
262177
endif()
263178

264-
if(CLI11_SINGLE_FILE)
265-
# Single file test
266-
if(CMAKE_VERSION VERSION_LESS 3.12)
267-
find_package(PythonInterp REQUIRED)
268-
add_executable(Python::Interpreter IMPORTED)
269-
set_target_properties(Python::Interpreter PROPERTIES IMPORTED_LOCATION "${PYTHON_EXECUTABLE}"
270-
VERSION "${PYTHON_VERSION_STRING}")
271-
else()
272-
find_package(
273-
Python
274-
COMPONENTS Interpreter
275-
REQUIRED)
276-
endif()
277-
278-
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/include")
279-
add_custom_command(
280-
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp"
281-
COMMAND
282-
Python::Interpreter "${CMAKE_CURRENT_SOURCE_DIR}/scripts/MakeSingleHeader.py"
283-
${CLI11_headers} ${CLI11_impl_headers} --main "${CMAKE_CURRENT_SOURCE_DIR}/CLI11.hpp.in"
284-
--output "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp" --version "${CLI11_VERSION}"
285-
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/CLI/CLI.hpp" ${CLI11_headers}
286-
${CLI11_impl_headers})
287-
add_custom_target(CLI11-generate-single-file ALL
288-
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp")
289-
set_property(TARGET CLI11-generate-single-file PROPERTY FOLDER "Scripts")
290-
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp" DESTINATION include)
291-
add_library(CLI11_SINGLE INTERFACE)
292-
target_link_libraries(CLI11_SINGLE INTERFACE CLI11)
293-
add_dependencies(CLI11_SINGLE CLI11-generate-single-file)
294-
target_compile_definitions(CLI11_SINGLE INTERFACE -DCLI11_SINGLE_FILE)
295-
target_include_directories(
296-
CLI11_SINGLE INTERFACE $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include/>
297-
$<INSTALL_INTERFACE:include>)
298-
endif()
299-
300179
if(CLI11_BUILD_TESTS)
301180
include(CTest)
302181
add_subdirectory(tests)

azure-pipelines.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ jobs:
110110
containerImage: gcc:8
111111
cli11.std: 17
112112
gcc4.8:
113-
containerImage: gcc:4.8
113+
containerImage: helics/buildenv:gcc4-8-builder
114114
cli11.std: 11
115115
cli11.options:
116116
clang3.4:

cmake/CLI11Warnings.cmake

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Special target that adds warnings. Is not exported.
2+
add_library(CLI11_warnings INTERFACE)
3+
4+
set(unix-warnings -Wall -Wextra -pedantic -Wshadow -Wsign-conversion -Wswitch-enum)
5+
6+
# Clang warnings
7+
# -Wfloat-equal could be added with Catch::literals and _a usage
8+
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
9+
list(
10+
APPEND
11+
unix-warnings
12+
-Wcast-align
13+
-Wimplicit-atomic-properties
14+
-Wmissing-declarations
15+
-Woverlength-strings
16+
-Wshadow
17+
-Wstrict-selector-match
18+
-Wundeclared-selector)
19+
# -Wunreachable-code Doesn't work on Clang 3.4
20+
endif()
21+
22+
# Buggy in GCC 4.8
23+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
24+
list(APPEND unix-warnings -Weffc++)
25+
endif()
26+
27+
target_compile_options(
28+
CLI11_warnings
29+
INTERFACE $<$<BOOL:${CLI11_FORCE_LIBCXX}>:-stdlib=libc++>
30+
$<$<CXX_COMPILER_ID:MSVC>:/W4
31+
$<$<BOOL:${CLI11_WARNINGS_AS_ERRORS}>:/WX>>
32+
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:${unix-warnings}
33+
$<$<BOOL:${CLI11_WARNINGS_AS_ERRORS}>:-Werror>>)
34+
35+
if(NOT CMAKE_VERSION VERSION_LESS 3.13)
36+
target_link_options(CLI11_warnings INTERFACE $<$<BOOL:${CLI11_FORCE_LIBCXX}>:-stdlib=libc++>)
37+
endif()

conanfile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class CLI11Conan(ConanFile):
2727
"LICENSE",
2828
"README.md",
2929
"include/*",
30+
"src/*",
3031
"extern/*",
3132
"cmake/*",
3233
"CMakeLists.txt",

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function(add_cli_exe T)
2-
add_executable(${T} ${ARGN} ${CLI11_headers})
2+
add_executable(${T} ${ARGN})
33
target_link_libraries(${T} PUBLIC CLI11)
44
set_property(TARGET ${T} PROPERTY FOLDER "Examples")
55
if(CLI11_FORCE_LIBCXX)

src/CMakeLists.txt

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
set(CLI11_headerLoc "${PROJECT_SOURCE_DIR}/include/CLI")
2+
3+
set(CLI11_headers
4+
${CLI11_headerLoc}/App.hpp
5+
${CLI11_headerLoc}/CLI.hpp
6+
${CLI11_headerLoc}/Config.hpp
7+
${CLI11_headerLoc}/ConfigFwd.hpp
8+
${CLI11_headerLoc}/Error.hpp
9+
${CLI11_headerLoc}/Formatter.hpp
10+
${CLI11_headerLoc}/FormatterFwd.hpp
11+
${CLI11_headerLoc}/Macros.hpp
12+
${CLI11_headerLoc}/Option.hpp
13+
${CLI11_headerLoc}/Split.hpp
14+
${CLI11_headerLoc}/StringTools.hpp
15+
${CLI11_headerLoc}/Timer.hpp
16+
${CLI11_headerLoc}/TypeTools.hpp
17+
${CLI11_headerLoc}/Validators.hpp
18+
${CLI11_headerLoc}/Version.hpp)
19+
20+
set(CLI11_implLoc "${PROJECT_SOURCE_DIR}/include/CLI/impl")
21+
22+
set(CLI11_impl_headers
23+
${CLI11_implLoc}/App_inl.hpp
24+
${CLI11_implLoc}/Config_inl.hpp
25+
${CLI11_implLoc}/Formatter_inl.hpp
26+
${CLI11_implLoc}/Option_inl.hpp
27+
${CLI11_implLoc}/Split_inl.hpp
28+
${CLI11_implLoc}/StringTools_inl.hpp
29+
${CLI11_implLoc}/Validators_inl.hpp)
30+
31+
if(CLI11_PRECOMPILED)
32+
# Create static lib
33+
file(GLOB CLI11_precompile_sources "${PROJECT_SOURCE_DIR}/src/*.cpp")
34+
add_library(CLI11 STATIC ${CLI11_headers} ${CLI11_impl_headers} ${CLI11_precompile_sources})
35+
target_compile_definitions(CLI11 PUBLIC -DCLI11_COMPILE)
36+
37+
set(PUBLIC_OR_INTERFACE PUBLIC)
38+
else()
39+
add_library(CLI11 INTERFACE)
40+
if(CMAKE_VERSION VERSION_GREATER 3.19)
41+
# This is only useful for visual studio and other IDE builds
42+
target_sources(CLI11 PRIVATE ${CLI11_headers} ${CLI11_impl_headers})
43+
endif()
44+
45+
set(PUBLIC_OR_INTERFACE INTERFACE)
46+
endif()
47+
48+
# Allow IDE's to group targets into folders
49+
add_library(CLI11::CLI11 ALIAS CLI11) # for add_subdirectory calls
50+
51+
# Duplicated because CMake adds the current source dir if you don't.
52+
target_include_directories(
53+
CLI11 ${PUBLIC_OR_INTERFACE} $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
54+
$<INSTALL_INTERFACE:include>)
55+
56+
if(CMAKE_VERSION VERSION_LESS 3.8)
57+
# This might not be a complete list
58+
target_compile_features(
59+
CLI11
60+
INTERFACE cxx_lambdas
61+
cxx_nullptr
62+
cxx_override
63+
cxx_range_for
64+
cxx_right_angle_brackets
65+
cxx_strong_enums
66+
cxx_constexpr
67+
cxx_auto_type)
68+
else()
69+
target_compile_features(CLI11 INTERFACE cxx_std_11)
70+
endif()
71+
72+
if(CLI11_SINGLE_FILE)
73+
# Single file test
74+
if(CMAKE_VERSION VERSION_LESS 3.12)
75+
find_package(PythonInterp REQUIRED)
76+
add_executable(Python::Interpreter IMPORTED)
77+
set_target_properties(Python::Interpreter PROPERTIES IMPORTED_LOCATION "${PYTHON_EXECUTABLE}"
78+
VERSION "${PYTHON_VERSION_STRING}")
79+
else()
80+
find_package(
81+
Python
82+
COMPONENTS Interpreter
83+
REQUIRED)
84+
endif()
85+
86+
file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/include")
87+
add_custom_command(
88+
OUTPUT "${PROJECT_BINARY_DIR}/include/CLI11.hpp"
89+
COMMAND
90+
Python::Interpreter "${PROJECT_SOURCE_DIR}/scripts/MakeSingleHeader.py" ${CLI11_headers}
91+
${CLI11_impl_headers} --main "${PROJECT_SOURCE_DIR}/CLI11.hpp.in" --output
92+
"${PROJECT_BINARY_DIR}/include/CLI11.hpp" --version "${CLI11_VERSION}"
93+
DEPENDS "${PROJECT_SOURCE_DIR}/include/CLI/CLI.hpp" ${CLI11_headers} ${CLI11_impl_headers})
94+
add_custom_target(CLI11-generate-single-file ALL
95+
DEPENDS "${PROJECT_BINARY_DIR}/include/CLI11.hpp")
96+
set_property(TARGET CLI11-generate-single-file PROPERTY FOLDER "Scripts")
97+
if(CLI11_INSTALL)
98+
install(FILES "${PROJECT_BINARY_DIR}/include/CLI11.hpp"
99+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
100+
endif()
101+
add_library(CLI11_SINGLE INTERFACE)
102+
target_link_libraries(CLI11_SINGLE INTERFACE CLI11)
103+
add_dependencies(CLI11_SINGLE CLI11-generate-single-file)
104+
target_compile_definitions(CLI11_SINGLE INTERFACE -DCLI11_SINGLE_FILE)
105+
target_include_directories(
106+
CLI11_SINGLE INTERFACE $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include/>
107+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
108+
endif()
109+
110+
if(CLI11_INSTALL)
111+
112+
# Make an export target
113+
install(TARGETS CLI11 EXPORT CLI11Targets)
114+
if(NOT CLI11_SINGLE_FILE)
115+
install(FILES ${CLI11_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/CLI")
116+
if(NOT CLI11_COMPILE)
117+
install(FILES ${CLI11_impl_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/CLI/impl")
118+
endif()
119+
endif()
120+
endif()

tests/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
list(APPEND CMAKE_MODULE_PATH "${CLI11_SOURCE_DIR}/cmake")
2-
31
if(CLI11_SANITIZERS)
42
message(STATUS "Using arsenm/sanitizers-cmake")
53
FetchContent_Declare(
@@ -109,7 +107,7 @@ foreach(T IN LISTS CLI11_TESTS)
109107
if(CLI11_CUDA_TESTS)
110108
set_property(SOURCE ${T}.cpp PROPERTY LANGUAGE CUDA)
111109
endif()
112-
add_executable(${T} ${T}.cpp ${CLI11_headers})
110+
add_executable(${T} ${T}.cpp)
113111
add_sanitizers(${T})
114112
if(NOT CLI11_CUDA_TESTS)
115113
target_link_libraries(${T} PRIVATE CLI11_warnings)
@@ -126,7 +124,7 @@ foreach(T IN LISTS CLI11_TESTS)
126124
endforeach()
127125

128126
foreach(T IN LISTS CLI11_MULTIONLY_TESTS)
129-
add_executable(${T} ${T}.cpp ${CLI11_headers})
127+
add_executable(${T} ${T}.cpp)
130128
add_sanitizers(${T})
131129
target_link_libraries(${T} PUBLIC CLI11)
132130
add_catch_test(${T})

0 commit comments

Comments
 (0)