From 5fdcea2f05b067a226c1670888b03e4dac0d1d38 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Mon, 27 Jan 2025 13:46:44 -0500 Subject: [PATCH 01/20] Add cmake command ystdlib_cpp_library and update cmake linting exclusion paths --- CMake/ystdlib-cpp-helpers.cmake | 48 +++++++++++++++++++++++++++++++++ CMakeLists.txt | 30 +++++++++++++++++++++ src/ystdlib/hello.cpp | 1 + taskfiles/lint-cmake.yaml | 5 +++- 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 CMake/ystdlib-cpp-helpers.cmake diff --git a/CMake/ystdlib-cpp-helpers.cmake b/CMake/ystdlib-cpp-helpers.cmake new file mode 100644 index 00000000..6517f10a --- /dev/null +++ b/CMake/ystdlib-cpp-helpers.cmake @@ -0,0 +1,48 @@ +# ystdlib_cpp_library() +# +# CMake function to imitate Bazel's cc_library rule. +# +# Parameters: +# NAME: name of target (see Note) +# HDRS: List of public header files for the library +# PUBLIC: Add this so that this library will be exported under ystdlib:: +# PRIVATE: Add this to make the library internal to ystdlib-cpp +# +# Note: +# When included as a subdirectory, ystdlib_cpp_library will always create a library named +# ystdlib_${NAME} and alias target ystdlib::${NAME}. The ystdlib:: form should always be used to +# reduce namespace pollution. +function(ystdlib_cpp_library) + set(options + PUBLIC + PRIVATE + ) + set(oneValueArgs NAME) + set(multiValueArgs HDRS) + cmake_parse_arguments( + arg_ystdlib_cpp_lib + "${options}" + "${oneValueArgs}" + "${multiValueArgs}" + ${ARGN} + ) + + if(YSTDLIB_CPP_ENABLE_INSTALL) + set(_TARGET_LIB_NAME "${arg_ystdlib_cpp_lib_NAME}") + else() + set(_TARGET_LIB_NAME "ystdlib_${arg_ystdlib_cpp_lib_NAME}") + endif() + + # TODO: add build process for libraries with source files + add_library(${_TARGET_LIB_NAME} INTERFACE) + target_include_directories( + ${_TARGET_LIB_NAME} + INTERFACE + "$" + "$" + ) + target_compile_features(${_TARGET_LIB_NAME} INTERFACE cxx_std_20) + add_library(ystdlib::${arg_ystdlib_cpp_lib_NAME} ALIAS ${_TARGET_LIB_NAME}) + + # TODO: install header files into target locations +endfunction() diff --git a/CMakeLists.txt b/CMakeLists.txt index 798b9658..02866f26 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,21 @@ cmake_minimum_required(VERSION 3.16.3) project(YSTDLIB_CPP LANGUAGES CXX) +# Static everything for now +option(BUILD_SHARED_LIBS OFF) +option(YSTDLIB_CPP_USE_STATIC_LIBS ON) +unset(YSTDLIB_CPP_SOVERSION) + set(YSTDLIB_CPP_VERSION "0.0.1" CACHE STRING "Project version.") +# When ystdlib-cpp is included as subproject (i.e. using add_subdirectory(ystdlib-cpp)) in the +# source tree of a project that uses it, install rules are disabled. +if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) + option(YSTDLIB_CPP_ENABLE_INSTALL "Enable installation rules" OFF) +else() + option(YSTDLIB_CPP_ENABLE_INSTALL "Enable installation rules" ON) +endif() + # Enable exporting compile commands set(CMAKE_EXPORT_COMPILE_COMMANDS ON @@ -11,6 +24,23 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS FORCE ) +# Configure include paths for building ystdlib-cpp. +list(APPEND YSTDLIB_CPP_BUILD_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src) + +# Configure include paths for projects using ystdlib-cpp after it has been installed. +if(YSTDLIB_CPP_ENABLE_INSTALL) + message(WARNING "Currently only supports using ystdlib-cpp as a subdirectory to a project.") + list(APPEND YSTDLIB_CPP_INSTALL_INCLUDE_DIRS ${YSTDLIB_CPP_BUILD_INCLUDE_DIRS}) +else() + list(APPEND YSTDLIB_CPP_INSTALL_INCLUDE_DIRS ${YSTDLIB_CPP_BUILD_INCLUDE_DIRS}) +endif() + +# Import CMake helper functions +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/CMake) +include(ystdlib-cpp-helpers) + +add_subdirectory(src/ystdlib) + add_executable(dummy) target_sources(dummy PRIVATE src/ystdlib/hello.cpp) target_compile_features(dummy PRIVATE cxx_std_20) diff --git a/src/ystdlib/hello.cpp b/src/ystdlib/hello.cpp index 5ed1c96a..d12219d0 100644 --- a/src/ystdlib/hello.cpp +++ b/src/ystdlib/hello.cpp @@ -1,3 +1,4 @@ +#include #include [[nodiscard]] auto main() -> int { diff --git a/taskfiles/lint-cmake.yaml b/taskfiles/lint-cmake.yaml index f3f2724a..8bff0be4 100644 --- a/taskfiles/lint-cmake.yaml +++ b/taskfiles/lint-cmake.yaml @@ -10,6 +10,8 @@ tasks: - "{{.ROOT_DIR}}/**/CMakeLists.txt" - "{{.TASKFILE}}" - exclude: "{{.ROOT_DIR}}/**/build/*" + - exclude: "{{.ROOT_DIR}}/**/cmake_install.cmake" + - exclude: "{{.ROOT_DIR}}/**/CMakeFiles/*" - exclude: "{{.ROOT_DIR}}/**/submodules/*" - exclude: "{{.ROOT_DIR}}/**/tools/*" deps: @@ -44,7 +46,8 @@ tasks: - |- . "{{.G_LINT_VENV_DIR}}/bin/activate" find . \ - \( -path '**/build' -o -path '**/submodules' -o -path '**/tools' \) -prune -o \ + \( -path '**/build' -o -path '**/CMakeFiles' -o -path '**/submodules' -o -path '**/tools' \) -prune -o \ + ! -path "**/cmake_install.cmake" \ \( -iname "CMakeLists.txt" -o -iname "*.cmake" -o -iname "*.cmake.in" \) \ -print0 | \ xargs -0 gersemi {{.FLAGS}} From e7df6bbeabe33853ae579c2a4138df51e47d3611 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Mon, 27 Jan 2025 17:13:39 -0500 Subject: [PATCH 02/20] Add cmake formatting file at library level --- src/ystdlib/.gersemirc | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/ystdlib/.gersemirc diff --git a/src/ystdlib/.gersemirc b/src/ystdlib/.gersemirc new file mode 100644 index 00000000..af63a057 --- /dev/null +++ b/src/ystdlib/.gersemirc @@ -0,0 +1,7 @@ +# yamllint disable-line rule:line-length +# yaml-language-server: $schema=https://raw.githubusercontent.com/BlankSpruce/gersemi/master/gersemi/configuration.schema.json + +definitions: ["../../CMake/ystdlib-cpp-helpers.cmake"] +indent: 2 +line_length: 0 # gersemi is unable to expand the list across multiple lines when using custom commands +list_expansion: "favour-expansion" From 0100bfbb048f9b30c190c661876de1c83fd46433 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Mon, 27 Jan 2025 17:14:29 -0500 Subject: [PATCH 03/20] Reformat lint-cmake.yaml --- taskfiles/lint-cmake.yaml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/taskfiles/lint-cmake.yaml b/taskfiles/lint-cmake.yaml index 8bff0be4..a726a81d 100644 --- a/taskfiles/lint-cmake.yaml +++ b/taskfiles/lint-cmake.yaml @@ -9,6 +9,8 @@ tasks: - "{{.ROOT_DIR}}/**/*.cmake.in" - "{{.ROOT_DIR}}/**/CMakeLists.txt" - "{{.TASKFILE}}" + - "{{.ROOT_DIR}}/.gersemirc" + - "{{.G_YSTDLIB_CPP_SRC_DIR}}/.gersemirc" - exclude: "{{.ROOT_DIR}}/**/build/*" - exclude: "{{.ROOT_DIR}}/**/cmake_install.cmake" - exclude: "{{.ROOT_DIR}}/**/CMakeFiles/*" @@ -46,8 +48,8 @@ tasks: - |- . "{{.G_LINT_VENV_DIR}}/bin/activate" find . \ - \( -path '**/build' -o -path '**/CMakeFiles' -o -path '**/submodules' -o -path '**/tools' \) -prune -o \ - ! -path "**/cmake_install.cmake" \ + \( -path '**/build' -o -path '**/cmake_install.cmake' -o -path '**/CMakeFiles' \ + -o -path '**/submodules' -o -path '**/tools' \) -prune -o \ \( -iname "CMakeLists.txt" -o -iname "*.cmake" -o -iname "*.cmake.in" \) \ -print0 | \ xargs -0 gersemi {{.FLAGS}} From c60837cd479fbe50c48b83533098ba3e7bec26c6 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Mon, 27 Jan 2025 17:52:17 -0500 Subject: [PATCH 04/20] Add a dummy library --- CMakeLists.txt | 2 ++ src/ystdlib/CMakeLists.txt | 3 +++ src/ystdlib/array/Array.hpp | 12 ++++++++++++ src/ystdlib/array/CMakeLists.txt | 6 ++++++ src/ystdlib/hello.cpp | 4 ++-- 5 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/ystdlib/CMakeLists.txt create mode 100644 src/ystdlib/array/Array.hpp create mode 100644 src/ystdlib/array/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 02866f26..32e44124 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,8 @@ include(ystdlib-cpp-helpers) add_subdirectory(src/ystdlib) +# Test dummy project add_executable(dummy) target_sources(dummy PRIVATE src/ystdlib/hello.cpp) target_compile_features(dummy PRIVATE cxx_std_20) +target_include_directories(dummy PRIVATE ${YSTDLIB_CPP_BUILD_INCLUDE_DIRS}) diff --git a/src/ystdlib/CMakeLists.txt b/src/ystdlib/CMakeLists.txt new file mode 100644 index 00000000..f8d9940f --- /dev/null +++ b/src/ystdlib/CMakeLists.txt @@ -0,0 +1,3 @@ +# gersemi: off +add_subdirectory(array) +# gersemi: on diff --git a/src/ystdlib/array/Array.hpp b/src/ystdlib/array/Array.hpp new file mode 100644 index 00000000..29916e21 --- /dev/null +++ b/src/ystdlib/array/Array.hpp @@ -0,0 +1,12 @@ +#ifndef YSTDLIB_ARRAY_HPP +#define YSTDLIB_ARRAY_HPP + +#include + +namespace ystdlib { +[[nodiscard]] inline auto hello() -> std::string { + return "Hello, world!"; +} +} // namespace ystdlib + +#endif // YSTDLIB_ARRAY_HPP diff --git a/src/ystdlib/array/CMakeLists.txt b/src/ystdlib/array/CMakeLists.txt new file mode 100644 index 00000000..f772e2b0 --- /dev/null +++ b/src/ystdlib/array/CMakeLists.txt @@ -0,0 +1,6 @@ +ystdlib_cpp_library( + NAME + array + HDRS + "Array.hpp" +) diff --git a/src/ystdlib/hello.cpp b/src/ystdlib/hello.cpp index d12219d0..04425050 100644 --- a/src/ystdlib/hello.cpp +++ b/src/ystdlib/hello.cpp @@ -1,7 +1,7 @@ -#include #include +#include [[nodiscard]] auto main() -> int { - std::cout << "Hello, world!" << '\n'; + std::cout << ystdlib::hello() << '\n'; return 0; } From b3da5de456a79143bec862ac22375144834763ba Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Mon, 27 Jan 2025 18:04:46 -0500 Subject: [PATCH 05/20] Use target link lib to show that ystdlib has actually been built --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 32e44124..f53cc7cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,5 +44,5 @@ add_subdirectory(src/ystdlib) # Test dummy project add_executable(dummy) target_sources(dummy PRIVATE src/ystdlib/hello.cpp) +target_link_libraries(dummy PRIVATE ystdlib::array) target_compile_features(dummy PRIVATE cxx_std_20) -target_include_directories(dummy PRIVATE ${YSTDLIB_CPP_BUILD_INCLUDE_DIRS}) From 7321bb7810a34293303d11f2fdb33c4458e2b07c Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Thu, 30 Jan 2025 14:19:24 -0500 Subject: [PATCH 06/20] Address offline review comments --- .gersemirc | 1 + CMake/ystdlib-cpp-helpers.cmake | 48 ++++++++----------- CMakeLists.txt | 24 ++-------- src/ystdlib/.gersemirc | 7 --- src/ystdlib/CMakeLists.txt | 4 +- src/ystdlib/array/CMakeLists.txt | 6 --- src/ystdlib/hello.cpp | 2 +- src/ystdlib/hello/CMakeLists.txt | 1 + .../{array/Array.hpp => hello/hello.hpp} | 0 9 files changed, 28 insertions(+), 65 deletions(-) delete mode 100644 src/ystdlib/.gersemirc delete mode 100644 src/ystdlib/array/CMakeLists.txt create mode 100644 src/ystdlib/hello/CMakeLists.txt rename src/ystdlib/{array/Array.hpp => hello/hello.hpp} (100%) diff --git a/.gersemirc b/.gersemirc index c6d6ef6c..b9cace7a 100644 --- a/.gersemirc +++ b/.gersemirc @@ -1,5 +1,6 @@ # yamllint disable-line rule:line-length # yaml-language-server: $schema=https://raw.githubusercontent.com/BlankSpruce/gersemi/master/gersemi/configuration.schema.json +definitions: ["CMake/ystdlib-cpp-helpers.cmake"] line_length: 100 list_expansion: "favour-expansion" diff --git a/CMake/ystdlib-cpp-helpers.cmake b/CMake/ystdlib-cpp-helpers.cmake index 6517f10a..ef32f116 100644 --- a/CMake/ystdlib-cpp-helpers.cmake +++ b/CMake/ystdlib-cpp-helpers.cmake @@ -1,48 +1,42 @@ -# ystdlib_cpp_library() +set(LIB_ENABLE_INSTALL ${YSTDLIB_CPP_ENABLE_INSTALL}) +set(LIB_BUILD_INTERFACE ${YSTDLIB_CPP_BUILD_INCLUDE_DIRS}) +set(LIB_INSTALL_INTERFACE ${YSTDLIB_CPP_INSTALL_INCLUDE_DIRS}) + +# cpp_library() # -# CMake function to imitate Bazel's cc_library rule. +# CMake function for adding C++ libraries with sources, dependencies, and build settings. # # Parameters: -# NAME: name of target (see Note) -# HDRS: List of public header files for the library -# PUBLIC: Add this so that this library will be exported under ystdlib:: -# PRIVATE: Add this to make the library internal to ystdlib-cpp -# -# Note: -# When included as a subdirectory, ystdlib_cpp_library will always create a library named -# ystdlib_${NAME} and alias target ystdlib::${NAME}. The ystdlib:: form should always be used to -# reduce namespace pollution. -function(ystdlib_cpp_library) - set(options - PUBLIC - PRIVATE +# NAMESPACE: namespace of the library +# NAME: name of target +function(cpp_library) + set(options) + set(oneValueArgs + NAME + NAMESPACE ) - set(oneValueArgs NAME) - set(multiValueArgs HDRS) + set(multiValueArgs) cmake_parse_arguments( - arg_ystdlib_cpp_lib + arg_cpp_lib # "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} ) - if(YSTDLIB_CPP_ENABLE_INSTALL) - set(_TARGET_LIB_NAME "${arg_ystdlib_cpp_lib_NAME}") + if(LIB_ENABLE_INSTALL) + set(_TARGET_LIB_NAME "${arg_cpp_lib_NAME}") else() - set(_TARGET_LIB_NAME "ystdlib_${arg_ystdlib_cpp_lib_NAME}") + set(_TARGET_LIB_NAME "${arg_cpp_lib_NAMESPACE}_${arg_cpp_lib_NAME}") endif() - # TODO: add build process for libraries with source files add_library(${_TARGET_LIB_NAME} INTERFACE) target_include_directories( ${_TARGET_LIB_NAME} INTERFACE - "$" - "$" + "$" + "$" ) target_compile_features(${_TARGET_LIB_NAME} INTERFACE cxx_std_20) - add_library(ystdlib::${arg_ystdlib_cpp_lib_NAME} ALIAS ${_TARGET_LIB_NAME}) - - # TODO: install header files into target locations + add_library(${arg_cpp_lib_NAMESPACE}::${arg_cpp_lib_NAME} ALIAS ${_TARGET_LIB_NAME}) endfunction() diff --git a/CMakeLists.txt b/CMakeLists.txt index f53cc7cf..ae0cfc40 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,21 +1,8 @@ cmake_minimum_required(VERSION 3.16.3) project(YSTDLIB_CPP LANGUAGES CXX) -# Static everything for now -option(BUILD_SHARED_LIBS OFF) -option(YSTDLIB_CPP_USE_STATIC_LIBS ON) -unset(YSTDLIB_CPP_SOVERSION) - set(YSTDLIB_CPP_VERSION "0.0.1" CACHE STRING "Project version.") -# When ystdlib-cpp is included as subproject (i.e. using add_subdirectory(ystdlib-cpp)) in the -# source tree of a project that uses it, install rules are disabled. -if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) - option(YSTDLIB_CPP_ENABLE_INSTALL "Enable installation rules" OFF) -else() - option(YSTDLIB_CPP_ENABLE_INSTALL "Enable installation rules" ON) -endif() - # Enable exporting compile commands set(CMAKE_EXPORT_COMPILE_COMMANDS ON @@ -27,13 +14,8 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS # Configure include paths for building ystdlib-cpp. list(APPEND YSTDLIB_CPP_BUILD_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src) -# Configure include paths for projects using ystdlib-cpp after it has been installed. -if(YSTDLIB_CPP_ENABLE_INSTALL) - message(WARNING "Currently only supports using ystdlib-cpp as a subdirectory to a project.") - list(APPEND YSTDLIB_CPP_INSTALL_INCLUDE_DIRS ${YSTDLIB_CPP_BUILD_INCLUDE_DIRS}) -else() - list(APPEND YSTDLIB_CPP_INSTALL_INCLUDE_DIRS ${YSTDLIB_CPP_BUILD_INCLUDE_DIRS}) -endif() +# Configure include paths for projects using ystdlib-cpp. +list(APPEND YSTDLIB_CPP_INSTALL_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src) # Import CMake helper functions list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/CMake) @@ -44,5 +26,5 @@ add_subdirectory(src/ystdlib) # Test dummy project add_executable(dummy) target_sources(dummy PRIVATE src/ystdlib/hello.cpp) -target_link_libraries(dummy PRIVATE ystdlib::array) +target_link_libraries(dummy PRIVATE ystdlib::hello) target_compile_features(dummy PRIVATE cxx_std_20) diff --git a/src/ystdlib/.gersemirc b/src/ystdlib/.gersemirc deleted file mode 100644 index af63a057..00000000 --- a/src/ystdlib/.gersemirc +++ /dev/null @@ -1,7 +0,0 @@ -# yamllint disable-line rule:line-length -# yaml-language-server: $schema=https://raw.githubusercontent.com/BlankSpruce/gersemi/master/gersemi/configuration.schema.json - -definitions: ["../../CMake/ystdlib-cpp-helpers.cmake"] -indent: 2 -line_length: 0 # gersemi is unable to expand the list across multiple lines when using custom commands -list_expansion: "favour-expansion" diff --git a/src/ystdlib/CMakeLists.txt b/src/ystdlib/CMakeLists.txt index f8d9940f..33bde8ad 100644 --- a/src/ystdlib/CMakeLists.txt +++ b/src/ystdlib/CMakeLists.txt @@ -1,3 +1 @@ -# gersemi: off -add_subdirectory(array) -# gersemi: on +add_subdirectory(hello) diff --git a/src/ystdlib/array/CMakeLists.txt b/src/ystdlib/array/CMakeLists.txt deleted file mode 100644 index f772e2b0..00000000 --- a/src/ystdlib/array/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -ystdlib_cpp_library( - NAME - array - HDRS - "Array.hpp" -) diff --git a/src/ystdlib/hello.cpp b/src/ystdlib/hello.cpp index 04425050..d7e1367d 100644 --- a/src/ystdlib/hello.cpp +++ b/src/ystdlib/hello.cpp @@ -1,5 +1,5 @@ #include -#include +#include [[nodiscard]] auto main() -> int { std::cout << ystdlib::hello() << '\n'; diff --git a/src/ystdlib/hello/CMakeLists.txt b/src/ystdlib/hello/CMakeLists.txt new file mode 100644 index 00000000..9def7e8f --- /dev/null +++ b/src/ystdlib/hello/CMakeLists.txt @@ -0,0 +1 @@ +cpp_library(NAME hello NAMESPACE ystdlib) diff --git a/src/ystdlib/array/Array.hpp b/src/ystdlib/hello/hello.hpp similarity index 100% rename from src/ystdlib/array/Array.hpp rename to src/ystdlib/hello/hello.hpp From 888189aebc9b245a612f0cab92eb8e0d52c2e824 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Thu, 30 Jan 2025 14:29:57 -0500 Subject: [PATCH 07/20] Trim --- CMake/ystdlib-cpp-helpers.cmake | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/CMake/ystdlib-cpp-helpers.cmake b/CMake/ystdlib-cpp-helpers.cmake index ef32f116..f403f789 100644 --- a/CMake/ystdlib-cpp-helpers.cmake +++ b/CMake/ystdlib-cpp-helpers.cmake @@ -1,4 +1,3 @@ -set(LIB_ENABLE_INSTALL ${YSTDLIB_CPP_ENABLE_INSTALL}) set(LIB_BUILD_INTERFACE ${YSTDLIB_CPP_BUILD_INCLUDE_DIRS}) set(LIB_INSTALL_INTERFACE ${YSTDLIB_CPP_INSTALL_INCLUDE_DIRS}) @@ -7,8 +6,8 @@ set(LIB_INSTALL_INTERFACE ${YSTDLIB_CPP_INSTALL_INCLUDE_DIRS}) # CMake function for adding C++ libraries with sources, dependencies, and build settings. # # Parameters: +# NAME: name of the library target # NAMESPACE: namespace of the library -# NAME: name of target function(cpp_library) set(options) set(oneValueArgs @@ -24,12 +23,6 @@ function(cpp_library) ${ARGN} ) - if(LIB_ENABLE_INSTALL) - set(_TARGET_LIB_NAME "${arg_cpp_lib_NAME}") - else() - set(_TARGET_LIB_NAME "${arg_cpp_lib_NAMESPACE}_${arg_cpp_lib_NAME}") - endif() - add_library(${_TARGET_LIB_NAME} INTERFACE) target_include_directories( ${_TARGET_LIB_NAME} From 16c2883335d69ccfbe202c7828b4230d446aced9 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Thu, 30 Jan 2025 14:47:50 -0500 Subject: [PATCH 08/20] Fix build error --- CMake/ystdlib-cpp-helpers.cmake | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/CMake/ystdlib-cpp-helpers.cmake b/CMake/ystdlib-cpp-helpers.cmake index f403f789..fba62e9b 100644 --- a/CMake/ystdlib-cpp-helpers.cmake +++ b/CMake/ystdlib-cpp-helpers.cmake @@ -1,5 +1,5 @@ -set(LIB_BUILD_INTERFACE ${YSTDLIB_CPP_BUILD_INCLUDE_DIRS}) -set(LIB_INSTALL_INTERFACE ${YSTDLIB_CPP_INSTALL_INCLUDE_DIRS}) +set(CPP_LIB_BUILD_INTERFACE ${YSTDLIB_CPP_BUILD_INCLUDE_DIRS}) +set(CPP_LIB_INSTALL_INTERFACE ${YSTDLIB_CPP_INSTALL_INCLUDE_DIRS}) # cpp_library() # @@ -15,21 +15,15 @@ function(cpp_library) NAMESPACE ) set(multiValueArgs) - cmake_parse_arguments( - arg_cpp_lib # - "${options}" - "${oneValueArgs}" - "${multiValueArgs}" - ${ARGN} - ) + cmake_parse_arguments(arg_cpp_lib "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) - add_library(${_TARGET_LIB_NAME} INTERFACE) + add_library(${arg_cpp_lib_NAME} INTERFACE) target_include_directories( - ${_TARGET_LIB_NAME} + ${arg_cpp_lib_NAME} INTERFACE - "$" - "$" + "$" + "$" ) - target_compile_features(${_TARGET_LIB_NAME} INTERFACE cxx_std_20) - add_library(${arg_cpp_lib_NAMESPACE}::${arg_cpp_lib_NAME} ALIAS ${_TARGET_LIB_NAME}) + target_compile_features(${arg_cpp_lib_NAME} INTERFACE cxx_std_20) + add_library(${arg_cpp_lib_NAMESPACE}::${arg_cpp_lib_NAME} ALIAS ${arg_cpp_lib_NAME}) endfunction() From bff4345ac8ddead3d42f766a9edcd795083c7256 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Fri, 31 Jan 2025 23:47:59 -0500 Subject: [PATCH 09/20] Address review concerns --- CMake/ystdlib-cpp-helpers.cmake | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/CMake/ystdlib-cpp-helpers.cmake b/CMake/ystdlib-cpp-helpers.cmake index fba62e9b..54bfc7e3 100644 --- a/CMake/ystdlib-cpp-helpers.cmake +++ b/CMake/ystdlib-cpp-helpers.cmake @@ -1,20 +1,17 @@ set(CPP_LIB_BUILD_INTERFACE ${YSTDLIB_CPP_BUILD_INCLUDE_DIRS}) set(CPP_LIB_INSTALL_INTERFACE ${YSTDLIB_CPP_INSTALL_INCLUDE_DIRS}) -# cpp_library() -# # CMake function for adding C++ libraries with sources, dependencies, and build settings. # -# Parameters: -# NAME: name of the library target -# NAMESPACE: namespace of the library +# @param NAME +# @param NAMESPACE function(cpp_library) - set(options) + set(options "") set(oneValueArgs NAME NAMESPACE ) - set(multiValueArgs) + set(multiValueArgs "") cmake_parse_arguments(arg_cpp_lib "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) add_library(${arg_cpp_lib_NAME} INTERFACE) From 56f2fa6ca90d7009c0fec6eb2d2084840d9e3149 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Sat, 1 Feb 2025 04:13:55 -0500 Subject: [PATCH 10/20] Change header guard name --- src/ystdlib/hello/hello.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ystdlib/hello/hello.hpp b/src/ystdlib/hello/hello.hpp index 29916e21..dea6498e 100644 --- a/src/ystdlib/hello/hello.hpp +++ b/src/ystdlib/hello/hello.hpp @@ -1,5 +1,5 @@ -#ifndef YSTDLIB_ARRAY_HPP -#define YSTDLIB_ARRAY_HPP +#ifndef YSTDLIB_HELLO_HPP +#define YSTDLIB_HELLO_HPP #include @@ -9,4 +9,4 @@ namespace ystdlib { } } // namespace ystdlib -#endif // YSTDLIB_ARRAY_HPP +#endif // YSTDLIB_HELLO_HPP From 0ec0d7598b66b04aa71da1cae96d79dc2aea0452 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Sun, 2 Feb 2025 19:34:44 -0500 Subject: [PATCH 11/20] Remove unused install interface --- CMake/ystdlib-cpp-helpers.cmake | 2 -- CMakeLists.txt | 6 ++---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/CMake/ystdlib-cpp-helpers.cmake b/CMake/ystdlib-cpp-helpers.cmake index 54bfc7e3..c496316f 100644 --- a/CMake/ystdlib-cpp-helpers.cmake +++ b/CMake/ystdlib-cpp-helpers.cmake @@ -1,5 +1,4 @@ set(CPP_LIB_BUILD_INTERFACE ${YSTDLIB_CPP_BUILD_INCLUDE_DIRS}) -set(CPP_LIB_INSTALL_INTERFACE ${YSTDLIB_CPP_INSTALL_INCLUDE_DIRS}) # CMake function for adding C++ libraries with sources, dependencies, and build settings. # @@ -19,7 +18,6 @@ function(cpp_library) ${arg_cpp_lib_NAME} INTERFACE "$" - "$" ) target_compile_features(${arg_cpp_lib_NAME} INTERFACE cxx_std_20) add_library(${arg_cpp_lib_NAMESPACE}::${arg_cpp_lib_NAME} ALIAS ${arg_cpp_lib_NAME}) diff --git a/CMakeLists.txt b/CMakeLists.txt index ae0cfc40..1b5af189 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,12 +11,10 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS FORCE ) -# Configure include paths for building ystdlib-cpp. +# Configure include paths for building `ystdlib-cpp`, as well as projects using `ystdlib-cpp` via +# the `add_subdirectory()` call. list(APPEND YSTDLIB_CPP_BUILD_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src) -# Configure include paths for projects using ystdlib-cpp. -list(APPEND YSTDLIB_CPP_INSTALL_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src) - # Import CMake helper functions list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/CMake) include(ystdlib-cpp-helpers) From 83c8123fd547642e6ea410936918ad4c56dd76bf Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Mon, 3 Feb 2025 03:29:32 -0500 Subject: [PATCH 12/20] Fix lint sources alphabetical order --- taskfiles/lint-cmake.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/taskfiles/lint-cmake.yaml b/taskfiles/lint-cmake.yaml index a726a81d..cd232888 100644 --- a/taskfiles/lint-cmake.yaml +++ b/taskfiles/lint-cmake.yaml @@ -5,12 +5,12 @@ tasks: desc: "Runs the CMake linters." sources: &cmake_format_src_files - "{{.G_LINT_VENV_CHECKSUM_FILE}}" + - "{{.G_YSTDLIB_CPP_SRC_DIR}}/.gersemirc" - "{{.ROOT_DIR}}/**/*.cmake" - "{{.ROOT_DIR}}/**/*.cmake.in" - "{{.ROOT_DIR}}/**/CMakeLists.txt" - - "{{.TASKFILE}}" - "{{.ROOT_DIR}}/.gersemirc" - - "{{.G_YSTDLIB_CPP_SRC_DIR}}/.gersemirc" + - "{{.TASKFILE}}" - exclude: "{{.ROOT_DIR}}/**/build/*" - exclude: "{{.ROOT_DIR}}/**/cmake_install.cmake" - exclude: "{{.ROOT_DIR}}/**/CMakeFiles/*" From 0f6857d3f5475cb5dd583179fd74764458244489 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Mon, 3 Feb 2025 03:36:22 -0500 Subject: [PATCH 13/20] Rebuild dummy project structure according to review --- CMakeLists.txt | 4 ++-- src/main.cpp | 7 +++++++ src/ystdlib/CMakeLists.txt | 2 +- src/ystdlib/hello.cpp | 7 ------- src/ystdlib/hello/CMakeLists.txt | 1 - src/ystdlib/hello/hello.hpp | 12 ------------ src/ystdlib/testlib/CMakeLists.txt | 1 + src/ystdlib/testlib/hello.hpp | 12 ++++++++++++ 8 files changed, 23 insertions(+), 23 deletions(-) create mode 100644 src/main.cpp delete mode 100644 src/ystdlib/hello.cpp delete mode 100644 src/ystdlib/hello/CMakeLists.txt delete mode 100644 src/ystdlib/hello/hello.hpp create mode 100644 src/ystdlib/testlib/CMakeLists.txt create mode 100644 src/ystdlib/testlib/hello.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b5af189..d2255bd0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,6 @@ add_subdirectory(src/ystdlib) # Test dummy project add_executable(dummy) -target_sources(dummy PRIVATE src/ystdlib/hello.cpp) -target_link_libraries(dummy PRIVATE ystdlib::hello) +target_sources(dummy PRIVATE src/main.cpp) +target_link_libraries(dummy PRIVATE ystdlib::testlib) target_compile_features(dummy PRIVATE cxx_std_20) diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 00000000..324fa36f --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,7 @@ +#include +#include + +[[nodiscard]] auto main() -> int { + std::cout << ystdlib::testlib::hello() << '\n'; + return 0; +} diff --git a/src/ystdlib/CMakeLists.txt b/src/ystdlib/CMakeLists.txt index 33bde8ad..0d4ea731 100644 --- a/src/ystdlib/CMakeLists.txt +++ b/src/ystdlib/CMakeLists.txt @@ -1 +1 @@ -add_subdirectory(hello) +add_subdirectory(testlib) diff --git a/src/ystdlib/hello.cpp b/src/ystdlib/hello.cpp deleted file mode 100644 index d7e1367d..00000000 --- a/src/ystdlib/hello.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include - -[[nodiscard]] auto main() -> int { - std::cout << ystdlib::hello() << '\n'; - return 0; -} diff --git a/src/ystdlib/hello/CMakeLists.txt b/src/ystdlib/hello/CMakeLists.txt deleted file mode 100644 index 9def7e8f..00000000 --- a/src/ystdlib/hello/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -cpp_library(NAME hello NAMESPACE ystdlib) diff --git a/src/ystdlib/hello/hello.hpp b/src/ystdlib/hello/hello.hpp deleted file mode 100644 index dea6498e..00000000 --- a/src/ystdlib/hello/hello.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef YSTDLIB_HELLO_HPP -#define YSTDLIB_HELLO_HPP - -#include - -namespace ystdlib { -[[nodiscard]] inline auto hello() -> std::string { - return "Hello, world!"; -} -} // namespace ystdlib - -#endif // YSTDLIB_HELLO_HPP diff --git a/src/ystdlib/testlib/CMakeLists.txt b/src/ystdlib/testlib/CMakeLists.txt new file mode 100644 index 00000000..7c113fee --- /dev/null +++ b/src/ystdlib/testlib/CMakeLists.txt @@ -0,0 +1 @@ +cpp_library(NAME testlib NAMESPACE ystdlib) diff --git a/src/ystdlib/testlib/hello.hpp b/src/ystdlib/testlib/hello.hpp new file mode 100644 index 00000000..d920f170 --- /dev/null +++ b/src/ystdlib/testlib/hello.hpp @@ -0,0 +1,12 @@ +#ifndef YSTDLIB_TESTLIB_HELLO_HPP +#define YSTDLIB_TESTLIB_HELLO_HPP + +#include + +namespace ystdlib::testlib { +[[nodiscard]] inline auto hello() -> std::string { + return "Hello, world!"; +} +} // namespace ystdlib + +#endif // YSTDLIB_TESTLIB_HELLO_HPP From 951284a23f934b57a831c2d47703a039155cba26 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Mon, 3 Feb 2025 03:44:29 -0500 Subject: [PATCH 14/20] Address review concern about lib namespaces --- CMake/ystdlib-cpp-helpers.cmake | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CMake/ystdlib-cpp-helpers.cmake b/CMake/ystdlib-cpp-helpers.cmake index c496316f..9152d8ac 100644 --- a/CMake/ystdlib-cpp-helpers.cmake +++ b/CMake/ystdlib-cpp-helpers.cmake @@ -1,6 +1,9 @@ -set(CPP_LIB_BUILD_INTERFACE ${YSTDLIB_CPP_BUILD_INCLUDE_DIRS}) +# Set up include paths for building the project and for external projects that incorporate this +# project using the add_subdirectory() function. +set(CPP_LIB_BUILD_INTERFACE "${PROJECT_SOURCE_DIR}/src") -# CMake function for adding C++ libraries with sources, dependencies, and build settings. +# Adds a c++20 interface library in the subdirectory NAME with the target NAME and alias +# NAMESPACE::NAME. Libraries with multiple levels of namespace nesting are currently not supported. # # @param NAME # @param NAMESPACE From ba2066fb97056ccf79941e9aa5309603c1728966 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Mon, 3 Feb 2025 03:45:20 -0500 Subject: [PATCH 15/20] lint fix --- src/ystdlib/testlib/hello.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ystdlib/testlib/hello.hpp b/src/ystdlib/testlib/hello.hpp index d920f170..183acb60 100644 --- a/src/ystdlib/testlib/hello.hpp +++ b/src/ystdlib/testlib/hello.hpp @@ -7,6 +7,6 @@ namespace ystdlib::testlib { [[nodiscard]] inline auto hello() -> std::string { return "Hello, world!"; } -} // namespace ystdlib +} // namespace ystdlib::testlib #endif // YSTDLIB_TESTLIB_HELLO_HPP From 3d2fecf47b668caecfc6b96b9e09d4fbb0f33844 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Mon, 3 Feb 2025 04:13:33 -0500 Subject: [PATCH 16/20] Introduce the build interface param for cpp_libraray() --- CMake/ystdlib-cpp-helpers.cmake | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/CMake/ystdlib-cpp-helpers.cmake b/CMake/ystdlib-cpp-helpers.cmake index 9152d8ac..30f621f6 100644 --- a/CMake/ystdlib-cpp-helpers.cmake +++ b/CMake/ystdlib-cpp-helpers.cmake @@ -1,26 +1,34 @@ -# Set up include paths for building the project and for external projects that incorporate this -# project using the add_subdirectory() function. -set(CPP_LIB_BUILD_INTERFACE "${PROJECT_SOURCE_DIR}/src") - # Adds a c++20 interface library in the subdirectory NAME with the target NAME and alias # NAMESPACE::NAME. Libraries with multiple levels of namespace nesting are currently not supported. # # @param NAME # @param NAMESPACE +# @param [LIB_BUILD_INTERFACE] The list of include paths for building the library and for external +# projects that link against it via the add_subdirectory() function. function(cpp_library) set(options "") set(oneValueArgs NAME NAMESPACE ) - set(multiValueArgs "") + set(multiValueArgs LIB_BUILD_INTERFACE) cmake_parse_arguments(arg_cpp_lib "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + # TODO: Turn this into a function for handling other optional params that have default values. + if("LIB_BUILD_INTERFACE" IN_LIST arg_cpp_lib_KEYWORDS_MISSING_VALUES) + message( + FATAL_ERROR + "Missing build interface list for ${arg_cpp_lib_NAMESPACE}::${arg_cpp_lib_NAME}." + ) + elseif(NOT DEFINED arg_cpp_lib_LIB_BUILD_INTERFACE) + set(arg_cpp_lib_LIB_BUILD_INTERFACE "${PROJECT_SOURCE_DIR}/src") + endif() + add_library(${arg_cpp_lib_NAME} INTERFACE) target_include_directories( ${arg_cpp_lib_NAME} INTERFACE - "$" + "$" ) target_compile_features(${arg_cpp_lib_NAME} INTERFACE cxx_std_20) add_library(${arg_cpp_lib_NAMESPACE}::${arg_cpp_lib_NAME} ALIAS ${arg_cpp_lib_NAME}) From cdefa7ddfd8c73fc0e6a0f9322b3193ef994f085 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Mon, 3 Feb 2025 04:23:35 -0500 Subject: [PATCH 17/20] Update usage doc --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index fcbd2aad..e00dbe9b 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,18 @@ Initialize and update submodules: git submodule update --init --recursive ``` +## Usage +Clone `ystdlib-cpp` into your project. Then, in your project's `CMakeLists.txt`, add the following: +```cmake +add_subdirectory(/path/to/ystdlib-cpp EXCLUDE_FROM_ALL) +target_link_libraries( + ystdlib:: ystdlib:: ... ystdlib:: + # other libs... +) +``` +Ensure that `ystdlib-cpp` is either within a subdirectory of the folder containing `CMakeLists.txt` +or at the same level. + ## Linting Before submitting a pull request, ensure you’ve run the linting commands below and have fixed all violations and suppressed all warnings. From 77135c070144d60047cce3f8485bbd6f4f455f90 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Mon, 3 Feb 2025 04:26:02 -0500 Subject: [PATCH 18/20] Apply suggestions from code review Co-authored-by: davidlion --- CMake/ystdlib-cpp-helpers.cmake | 4 ++-- CMakeLists.txt | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/CMake/ystdlib-cpp-helpers.cmake b/CMake/ystdlib-cpp-helpers.cmake index 30f621f6..997cd7dd 100644 --- a/CMake/ystdlib-cpp-helpers.cmake +++ b/CMake/ystdlib-cpp-helpers.cmake @@ -3,8 +3,8 @@ # # @param NAME # @param NAMESPACE -# @param [LIB_BUILD_INTERFACE] The list of include paths for building the library and for external -# projects that link against it via the add_subdirectory() function. +# @param [LIB_BUILD_INTERFACE="${PROJECT_SOURCE_DIR}/src"] The list of include paths for building +# the library and for external projects that link against it via the add_subdirectory() function. function(cpp_library) set(options "") set(oneValueArgs diff --git a/CMakeLists.txt b/CMakeLists.txt index d2255bd0..fbffc4e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,10 +11,6 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS FORCE ) -# Configure include paths for building `ystdlib-cpp`, as well as projects using `ystdlib-cpp` via -# the `add_subdirectory()` call. -list(APPEND YSTDLIB_CPP_BUILD_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src) - # Import CMake helper functions list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/CMake) include(ystdlib-cpp-helpers) From 75d680a66af8f14dba91fa33b3aa0c2a6df53ad8 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Mon, 3 Feb 2025 04:32:14 -0500 Subject: [PATCH 19/20] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3f8f9443..f3605614 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Clone `ystdlib-cpp` into your project. Then, in your project's `CMakeLists.txt`, add_subdirectory(/path/to/ystdlib-cpp EXCLUDE_FROM_ALL) target_link_libraries( ystdlib:: ystdlib:: ... ystdlib:: - # other libs... + # other libs... ) ``` Ensure that `ystdlib-cpp` is either within a subdirectory of the folder containing `CMakeLists.txt` From f5e77f72a69495fbaa587f5e493e1c4f5a45807e Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Mon, 3 Feb 2025 04:38:18 -0500 Subject: [PATCH 20/20] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f3605614..7dc05193 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Initialize and update submodules: git submodule update --init --recursive ``` -# Building +## Building To build all targets in `ystdlib-cpp`: ```shell task build:target