Skip to content

Commit

Permalink
Add C++20 Module as Shared Libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
ClausKlein committed Apr 9, 2024
1 parent fcfcda3 commit 54eb389
Show file tree
Hide file tree
Showing 23 changed files with 372 additions and 152 deletions.
17 changes: 6 additions & 11 deletions .CMakeUserPresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
"conan"
],
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/${presetName}",
"cacheVariables": {
"CMAKE_EXPORT_COMPILE_COMMANDS": true,
"BUILD_MCSS_DOCS": false,
"BUILD_SHARED_LIBS": true
}
},
{
"name": "dev-linux",
"binaryDir": "${sourceDir}/build/dev-linux",
"name": "dev-Linux",
"inherits": [
"dev-common",
"ci-linux"
Expand All @@ -32,8 +32,7 @@
}
},
{
"name": "dev-darwin",
"binaryDir": "${sourceDir}/build/dev-darwin",
"name": "dev-Darwin",
"inherits": [
"dev-common",
"ci-darwin"
Expand All @@ -44,7 +43,6 @@
},
{
"name": "dev-win64",
"binaryDir": "${sourceDir}/build/dev-win64",
"inherits": [
"dev-common",
"cppcheck",
Expand All @@ -57,26 +55,23 @@
},
{
"name": "dev",
"binaryDir": "${sourceDir}/build/dev",
"generator": "Ninja",
"inherits": "dev-linux"
"inherits": "dev-<hostSystemName>"
},
{
"name": "dev-coverage",
"binaryDir": "${sourceDir}/build/dev",
"generator": "Ninja",
"inherits": [
"ci-coverage",
"dev-linux"
"dev-Linux"
]
},
{
"name": "dev-sanitize",
"binaryDir": "${sourceDir}/build/dev",
"generator": "Ninja",
"inherits": [
"ci-sanitize",
"dev-linux"
"dev-Linux"
]
}
],
Expand Down
109 changes: 109 additions & 0 deletions .cmake-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
_help_format: Options affecting formatting.
format:
_help_disable:
- Disable formatting entirely, making cmake-format a no-op
disable: false
_help_line_width:
- How wide to allow formatted cmake files
line_width: 100
_help_tab_size:
- How many spaces to tab for indent
tab_size: 2
_help_use_tabchars:
- If true, lines are indented using tab characters (utf-8
- 0x09) instead of <tab_size> space characters (utf-8 0x20).
- In cases where the layout would require a fractional tab
- character, the behavior of the fractional indentation is
- governed by <fractional_tab_policy>
use_tabchars: false
_help_fractional_tab_policy:
- If <use_tabchars> is True, then the value of this variable
- indicates how fractional indentions are handled during
- whitespace replacement. If set to 'use-space', fractional
- indentation is left as spaces (utf-8 0x20). If set to
- '`round-up` fractional indentation is replaced with a single'
- tab character (utf-8 0x09) effectively shifting the column
- to the next tabstop
fractional_tab_policy: use-space
_help_max_subgroups_hwrap:
- If an argument group contains more than this many sub-groups
- (parg or kwarg groups) then force it to a vertical layout.
max_subgroups_hwrap: 4
_help_max_pargs_hwrap:
- If a positional argument group contains more than this many
- arguments, then force it to a vertical layout.
max_pargs_hwrap: 6
_help_max_rows_cmdline:
- If a cmdline positional group consumes more than this many
- lines without nesting, then invalidate the layout (and nest)
max_rows_cmdline: 2
_help_separate_ctrl_name_with_space:
- If true, separate flow control names from their parentheses
- with a space
separate_ctrl_name_with_space: false
_help_separate_fn_name_with_space:
- If true, separate function names from parentheses with a
- space
separate_fn_name_with_space: false
_help_dangle_parens:
- If a statement is wrapped to more than one line, than dangle
- the closing parenthesis on its own line.
dangle_parens: true
_help_dangle_align:
- If the trailing parenthesis must be 'dangled' on its on
- 'line, then align it to this reference: `prefix`: the start'
- 'of the statement, `prefix-indent`: the start of the'
- 'statement, plus one indentation level, `child`: align to'
- the column of the arguments
dangle_align: prefix
_help_min_prefix_chars:
- If the statement spelling length (including space and
- parenthesis) is smaller than this amount, then force reject
- nested layouts.
min_prefix_chars: 4
_help_max_prefix_chars:
- If the statement spelling length (including space and
- parenthesis) is larger than the tab width by more than this
- amount, then force reject un-nested layouts.
max_prefix_chars: 10
_help_max_lines_hwrap:
- If a candidate layout is wrapped horizontally but it exceeds
- this many lines, then reject the layout.
max_lines_hwrap: 2
_help_line_ending:
- What style line endings to use in the output.
line_ending: unix
_help_command_case:
- Format command names consistently as 'lower' or 'upper' case
command_case: canonical
_help_keyword_case:
- Format keywords consistently as 'lower' or 'upper' case
keyword_case: unchanged
_help_always_wrap:
- A list of command names which should always be wrapped
always_wrap: [file, install]
_help_enable_sort:
- If true, the argument lists which are known to be sortable
- will be sorted lexicographicall
enable_sort: true
_help_autosort:
- If true, the parsers may infer whether or not an argument
- list is sortable (without annotation).
autosort: false
_help_require_valid_layout:
- By default, if cmake-format cannot successfully fit
- everything into the desired linewidth it will apply the
- last, most agressive attempt that it made. If this flag is
- True, however, cmake-format will print error, exit with non-
- zero status code, and write-out nothing
require_valid_layout: false
_help_layout_passes:
- A dictionary mapping layout nodes to a list of wrap
- decisions. See the documentation for more information.
layout_passes: {}

_help_markup: Options affecting comment reflow and formatting.
markup:
_help_enable_markup:
- enable comment markup parsing and reflow
enable_markup: false
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ conan/
prefix/
stagedir/
.clangd
.init
CMakeLists.txt.user
CMakeUserPresets.json
compile_commands.json
104 changes: 85 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,88 @@
cmake_minimum_required(VERSION 3.28...3.29)
cmake_minimum_required(VERSION 3.28.4...3.29)

include(cmake/prelude.cmake)

project(
cmake-init-modules
VERSION 0.1.0
DESCRIPTION "Short description"
HOMEPAGE_URL "https://example.com/"
LANGUAGES CXX)
project(cxx_modules_example VERSION 0.1.0 LANGUAGES CXX)

# This property setting also needs to be consistent between the
# installed shared library and its consumer, otherwise most
# toolchains will once again reject the consumer’s generated BMI.
set(CMAKE_CXX_EXTENSIONS FALSE)

set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN TRUE)

