From f6232a7f26ea72f16268caccb7ba5f56e361ecd1 Mon Sep 17 00:00:00 2001 From: Haowen Liu <35328328+lunacd@users.noreply.github.com> Date: Sun, 4 May 2025 10:52:13 -0400 Subject: [PATCH] Improve CMake setup (#118) * Support CMake 4 CMake 4 has removed compatibility with CMake < 3.5 Bumping minimum required version to 3.5 enables CMake 4 to build this code. * Move header into subdir * Improve CMake setup This commit configures and installs CMake metadata files. This also provides the namespaced ALIAS target `cpp-subprocess::subprocess`. * Update README with CMake instructions * Update include paths in tests --- CMakeLists.txt | 49 +++++++++++++++++-- README.md | 16 +++++- cmake/subprocess-config.cmake | 7 +++ .../subprocess.hpp | 0 test/test_cat.cc | 2 +- test/test_double_quotes.cc | 2 +- test/test_env.cc | 2 +- test/test_err_redirection.cc | 2 +- test/test_exception.cc | 2 +- test/test_redirection.cc.in | 2 +- test/test_ret_code.cc | 2 +- test/test_subprocess.cc | 2 +- 12 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 cmake/subprocess-config.cmake rename subprocess.hpp => cpp-subprocess/subprocess.hpp (100%) mode change 100755 => 100644 diff --git a/CMakeLists.txt b/CMakeLists.txt index 5acf5f8..c21809e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.1) -project(subprocess VERSION 0.0.1 LANGUAGES CXX) +cmake_minimum_required(VERSION 3.5) +project(subprocess VERSION 2.2 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 11 CACHE STRING "C++ standard to use") option(EXPORT_COMPILE_COMMANDS "create clang compile database" ON) @@ -10,10 +10,51 @@ find_package(Threads REQUIRED) add_library(subprocess INTERFACE) target_link_libraries(subprocess INTERFACE Threads::Threads) -target_include_directories(subprocess INTERFACE . ) +target_sources(subprocess PUBLIC + FILE_SET HEADERS + FILES + cpp-subprocess/subprocess.hpp + BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/ +) +add_library(cpp-subprocess::subprocess ALIAS subprocess) if(SUBPROCESS_INSTALL) - install(FILES subprocess.hpp DESTINATION include/cpp-subprocess/) + install( + TARGETS subprocess COMPONENT subprocess + EXPORT subprocess + FILE_SET HEADERS DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + ) + + include(CMakePackageConfigHelpers) + + configure_package_config_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/subprocess-config.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/subprocess-config.cmake" + INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/subprocess" + PATH_VARS PROJECT_NAME PROJECT_VERSION + ) + + write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/beman.exemplar-version.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY ExactVersion + ) + + install( + FILES + "${CMAKE_CURRENT_BINARY_DIR}/subprocess-config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/subprocess-version.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/subprocess" + COMPONENT subprocess + ) + + install( + EXPORT subprocess + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/beman.exemplar" + NAMESPACE cpp-subprocess:: + FILE subprocess-targets.cmake + COMPONENT subprocess + ) endif() if(SUBPROCESS_TESTS) diff --git a/README.md b/README.md index a27e68c..ab0dcc2 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ and they will be fixed as they are reported. Subprocess library has just a single source `subprocess.hpp` present at the top directory of this repository. All you need to do is add ```cpp -#inlcude "cpp-subprocess/subprocess.hpp" +#include "cpp-subprocess/subprocess.hpp" using namespace subprocess; // OR @@ -33,6 +33,20 @@ to the files where you want to make use of subprocessing. Make sure to add neces Checkout http://templated-thoughts.blogspot.in/2016/03/sub-processing-with-modern-c.html as well. +## CMake Projects + +```cmake +include(FetchContent) +FetchContent_Declare( + cpp-subprocess + GIT_REPOSITORY https://github.com/arun11299/cpp-subprocess.git + GIT_TAG v2.2 +) +FetchContent_MakeAvailable(cpp-subprocess) + +target_link_libraries( PRIVATE cpp-subprocess::subprocess) +``` + ## Compiler Support Linux - g++ 4.8 and above Mac OS - Clang 3.4 and later diff --git a/cmake/subprocess-config.cmake b/cmake/subprocess-config.cmake new file mode 100644 index 0000000..004d900 --- /dev/null +++ b/cmake/subprocess-config.cmake @@ -0,0 +1,7 @@ +set(SUBPROCESS_VERSION @PROJECT_VERSION@) + +@PACKAGE_INIT@ + +include(${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-targets.cmake) + +check_required_components(@PROJECT_NAME@) diff --git a/subprocess.hpp b/cpp-subprocess/subprocess.hpp old mode 100755 new mode 100644 similarity index 100% rename from subprocess.hpp rename to cpp-subprocess/subprocess.hpp diff --git a/test/test_cat.cc b/test/test_cat.cc index 1c2ac54..7077d28 100755 --- a/test/test_cat.cc +++ b/test/test_cat.cc @@ -1,5 +1,5 @@ #include -#include +#include namespace sp = subprocess; diff --git a/test/test_double_quotes.cc b/test/test_double_quotes.cc index 0378822..d90e6d0 100644 --- a/test/test_double_quotes.cc +++ b/test/test_double_quotes.cc @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/test/test_env.cc b/test/test_env.cc index 8a561d5..11cbc7e 100644 --- a/test/test_env.cc +++ b/test/test_env.cc @@ -1,5 +1,5 @@ #include -#include +#include using namespace subprocess; diff --git a/test/test_err_redirection.cc b/test/test_err_redirection.cc index 97c1459..eac109d 100644 --- a/test/test_err_redirection.cc +++ b/test/test_err_redirection.cc @@ -1,5 +1,5 @@ #include -#include +#include using namespace subprocess; diff --git a/test/test_exception.cc b/test/test_exception.cc index 51f4fb1..09d2678 100644 --- a/test/test_exception.cc +++ b/test/test_exception.cc @@ -1,6 +1,6 @@ #include #include -#include +#include namespace sp = subprocess; diff --git a/test/test_redirection.cc.in b/test/test_redirection.cc.in index 787ecc9..d0b2eec 100644 --- a/test/test_redirection.cc.in +++ b/test/test_redirection.cc.in @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/test/test_ret_code.cc b/test/test_ret_code.cc index e589653..332079b 100644 --- a/test/test_ret_code.cc +++ b/test/test_ret_code.cc @@ -1,5 +1,5 @@ #include -#include +#include namespace sp = subprocess; diff --git a/test/test_subprocess.cc b/test/test_subprocess.cc index 01d258d..f6ea520 100755 --- a/test/test_subprocess.cc +++ b/test/test_subprocess.cc @@ -1,5 +1,5 @@ #include -#include +#include using namespace subprocess;