Skip to content

Commit 1c44077

Browse files
committed
Merge branch 'dev'
2 parents 2f9f30b + 521c1f9 commit 1c44077

File tree

10 files changed

+291
-76
lines changed

10 files changed

+291
-76
lines changed

CMake/ystdlib-config.cmake.in

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,9 @@ include(CMakeFindDependencyMacro)
44

55
@PACKAGE_INIT@
66

7-
if(@Catch2_FOUND@)
8-
find_dependency(Catch2)
9-
endif()
10-
11-
if(@outcome_FOUND@)
12-
find_dependency(outcome)
13-
endif()
7+
find_dependency(Boost REQUIRED)
148

15-
set_and_check(ystdlib_INCLUDE_DIR "@PACKAGE_YSTDLIB_INSTALL_INCLUDE_DIR@")
9+
set_and_check(ystdlib_INCLUDE_DIR "@PACKAGE_ystdlib_INSTALL_INCLUDE_DIR@")
1610

1711
check_required_components(ystdlib)
1812

CMake/ystdlib-cpp-helpers.cmake

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ endfunction()
2828
# Adds a c++20 interface library in the subdirectory NAME with the target NAME and alias
2929
# NAMESPACE::NAME. Libraries with multiple levels of namespace nesting are currently not supported.
3030
#
31-
# If `YSTDLIB_CPP_ENABLE_TESTS` is ON, builds the unit tests specific to the current library, and
32-
# links this library against the unified unit test target for the entire `ystdlib-cpp` project.
31+
# If `ystdlib_ENABLE_TESTS` is ON, builds the unit tests specific to the current library, and
32+
# links this library against the unified unit test target for the entire `ystdlib` project.
3333
#
3434
# @param {string} NAME
3535
# @param {string} NAMESPACE
@@ -40,7 +40,7 @@ endfunction()
4040
# @param {string[]} [PRIVATE_LINK_LIBRARIES]
4141
# @param {string[]} [TESTS_LINK_LIBRARIES]
4242
# @param {string[]} [BUILD_INCLUDE_DIR="${PROJECT_SOURCE_DIR}/src"] The list of include paths for
43-
# building the library and for external projects that builds `ystdlib-cpp` as a CMAKE subproject via
43+
# building the library and for external projects that builds `ystdlib` as a CMAKE subproject via
4444
# the add_subdirectory() function.
4545
function(cpp_library)
4646
set(options "")
@@ -102,6 +102,12 @@ function(cpp_library)
102102
"$<BUILD_INTERFACE:${arg_cpp_lib_BUILD_INCLUDE_DIR}>"
103103
)
104104
target_compile_features(${arg_cpp_lib_NAME} INTERFACE cxx_std_20)
105+
106+
# tells where headers are
107+
target_include_directories(${arg_cpp_lib_NAME} INTERFACE
108+
# "$<INSTALL_INTERFACE:include/${arg_cpp_lib_NAME}>")
109+
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
110+
105111
else()
106112
# The library type is specified by `BUILD_SHARED_LIBS` if it is defined. Otherwise, the type
107113
# defaults to static.
@@ -129,7 +135,22 @@ function(cpp_library)
129135

130136
add_library(${_ALIAS_TARGET_NAME} ALIAS ${arg_cpp_lib_NAME})
131137

132-
if(YSTDLIB_CPP_ENABLE_TESTS)
138+
# install headers
139+
set(INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}/ystdlib/${arg_cpp_lib_NAME}")
140+
install(
141+
FILES
142+
${arg_cpp_lib_PUBLIC_HEADERS}
143+
DESTINATION "${INSTALL_INCLUDE_DIR}"
144+
)
145+
146+
set_target_properties(
147+
${arg_cpp_lib_NAME}
148+
PROPERTIES
149+
LINKER_LANGUAGE
150+
CXX
151+
)
152+
153+
if(ystdlib_ENABLE_TESTS)
133154
# Build library-specific unit test target
134155
set(_UNIT_TEST_TARGET "unit-test-${arg_cpp_lib_NAME}")
135156
add_executable(${_UNIT_TEST_TARGET})
@@ -150,6 +171,13 @@ function(cpp_library)
150171
${CMAKE_BINARY_DIR}/testbin
151172
)
152173

