Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gersemirc
Original file line number Diff line number Diff line change
@@ -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"
35 changes: 35 additions & 0 deletions CMake/ystdlib-cpp-helpers.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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="${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
NAME
NAMESPACE
)
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
"$<BUILD_INTERFACE:${arg_cpp_lib_LIB_BUILD_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})
endfunction()
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS
FORCE
)

# Import CMake helper functions
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/CMake)
include(ystdlib-cpp-helpers)

add_subdirectory(src/ystdlib)

# Test dummy project
add_executable(dummy)
target_sources(dummy PRIVATE src/ystdlib/hello.cpp)
target_sources(dummy PRIVATE src/main.cpp)
target_link_libraries(dummy PRIVATE ystdlib::testlib)
target_compile_features(dummy PRIVATE cxx_std_20)
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ ystdlib-cpp
===================================
An open-source C++ library developed and used at YScope.

# 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(<target_name> <link_options>
ystdlib::<lib_1> ystdlib::<lib_2> ... ystdlib::<lib_N>
# other libs...
)
```
Ensure that `ystdlib-cpp` is either within a subdirectory of the folder containing `CMakeLists.txt`
or at the same level.

# Contributing
Follow the steps below to develop and contribute to the project.

Expand All @@ -15,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
Expand Down
7 changes: 7 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <iostream>
#include <ystdlib/testlib/hello.hpp>

[[nodiscard]] auto main() -> int {
std::cout << ystdlib::testlib::hello() << '\n';
return 0;
}
1 change: 1 addition & 0 deletions src/ystdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(testlib)
6 changes: 0 additions & 6 deletions src/ystdlib/hello.cpp

This file was deleted.

1 change: 1 addition & 0 deletions src/ystdlib/testlib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp_library(NAME testlib NAMESPACE ystdlib)
12 changes: 12 additions & 0 deletions src/ystdlib/testlib/hello.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef YSTDLIB_TESTLIB_HELLO_HPP
#define YSTDLIB_TESTLIB_HELLO_HPP

#include <string>

namespace ystdlib::testlib {
[[nodiscard]] inline auto hello() -> std::string {
return "Hello, world!";
}
} // namespace ystdlib::testlib

#endif // YSTDLIB_TESTLIB_HELLO_HPP
7 changes: 6 additions & 1 deletion taskfiles/lint-cmake.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ 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"
- "{{.ROOT_DIR}}/.gersemirc"
- "{{.TASKFILE}}"
- exclude: "{{.ROOT_DIR}}/**/build/*"
- exclude: "{{.ROOT_DIR}}/**/cmake_install.cmake"
- exclude: "{{.ROOT_DIR}}/**/CMakeFiles/*"
- exclude: "{{.ROOT_DIR}}/**/submodules/*"
- exclude: "{{.ROOT_DIR}}/**/tools/*"
deps:
Expand Down Expand Up @@ -44,7 +48,8 @@ tasks:
- |-
. "{{.G_LINT_VENV_DIR}}/bin/activate"
find . \
\( -path '**/build' -o -path '**/submodules' -o -path '**/tools' \) -prune -o \
\( -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}}