new build scripts

This commit is contained in:
BlackMATov
2023-01-05 09:28:19 +07:00
parent e5306a287f
commit 6324945be2
31 changed files with 710 additions and 6478 deletions

View File

@@ -1,19 +0,0 @@
#!/bin/bash
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE}" )" && pwd )"
ROOT_DIR="${DIR}/.."
BUILD_DIR="${ROOT_DIR}/build/coverage"
mkdir -p "${BUILD_DIR}"
(cd "${BUILD_DIR}" && cmake "${ROOT_DIR}" -DCMAKE_BUILD_TYPE=Debug -DBUILD_WITH_COVERAGE=ON)
(cd "${BUILD_DIR}" && cmake --build .)
(cd "${BUILD_DIR}" && lcov -d . -z)
(cd "${BUILD_DIR}" && ctest --verbose)
(cd "${BUILD_DIR}" && lcov -d . -c -o "coverage.info")
(cd "${BUILD_DIR}" && lcov -r "coverage.info" "*/usr/*" "*/untests/*" -o "coverage.info")
(cd "${BUILD_DIR}" && lcov -l "coverage.info")
bash <(curl -s https://codecov.io/bash) -f "${BUILD_DIR}/coverage.info" || echo "Codecov did not collect coverage reports"

View File

@@ -1,11 +0,0 @@
#!/bin/bash
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE}" )" && pwd )"
ROOT_DIR="${DIR}/.."
BUILD_DIR="${ROOT_DIR}/build/darwin_release"
mkdir -p "${BUILD_DIR}"
(cd "${BUILD_DIR}" && cmake "${ROOT_DIR}" -DCMAKE_BUILD_TYPE=Release)
(cd "${BUILD_DIR}" && cmake --build .)
(cd "${BUILD_DIR}" && ctest --verbose)

View File

@@ -1,11 +0,0 @@
#!/bin/bash
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE}" )" && pwd )"
ROOT_DIR="${DIR}/.."
BUILD_DIR="${ROOT_DIR}/build/linux_release"
mkdir -p "${BUILD_DIR}"
(cd "${BUILD_DIR}" && cmake "${ROOT_DIR}" -DCMAKE_BUILD_TYPE=Release)
(cd "${BUILD_DIR}" && cmake --build .)
(cd "${BUILD_DIR}" && ctest --verbose)

View File

@@ -1,19 +0,0 @@
@echo off
set DIR=%~dp0
set ROOT_DIR=%DIR%..\
set BUILD_DIR=%ROOT_DIR%build\windows_release_x64\
if not exist %BUILD_DIR% mkdir %BUILD_DIR% || goto :error
pushd %BUILD_DIR% || goto :error
cmake %ROOT_DIR% -A x64 || goto :error
cmake --build . --config Release || goto :error
ctest --verbose || goto :error
popd || goto :error
goto :EOF
:error
echo Failed with error #%errorlevel%.
exit /b %errorlevel%

View File

@@ -1,19 +0,0 @@
@echo off
set DIR=%~dp0
set ROOT_DIR=%DIR%..\
set BUILD_DIR=%ROOT_DIR%build\windows_release_x86\
if not exist %BUILD_DIR% mkdir %BUILD_DIR% || goto :error
pushd %BUILD_DIR% || goto :error
cmake %ROOT_DIR% -A Win32 || goto :error
cmake --build . --config Release || goto :error
ctest --verbose || goto :error
popd || goto :error
goto :EOF
:error
echo Failed with error #%errorlevel%.
exit /b %errorlevel%

23
.clang-tidy Normal file
View File

@@ -0,0 +1,23 @@
---
Checks: '-*,
bugprone-*,
clang-analyzer-*,
concurrency-*,
cppcoreguidelines-*,
-cppcoreguidelines-macro-usage,
modernize-*,
-modernize-use-trailing-return-type,
performance-*,
portability-*,
readability-*,
-readability-identifier-length,
'
...

View File

@@ -1,16 +0,0 @@
name: coverage
on: [push, pull_request]
jobs:
build:
runs-on: macos-10.15
name: "coverage"
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Install lcov by Homebrew
run: brew install lcov
- name: Build && Test && Upload
run: .ci/build_coverage.sh

View File

