Skip to content

Commit b56ae23

Browse files
dherrera-metahenryiii
authored andcommitted
[precompiled] Initial proof-of-concept with App.cpp
- Add C11_COMPILE cmake option that creates a static lib instead of header-only - Add C11_INLINE macro that depends on C11_COMPILE - Split App.hpp into App.hpp and impl/App_inl.hpp - Add App.cpp that compiles App_inl.hpp into an object file - CMake modifications to handle impl headers differently for sinlge-header, headers-only, and compiled versions
1 parent c57000e commit b56ae23

File tree

6 files changed

+2233
-1966
lines changed

6 files changed

+2233
-1966
lines changed

CLI11.hpp.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ namespace {namespace} {{
6262

6363
{app_hpp}
6464

65+
{app_inl_hpp}
66+
6567
{config_hpp}
6668

6769
{formatter_hpp}

CMakeLists.txt

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ endif()
7777

7878
option(CLI11_WARNINGS_AS_ERRORS "Turn all warnings into errors (for CI)")
7979
option(CLI11_SINGLE_FILE "Generate a single header file")
80+
option(CLI11_PRECOMPILED "Generate a precompiled static library instead of a header-only")
8081
cmake_dependent_option(CLI11_SANITIZERS "Download the sanitizers CMake config" OFF
8182
"NOT CMAKE_VERSION VERSION_LESS 3.11" OFF)
8283

@@ -105,6 +106,11 @@ cmake_dependent_option(
105106
CLI11_CUDA_TESTS "Build the tests with NVCC to check for warnings there - requires CMake 3.9+"
106107
OFF "CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME" OFF)
107108

109+
if(CLI11_PRECOMPILED AND CLI11_SINGLE_FILE)
110+
# Sanity check
111+
message(FATAL_ERROR "CLI11_PRECOMPILE and CLI11_SINGLE_FILE are mutually exclusive")
112+
endif()
113+
108114
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND NOT DEFINED CMAKE_CXX_STANDARD)
109115
set(CMAKE_CXX_STANDARD 11)
110116
endif()
@@ -160,13 +166,35 @@ if(NOT CMAKE_VERSION VERSION_LESS 3.13)
160166
target_link_options(CLI11_warnings INTERFACE $<$<BOOL:${CLI11_FORCE_LIBCXX}>:-stdlib=libc++>)
161167
endif()
162168

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")
178+
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+
163191
# Allow IDE's to group targets into folders
164-
add_library(CLI11 INTERFACE)
165192
add_library(CLI11::CLI11 ALIAS CLI11) # for add_subdirectory calls
166193

167194
# Duplicated because CMake adds the current source dir if you don't.
168-
target_include_directories(CLI11 INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
169-
$<INSTALL_INTERFACE:include>)
195+
target_include_directories(
196+
CLI11 ${PUBLIC_OR_INTERFACE} $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
197+
$<INSTALL_INTERFACE:include>)
170198

171199
if(CMAKE_VERSION VERSION_LESS 3.8)
172200
# This might not be a complete list
@@ -184,14 +212,6 @@ else()
184212
target_compile_features(CLI11 INTERFACE cxx_std_11)
185213
endif()
186214

187-
# To see in IDE, headers must be listed for target
188-
set(header-patterns "${PROJECT_SOURCE_DIR}/include/CLI/*")
189-
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND NOT CMAKE_VERSION VERSION_LESS 3.12)
190-
list(INSERT header-patterns 0 CONFIGURE_DEPENDS)
191-
endif()
192-
193-
file(GLOB CLI11_headers ${header-patterns})
194-
195215
# Allow tests to be run on CUDA
196216
if(CLI11_CUDA_TESTS)
197217
enable_language(CUDA)
@@ -202,7 +222,10 @@ endif()
202222

203223
# This folder should be installed
204224
if(CLI11_INSTALL)
205-
install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
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()
206229

207230
# Make an export target
208231
install(TARGETS CLI11 EXPORT CLI11Targets)
@@ -257,9 +280,10 @@ if(CLI11_SINGLE_FILE)
257280
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp"
258281
COMMAND
259282
Python::Interpreter "${CMAKE_CURRENT_SOURCE_DIR}/scripts/MakeSingleHeader.py"
260-
${CLI11_headers} --main "${CMAKE_CURRENT_SOURCE_DIR}/CLI11.hpp.in" --output
261-
"${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp" --version "${CLI11_VERSION}"
262-
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/CLI/CLI.hpp" ${CLI11_headers})
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})
263287
add_custom_target(CLI11-generate-single-file ALL
264288
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp")
265289
set_property(TARGET CLI11-generate-single-file PROPERTY FOLDER "Scripts")

0 commit comments

Comments
 (0)