add_library(Algo SHARED)

#########################################################################
# cmake-format: off
target_sources(Algo
PRIVATE
algo-impl.cpp
PUBLIC
FILE_SET CXX_MODULES
FILES
algo-interface.cppm
)

# CMake requires the language standard to be specified as compile feature
# when a target provides C++20 modules and the target will be installed
target_compile_features(Algo PUBLIC cxx_std_20)

include(GenerateExportHeader)
generate_export_header(Algo)
target_sources(Algo
PUBLIC
FILE_SET HEADERS
BASE_DIRS ${CMAKE_CURRENT_BINARY_DIR}
FILES
${CMAKE_CURRENT_BINARY_DIR}/algo_export.h
)

include(GNUInstallDirs)

install(TARGETS Algo
EXPORT my_package-targets
# ... a few details omitted, see the "Deep CMake For Library Authors" talk
FILE_SET CXX_MODULES
# There's currently no convention for this location, see discussion below
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/my_package/src
FILE_SET HEADERS
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} # Same as default, could be omitted
INCLUDES
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(EXPORT my_package-targets
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/my_package
CXX_MODULES_DIRECTORY .
)
# The following file includes the my_package-targets.cmake file
install(FILES cmake/my_package-config.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/my_package
)

# cmake-format: on
#########################################################################

# enable_testing()
# add_subdirectory(example)
# return()

##################################################

# project(
# cmake-init-modules
# VERSION 0.1.0
# DESCRIPTION "Short description"
# HOMEPAGE_URL "https://example.com/"
# LANGUAGES CXX)

include(cmake/variables.cmake)

# ---- Declare library ----

add_library(cmake-init-modules_cmake-init-modules source/cmake-init-modules.cpp)
add_library(cmake-init-modules::cmake-init-modules ALIAS
cmake-init-modules_cmake-init-modules)
add_library(cmake-init-modules::cmake-init-modules ALIAS cmake-init-modules_cmake-init-modules)

include(GenerateExportHeader)
generate_export_header(
Expand All @@ -25,8 +92,8 @@ generate_export_header(
EXPORT_FILE_NAME
export/cmake-init-modules/cmake-init-modules_export.hpp
CUSTOM_CONTENT_FROM_VARIABLE
pragma_suppress_c4251)

pragma_suppress_c4251
)

# cmake-format: off
target_sources(
Expand All @@ -42,8 +109,9 @@ target_sources(
# cmake-format: on

if(NOT BUILD_SHARED_LIBS)
target_compile_definitions(cmake-init-modules_cmake-init-modules
PUBLIC CMAKE_INIT_MODULES_STATIC_DEFINE)
target_compile_definitions(
cmake-init-modules_cmake-init-modules PUBLIC CMAKE_INIT_MODULES_STATIC_DEFINE
)
endif()

set_target_properties(
Expand All @@ -53,7 +121,8 @@ set_target_properties(
VERSION "${PROJECT_VERSION}"
SOVERSION "${PROJECT_VERSION_MAJOR}"
EXPORT_NAME cmake-init-modules
OUTPUT_NAME cmake-init-modules)
OUTPUT_NAME cmake-init-modules
)

target_compile_features(cmake-init-modules_cmake-init-modules PUBLIC cxx_std_20)

Expand All @@ -70,8 +139,7 @@ endif()
enable_testing()

if(PROJECT_IS_TOP_LEVEL)
option(BUILD_EXAMPLES "Build examples tree."
"${cmake-init-modules_DEVELOPER_MODE}")
option(BUILD_EXAMPLES "Build examples tree." "${cmake-init-modules_DEVELOPER_MODE}")
if(BUILD_EXAMPLES)
add_subdirectory(example)
endif()
Expand All @@ -82,9 +150,7 @@ endif()
if(NOT cmake-init-modules_DEVELOPER_MODE)
return()
elseif(NOT PROJECT_IS_TOP_LEVEL)
message(
AUTHOR_WARNING
"Developer mode is intended for developers of cmake-init-modules")
message(AUTHOR_WARNING "Developer mode is intended for developers of cmake-init-modules")
endif()

include(cmake/dev-mode.cmake)
12 changes: 12 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@
"conan"
]
},
{
"name": "ci-Darwin",
"inherits": "ci-macos"
},
{
"name": "ci-ubuntu",
"inherits": [
Expand All @@ -206,6 +210,10 @@
"conan"
]
},
{
"name": "ci-Linux",
"inherits": "ci-ubuntu"
},
{
"name": "ci-windows",
"inherits": [
Expand All @@ -214,6 +222,10 @@
"dev-mode",
"conan"
]
},
{
"name": "ci-Windows",
"inherits": "ci-windows"
}
]
}
Loading

0 comments on commit 54eb389

Please sign in to comment.