Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake Update #768

Merged
merged 1 commit into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ci/azure-build.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
steps:
# Needed on GCC 4.8 docker image for some reason
- script: mkdir build
displalyName: "Make build directory"
displayName: "Make build directory"

- task: CMake@1
inputs:
Expand Down
129 changes: 4 additions & 125 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ project(
LANGUAGES CXX
VERSION ${VERSION_STRING})

list(APPEND CMAKE_MODULE_PATH "${CLI11_SOURCE_DIR}/cmake")

# Print the version number of CMake if this is the main project
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
message(STATUS "CMake ${CMAKE_VERSION}")
Expand Down Expand Up @@ -128,89 +130,9 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
endif()

# Special target that adds warnings. Is not exported.
add_library(CLI11_warnings INTERFACE)

set(unix-warnings -Wall -Wextra -pedantic -Wshadow -Wsign-conversion -Wswitch-enum)

# Clang warnings
# -Wfloat-equal could be added with Catch::literals and _a usage
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
list(
APPEND
unix-warnings
-Wcast-align
-Wimplicit-atomic-properties
-Wmissing-declarations
-Woverlength-strings
-Wshadow
-Wstrict-selector-match
-Wundeclared-selector)
# -Wunreachable-code Doesn't work on Clang 3.4
endif()

# Buggy in GCC 4.8
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
list(APPEND unix-warnings -Weffc++)
endif()

target_compile_options(
CLI11_warnings
INTERFACE $<$<BOOL:${CLI11_FORCE_LIBCXX}>:-stdlib=libc++>
$<$<CXX_COMPILER_ID:MSVC>:/W4
$<$<BOOL:${CLI11_WARNINGS_AS_ERRORS}>:/WX>>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:${unix-warnings}
$<$<BOOL:${CLI11_WARNINGS_AS_ERRORS}>:-Werror>>)

if(NOT CMAKE_VERSION VERSION_LESS 3.13)
target_link_options(CLI11_warnings INTERFACE $<$<BOOL:${CLI11_FORCE_LIBCXX}>:-stdlib=libc++>)
endif()

# To see in IDE, headers must be listed for target
set(MAYBE_CONFIGURE_DEPENDS "")
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND NOT CMAKE_VERSION VERSION_LESS 3.12)
list(INSERT MAYBE_CONFIGURE_DEPENDS 0 CONFIGURE_DEPENDS)
endif()

file(GLOB CLI11_headers ${MAYBE_CONFIGURE_DEPENDS} "${PROJECT_SOURCE_DIR}/include/CLI/*.hpp")
file(GLOB CLI11_impl_headers ${MAYBE_CONFIGURE_DEPENDS}
"${PROJECT_SOURCE_DIR}/include/CLI/impl/*.hpp")
include(CLI11Warnings)

if(CLI11_PRECOMPILED)
# Create static lib
file(GLOB CLI11_precompile_sources "${PROJECT_SOURCE_DIR}/src/*.cpp")
add_library(CLI11 STATIC ${CLI11_headers} ${CLI11_impl_headers} ${CLI11_precompile_sources})
target_compile_definitions(CLI11 PUBLIC -DCLI11_COMPILE)

set(PUBLIC_OR_INTERFACE PUBLIC)
else()
add_library(CLI11 INTERFACE)
set(PUBLIC_OR_INTERFACE INTERFACE)
endif()

# Allow IDE's to group targets into folders
add_library(CLI11::CLI11 ALIAS CLI11) # for add_subdirectory calls

