Skip to content

Modernize the cmake files a bit #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
7 changes: 7 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
---
BasedOnStyle: Google
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2
IndentWidth: 2
TabWidth: 2
UseTab: Never
---
Language: Cpp
Standard: c++17
UseTab: Never
Expand Down
16 changes: 16 additions & 0 deletions .cmake-format.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
format:
line_width: 120
tab_size: 4
max_subgroups_hwrap: 4
max_pargs_hwrap: 4
max_rows_cmdline: 8
separate_ctrl_name_with_space: false
separate_fn_name_with_space: false
dangle_parens: true
min_prefix_chars: 8
max_lines_hwrap: 3
line_ending: unix
markup:
bullet_char: '*'
enum_char: .
enable_markup: false
74 changes: 74 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
name: Continuous Integration

on:
push:
branches:
- master
- develop

pull_request:
branches:
- develop

jobs:
test:
strategy:
fail-fast: false

matrix:
os: [macos-15, ubuntu-24.04, windows-2022]

type: [Debug, Release]

# link: [shared, static]
# include:
# - {link: shared, shared: YES}
# - {link: static, shared: NO}

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4

- name: Setup Cpp
if: startsWith(matrix.os, 'macos')
uses: aminya/setup-cpp@v1
with:
# compiler: llvm-18
clangtidy: true
cmake: true
ninja: true

- name: Install static analyzers
if: matrix.os == 'ubuntu-24.04'
run: >-
sudo apt-get install clang-tidy-18 ninja-build -y -q

sudo update-alternatives --install
/usr/bin/clang-tidy clang-tidy
/usr/bin/clang-tidy-18 140

- name: Setup MultiToolTask
if: matrix.os == 'windows-2022'
run: |
Add-Content "$env:GITHUB_ENV" 'UseMultiToolTask=true'
Add-Content "$env:GITHUB_ENV" 'EnforceProcessCountAcrossBuilds=true'

- name: Configure
shell: pwsh
run: cmake --preset=${{ matrix.type }}

- name: Setup PATH
if: matrix.os == 'windows-2022' && matrix.type == 'shared'
run: Add-Content "$env:GITHUB_PATH" "$(Get-Location)\build\${{ matrix.type }}"

- name: Build
run: cmake --build build --config ${{ matrix.type }} -j 2

- name: Test
working-directory: build
run: ctest --output-on-failure --no-tests=error -C ${{ matrix.type }}

