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
This commit is contained in:
Haowen Liu 2025-05-04 10:52:13 -04:00 committed by GitHub
parent 9974ff69cd
commit f6232a7f26
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 75 additions and 13 deletions

View File

@ -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)

View File

@ -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(<your_target> PRIVATE cpp-subprocess::subprocess)
```
## Compiler Support
Linux - g++ 4.8 and above
Mac OS - Clang 3.4 and later

View File

@ -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@)

0
subprocess.hpp → cpp-subprocess/subprocess.hpp Executable file → Normal file
View File

View File

@ -1,5 +1,5 @@
#include <iostream>
#include <subprocess.hpp>
#include <cpp-subprocess/subprocess.hpp>
namespace sp = subprocess;

View File

@ -1,4 +1,4 @@
#include <subprocess.hpp>
#include <cpp-subprocess/subprocess.hpp>
#include <cassert>
#include <string>

View File

@ -1,5 +1,5 @@
#include <iostream>
#include <subprocess.hpp>
#include <cpp-subprocess/subprocess.hpp>
using namespace subprocess;

View File

@ -1,5 +1,5 @@
#include <iostream>
#include <subprocess.hpp>
#include <cpp-subprocess/subprocess.hpp>
using namespace subprocess;

View File

@ -1,6 +1,6 @@
#include <cassert>
#include <cstring>
#include <subprocess.hpp>
#include <cpp-subprocess/subprocess.hpp>
namespace sp = subprocess;

View File

@ -1,4 +1,4 @@
#include <subprocess.hpp>
#include <cpp-subprocess/subprocess.hpp>
#include <cstdlib>
#include <string>

View File

@ -1,5 +1,5 @@
#include <iostream>
#include <subprocess.hpp>
#include <cpp-subprocess/subprocess.hpp>
namespace sp = subprocess;

View File

@ -1,5 +1,5 @@
#include <iostream>
#include <subprocess.hpp>
#include <cpp-subprocess/subprocess.hpp>
using namespace subprocess;