@@ -10,15 +10,22 @@ jobs:
matrix:
config:
# https://github.com/actions/virtual-environments/tree/main/images/macos
- { os: "macos-10.15", xcode: "10.3" }
- { os: "macos-10.15", xcode: "11.7" }
- { os: "macos-10.15", xcode: "12.4" }
- { os: "macos-10.15", xcode: "10.3", arch: "x64" }
- { os: "macos-12", xcode: "14.2", arch: "x64" }
name: "xcode-${{matrix.config.xcode}}"
steps:
- uses: actions/checkout@v2
- name: Setup
run: brew install cmake ninja
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive
submodules: true
- name: Select Xcode
run: sudo xcode-select --switch "/Applications/Xcode_${{matrix.config.xcode}}.app"
- name: Build && Test
run: .ci/build_darwin.sh
- name: Build
run: |
cmake --preset macos-${{matrix.config.arch}}
cmake --build --preset macos-${{matrix.config.arch}}-release
- name: Test
run: |
ctest --preset macos-${{matrix.config.arch}}-release

View File

@@ -11,21 +11,21 @@ jobs:
config:
# https://github.com/actions/virtual-environments/tree/main/images/linux
- { os: "ubuntu-20.04", cc: "gcc-7", cxx: "g++-7" }
- { os: "ubuntu-20.04", cc: "gcc-8", cxx: "g++-8" }
- { os: "ubuntu-20.04", cc: "gcc-9", cxx: "g++-9" }
- { os: "ubuntu-20.04", cc: "gcc-10", cxx: "g++-10" }
- { os: "ubuntu-20.04", cc: "clang-7", cxx: "clang++-7" }
- { os: "ubuntu-20.04", cc: "clang-8", cxx: "clang++-8" }
- { os: "ubuntu-20.04", cc: "clang-9", cxx: "clang++-9" }
- { os: "ubuntu-20.04", cc: "clang-10", cxx: "clang++-10" }
- { os: "ubuntu-22.04", cc: "gcc-12", cxx: "g++-12" }
- { os: "ubuntu-22.04", cc: "clang-14", cxx: "clang++-14" }
name: "${{matrix.config.cxx}}"
steps:
- name: Setup
run: sudo apt-get -y install "${{matrix.config.cc}}" "${{matrix.config.cxx}}"
run: sudo apt-get -y install cmake ninja-build ${{matrix.config.cc}} ${{matrix.config.cxx}}
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
submodules: recursive
- name: Build && Test
run: .ci/build_linux.sh
env: { CC: "${{matrix.config.cc}}", CXX: "${{matrix.config.cxx}}" }
submodules: true
- name: Build
run: |
cmake --preset linux-${{matrix.config.cc}}
cmake --build --preset linux-${{matrix.config.cc}}-release
- name: Test
run: |
ctest --preset linux-${{matrix.config.cc}}-release

View File

@@ -10,14 +10,24 @@ jobs:
matrix:
config:
# https://github.com/actions/virtual-environments/tree/main/images/win
- { os: "windows-2016", vs: "Visual Studio 2017", arch: "x86" }
- { os: "windows-2016", vs: "Visual Studio 2017", arch: "x64" }
- { os: "windows-2019", vs: "Visual Studio 2019", arch: "x86" }
- { os: "windows-2019", vs: "Visual Studio 2019", arch: "x64" }
name: "${{matrix.config.vs}} ${{matrix.config.arch}}"
- { os: "windows-2019", vc: "msvc2019", arch: "x86" }
- { os: "windows-2019", vc: "msvc2019", arch: "x64" }
- { os: "windows-2022", vc: "msvc2022", arch: "x86" }
- { os: "windows-2022", vc: "msvc2022", arch: "x64" }
name: "${{matrix.config.vc}} ${{matrix.config.arch}}"
steps:
- uses: actions/checkout@v2
- name: Setup
run: choco install cmake ninja
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive
- name: Build && Test
run: .ci\build_windows_${{matrix.config.arch}}.bat
submodules: true
- name: Select MSVC
uses: ilammy/msvc-dev-cmd@v1
- name: Build
run: |
cmake --preset windows-${{matrix.config.arch}}-${{matrix.config.vc}}
cmake --build --preset windows-${{matrix.config.arch}}-${{matrix.config.vc}}-release
- name: Test
run: |
ctest --preset windows-${{matrix.config.arch}}-${{matrix.config.vc}}-release

3
.gitignore vendored
View File

