Skip to content

Commit

Permalink
Merge branch 'aminya:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
abeimler authored Oct 28, 2022
2 parents cfbbbb0 + 0a757c1 commit bd90abd
Show file tree
Hide file tree
Showing 24 changed files with 321 additions and 203 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,14 @@ jobs:
vcvarsall: true
- os: "windows-2022"
compiler: "msvc"
cmake: 3.18.0
cmake_generator: "Ninja"
vcvarsall: true
- os: "windows-2022"
compiler: "msvc"
cmake: 3.18.0
cmake_generator: "Ninja"
vcvarsall: false
- os: "windows-2022"
compiler: "msvc"
cmake: 3.18.0
vcvarsall: false
steps:
- uses: actions/checkout@v2
Expand Down
78 changes: 67 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
# project_options

A general-purpose CMake library that provides functions that improve the CMake experience.

It provides different functions such as `project_options`, `package_project`, `dynamic_project_options`, `run_vcpkg`, `target_link_system_libraries`, etc.
A general-purpose CMake library that provides functions that improve the CMake experience following the best practices.

## Features

- `project_options`:
- compiler warnings,
- compiler optimizations (intraprocedural, native),
- caching (ccache, sccache),
- sanitizers,
- static code analyzers (clang-tidy, cppcheck, visual studio, include-what-you-use),
- document generation (doxygen),
- test coverage analysis,
- precompiled headers,
- build time measurement,
- unity builds
- using custom linkers (e.g. lld)
- `package_project`: automatic packaging/installation of the project for seamless usage via find_package/target_link through CMake's FetchContent, vcpkg, etc.
- `run_vcpkg`: automatic installation of vcpkg and the project dependencies
- `ENABLE_CONAN` in `project_options`: automatic installation of Conan and the project dependencies
- `dynamic_project_options`: a wrapper around `project_options` to change the options on the fly dynamically
- `target_link_system_libraries` and `target_include_system_directories`: linking/including external dependencies/headers without warnings
- `target_link_cuda`: linking Cuda to a target