# Duplicated because CMake adds the current source dir if you don't.
target_include_directories(
CLI11 ${PUBLIC_OR_INTERFACE} $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

if(CMAKE_VERSION VERSION_LESS 3.8)
# This might not be a complete list
target_compile_features(
CLI11
INTERFACE cxx_lambdas
cxx_nullptr
cxx_override
cxx_range_for
cxx_right_angle_brackets
cxx_strong_enums
cxx_constexpr
cxx_auto_type)
else()
target_compile_features(CLI11 INTERFACE cxx_std_11)
endif()
add_subdirectory(src)

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

# This folder should be installed
if(CLI11_INSTALL)
install(FILES ${CLI11_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/CLI")
if(NOT CLI11_COMPILE)
install(FILES ${CLI11_impl_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/CLI/impl")
endif()

# Make an export target
install(TARGETS CLI11 EXPORT CLI11Targets)

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

if(CLI11_SINGLE_FILE)
# Single file test
if(CMAKE_VERSION VERSION_LESS 3.12)
find_package(PythonInterp REQUIRED)
add_executable(Python::Interpreter IMPORTED)
set_target_properties(Python::Interpreter PROPERTIES IMPORTED_LOCATION "${PYTHON_EXECUTABLE}"
VERSION "${PYTHON_VERSION_STRING}")
else()
find_package(
Python
COMPONENTS Interpreter
REQUIRED)
endif()

file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/include")
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp"
COMMAND
Python::Interpreter "${CMAKE_CURRENT_SOURCE_DIR}/scripts/MakeSingleHeader.py"
${CLI11_headers} ${CLI11_impl_headers} --main "${CMAKE_CURRENT_SOURCE_DIR}/CLI11.hpp.in"
--output "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp" --version "${CLI11_VERSION}"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/CLI/CLI.hpp" ${CLI11_headers}
${CLI11_impl_headers})
add_custom_target(CLI11-generate-single-file ALL
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp")
set_property(TARGET CLI11-generate-single-file PROPERTY 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 CLI11-generate-single-file)
target_compile_definitions(CLI11_SINGLE INTERFACE -DCLI11_SINGLE_FILE)
target_include_directories(
CLI11_SINGLE INTERFACE $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include/>
$<INSTALL_INTERFACE:include>)
endif()

if(CLI11_BUILD_TESTS)
include(CTest)
add_subdirectory(tests)
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ jobs:
containerImage: gcc:8
cli11.std: 17
gcc4.8:
containerImage: gcc:4.8
Copy link
Collaborator

@henryiii henryiii Aug 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this can't just change to helics/buildenv:gcc4-8-builder? Does .ci/azure-cmake.yml break?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess would be no, but CMake is already installed so just seemed a waste of time, I will put it back to make sure.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put it back in the Docker section, installing CMake takes about 30 seconds on the CI test, so we can reduce the build time slightly by leaving it in its own section, or just leave the extra CMake install in place.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've got enough jobs where I'm not too worried about it. I'd rather like to move to GHA eventually & then we can probably use the CMake action. Keeping this simple makes it easier to move. ;)

containerImage: helics/buildenv:gcc4-8-builder
cli11.std: 11
cli11.options:
clang3.4:
Expand Down
37 changes: 37 additions & 0 deletions cmake/CLI11Warnings.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Special target that adds warnings. Is not exported.
add_library(CLI11_warnings INTERFACE)

set(unix-warnings -Wall -Wextra -pedantic -Wshadow -Wsign-conversion -Wswitch-enum)

# Clang warnings
# -Wfloat-equal could be added with Catch::literals and _a usage
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
list(
APPEND
unix-warnings
-Wcast-align
-Wimplicit-atomic-properties
-Wmissing-declarations
-Woverlength-strings
-Wshadow
-Wstrict-selector-match
-Wundeclared-selector)
# -Wunreachable-code Doesn't work on Clang 3.4
endif()

# Buggy in GCC 4.8
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
list(APPEND unix-warnings -Weffc++)
endif()

target_compile_options(
CLI11_warnings
INTERFACE $<$<BOOL:${CLI11_FORCE_LIBCXX}>:-stdlib=libc++>
$<$<CXX_COMPILER_ID:MSVC>:/W4
$<$<BOOL:${CLI11_WARNINGS_AS_ERRORS}>:/WX>>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:${unix-warnings}
$<$<BOOL:${CLI11_WARNINGS_AS_ERRORS}>:-Werror>>)

if(NOT CMAKE_VERSION VERSION_LESS 3.13)
target_link_options(CLI11_warnings INTERFACE $<$<BOOL:${CLI11_FORCE_LIBCXX}>:-stdlib=libc++>)
endif()
1 change: 1 addition & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CLI11Conan(ConanFile):
"LICENSE",
"README.md",
"include/*",
"src/*",
"extern/*",
"cmake/*",
"CMakeLists.txt",
Expand Down
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function(add_cli_exe T)
add_executable(${T} ${ARGN} ${CLI11_headers})
add_executable(${T} ${ARGN})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By removing the headers, doesn't this mean you can't access these in an IDE anymore? (Though if you are using the build-ahead mode, you can - so maybe that's why?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In an IDE, the files are now added to the CLI11 Project, which is new. They are added as build in precompile mode or with newer CMakes they are added as private sources in the interface library.

target_link_libraries(${T} PUBLIC CLI11)
set_property(TARGET ${T} PROPERTY FOLDER "Examples")
if(CLI11_FORCE_LIBCXX)
Expand Down
120 changes: 120 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
set(CLI11_headerLoc "${PROJECT_SOURCE_DIR}/include/CLI")

set(CLI11_headers
${CLI11_headerLoc}/App.hpp
${CLI11_headerLoc}/CLI.hpp
${CLI11_headerLoc}/Config.hpp
${CLI11_headerLoc}/ConfigFwd.hpp
${CLI11_headerLoc}/Error.hpp
${CLI11_headerLoc}/Formatter.hpp
${CLI11_headerLoc}/FormatterFwd.hpp
${CLI11_headerLoc}/Macros.hpp
${CLI11_headerLoc}/Option.hpp
${CLI11_headerLoc}/Split.hpp
${CLI11_headerLoc}/StringTools.hpp
${CLI11_headerLoc}/Timer.hpp
${CLI11_headerLoc}/TypeTools.hpp
${CLI11_headerLoc}/Validators.hpp
${CLI11_headerLoc}/Version.hpp)

set(CLI11_implLoc "${PROJECT_SOURCE_DIR}/include/CLI/impl")

set(CLI11_impl_headers
${CLI11_implLoc}/App_inl.hpp
${CLI11_implLoc}/Config_inl.hpp
${CLI11_implLoc}/Formatter_inl.hpp
${CLI11_implLoc}/Option_inl.hpp
${CLI11_implLoc}/Split_inl.hpp
${CLI11_implLoc}/StringTools_inl.hpp
${CLI11_implLoc}/Validators_inl.hpp)

if(CLI11_PRECOMPILED)
# Create static lib
file(GLOB CLI11_precompile_sources "${PROJECT_SOURCE_DIR}/src/*.cpp")
add_library(CLI11 STATIC ${CLI11_headers} ${CLI11_impl_headers} ${CLI11_precompile_sources})
target_compile_definitions(CLI11 PUBLIC -DCLI11_COMPILE)

set(PUBLIC_OR_INTERFACE PUBLIC)
else()
add_library(CLI11 INTERFACE)
if(CMAKE_VERSION VERSION_GREATER 3.19)
# This is only useful for visual studio and other IDE builds
target_sources(CLI11 PRIVATE ${CLI11_headers} ${CLI11_impl_headers})
endif()

set(PUBLIC_OR_INTERFACE INTERFACE)
endif()

# Allow IDE's to group targets into folders
add_library(CLI11::CLI11 ALIAS CLI11) # for add_subdirectory calls

# Duplicated because CMake adds the current source dir if you don't.
target_include_directories(
CLI11 ${PUBLIC_OR_INTERFACE} $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

if(CMAKE_VERSION VERSION_LESS 3.8)
# This might not be a complete list
target_compile_features(
CLI11
INTERFACE cxx_lambdas
cxx_nullptr
cxx_override
cxx_range_for
cxx_right_angle_brackets
cxx_strong_enums
cxx_constexpr
cxx_auto_type)
else()
target_compile_features(CLI11 INTERFACE cxx_std_11)
endif()

if(CLI11_SINGLE_FILE)
# Single file test
if(CMAKE_VERSION VERSION_LESS 3.12)
find_package(PythonInterp REQUIRED)
add_executable(Python::Interpreter IMPORTED)
set_target_properties(Python::Interpreter PROPERTIES IMPORTED_LOCATION "${PYTHON_EXECUTABLE}"
VERSION "${PYTHON_VERSION_STRING}")
else()
find_package(
Python
COMPONENTS Interpreter
REQUIRED)
endif()

file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/include")
add_custom_command(
OUTPUT "${PROJECT_BINARY_DIR}/include/CLI11.hpp"
COMMAND
Python::Interpreter "${PROJECT_SOURCE_DIR}/scripts/MakeSingleHeader.py" ${CLI11_headers}
${CLI11_impl_headers} --main "${PROJECT_SOURCE_DIR}/CLI11.hpp.in" --output
"${PROJECT_BINARY_DIR}/include/CLI11.hpp" --version "${CLI11_VERSION}"
DEPENDS "${PROJECT_SOURCE_DIR}/include/CLI/CLI.hpp" ${CLI11_headers} ${CLI11_impl_headers})
add_custom_target(CLI11-generate-single-file ALL
DEPENDS "${PROJECT_BINARY_DIR}/include/CLI11.hpp")
set_property(TARGET CLI11-generate-single-file PROPERTY FOLDER "Scripts")
if(CLI11_INSTALL)
install(FILES "${PROJECT_BINARY_DIR}/include/CLI11.hpp"
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
endif()
add_library(CLI11_SINGLE INTERFACE)
target_link_libraries(CLI11_SINGLE INTERFACE CLI11)
add_dependencies(CLI11_SINGLE CLI11-generate-single-file)
target_compile_definitions(CLI11_SINGLE INTERFACE -DCLI11_SINGLE_FILE)
target_include_directories(
CLI11_SINGLE INTERFACE $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include/>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
endif()

if(CLI11_INSTALL)

# Make an export target
install(TARGETS CLI11 EXPORT CLI11Targets)
if(NOT CLI11_SINGLE_FILE)
install(FILES ${CLI11_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/CLI")
if(NOT CLI11_COMPILE)
install(FILES ${CLI11_impl_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/CLI/impl")
endif()
endif()
endif()
6 changes: 2 additions & 4 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
list(APPEND CMAKE_MODULE_PATH "${CLI11_SOURCE_DIR}/cmake")

if(CLI11_SANITIZERS)
message(STATUS "Using arsenm/sanitizers-cmake")
FetchContent_Declare(
Expand Down Expand Up @@ -109,7 +107,7 @@ foreach(T IN LISTS CLI11_TESTS)
if(CLI11_CUDA_TESTS)
set_property(SOURCE ${T}.cpp PROPERTY LANGUAGE CUDA)
endif()
add_executable(${T} ${T}.cpp ${CLI11_headers})
add_executable(${T} ${T}.cpp)
add_sanitizers(${T})
if(NOT CLI11_CUDA_TESTS)
target_link_libraries(${T} PRIVATE CLI11_warnings)
Expand All @@ -126,7 +124,7 @@ foreach(T IN LISTS CLI11_TESTS)
endforeach()

foreach(T IN LISTS CLI11_MULTIONLY_TESTS)
add_executable(${T} ${T}.cpp ${CLI11_headers})
add_executable(${T} ${T}.cpp)
add_sanitizers(${T})
target_link_libraries(${T} PUBLIC CLI11)
add_catch_test(${T})
Expand Down