@@ -1,3 +1,4 @@
build/*
.vscode/*
install/*
.clangd/*
CMakeLists.txt.user

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "vendors/doctest"]
path = vendors/doctest
url = https://github.com/doctest/doctest/

11
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,11 @@
{
"version": "0.2.0",
"configurations": [{
"name": "LLDB Debug",
"type": "lldb",
"request": "launch",
"program": "${command:cmake.launchTargetPath}",
"args": [],
"cwd": "${workspaceFolder}"
}]
}

29
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,29 @@
{
"[cpp]": {
"files.encoding": "utf8",
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true
},
"[cmake]": {
"files.encoding": "utf8",
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true
},
"[python]": {
"files.encoding": "utf8",
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true
},
"clangd.arguments": [
"--all-scopes-completion",
"--background-index",
"--clang-tidy",
"--compile-commands-dir=${workspaceFolder}/.clangd",
"--completion-style=detailed",
"--header-insertion=never"
],
"cmake.copyCompileCommands": "${workspaceFolder}/.clangd/compile_commands.json"
}

View File

@@ -1,30 +1,95 @@
# 3.8 version is required for `cxx_std_17`
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
if(NOT DEFINED PROJECT_NAME)
set(BUILD_AS_STANDALONE ON)
else()
set(BUILD_AS_STANDALONE OFF)
endif()
project(defer.hpp
VERSION "0.0.1"
DESCRIPTION "C++17 tiny vector math library"
HOMEPAGE_URL "https://github.com/blackmatov/defer.hpp")
project(defer.hpp)
#
# LIBRARY
#
add_library(${PROJECT_NAME} INTERFACE)
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17)
target_include_directories(${PROJECT_NAME} INTERFACE headers)
add_library(defer.hpp::defer.hpp ALIAS ${PROJECT_NAME})
target_compile_options(${PROJECT_NAME}
INTERFACE
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:
-Wno-c++98-compat-pedantic
-Wno-padded
-Wno-shadow
-Wno-unknown-warning-option>)
target_compile_features(${PROJECT_NAME} INTERFACE
cxx_std_17)
if(BUILD_AS_STANDALONE)
option(BUILD_WITH_UNTESTS "Build with unit tests" ON)
if(BUILD_WITH_UNTESTS)
enable_testing()
add_subdirectory(untests)
endif()
target_include_directories(${PROJECT_NAME} INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/headers>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
#
# INSTALL
#
if(PROJECT_IS_TOP_LEVEL)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
set(DEFER_HPP_INSTALL_CONFIG_DIR
"${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
set(DEFER_HPP_INSTALL_CONFIG_INPUT
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/Config.cmake.in")
set(DEFER_HPP_INSTALL_GENERATED_CONFIG_CMAKE
"${CMAKE_CURRENT_BINARY_DIR}/generated/${PROJECT_NAME}-config.cmake")
set(DEFER_HPP_INSTALL_GENERATED_CONFIG_VERSION_CMAKE
"${CMAKE_CURRENT_BINARY_DIR}/generated/${PROJECT_NAME}-config-version.cmake")
configure_package_config_file(
"${DEFER_HPP_INSTALL_CONFIG_INPUT}"
"${DEFER_HPP_INSTALL_GENERATED_CONFIG_CMAKE}"
INSTALL_DESTINATION "${DEFER_HPP_INSTALL_CONFIG_DIR}"
NO_SET_AND_CHECK_MACRO
NO_CHECK_REQUIRED_COMPONENTS_MACRO)
write_basic_package_version_file(
"${DEFER_HPP_INSTALL_GENERATED_CONFIG_VERSION_CMAKE}"
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
ARCH_INDEPENDENT)
install(
TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}-targets
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
install(
DIRECTORY headers/${PROJECT_NAME}
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
install(
EXPORT ${PROJECT_NAME}-targets
FILE ${PROJECT_NAME}-targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION "${DEFER_HPP_INSTALL_CONFIG_DIR}")
install(
FILES "${DEFER_HPP_INSTALL_GENERATED_CONFIG_CMAKE}"
"${DEFER_HPP_INSTALL_GENERATED_CONFIG_VERSION_CMAKE}"
DESTINATION "${DEFER_HPP_INSTALL_CONFIG_DIR}")
endif()
#
# DEVELOPER
#
if(PROJECT_IS_TOP_LEVEL)
option(BUILD_WITH_COVERAGE "Build with coverage" OFF)
option(BUILD_WITH_SANITIZERS "Build with sanitizers" OFF)
enable_testing()
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "CMake")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(EnableASan)
include(EnableGCov)
include(EnableUBSan)
add_subdirectory(vendors)
add_subdirectory(untests)
endif()

342
CMakePresets.json Normal file
View File

@@ -0,0 +1,342 @@
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 21,
"patch": 0
},
"configurePresets": [
{
"name": "ninja-base",
"hidden": true,
"generator": "Ninja Multi-Config",
"binaryDir": "${sourceDir}/build/${presetName}",
"installDir": "${sourceDir}/install/${presetName}",
"cacheVariables": {
"CMAKE_EXPORT_COMPILE_COMMANDS": true
}
},
{
"name": "linux-base",
"hidden": true,
"inherits": "ninja-base",
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Linux"
}
},
{
"name": "linux-clang-7",
"inherits": "linux-base",
"cacheVariables": {
"CMAKE_C_COMPILER": "clang-7",
"CMAKE_CXX_COMPILER": "clang++-7"
}
},
{
"name": "linux-clang-14",
"inherits": "linux-base",
"cacheVariables": {
"CMAKE_C_COMPILER": "clang-14",
"CMAKE_CXX_COMPILER": "clang++-14"
}
},
{
"name": "linux-gcc-7",
"inherits": "linux-base",
"cacheVariables": {
"CMAKE_C_COMPILER": "gcc-7",
"CMAKE_CXX_COMPILER": "g++-7"
}
},
{
"name": "linux-gcc-12",
"inherits": "linux-base",
"cacheVariables": {
"CMAKE_C_COMPILER": "gcc-12",
"CMAKE_CXX_COMPILER": "g++-12"
}
},
{
"name": "macos-base",
"hidden": true,
"inherits": "ninja-base",
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Darwin"
}
},
{
"name": "macos-arm64",
"inherits": "macos-base",
"architecture": {
"value": "arm64",
"strategy": "external"
}
},
{
"name": "macos-arm64-san",
"inherits": "macos-arm64",
"cacheVariables": {
"BUILD_WITH_SANITIZERS": true
}
},
{
"name": "macos-x64",
"inherits": "macos-base",
"architecture": {
"value": "x64",
"strategy": "external"
}
},
{
"name": "macos-x64-san",
"inherits": "macos-x64",
"cacheVariables": {
"BUILD_WITH_SANITIZERS": true
}
},
{
"name": "windows-base",
"hidden": true,
"inherits": "ninja-base",
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Windows"
}
},
{
"name": "windows-x86-msvc2019",
"inherits": "windows-base",
"architecture": {
"value": "x86",
"strategy": "external"
},
"toolset": {
"value": "v142,host=x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_C_COMPILER": "cl",
"CMAKE_CXX_COMPILER": "cl"
}
},
{
"name": "windows-x64-msvc2019",
"inherits": "windows-base",
"architecture": {
"value": "x64",
"strategy": "external"
},
"toolset": {
"value": "v142,host=x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_C_COMPILER": "cl",
"CMAKE_CXX_COMPILER": "cl"
}
},
{
"name": "windows-x86-msvc2022",
"inherits": "windows-base",
"architecture": {
"value": "x86",
"strategy": "external"
},
"toolset": {
"value": "v143,host=x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_C_COMPILER": "cl",
"CMAKE_CXX_COMPILER": "cl"
}
},
{
"name": "windows-x64-msvc2022",
"inherits": "windows-base",
"architecture": {
"value": "x64",
"strategy": "external"
},
"toolset": {
"value": "v143,host=x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_C_COMPILER": "cl",
"CMAKE_CXX_COMPILER": "cl"
}
}
],
"buildPresets": [
{
"name": "linux-clang-7-debug",
"configuration": "Debug",
"configurePreset": "linux-clang-7"
},
{
"name": "linux-clang-7-release",
"configuration": "Release",
"configurePreset": "linux-clang-7"
},
{
"name": "linux-clang-14-debug",
"configuration": "Debug",
"configurePreset": "linux-clang-14"
},
{
"name": "linux-clang-14-release",
"configuration": "Release",
"configurePreset": "linux-clang-14"
},
{
"name": "linux-gcc-7-debug",
"configuration": "Debug",
"configurePreset": "linux-gcc-7"
},
{
"name": "linux-gcc-7-release",
"configuration": "Release",
"configurePreset": "linux-gcc-7"
},
{
"name": "linux-gcc-12-debug",
"configuration": "Debug",
"configurePreset": "linux-gcc-12"
},
{
"name": "linux-gcc-12-release",
"configuration": "Release",
"configurePreset": "linux-gcc-12"
},
{
"name": "macos-arm64-debug",
"configuration": "Debug",
"configurePreset": "macos-arm64-san"
},
{
"name": "macos-arm64-release",
"configuration": "Release",
"configurePreset": "macos-arm64"
},
{
"name": "macos-x64-debug",
"configuration": "Debug",
"configurePreset": "macos-x64-san"
},
{
"name": "macos-x64-release",
"configuration": "Release",
"configurePreset": "macos-x64"
},
{
"name": "windows-x86-msvc2019-debug",
"configuration": "Debug",
"configurePreset": "windows-x86-msvc2019"
},
{
"name": "windows-x86-msvc2019-release",
"configuration": "Release",
"configurePreset": "windows-x86-msvc2019"
},
{
"name": "windows-x64-msvc2019-debug",
"configuration": "Debug",
"configurePreset": "windows-x64-msvc2019"
},
{
"name": "windows-x64-msvc2019-release",
"configuration": "Release",
"configurePreset": "windows-x64-msvc2019"
},
{
"name": "windows-x86-msvc2022-debug",
"configuration": "Debug",
"configurePreset": "windows-x86-msvc2022"
},
{
"name": "windows-x86-msvc2022-release",
"configuration": "Release",
"configurePreset": "windows-x86-msvc2022"
},
{
"name": "windows-x64-msvc2022-debug",
"configuration": "Debug",
"configurePreset": "windows-x64-msvc2022"
},
{
"name": "windows-x64-msvc2022-release",
"configuration": "Release",
"configurePreset": "windows-x64-msvc2022"
}
],
"testPresets": [
{
"name": "test-base",
"hidden": true,
"output": {
"verbosity": "verbose"
},
"configuration": "Release"
},
{
"name": "linux-clang-7-release",
"inherits": "test-base",
"configurePreset": "linux-clang-7"
},
{
"name": "linux-clang-14-release",
"inherits": "test-base",
"configurePreset": "linux-clang-14"
},
{
"name": "linux-gcc-7-release",
"inherits": "test-base",
"configurePreset": "linux-gcc-7"
},
{
"name": "linux-gcc-12-release",
"inherits": "test-base",
"configurePreset": "linux-gcc-12"
},
{
"name": "macos-arm64-release",
"inherits": "test-base",
"configurePreset": "macos-arm64"
},
{
"name": "macos-x64-release",
"inherits": "test-base",
"configurePreset": "macos-x64"
},
{
"name": "windows-x86-msvc2019-release",
"inherits": "test-base",
"configurePreset": "windows-x86-msvc2019"
},
{
"name": "windows-x64-msvc2019-release",
"inherits": "test-base",
"configurePreset": "windows-x64-msvc2019"
},
{
"name": "windows-x86-msvc2022-release",
"inherits": "test-base",
"configurePreset": "windows-x86-msvc2022"
},
{
"name": "windows-x64-msvc2022-release",
"inherits": "test-base",
"configurePreset": "windows-x64-msvc2022"
}
]
}

View File

@@ -5,21 +5,18 @@
[![linux][badge.linux]][linux]
[![darwin][badge.darwin]][darwin]
[![windows][badge.windows]][windows]
[![codecov][badge.codecov]][codecov]
[![language][badge.language]][language]
[![license][badge.license]][license]
[badge.darwin]: https://img.shields.io/github/workflow/status/BlackMATov/defer.hpp/darwin/main?label=Xcode&logo=xcode
[badge.linux]: https://img.shields.io/github/workflow/status/BlackMATov/defer.hpp/linux/main?label=GCC%2FClang&logo=linux
[badge.windows]: https://img.shields.io/github/workflow/status/BlackMATov/defer.hpp/windows/main?label=Visual%20Studio&logo=visual-studio
[badge.codecov]: https://img.shields.io/codecov/c/github/BlackMATov/defer.hpp/main?logo=codecov
[badge.darwin]: https://img.shields.io/github/actions/workflow/status/BlackMATov/defer.hpp/.github/workflows/darwin.yml?label=Xcode&logo=xcode
[badge.linux]: https://img.shields.io/github/actions/workflow/status/BlackMATov/defer.hpp/.github/workflows/linux.yml?label=GCC%2FClang&logo=linux
[badge.windows]: https://img.shields.io/github/actions/workflow/status/BlackMATov/defer.hpp/.github/workflows/windows.yml?label=Visual%20Studio&logo=visual-studio
[badge.language]: https://img.shields.io/badge/language-C%2B%2B17-yellow
[badge.license]: https://img.shields.io/badge/license-MIT-blue
[darwin]: https://github.com/BlackMATov/defer.hpp/actions?query=workflow%3Adarwin
[linux]: https://github.com/BlackMATov/defer.hpp/actions?query=workflow%3Alinux
[windows]: https://github.com/BlackMATov/defer.hpp/actions?query=workflow%3Awindows
[codecov]: https://codecov.io/gh/BlackMATov/defer.hpp
[language]: https://en.wikipedia.org/wiki/C%2B%2B17
[license]: https://en.wikipedia.org/wiki/MIT_License
@@ -27,9 +24,10 @@
## Requirements
- [gcc](https://www.gnu.org/software/gcc/) **>= 7**
- [clang](https://clang.llvm.org/) **>= 7**
- [msvc](https://visualstudio.microsoft.com/) **>= 2017**
- [gcc](https://www.gnu.org/software/gcc/) **>= 7**
- [msvc](https://visualstudio.microsoft.com/) **>= 2019**
- [xcode](https://developer.apple.com/xcode/) **>= 10.3**
## Installation
@@ -43,7 +41,7 @@ Also, you can add the root repository directory to your [cmake](https://cmake.or
```cmake
add_subdirectory(external/defer.hpp)
target_link_libraries(your_project_target defer.hpp)
target_link_libraries(your_project_target PUBLIC defer.hpp::defer.hpp)
```
## Examples

3
cmake/Config.cmake.in Normal file
View File

@@ -0,0 +1,3 @@
@PACKAGE_INIT@
include("${CMAKE_CURRENT_LIST_DIR}/defer.hpp-targets.cmake")

16
cmake/EnableASan.cmake Normal file
View File

@@ -0,0 +1,16 @@
# https://clang.llvm.org/docs/AddressSanitizer.html
add_library(${PROJECT_NAME}.enable_asan INTERFACE)
add_library(${PROJECT_NAME}::enable_asan ALIAS ${PROJECT_NAME}.enable_asan)
target_compile_options(${PROJECT_NAME}.enable_asan INTERFACE
-fsanitize=address
-fno-omit-frame-pointer
-fsanitize-address-use-after-scope
-fsanitize-address-use-after-return=always)
target_link_options(${PROJECT_NAME}.enable_asan INTERFACE
-fsanitize=address
-fno-omit-frame-pointer
-fsanitize-address-use-after-scope
-fsanitize-address-use-after-return=always)

10
cmake/EnableGCov.cmake Normal file
View File

@@ -0,0 +1,10 @@
# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
add_library(${PROJECT_NAME}.enable_gcov INTERFACE)
add_library(${PROJECT_NAME}::enable_gcov ALIAS ${PROJECT_NAME}.enable_gcov)
target_compile_options(${PROJECT_NAME}.enable_gcov INTERFACE
--coverage)
target_link_options(${PROJECT_NAME}.enable_gcov INTERFACE
--coverage)

12
cmake/EnableUBSan.cmake Normal file
View File

@@ -0,0 +1,12 @@
# https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
add_library(${PROJECT_NAME}.enable_ubsan INTERFACE)
add_library(${PROJECT_NAME}::enable_ubsan ALIAS ${PROJECT_NAME}.enable_ubsan)
target_compile_options(${PROJECT_NAME}.enable_ubsan INTERFACE
-fsanitize=undefined
-fno-omit-frame-pointer)
target_link_options(${PROJECT_NAME}.enable_ubsan INTERFACE
-fsanitize=undefined
-fno-omit-frame-pointer)

View File

@@ -15,18 +15,15 @@ namespace defer_hpp
{
namespace impl
{
class noncopyable {
public:
noncopyable(const noncopyable&) = delete;
noncopyable& operator=(const noncopyable&) = delete;
protected:
noncopyable() = default;
~noncopyable() = default;
};
template < typename F, typename... Args >
class defer_impl : noncopyable {
class defer_impl {
public:
defer_impl() = delete;
defer_impl(defer_impl&&) = delete;
defer_impl(const defer_impl&) = delete;
defer_impl& operator=(defer_impl&&) = delete;
defer_impl& operator=(const defer_impl&) = delete;
template < typename UF >
explicit defer_impl(
UF&& f,
@@ -43,6 +40,7 @@ namespace defer_hpp
void dismiss() noexcept {
dismissed_ = true;
}
private:
F f_;
std::tuple<Args...> args_;
@@ -52,6 +50,12 @@ namespace defer_hpp
template < typename F, typename... Args >
class error_defer_impl final : public defer_impl<F, Args...> {
public:
error_defer_impl() = delete;
error_defer_impl(error_defer_impl&&) = delete;
error_defer_impl(const error_defer_impl&) = delete;
error_defer_impl& operator=(error_defer_impl&&) = delete;
error_defer_impl& operator=(const error_defer_impl&) = delete;
template < typename UF >
explicit error_defer_impl(
UF&& f,
@@ -64,6 +68,7 @@ namespace defer_hpp
this->dismiss();
}
}
private:
int exceptions_{};
};
@@ -71,6 +76,12 @@ namespace defer_hpp
template < typename F, typename... Args >
class return_defer_impl final : public defer_impl<F, Args...> {
public:
return_defer_impl() = delete;
return_defer_impl(return_defer_impl&&) = delete;
return_defer_impl(const return_defer_impl&) = delete;
return_defer_impl& operator=(return_defer_impl&&) = delete;
return_defer_impl& operator=(const return_defer_impl&) = delete;
template < typename UF >
explicit return_defer_impl(
UF&& f,
@@ -83,6 +94,7 @@ namespace defer_hpp
this->dismiss();
}
}
private:
int exceptions_{};
};

18
untests/.clang-tidy Normal file
View File

@@ -0,0 +1,18 @@
---
Checks: '-*,
bugprone-*,
clang-analyzer-*,
concurrency-*,
modernize-*,
-modernize-avoid-c-arrays,
-modernize-use-trailing-return-type,
performance-*,
portability-*,
'
...

View File

@@ -1,39 +1,73 @@
project(defer.hpp.untests)
#
# coverage
#
option(BUILD_WITH_COVERAGE "Build with coverage" OFF)
if(BUILD_WITH_COVERAGE AND (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang"))
set(COVERAGE_FLAGS "--coverage")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${COVERAGE_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${COVERAGE_FLAGS}")
set(CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} ${COVERAGE_FLAGS}")
endif()
#
# executable
#
file(GLOB_RECURSE UNTESTS_SOURCES "*.cpp" "*.hpp")
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${UNTESTS_SOURCES})
add_executable(${PROJECT_NAME} ${UNTESTS_SOURCES})
target_link_libraries(${PROJECT_NAME} defer.hpp)
target_link_libraries(${PROJECT_NAME} PRIVATE defer.hpp::defer.hpp)
target_compile_options(${PROJECT_NAME}
PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:
/WX /W4>
PRIVATE
$<$<CXX_COMPILER_ID:GNU>:
-Werror -Wall -Wextra -Wpedantic>
PRIVATE
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:
-Werror -Weverything -Wconversion>)
#
# setup defines
#
target_compile_definitions(${PROJECT_NAME}
PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:
-D_CRT_SECURE_NO_WARNINGS>)
function(setup_defines_for_target TARGET)
target_compile_definitions(${TARGET} PRIVATE
DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS
DOCTEST_CONFIG_USE_STD_HEADERS)
endfunction()
setup_defines_for_target(${PROJECT_NAME})
#
# setup libraries
#
function(setup_libraries_for_target TARGET)
target_link_libraries(${TARGET} PRIVATE doctest::doctest_with_main)
if(${BUILD_WITH_COVERAGE})
target_link_libraries(${TARGET} PRIVATE defer.hpp::enable_gcov)
endif()
if(${BUILD_WITH_SANITIZERS})
target_link_libraries(${TARGET} PRIVATE defer.hpp::enable_asan defer.hpp::enable_ubsan)
endif()
endfunction()
setup_libraries_for_target(${PROJECT_NAME})
#
# setup warnings
#
function(setup_warnings_for_target TARGET)
target_compile_options(${TARGET}
PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:
/WX /W4>
PRIVATE
$<$<CXX_COMPILER_ID:GNU>:
-Werror -Wall -Wextra -Wpedantic>
PRIVATE
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:
-Werror -Weverything -Wconversion
-Wno-c++98-compat
-Wno-c++98-compat-pedantic
-Wno-padded
-Wno-unknown-warning-option
-Wno-unused-macros
>)
target_compile_definitions(${PROJECT_NAME}
PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:
-D_CRT_SECURE_NO_WARNINGS>)
endfunction()
setup_warnings_for_target(${PROJECT_NAME})
#
# add tests
#
add_test(${PROJECT_NAME} ${PROJECT_NAME})

View File

@@ -5,7 +5,7 @@
******************************************************************************/
#include <defer.hpp/defer.hpp>
#include "doctest/doctest.hpp"
#include <doctest/doctest.h>
#include <cstdio>
#include <cstring>

