From 6ff2947865841e4631f7e5eea5d74a5d2f7e577d Mon Sep 17 00:00:00 2001 From: guoh27 Date: Fri, 2 Jun 2023 11:33:15 +0800 Subject: [PATCH 01/10] add CMakeLists.txt --- CMakeLists.txt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..8d89f06 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.10) +project(noshell VERSION 0.1.0 LANGUAGES CXX) + +file(GLOB NOSHELL_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/lib/*.cc" + "${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp" + "${CMAKE_CURRENT_SOURCE_DIR}/include/noshell/*.hpp") + +add_library(noshell STATIC ${NOSHELL_SRCS}) +add_library(noshell::noshell ALIAS noshell) +target_include_directories(noshell + PUBLIC + $ + $) +set_target_properties(noshell + PROPERTIES + CXX_STANDARD 11) \ No newline at end of file From a6830e1d5261adb821ad38c94cf982066d0bc925 Mon Sep 17 00:00:00 2001 From: guoh27 Date: Fri, 2 Jun 2023 11:33:54 +0800 Subject: [PATCH 02/10] Fix header function no inline --- include/noshell/handle.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/noshell/handle.hpp b/include/noshell/handle.hpp index f97e878..ac442bf 100644 --- a/include/noshell/handle.hpp +++ b/include/noshell/handle.hpp @@ -152,7 +152,7 @@ class Exit { void wait() { for(auto& h : handles) h.wait(); } }; -std::ostream& operator<<(std::ostream& os, const Exit& exit) { +inline std::ostream& operator<<(std::ostream& os, const Exit& exit) { auto it = exit.begin(); if(it != exit.end()) { os << *it; From d312ba354b2a8402040d7d031f3046b35b9368af Mon Sep 17 00:00:00 2001 From: guoh27 Date: Fri, 2 Jun 2023 11:41:13 +0800 Subject: [PATCH 03/10] change version to 0.2.0 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d89f06..f1c0f7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(noshell VERSION 0.1.0 LANGUAGES CXX) +project(noshell VERSION 0.2.0 LANGUAGES CXX) file(GLOB NOSHELL_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/lib/*.cc" "${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp" From 202bd182828228f4bd04b6d0151810535b9dfb75 Mon Sep 17 00:00:00 2001 From: "hong.guo" Date: Sat, 3 Jun 2023 18:23:34 +0800 Subject: [PATCH 04/10] Complete CMake build configuration --- CMakeLists.txt | 121 +++++++++++++++++++++++++++++++++++++---- noshellConfig.cmake.in | 6 ++ tests/CMakeLists.txt | 36 ++++++++++++ 3 files changed, 152 insertions(+), 11 deletions(-) create mode 100644 noshellConfig.cmake.in create mode 100644 tests/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index f1c0f7e..8f23318 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,16 +1,115 @@ cmake_minimum_required(VERSION 3.10) -project(noshell VERSION 0.2.0 LANGUAGES CXX) +set(project_name noshell) -file(GLOB NOSHELL_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/lib/*.cc" - "${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp" - "${CMAKE_CURRENT_SOURCE_DIR}/include/noshell/*.hpp") +include(GNUInstallDirs) -add_library(noshell STATIC ${NOSHELL_SRCS}) -add_library(noshell::noshell ALIAS noshell) -target_include_directories(noshell +set(NOSHELL_MASTER_PROJECT OFF) +if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + set(NOSHELL_MASTER_PROJECT ON) +endif() + +option(NOSHELL_BUILD_TESTS "Set to ON to build tests" ${NOSHELL_MASTER_PROJECT}) +option(NOSHELL_ENABLE_INSTALL "Generate the install target" ${NOSHELL_MASTER_PROJECT}) + +# get version +file(READ "${CMAKE_SOURCE_DIR}/configure.ac" configure_ac) +string(REGEX MATCH "AC_INIT\\\(\\\[noshell\\\],\\\[(.*)\\\],\\\[gmarcais@cmu.edu\\\]\\\)" _ "${configure_ac}") +if(NOT CMAKE_MATCH_COUNT EQUAL 1) + message(FATAL_ERROR "Could not extract major version number from configure.ac") +endif() +set(noshell_version ${CMAKE_MATCH_1}) +message(STATUS "noshell version: ${noshell_version}") + + +project(${project_name} VERSION "${noshell_version}" LANGUAGES CXX) + + +set(NOSHELL_SRCS lib/noshell.cc lib/setters.cc lib/utils.cc) + +add_library(${project_name} SHARED ${NOSHELL_SRCS}) +add_library(${project_name}-static STATIC ${NOSHELL_SRCS}) +add_library(${project_name}::${project_name} ALIAS ${project_name}) +add_library(${project_name}::${project_name}-static ALIAS ${project_name}-static) + +target_include_directories(${project_name} + PUBLIC + $ + $) +target_include_directories(${project_name}-static PUBLIC - $ - $) -set_target_properties(noshell + $ + $) +set_target_properties(${project_name} PROPERTIES - CXX_STANDARD 11) \ No newline at end of file + CXX_STANDARD 11) +set_target_properties(${project_name}-static + PROPERTIES + CXX_STANDARD 11) + + +if(NOSHELL_ENABLE_INSTALL) + set(project_config_in "${CMAKE_CURRENT_LIST_DIR}/noshellConfig.cmake.in") + set(project_config_out "${CMAKE_CURRENT_BINARY_DIR}/noshellConfig.cmake") + set(config_targets_file "noshellConfigTargets.cmake") + set(version_config_file "${CMAKE_CURRENT_BINARY_DIR}/noshellConfigVersion.cmake") + set(export_dest_dir "${CMAKE_INSTALL_LIBDIR}/cmake/noshell") + set(pkgconfig_install_dir "${CMAKE_INSTALL_LIBDIR}/pkgconfig") + set(pkgconfig "${CMAKE_CURRENT_BINARY_DIR}/noshell.pc") + + set(prefix "${CMAKE_INSTALL_PREFIX}") + set(exec_prefix "\${prefix}") + if (IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}") + set(libdir "${CMAKE_INSTALL_LIBDIR}") + else() + set(libdir "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}") + endif() + if (IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}") + set(includedir "${CMAKE_INSTALL_INCLUDEDIR}") + else() + set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}") + endif() + set(PACKAGE_VERSION "${noshell_version}") + configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/noshell.pc.in" + "${pkgconfig}" + @ONLY) + + install(FILES "${pkgconfig}" + DESTINATION "${pkgconfig_install_dir}") + + install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/" + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/noshell-${noshell_version}") + + install(TARGETS ${project_name} ${project_name}-static + EXPORT ${project_name} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) + + export( + TARGETS ${project_name} + NAMESPACE ${project_name}:: + FILE "${CMAKE_CURRENT_BINARY_DIR}/${config_targets_file}") + + install(EXPORT ${project_name} + DESTINATION ${export_dest_dir} + NAMESPACE ${project_name}:: + FILE ${config_targets_file}) + + include(CMakePackageConfigHelpers) + configure_package_config_file("${project_config_in}" "${project_config_out}" + INSTALL_DESTINATION ${export_dest_dir}) + + + write_basic_package_version_file("${version_config_file}" + VERSION "${noshell_version}" + COMPATIBILITY AnyNewerVersion) + + install(FILES "${project_config_out}" "${version_config_file}" DESTINATION "${export_dest_dir}") +endif(NOSHELL_ENABLE_INSTALL) + + +if(NOSHELL_BUILD_TESTS) + enable_testing() + add_subdirectory(tests) +endif() \ No newline at end of file diff --git a/noshellConfig.cmake.in b/noshellConfig.cmake.in new file mode 100644 index 0000000..397ea9a --- /dev/null +++ b/noshellConfig.cmake.in @@ -0,0 +1,6 @@ +@PACKAGE_INIT@ + +set(config_targets_file @config_targets_file@) +include("${CMAKE_CURRENT_LIST_DIR}/${config_targets_file}") + +check_required_components(noshell) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..621b160 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,36 @@ +cmake_minimum_required(VERSION 3.10) +project(noshell_tests CXX) + +if(NOT TARGET noshell) + # Stand-alone build + find_package(noshell REQUIRED) +endif() + +list(APPEND NOSHELL_TESTS_HELPER + check_open_fd.cc + kill_self.cc + puts_to.cc) + +list(APPEND NOSHELL_TESTS_LIST + libtest_misc.cc + test_cmd_redirection.cc + test_error.cc + test_extra_fds.cc + test_fd_type.cc + test_literal.cc + test_pipeline.cc + test_resources.cc + test_simple_command.cc) + +find_package(GTest REQUIRED) +find_package(Threads REQUIRED) + +foreach(src ${NOSHELL_TESTS_HELPER}) + GET_FILENAME_COMPONENT(target_name ${src} NAME_WE) + add_executable(${target_name} ${src}) +endforeach() + +add_executable(noshell_tests ${NOSHELL_TESTS_LIST}) +target_link_libraries(noshell_tests GTest::Main GTest::GTest Threads::Threads noshell) + +add_test(noshell_tests noshell_tests) From cb1e9c80cd1e638daf1d0860d64405f04abcba50 Mon Sep 17 00:00:00 2001 From: "hong.guo" Date: Sat, 3 Jun 2023 18:41:00 +0800 Subject: [PATCH 05/10] Fix a get version glitch. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8f23318..6754e31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ option(NOSHELL_BUILD_TESTS "Set to ON to build tests" ${NOSHELL_MASTER_PROJECT}) option(NOSHELL_ENABLE_INSTALL "Generate the install target" ${NOSHELL_MASTER_PROJECT}) # get version -file(READ "${CMAKE_SOURCE_DIR}/configure.ac" configure_ac) +file(READ "${CMAKE_CURRENT_SOURCE_DIR}/configure.ac" configure_ac) string(REGEX MATCH "AC_INIT\\\(\\\[noshell\\\],\\\[(.*)\\\],\\\[gmarcais@cmu.edu\\\]\\\)" _ "${configure_ac}") if(NOT CMAKE_MATCH_COUNT EQUAL 1) message(FATAL_ERROR "Could not extract major version number from configure.ac") From 86bfd11a31291aa7fb55a34d75a87d5a419be981 Mon Sep 17 00:00:00 2001 From: "hong.guo" Date: Sat, 3 Jun 2023 18:48:57 +0800 Subject: [PATCH 06/10] Update Readme, and pic to static library --- CMakeLists.txt | 6 +++--- README.md | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6754e31..203ff45 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,8 +44,8 @@ set_target_properties(${project_name} CXX_STANDARD 11) set_target_properties(${project_name}-static PROPERTIES - CXX_STANDARD 11) - + CXX_STANDARD 11 + POSITION_INDEPENDENT_CODE 1) if(NOSHELL_ENABLE_INSTALL) set(project_config_in "${CMAKE_CURRENT_LIST_DIR}/noshellConfig.cmake.in") @@ -112,4 +112,4 @@ endif(NOSHELL_ENABLE_INSTALL) if(NOSHELL_BUILD_TESTS) enable_testing() add_subdirectory(tests) -endif() \ No newline at end of file +endif() diff --git a/README.md b/README.md index 47df1ba..5a443ea 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,23 @@ sudo make install At this point, this library has only been tested on Linux, with `g++` version 4.7 or newer and `clang++` version 3.2. +### CMake + +Alternatively, you can use CMake to build the library: + +```sh +mkdir build +cmake -S . -B build +cmake --build build +sudo cmake --install build +``` + +Run the tests with: + +```sh +cd build && ctest +``` + ## Using the library Add the following to your make file: @@ -86,3 +103,17 @@ Add the following to your make file: CXXFLAGS = $(shell pkg-config --cflags noshell) LDFLAGS = $(shell pkg-config --libs noshell) ``` + +### CMake + +Add the following to your `CMakeLists.txt`: + +```cmake +find_package(noshell REQUIRED) + +# link shared library +target_link_libraries(mytarget noshell::noshell) + +# link static library +target_link_libraries(mytarget noshell::noshell-static) +``` From 47dd743aa66cc9decf96a3dab1ff08eb68772c5f Mon Sep 17 00:00:00 2001 From: "hong.guo" Date: Sat, 3 Jun 2023 18:57:33 +0800 Subject: [PATCH 07/10] Add ignore build dir --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index da59f79..cf01227 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ install-sh ltmain.sh missing test-driver + +/build/ \ No newline at end of file From ccf66382a8b3b0fa8a9f14c265939c46cb3bebf8 Mon Sep 17 00:00:00 2001 From: "hong.guo" Date: Sat, 3 Jun 2023 20:00:48 +0800 Subject: [PATCH 08/10] fix cmake GNUInstallDirs warning --- CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 203ff45..fcf2f69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,6 @@ cmake_minimum_required(VERSION 3.10) set(project_name noshell) -include(GNUInstallDirs) - set(NOSHELL_MASTER_PROJECT OFF) if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) set(NOSHELL_MASTER_PROJECT ON) @@ -23,6 +21,7 @@ message(STATUS "noshell version: ${noshell_version}") project(${project_name} VERSION "${noshell_version}" LANGUAGES CXX) +include(GNUInstallDirs) set(NOSHELL_SRCS lib/noshell.cc lib/setters.cc lib/utils.cc) From 4f17d88db0b11fa25ffd04d05ae7adde0a174e75 Mon Sep 17 00:00:00 2001 From: "hong.guo" Date: Sat, 3 Jun 2023 21:14:51 +0800 Subject: [PATCH 09/10] Make the static library and the shared library have the same name --- CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fcf2f69..0ec7807 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,7 +44,8 @@ set_target_properties(${project_name} set_target_properties(${project_name}-static PROPERTIES CXX_STANDARD 11 - POSITION_INDEPENDENT_CODE 1) + POSITION_INDEPENDENT_CODE 1 + OUTPUT_NAME ${project_name}) if(NOSHELL_ENABLE_INSTALL) set(project_config_in "${CMAKE_CURRENT_LIST_DIR}/noshellConfig.cmake.in") From 17cadf54c4af94ae3b0ec64788bfc0c324d69242 Mon Sep 17 00:00:00 2001 From: "hong.guo" Date: Sun, 4 Jun 2023 20:37:47 +0800 Subject: [PATCH 10/10] Misc update of cmake config --- CMakeLists.txt | 4 ++-- tests/CMakeLists.txt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ec7807..aac8db4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,10 +103,10 @@ if(NOSHELL_ENABLE_INSTALL) write_basic_package_version_file("${version_config_file}" VERSION "${noshell_version}" - COMPATIBILITY AnyNewerVersion) + COMPATIBILITY ExactVersion) install(FILES "${project_config_out}" "${version_config_file}" DESTINATION "${export_dest_dir}") -endif(NOSHELL_ENABLE_INSTALL) +endif() if(NOSHELL_BUILD_TESTS) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 621b160..cb49b07 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -31,6 +31,6 @@ foreach(src ${NOSHELL_TESTS_HELPER}) endforeach() add_executable(noshell_tests ${NOSHELL_TESTS_LIST}) -target_link_libraries(noshell_tests GTest::Main GTest::GTest Threads::Threads noshell) +target_link_libraries(noshell_tests GTest::Main GTest::GTest Threads::Threads noshell::noshell) add_test(noshell_tests noshell_tests)