- name: Install
run: cmake --install build --prefix prefix --config ${{ matrix.type }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
build*/
_build*/
cmake-build*/
/stagedir/

# CMake
CMakeUserPresets.json
Expand Down
127 changes: 72 additions & 55 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
cmake_minimum_required(VERSION 3.14)
project(mylib
VERSION 1.0.0
DESCRIPTION "Template for C++ library built with CMake"
LANGUAGES CXX)
cmake_minimum_required(VERSION 3.25...3.30)
project(mylib VERSION 1.0.0 DESCRIPTION "Template for C++ library built with CMake" LANGUAGES CXX)

#----------------------------------------------------------------------------------------------------------------------
# general settings and options
Expand All @@ -11,25 +8,39 @@ project(mylib
include(cmake/utils.cmake)
include(GNUInstallDirs)

string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" is_top_level)

# MYLIB_SHARED_LIBS option (undefined by default) can be used to force shared/static build
option(MYLIB_BUILD_TESTS "Build mylib tests" OFF)
option(MYLIB_BUILD_EXAMPLES "Build mylib examples" OFF)
option(MYLIB_BUILD_TESTS "Build mylib tests" ${PROJECT_IS_TOP_LEVEL})
option(MYLIB_BUILD_EXAMPLES "Build mylib examples" ${PROJECT_IS_TOP_LEVEL})
option(MYLIB_BUILD_DOCS "Build mylib documentation" OFF)
option(MYLIB_INSTALL "Generate target for installing mylib" ${is_top_level})
set_if_undefined(MYLIB_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/mylib" CACHE STRING
"Install path for mylib package-related CMake files")
option(MYLIB_INSTALL "Generate target for installing mylib" ${PROJECT_IS_TOP_LEVEL})
set_if_undefined(
MYLIB_INSTALL_CMAKEDIR
"${CMAKE_INSTALL_LIBDIR}/cmake/mylib"
CACHE
STRING
"Install path for mylib package-related CMake files"
)

if(DEFINED MYLIB_SHARED_LIBS)
set(BUILD_SHARED_LIBS ${MYLIB_SHARED_LIBS})
endif()

if(NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
set_property(
CACHE CMAKE_BUILD_TYPE
PROPERTY STRINGS
"Debug"
"Release"
"MinSizeRel"
"RelWithDebInfo"
)
endif()

# Neither of these two are technically needed, but they make the expectation clear
set_if_undefined(CMAKE_CXX_STANDARD 17)
set_if_undefined(CMAKE_CXX_EXTENSIONS FALSE)

set_if_undefined(CMAKE_CXX_VISIBILITY_PRESET hidden)
set_if_undefined(CMAKE_VISIBILITY_INLINES_HIDDEN ON)

Expand All @@ -55,10 +66,7 @@ endif()

generate_export_header(mylib EXPORT_FILE_NAME include/mylib/${export_file_name})

set(sources
include/mylib/export.h
include/mylib/mylib.h
src/mylib.cpp)
set(sources include/mylib/export.h include/mylib/mylib.h src/mylib.cpp)
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${sources})

#----------------------------------------------------------------------------------------------------------------------
Expand All @@ -67,36 +75,42 @@ source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${sources})

include(CMakePackageConfigHelpers)

target_sources(mylib PRIVATE ${sources})
target_compile_definitions(mylib PUBLIC "$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:MYLIB_STATIC_DEFINE>")
target_sources(mylib PRIVATE src/mylib.cpp)

target_include_directories(mylib
# cmake-format: off
target_sources(mylib
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>")
FILE_SET HEADERS
BASE_DIRS
include
${CMAKE_CURRENT_BINARY_DIR}/include
FILES
include/mylib/mylib.h
include/mylib/export.h
${CMAKE_CURRENT_BINARY_DIR}/include/mylib/${export_file_name}
)
# cmake-format: on

target_compile_definitions(mylib PUBLIC "$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:MYLIB_STATIC_DEFINE>")

set_target_properties(mylib PROPERTIES
SOVERSION ${PROJECT_VERSION_MAJOR}
VERSION ${PROJECT_VERSION})
set_target_properties(mylib PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR} VERSION ${PROJECT_VERSION})

if(MYLIB_INSTALL AND NOT CMAKE_SKIP_INSTALL_RULES)
configure_package_config_file(cmake/mylib-config.cmake.in mylib-config.cmake
INSTALL_DESTINATION "${MYLIB_INSTALL_CMAKEDIR}")

write_basic_package_version_file(mylib-config-version.cmake
COMPATIBILITY SameMajorVersion)

install(TARGETS mylib EXPORT mylib_export
RUNTIME COMPONENT mylib
LIBRARY COMPONENT mylib NAMELINK_COMPONENT mylib-dev
ARCHIVE COMPONENT mylib-dev
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
install(DIRECTORY include/
TYPE INCLUDE
COMPONENT mylib-dev)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/include/mylib/${export_file_name}"
COMPONENT mylib-dev
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/mylib")
configure_package_config_file(
cmake/mylib-config.cmake.in mylib-config.cmake INSTALL_DESTINATION "${MYLIB_INSTALL_CMAKEDIR}"
)

write_basic_package_version_file(mylib-config-version.cmake COMPATIBILITY SameMajorVersion)

# cmake-format: off
install(TARGETS mylib
EXPORT mylib_export
RUNTIME COMPONENT mylib
LIBRARY COMPONENT mylib NAMELINK_COMPONENT mylib-dev
ARCHIVE COMPONENT mylib-dev
FILE_SET HEADERS COMPONENT mylib-dev
)
# cmake-format: on

set(targets_file "mylib-shared-targets.cmake")

Expand All @@ -105,16 +119,16 @@ if(MYLIB_INSTALL AND NOT CMAKE_SKIP_INSTALL_RULES)
endif()

install(EXPORT mylib_export
COMPONENT mylib-dev
FILE "${targets_file}"
DESTINATION "${MYLIB_INSTALL_CMAKEDIR}"
NAMESPACE mylib::)
COMPONENT mylib-dev
FILE "${targets_file}"
DESTINATION "${MYLIB_INSTALL_CMAKEDIR}"
NAMESPACE mylib::
)

install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/mylib-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/mylib-config-version.cmake"
COMPONENT mylib-dev
DESTINATION "${MYLIB_INSTALL_CMAKEDIR}")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/mylib-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/mylib-config-version.cmake" COMPONENT mylib-dev
DESTINATION "${MYLIB_INSTALL_CMAKEDIR}"
)

if(MSVC)
set(pdb_file "")
Expand All @@ -130,11 +144,14 @@ if(MYLIB_INSTALL AND NOT CMAKE_SKIP_INSTALL_RULES)
endif()

install(FILES "${pdb_file}"
COMPONENT mylib-dev
CONFIGURATIONS Debug RelWithDebInfo
DESTINATION "${pdb_file_destination}"
OPTIONAL)
COMPONENT mylib-dev
CONFIGURATIONS Debug RelWithDebInfo
DESTINATION "${pdb_file_destination}"
OPTIONAL
)
endif()

include(CPack)
endif()

#----------------------------------------------------------------------------------------------------------------------
Expand Down
Loading