[![ci](https://github.com/aminya/project_options/actions/workflows/ci.yml/badge.svg)](https://github.com/aminya/project_options/actions/workflows/ci.yml)

## Usage

Expand All @@ -11,17 +32,17 @@ See `project_options()` in action in [this template repository](https://github.c
Here is a full example:

```cmake
cmake_minimum_required(VERSION 3.16)
cmake_minimum_required(VERSION 3.20)
# set a default CXX standard for the tools and targets that do not specify them.
# If commented, the latest supported standard for your compiler is automatically set.
# set(CMAKE_CXX_STANDARD 20)
# Add project_options v0.22.4
# Add project_options v0.26.1
# https://github.com/aminya/project_options
# Change the version in the following URL to update the package (watch the releases of the repository for future updates)
include(FetchContent)
FetchContent_Declare(_project_options URL https://github.com/aminya/project_options/archive/refs/tags/v0.22.4.zip)
FetchContent_Declare(_project_options URL https://github.com/aminya/project_options/archive/refs/tags/v0.26.1.zip)
FetchContent_MakeAvailable(_project_options)
include(${_project_options_SOURCE_DIR}/Index.cmake)
Expand Down Expand Up @@ -221,6 +242,7 @@ It accepts the following named flags:

It gets the following named parameters that can have different values in front of them:

- `PREFIX`: the optional prefix that is used to define `${PREFIX}_project_options` and `${PREFIX}_project_warnings` targets when the function is used in a multi-project fashion.
- `DOXYGEN_THEME`: the name of the Doxygen theme to use. Supported themes:
- `awesome-sidebar` (default)
- `awesome`
Expand All @@ -232,15 +254,32 @@ It gets the following named parameters that can have different values in front o
- `CLANG_WARNINGS`: Override the defaults for the CLANG warnings
- `GCC_WARNINGS`: Override the defaults for the GCC warnings
- `CUDA_WARNINGS`: Override the defaults for the CUDA warnings
- `CPPCHECK_WARNINGS`: Override the defaults for the options passed to cppcheck
- `CPPCHECK_OPTIONS`: Override the defaults for the options passed to cppcheck
- `VS_ANALYSIS_RULESET`: Override the defaults for the code analysis rule set in Visual Studio.
- `CONAN_OPTIONS`: Extra Conan options

## `run_vcpkg` function

```cmake
run_vcpkg()
```

Or by specifying the options

```cmake
run_vcpkg(
VCPKG_URL "https://github.com/microsoft/vcpkg.git"
VCPKG_REV "33c8f025390f8682811629b6830d2d66ecedcaa5"
ENABLE_VCPKG_UPDATE
)
```

Named Option:

- `ENABLE_VCPKG_UPDATE`: (Disabled by default). If enabled, the vcpkg registry is updated before building (using `git pull`). As a result, if some of your vcpkg dependencies have been updated in the registry, they will be rebuilt.
- `ENABLE_VCPKG_UPDATE`: (Disabled by default). If enabled, the vcpkg registry is updated before building (using `git pull`).

If `VCPKG_REV` is set to a specific commit sha, no rebuilds are triggered.
If `VCPKG_REV` is not specified or is a branch, enabling `ENABLE_VCPKG_UPDATE` will rebuild your updated vcpkg dependencies.

Named String:

Expand All @@ -249,6 +288,9 @@ Named String:

- `VCPKG_URL`: (Defaults to `https://github.com/microsoft/vcpkg.git`). This option allows setting the URL of the vcpkg repository. By default, the official vcpkg repository is used.

- `VCPKG_REV`: This option allows checking out a specific branch name or a commit sha.
If `VCPKG_REV` is set to a specific commit sha, the builds will become reproducible because that exact commit is always used for the builds. To make sure that this commit sha is pulled, enable `ENABLE_VCPKG_UPDATE`

## `target_link_system_libraries` function

A function that accepts the same arguments as `target_link_libraries`. It has the following features:
Expand Down Expand Up @@ -303,6 +345,20 @@ Other arguments that are automatically found and manually specifying them is not

- `CONFIG_INSTALL_DESTINATION`: the destination for installation of the configuration files. Defaults to `${CMAKE_INSTALL_DATADIR}/${NAME}`

## Disabling static analysis for external targets

This function disables static analysis for the given target:

```cmake
target_disable_static_analysis(some_external_target)
```

There is also individual functions to disable a specific analysis for the target:

- `target_disable_cpp_check(target)`
- `target_disable_vs_analysis(target)`
- `target_disable_clang_tidy(target)`

## Changing the project_options dynamically

During the test and development, it can be useful to change options on the fly. For example, to enable sanitizers when running tests. You can include `DynamicOptions.cmake`, which imports the `dynamic_project_options` function.
Expand All @@ -329,17 +385,17 @@ See `dynamic_project_options()` in action in [this template repository](https://
<summary> 👉 Click to show the example:</summary>

```cmake
cmake_minimum_required(VERSION 3.16)
cmake_minimum_required(VERSION 3.20)
# set a default CXX standard for the tools and targets that do not specify them.
# If commented, the latest supported standard for your compiler is automatically set.
# set(CMAKE_CXX_STANDARD 20)
# Add project_options v0.22.4
# Add project_options v0.26.1
# https://github.com/aminya/project_options
# Change the version in the following URL to update the package (watch the releases of the repository for future updates)
include(FetchContent)
FetchContent_Declare(_project_options URL https://github.com/aminya/project_options/archive/refs/tags/v0.22.4.zip)
FetchContent_Declare(_project_options URL https://github.com/aminya/project_options/archive/refs/tags/v0.26.1.zip)
FetchContent_MakeAvailable(_project_options)
include(${_project_options_SOURCE_DIR}/Index.cmake)
Expand Down
11 changes: 4 additions & 7 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ words:
- ccmake
- choco
- cmake
- CMAKE
- cmakelint
- cmds
- cond
- CPATH
- Cppcheck
- cppcoreguidelines
- cppdbg
- CPPFLAGS
- cpprc
- ctest
Expand All @@ -45,6 +47,7 @@ words:
- gcovr
- Graphviz
- hwrap
- iwwu
- kcov
- LDFLAGS
- libc
Expand All @@ -62,6 +65,7 @@ words:
- myprogram
- myproj
- myproject
- mythirdpartylib
- nothrow
- nvcc
- Opencppcoverage
Expand Down Expand Up @@ -104,10 +108,3 @@ words:
- Wuseless
- xcrun
- Yahyaabadi
- "ARGN"
- "CMAKE"
- "fargs"
- "shlib"
- "vcpkg"
- cppdbg
- mythirdpartylib
10 changes: 10 additions & 0 deletions src/Common.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
include_guard()

# This function sets ProjectOptions_SRC_DIR using the current lists path
macro(set_project_options_src_dir)
get_directory_property(LISTFILE_STACK LISTFILE_STACK)
list(POP_BACK LISTFILE_STACK _LIST_FILE)
cmake_path(GET _LIST_FILE PARENT_PATH ProjectOptions_SRC_DIR)
endmacro()

# Common project settings run by default for all the projects that call `project_options()`
macro(common_project_options)
set_project_options_src_dir()
message(DEBUG "${ProjectOptions_SRC_DIR}")

include("${ProjectOptions_SRC_DIR}/PreventInSourceBuilds.cmake")
assure_out_of_source_builds()

Expand Down
1 change: 1 addition & 0 deletions src/CompilerWarnings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function(
set(CLANG_WARNINGS
-Wall
-Wextra # reasonable and standard
-Wextra-semi # Warn about semicolon after in-class function definition.
-Wshadow # warn the user if a variable declaration shadows one from a parent context
-Wnon-virtual-dtor # warn the user if a class with virtual functions has a non-virtual destructor. This helps
# catch hard to track down memory errors
Expand Down
8 changes: 6 additions & 2 deletions src/Conan.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ macro(run_conan)
if(NOT ${_is_verbose})
set(OUTPUT_QUIET "OUTPUT_QUIET")
else()
set(OUTPUT_QUIET OFF)
set(OUTPUT_QUIET)
endif()

foreach(TYPE ${LIST_OF_BUILD_TYPES})
Expand All @@ -69,7 +69,11 @@ macro(run_conan)
set(CONAN_ENV ENV "CC=${CMAKE_C_COMPILER}" "CXX=${CMAKE_CXX_COMPILER}")
else()
# Derive all conan settings from a conan profile
set(CONAN_SETTINGS PROFILE ${ProjectOptions_CONAN_PROFILE})
set(CONAN_SETTINGS
PROFILE
${ProjectOptions_CONAN_PROFILE}
SETTINGS
"build_type=${TYPE}")
# CONAN_ENV should be redundant, since the profile can set CC & CXX
endif()

Expand Down
Loading

0 comments on commit bd90abd

Please sign in to comment.