174+
set_target_properties(
175+
${_UNIT_TEST_TARGET}
176+
PROPERTIES
177+
LINKER_LANGUAGE
178+
CXX
179+
)
180+
153181
# Link against unified unit test
154182
target_sources(${UNIFIED_UNIT_TEST_TARGET} PRIVATE ${arg_cpp_lib_TESTS_SOURCES})
155183
target_link_libraries(

CMakeLists.txt

Lines changed: 31 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
cmake_minimum_required(VERSION 3.22.1)
22

3+
set(ystdlib_VERSION "0.0.1" CACHE STRING "Project version.")
4+
5+
project(ystdlib VERSION "${ystdlib_VERSION}" LANGUAGES CXX)
6+
37
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMake")
48
include(ystdlib-cpp-helpers)
59
include(CMakePackageConfigHelpers)
610
include(GNUInstallDirs)
711

8-
set(YSTDLIB_CPP_VERSION "0.0.1" CACHE STRING "Project version.")
9-
10-
project(YSTDLIB_CPP VERSION "${YSTDLIB_CPP_VERSION}" LANGUAGES CXX)
11-
1212
option(BUILD_SHARED_LIBS "Build using shared libraries." OFF)
13-
option(YSTDLIB_CPP_BUILD_TESTING "Build the testing tree for ystdlib-cpp." ON)
13+
option(ystdlib_BUILD_TESTING "Build the testing tree for ystdlib-cpp." ON)
1414

1515
# Require compiler versions that support the C++20 features necessary for compiling ystdlib-cpp
1616
if("AppleClang" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
17-
set(YSTDLIB_CPP_CMAKE_CXX_COMPILER_MIN_VERSION "16")
17+
set(ystdlib_CMAKE_CXX_COMPILER_MIN_VERSION "16")
1818
elseif("Clang" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
19-
set(YSTDLIB_CPP_CMAKE_CXX_COMPILER_MIN_VERSION "16")
19+
set(ystdlib_CMAKE_CXX_COMPILER_MIN_VERSION "16")
2020
elseif("GNU" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
21-
set(YSTDLIB_CPP_CMAKE_CXX_COMPILER_MIN_VERSION "11")
21+
set(ystdlib_CMAKE_CXX_COMPILER_MIN_VERSION "11")
2222
else()
2323
message(
2424
FATAL_ERROR
2525
"Unsupported compiler: ${CMAKE_CXX_COMPILER_ID}. Please use AppleClang, Clang, or GNU."
2626
)
2727
endif()
28-
if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "${YSTDLIB_CPP_CMAKE_CXX_COMPILER_MIN_VERSION}")
28+
if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "${ystdlib_CMAKE_CXX_COMPILER_MIN_VERSION}")
2929
message(
3030
FATAL_ERROR
3131
"${CMAKE_CXX_COMPILER_ID} version ${CMAKE_CXX_COMPILER_VERSION} is too low. Must be at \
32-
least ${YSTDLIB_CPP_CMAKE_CXX_COMPILER_MIN_VERSION}."
32+
least ${ystdlib_CMAKE_CXX_COMPILER_MIN_VERSION}."
3333
)
3434
endif()
3535

@@ -41,7 +41,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS
4141
FORCE
4242
)
4343

44-
if(YSTDLIB_CPP_IS_TOP_LEVEL)
44+
if(ystdlib_IS_TOP_LEVEL)
4545
# Include dependency settings if the project isn't being included as a subproject.
4646
# NOTE: We mark the file optional because if the user happens to have the dependencies
4747
# installed, this file is not necessary.
@@ -51,16 +51,16 @@ if(YSTDLIB_CPP_IS_TOP_LEVEL)
5151
include(CTest)
5252
endif()
5353

54-
if(BUILD_TESTING AND YSTDLIB_CPP_BUILD_TESTING)
55-
set(YSTDLIB_CPP_ENABLE_TESTS ON)
54+
if(BUILD_TESTING AND ystdlib_BUILD_TESTING)
55+
set(ystdlib_ENABLE_TESTS ON)
5656
endif()
5757

5858
find_package(Boost REQUIRED)
5959
if(Boost_FOUND)
6060
message(STATUS "Found Boost ${Boost_VERSION}.")
6161
endif()
6262

63-
if(YSTDLIB_CPP_ENABLE_TESTS)
63+
if(ystdlib_ENABLE_TESTS)
6464
find_package(Catch2 3.8.0 REQUIRED)
6565
if(Catch2_FOUND)
6666
message(STATUS "Found Catch2 ${Catch2_VERSION}.")
@@ -84,60 +84,31 @@ if(YSTDLIB_CPP_ENABLE_TESTS)
8484
catch_discover_tests(${UNIFIED_UNIT_TEST_TARGET} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/testbin)
8585
endif()
8686

87-
set(YSTDLIB_INSTALL_CONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/ystdlib)
88-
set(YSTDLIB_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
87+
set(ystdlib_INSTALL_CONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/ystdlib)
88+
set(ystdlib_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
8989

90-
file(
91-
GLOB_RECURSE SOURCE_FILES
92-
CONFIGURE_DEPENDS
93-
"${PROJECT_SOURCE_DIR}/src/*.cpp"
94-
"${PROJECT_SOURCE_DIR}/src/*.hpp"
95-
"${PROJECT_SOURCE_DIR}/src/*.h"
96-
)
97-
list(FILTER SOURCE_FILES EXCLUDE REGEX ".*test_*\..*")
98-
99-
add_library(ystdlib ${SOURCE_FILES})
100-
add_library(ystdlib::ystdlib ALIAS ystdlib)
101-
102-
target_include_directories(
103-
ystdlib
104-
PUBLIC
105-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
106-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
107-
)
108-
109-
target_compile_features(ystdlib PUBLIC cxx_std_20)
90+
add_subdirectory(src/ystdlib)
11091

111-
set_target_properties(
112-
ystdlib
113-
PROPERTIES
114-
LINKER_LANGUAGE
115-
CXX
92+
install(TARGETS
93+
containers
94+
error_handling
95+
io_interface
96+
wrapped_facade_headers
97+
EXPORT ystdlib-targets
98+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
99+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
100+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
101+
FILE_SET HEADERS
116102
)
117103

118-
install(TARGETS ystdlib EXPORT ystdlib-targets)
119-
120-
install(EXPORT ystdlib-targets NAMESPACE ystdlib:: DESTINATION ${YSTDLIB_INSTALL_CONFIG_DIR})
121-
122-
install(
123-
DIRECTORY
124-
"${PROJECT_SOURCE_DIR}/src/ystdlib"
125-
DESTINATION "${YSTDLIB_INSTALL_INCLUDE_DIR}"
126-
FILES_MATCHING
127-
PATTERN
128-
"*.h"
129-
PATTERN
130-
"*.hpp"
131-
PATTERN
132-
"*.tpp"
133-
)
104+
install(EXPORT ystdlib-targets NAMESPACE ystdlib:: DESTINATION ${ystdlib_INSTALL_CONFIG_DIR})
134105

135106
configure_package_config_file(
136107
${CMAKE_CURRENT_LIST_DIR}/CMake/ystdlib-config.cmake.in
137108
${CMAKE_CURRENT_BINARY_DIR}/ystdlib-config.cmake
138-
INSTALL_DESTINATION ${YSTDLIB_INSTALL_CONFIG_DIR}
109+
INSTALL_DESTINATION ${ystdlib_INSTALL_CONFIG_DIR}
139110
PATH_VARS
140-
YSTDLIB_INSTALL_INCLUDE_DIR
111+
ystdlib_INSTALL_INCLUDE_DIR
141112
)
142113

143114
write_basic_package_version_file(
@@ -149,7 +120,6 @@ install(
149120
FILES
150121
${CMAKE_CURRENT_BINARY_DIR}/ystdlib-config.cmake
151122
${CMAKE_CURRENT_BINARY_DIR}/ystdlib-config-version.cmake
152-
DESTINATION ${YSTDLIB_INSTALL_CONFIG_DIR}
123+
DESTINATION ${ystdlib_INSTALL_CONFIG_DIR}
153124
)
154125

155-
add_subdirectory(src/ystdlib)

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ An open-source C++ library developed and used at YScope.
44
# Usage
55

66
## Via CMake's add_subdirectory()
7+
> Note: this is not accurate in this branch, which is a work in progress to use ystdlib-cpp as a
8+
CMake library
9+
710
Clone `ystdlib-cpp` into your project. Then, in your project's `CMakeLists.txt`, add the following:
811
```cmake
9-
# Set `YSTDLIB_CPP_BUILD_TESTING` to an accepted `FALSE` class value to skip building unit tests.
10-
# option(YSTDLIB_CPP_BUILD_TESTING "" OFF)
12+
# Set `ystdlib_BUILD_TESTING` to an accepted `FALSE` class value to skip building unit tests.
13+
# option(ystdlib_BUILD_TESTING "" OFF)
1114
add_subdirectory(/path/to/ystdlib-cpp EXCLUDE_FROM_ALL)
1215
target_link_libraries(<target_name> <link_options>
1316
ystdlib::<lib_1> ystdlib::<lib_2> ... ystdlib::<lib_N>
@@ -64,7 +67,7 @@ task test-<lib_name>
6467
```
6568

6669
When generating a testing target, the CMake variable `BUILD_TESTING` is followed (unless overruled
67-
by setting `YSTDLIB_CPP_BUILD_TESTING` to false). By default, if built as a top-level project,
70+
by setting `ystdlib_BUILD_TESTING` to false). By default, if built as a top-level project,
6871
`BUILD_TESTING` is set to true and unit tests are built.
6972

7073
## Linting

examples/CMakeLists.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
cmake_minimum_required(VERSION 3.22.1)
2+
3+
project(ystdlib-examples)
4+
5+
if(ystdlib-examples_IS_TOP_LEVEL)
6+
message("${CMAKE_SOURCE_DIR}/../build/deps/cmake-settings/settings.cmake")
7+
include("${CMAKE_SOURCE_DIR}/../build/deps/cmake-settings/settings.cmake" OPTIONAL)
8+
endif()
9+
10+
set(ystdlib_ROOT "${CMAKE_SOURCE_DIR}/../build/build_install")
11+
find_package(ystdlib REQUIRED)
12+
13+
add_library(
14+
common
15+
OBJECT
16+
types.cpp
17+
types.hpp
18+
constants.hpp
19+
)
20+
target_link_libraries(common PUBLIC ystdlib::error_handling)
21+
add_executable(main main.cpp)
22+
23+
get_target_property(_type ystdlib::error_handling TYPE)
24+
get_target_property(_sources ystdlib::error_handling SOURCES)
25+
get_target_property(_includes ystdlib::error_handling INCLUDE_DIRECTORIES)
26+
get_target_property(_libs ystdlib::error_handling LINK_LIBRARIES)
27+
28+
message(STATUS "Target Type: ${_type}")
29+
message(STATUS "Sources: ${_sources}")
30+
message(STATUS "Include Dirs: ${_includes}")
31+
message(STATUS "Linked Libraries: ${_libs}")
32+
33+
target_compile_features(main PRIVATE cxx_std_20)
34+
target_compile_features(common PRIVATE cxx_std_20)
35+
36+
target_link_libraries(
37+
main
38+
PUBLIC
39+
ystdlib::error_handling
40+
common
41+
)

examples/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake ..
2+
3+
Run above command from examples/build
4+
5+
Dependencies:
6+
7+
After building main project, cd to build and run
8+
9+
cmake --install . --prefix=build_install to have an install location

examples/constants.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef YSTDLIB_ERROR_HANDLING_TEST_CONSTANTS_HPP
2+
#define YSTDLIB_ERROR_HANDLING_TEST_CONSTANTS_HPP
3+
4+
#include <array>
5+
#include <string_view>
6+
#include <system_error>
7+
8+
namespace ystdlib::error_handling::test {
9+
constexpr std::string_view cAlwaysSuccessErrorCategoryName{"Always Success Error Code"};
10+
constexpr std::string_view cBinaryTestErrorCategoryName{"Binary Error Code"};
11+
constexpr std::string_view cSuccessErrorMsg{"Success"};
12+
constexpr std::string_view cFailureErrorMsg{"Failure"};
13+
constexpr std::string_view cUnrecognizedErrorCode{"Unrecognized Error Code"};
14+
constexpr std::array cFailureConditions{std::errc::not_connected, std::errc::timed_out};
15+
constexpr std::array cNoneFailureConditions{std::errc::broken_pipe, std::errc::address_in_use};
16+
} // namespace ystdlib::error_handling::test
17+
18+
#endif // YSTDLIB_ERROR_HANDLING_TEST_CONSTANTS_HPP

examples/main.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <algorithm>
2+
#include <string_view>
3+
#include <system_error>
4+
5+
#include <ystdlib/error_handling/ErrorCode.hpp>
6+
7+
#include "constants.hpp"
8+
#include "types.hpp"
9+
10+
#include <cassert>
11+
#include <iostream>
12+
13+
using namespace ystdlib::error_handling::test;
14+
15+
int main() {
16+
// Test error codes within the same error category
17+
BinaryErrorCode const success{BinaryErrorCodeEnum::Success};
18+
std::error_code const success_error_code{success};
19+
assert((success == success_error_code));
20+
assert((cSuccessErrorMsg == success_error_code.message()));
21+
assert((BinaryErrorCode::get_category() == success_error_code.category()));
22+
assert((cBinaryTestErrorCategoryName == success_error_code.category().name()));
23+
24+
BinaryErrorCode const failure{BinaryErrorCodeEnum::Failure};
25+
std::error_code const failure_error_code{failure};
26+
assert((failure == failure_error_code));
27+
assert((cFailureErrorMsg == failure_error_code.message()));
28+
assert((BinaryErrorCode::get_category() == failure_error_code.category()));
29+
assert((cBinaryTestErrorCategoryName == failure_error_code.category().name()));
30+
31+
assert((success_error_code != failure_error_code));
32+
assert((success_error_code.category() == failure_error_code.category()));
33+
34+
AlwaysSuccessErrorCode const always_success{AlwaysSuccessErrorCodeEnum::Success};
35+
std::error_code const always_success_error_code{always_success};
36+
assert((always_success_error_code == always_success));
37+
assert((cSuccessErrorMsg == always_success_error_code.message()));
38+
assert((AlwaysSuccessErrorCode::get_category() == always_success_error_code.category()));
39+
assert((cAlwaysSuccessErrorCategoryName == always_success_error_code.category().name()));
40+
41+
// Compare error codes from different error category
42+
// Error codes that have the same value or message won't be the same with each other if they are
43+
// from different error categories.
44+
assert((success_error_code.value() == always_success_error_code.value()));
45+
assert((success_error_code.message() == always_success_error_code.message()));
46+
assert((success_error_code.category() != always_success_error_code.category()));
47+
assert((success_error_code != always_success_error_code));
48+
assert((AlwaysSuccessErrorCode{AlwaysSuccessErrorCodeEnum::Success} != success_error_code));
49+
assert((BinaryErrorCode{BinaryErrorCodeEnum::Success} != always_success_error_code));
50+
51+
std::cout << "test is good!" << std::endl;
52+
return 0;
53+
}

0 commit comments

Comments
 (0)