View File

@@ -5,7 +5,7 @@
******************************************************************************/
#include <defer.hpp/defer.hpp>
#include "doctest/doctest.hpp"
#include <doctest/doctest.h>
#include <functional>
@@ -22,7 +22,7 @@ TEST_CASE("defer") {
SUBCASE("simple_with_arg") {
int i = 0;
{
DEFER_HPP([](int& i){ ++i; }, std::ref(i));
DEFER_HPP([](int& ni){ ++ni; }, std::ref(i));
REQUIRE(i == 0);
}
REQUIRE(i == 1);
@@ -31,7 +31,7 @@ TEST_CASE("defer") {
SUBCASE("simple_with_args") {
int i = 0, j = 0;
{
DEFER_HPP([](int& i, int& j){ ++i; j += 2; }, std::ref(i), std::ref(j));
DEFER_HPP([](int& ni, int& nj){ ++ni; nj += 2; }, std::ref(i), std::ref(j));
REQUIRE(i == 0);
REQUIRE(j == 0);
}
@@ -64,7 +64,7 @@ TEST_CASE("error_defer") {
SUBCASE("simple_with_arg") {
int i = 0;
{
ERROR_DEFER_HPP([](int& i){ ++i; }, std::ref(i));
ERROR_DEFER_HPP([](int& ni){ ++ni; }, std::ref(i));
REQUIRE(i == 0);
}
REQUIRE(i == 0);
@@ -73,7 +73,7 @@ TEST_CASE("error_defer") {
SUBCASE("simple_with_args") {
int i = 0, j = 0;
{
ERROR_DEFER_HPP([](int& i, int& j){ ++i; j += 2; }, std::ref(i), std::ref(j));
ERROR_DEFER_HPP([](int& ni, int& nj){ ++ni; nj += 2; }, std::ref(i), std::ref(j));
REQUIRE(i == 0);
REQUIRE(j == 0);
}
@@ -106,7 +106,7 @@ TEST_CASE("return_defer") {
SUBCASE("simple_with_arg") {
int i = 0;
{
RETURN_DEFER_HPP([](int& i){ ++i; }, std::ref(i));
RETURN_DEFER_HPP([](int& ni){ ++ni; }, std::ref(i));
REQUIRE(i == 0);
}
REQUIRE(i == 1);
@@ -115,7 +115,7 @@ TEST_CASE("return_defer") {
SUBCASE("simple_with_args") {
int i = 0, j = 0;
{
RETURN_DEFER_HPP([](int& i, int& j){ ++i; j += 2; }, std::ref(i), std::ref(j));
RETURN_DEFER_HPP([](int& ni, int& nj){ ++ni; nj += 2; }, std::ref(i), std::ref(j));
REQUIRE(i == 0);
REQUIRE(j == 0);
}

View File

@@ -1,2 +0,0 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"

File diff suppressed because it is too large Load Diff

View File

@@ -1,11 +0,0 @@
#pragma once
#include "doctest.h"
#define STATIC_REQUIRE(...)\
static_assert(__VA_ARGS__, #__VA_ARGS__);\
REQUIRE(__VA_ARGS__);
#define STATIC_REQUIRE_FALSE(...)\
static_assert(!(__VA_ARGS__), "!(" #__VA_ARGS__ ")");\
REQUIRE(!(__VA_ARGS__));

5
vendors/CMakeLists.txt vendored Normal file
View File

@@ -0,0 +1,5 @@
project(defer.hpp.vendors)
set(DOCTEST_NO_INSTALL ON CACHE INTERNAL "")
add_subdirectory(doctest)
set_target_properties(doctest_with_main PROPERTIES FOLDER defer.hpp.vendors)

1
vendors/doctest vendored Submodule

Submodule vendors/doctest added at 86892fc480