Skip to content

Commit b92875f

Browse files
feat: Add CMakeLists.txt files and helper function for building interface libraries. (#17)
Co-authored-by: davidlion <[email protected]>
1 parent 88c0b3f commit b92875f

File tree

10 files changed

+85
-9
lines changed

10 files changed

+85
-9
lines changed

.gersemirc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# yamllint disable-line rule:line-length
22
# yaml-language-server: $schema=https://raw.githubusercontent.com/BlankSpruce/gersemi/master/gersemi/configuration.schema.json
33

4+
definitions: ["CMake/ystdlib-cpp-helpers.cmake"]
45
line_length: 100
56
list_expansion: "favour-expansion"

CMake/ystdlib-cpp-helpers.cmake

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Adds a c++20 interface library in the subdirectory NAME with the target NAME and alias
2+
# NAMESPACE::NAME. Libraries with multiple levels of namespace nesting are currently not supported.
3+
#
4+
# @param NAME
5+
# @param NAMESPACE
6+
# @param [LIB_BUILD_INTERFACE="${PROJECT_SOURCE_DIR}/src"] The list of include paths for building
7+
# the library and for external projects that link against it via the add_subdirectory() function.
8+
function(cpp_library)
9+
set(options "")
10+
set(oneValueArgs
11+
NAME
12+
NAMESPACE
13+
)
14+
set(multiValueArgs LIB_BUILD_INTERFACE)
15+
cmake_parse_arguments(arg_cpp_lib "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
16+
17+
# TODO: Turn this into a function for handling other optional params that have default values.
18+
if("LIB_BUILD_INTERFACE" IN_LIST arg_cpp_lib_KEYWORDS_MISSING_VALUES)
19+
message(
20+
FATAL_ERROR
21+
"Missing build interface list for ${arg_cpp_lib_NAMESPACE}::${arg_cpp_lib_NAME}."
22+
)
23+
elseif(NOT DEFINED arg_cpp_lib_LIB_BUILD_INTERFACE)
24+
set(arg_cpp_lib_LIB_BUILD_INTERFACE "${PROJECT_SOURCE_DIR}/src")
25+
endif()
26+
27+
add_library(${arg_cpp_lib_NAME} INTERFACE)
28+
target_include_directories(
29+
${arg_cpp_lib_NAME}
30+
INTERFACE
31+
"$<BUILD_INTERFACE:${arg_cpp_lib_LIB_BUILD_INTERFACE}>"
32+
)
33+
target_compile_features(${arg_cpp_lib_NAME} INTERFACE cxx_std_20)
34+
add_library(${arg_cpp_lib_NAMESPACE}::${arg_cpp_lib_NAME} ALIAS ${arg_cpp_lib_NAME})
35+
endfunction()

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS
1111
FORCE
1212
)
1313

14+
# Import CMake helper functions
15+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/CMake)
16+
include(ystdlib-cpp-helpers)
17+
18+
add_subdirectory(src/ystdlib)
19+
20+
# Test dummy project
1421
add_executable(dummy)
15-
target_sources(dummy PRIVATE src/ystdlib/hello.cpp)
22+
target_sources(dummy PRIVATE src/main.cpp)
23+
target_link_libraries(dummy PRIVATE ystdlib::testlib)
1624
target_compile_features(dummy PRIVATE cxx_std_20)

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ ystdlib-cpp
22
===================================
33
An open-source C++ library developed and used at YScope.
44

5+
# Usage
6+
Clone `ystdlib-cpp` into your project. Then, in your project's `CMakeLists.txt`, add the following:
7+
```cmake
8+
add_subdirectory(/path/to/ystdlib-cpp EXCLUDE_FROM_ALL)
9+
target_link_libraries(<target_name> <link_options>
10+
ystdlib::<lib_1> ystdlib::<lib_2> ... ystdlib::<lib_N>
11+
# other libs...
12+
)
13+
```
14+
Ensure that `ystdlib-cpp` is either within a subdirectory of the folder containing `CMakeLists.txt`
15+
or at the same level.
16+
517
# Contributing
618
Follow the steps below to develop and contribute to the project.
719

@@ -15,7 +27,7 @@ Initialize and update submodules:
1527
git submodule update --init --recursive
1628
```
1729

18-
# Building
30+
## Building
1931
To build all targets in `ystdlib-cpp`:
2032
```shell
2133
task build:target

src/main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <iostream>
2+
#include <ystdlib/testlib/hello.hpp>
3+
4+
[[nodiscard]] auto main() -> int {
5+
std::cout << ystdlib::testlib::hello() << '\n';
6+
return 0;
7+
}

src/ystdlib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(testlib)

src/ystdlib/hello.cpp

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/ystdlib/testlib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp_library(NAME testlib NAMESPACE ystdlib)

src/ystdlib/testlib/hello.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef YSTDLIB_TESTLIB_HELLO_HPP
2+
#define YSTDLIB_TESTLIB_HELLO_HPP
3+
4+
#include <string>
5+
6+
namespace ystdlib::testlib {
7+
[[nodiscard]] inline auto hello() -> std::string {
8+
return "Hello, world!";
9+
}
10+
} // namespace ystdlib::testlib
11+
12+
#endif // YSTDLIB_TESTLIB_HELLO_HPP

taskfiles/lint-cmake.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ tasks:
55
desc: "Runs the CMake linters."
66
sources: &cmake_format_src_files
77
- "{{.G_LINT_VENV_CHECKSUM_FILE}}"
8+
- "{{.G_YSTDLIB_CPP_SRC_DIR}}/.gersemirc"
89
- "{{.ROOT_DIR}}/**/*.cmake"
910
- "{{.ROOT_DIR}}/**/*.cmake.in"
1011
- "{{.ROOT_DIR}}/**/CMakeLists.txt"
12+
- "{{.ROOT_DIR}}/.gersemirc"
1113
- "{{.TASKFILE}}"
1214
- exclude: "{{.ROOT_DIR}}/**/build/*"
15+
- exclude: "{{.ROOT_DIR}}/**/cmake_install.cmake"
16+
- exclude: "{{.ROOT_DIR}}/**/CMakeFiles/*"
1317
- exclude: "{{.ROOT_DIR}}/**/submodules/*"
1418
- exclude: "{{.ROOT_DIR}}/**/tools/*"
1519
deps:
@@ -44,7 +48,8 @@ tasks:
4448
- |-
4549
. "{{.G_LINT_VENV_DIR}}/bin/activate"
4650
find . \
47-
\( -path '**/build' -o -path '**/submodules' -o -path '**/tools' \) -prune -o \
51+
\( -path '**/build' -o -path '**/cmake_install.cmake' -o -path '**/CMakeFiles' \
52+
-o -path '**/submodules' -o -path '**/tools' \) -prune -o \
4853
\( -iname "CMakeLists.txt" -o -iname "*.cmake" -o -iname "*.cmake.in" \) \
4954
-print0 | \
5055
xargs -0 gersemi {{.FLAGS}}

0 commit comments

Comments
 (0)