Skip to content

Commit 5fdcea2

Browse files
committed
Add cmake command ystdlib_cpp_library and update cmake linting exclusion paths
1 parent 9cc7027 commit 5fdcea2

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

CMake/ystdlib-cpp-helpers.cmake

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# ystdlib_cpp_library()
2+
#
3+
# CMake function to imitate Bazel's cc_library rule.
4+
#
5+
# Parameters:
6+
# NAME: name of target (see Note)
7+
# HDRS: List of public header files for the library
8+
# PUBLIC: Add this so that this library will be exported under ystdlib::
9+
# PRIVATE: Add this to make the library internal to ystdlib-cpp
10+
#
11+
# Note:
12+
# When included as a subdirectory, ystdlib_cpp_library will always create a library named
13+
# ystdlib_${NAME} and alias target ystdlib::${NAME}. The ystdlib:: form should always be used to
14+
# reduce namespace pollution.
15+
function(ystdlib_cpp_library)
16+
set(options
17+
PUBLIC
18+
PRIVATE
19+
)
20+
set(oneValueArgs NAME)
21+
set(multiValueArgs HDRS)
22+
cmake_parse_arguments(
23+
arg_ystdlib_cpp_lib
24+
"${options}"
25+
"${oneValueArgs}"
26+
"${multiValueArgs}"
27+
${ARGN}
28+
)
29+
30+
if(YSTDLIB_CPP_ENABLE_INSTALL)
31+
set(_TARGET_LIB_NAME "${arg_ystdlib_cpp_lib_NAME}")
32+
else()
33+
set(_TARGET_LIB_NAME "ystdlib_${arg_ystdlib_cpp_lib_NAME}")
34+
endif()
35+
36+
# TODO: add build process for libraries with source files
37+
add_library(${_TARGET_LIB_NAME} INTERFACE)
38+
target_include_directories(
39+
${_TARGET_LIB_NAME}
40+
INTERFACE
41+
"$<BUILD_INTERFACE:${YSTDLIB_CPP_BUILD_INCLUDE_DIRS}>"
42+
"$<INSTALL_INTERFACE:${YSTDLIB_CPP_INSTALL_INCLUDE_DIRS}>"
43+
)
44+
target_compile_features(${_TARGET_LIB_NAME} INTERFACE cxx_std_20)
45+
add_library(ystdlib::${arg_ystdlib_cpp_lib_NAME} ALIAS ${_TARGET_LIB_NAME})
46+
47+
# TODO: install header files into target locations
48+
endfunction()

CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
cmake_minimum_required(VERSION 3.16.3)
22
project(YSTDLIB_CPP LANGUAGES CXX)
33

4+
# Static everything for now
5+
option(BUILD_SHARED_LIBS OFF)
6+
option(YSTDLIB_CPP_USE_STATIC_LIBS ON)
7+
unset(YSTDLIB_CPP_SOVERSION)
8+
49
set(YSTDLIB_CPP_VERSION "0.0.1" CACHE STRING "Project version.")
510

11+
# When ystdlib-cpp is included as subproject (i.e. using add_subdirectory(ystdlib-cpp)) in the
12+
# source tree of a project that uses it, install rules are disabled.
13+
if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
14+
option(YSTDLIB_CPP_ENABLE_INSTALL "Enable installation rules" OFF)
15+
else()
16+
option(YSTDLIB_CPP_ENABLE_INSTALL "Enable installation rules" ON)
17+
endif()
18+
619
# Enable exporting compile commands
720
set(CMAKE_EXPORT_COMPILE_COMMANDS
821
ON
@@ -11,6 +24,23 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS
1124
FORCE
1225
)
1326

27+
# Configure include paths for building ystdlib-cpp.
28+
list(APPEND YSTDLIB_CPP_BUILD_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src)
29+
30+
# Configure include paths for projects using ystdlib-cpp after it has been installed.
31+
if(YSTDLIB_CPP_ENABLE_INSTALL)
32+
message(WARNING "Currently only supports using ystdlib-cpp as a subdirectory to a project.")
33+
list(APPEND YSTDLIB_CPP_INSTALL_INCLUDE_DIRS ${YSTDLIB_CPP_BUILD_INCLUDE_DIRS})
34+
else()
35+
list(APPEND YSTDLIB_CPP_INSTALL_INCLUDE_DIRS ${YSTDLIB_CPP_BUILD_INCLUDE_DIRS})
36+
endif()
37+
38+
# Import CMake helper functions
39+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/CMake)
40+
include(ystdlib-cpp-helpers)
41+
42+
add_subdirectory(src/ystdlib)
43+
1444
add_executable(dummy)
1545
target_sources(dummy PRIVATE src/ystdlib/hello.cpp)
1646
target_compile_features(dummy PRIVATE cxx_std_20)

src/ystdlib/hello.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <ystdlib/array/Array.hpp>
12
#include <iostream>
23

34
[[nodiscard]] auto main() -> int {

taskfiles/lint-cmake.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ tasks:
1010
- "{{.ROOT_DIR}}/**/CMakeLists.txt"
1111
- "{{.TASKFILE}}"
1212
- exclude: "{{.ROOT_DIR}}/**/build/*"
13+
- exclude: "{{.ROOT_DIR}}/**/cmake_install.cmake"
14+
- exclude: "{{.ROOT_DIR}}/**/CMakeFiles/*"
1315
- exclude: "{{.ROOT_DIR}}/**/submodules/*"
1416
- exclude: "{{.ROOT_DIR}}/**/tools/*"
1517
deps:
@@ -44,7 +46,8 @@ tasks:
4446
- |-
4547
. "{{.G_LINT_VENV_DIR}}/bin/activate"
4648
find . \
47-
\( -path '**/build' -o -path '**/submodules' -o -path '**/tools' \) -prune -o \
49+
\( -path '**/build' -o -path '**/CMakeFiles' -o -path '**/submodules' -o -path '**/tools' \) -prune -o \
50+
! -path "**/cmake_install.cmake" \
4851
\( -iname "CMakeLists.txt" -o -iname "*.cmake" -o -iname "*.cmake.in" \) \
4952
-print0 | \
5053
xargs -0 gersemi {{.FLAGS}}

0 commit comments

Comments
 (0)