Skip to content

Commit 7ba1763

Browse files
committed
Merge branch 'main' into msvc
2 parents 541db76 + 836380b commit 7ba1763

27 files changed

Lines changed: 233 additions & 140 deletions

.github/workflows/ci.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ jobs:
5050
cmake: true
5151
ninja: true
5252
vcpkg: true
53+
conan: true
5354
cppcheck: true
5455
clangtidy: true
5556
task: true
@@ -59,4 +60,12 @@ jobs:
5960
run: |
6061
task test
6162
task test_release
62-
task test_install
63+
task test_install
64+
65+
- name: Lint
66+
if: ${{ matrix.os == 'ubuntu-20.04' && matrix.compiler == 'gcc' }}
67+
run: |
68+
# TODO add to setup-cpp
69+
python3 -m pip install --user cmakelint cmake-format
70+
71+
task lint

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
It provdes different functions such as `project_options`, `package_project`, `dynamic_project_options`, `run_vcpkg`, `target_link_system_libraries`, etc.
5+
It provides different functions such as `project_options`, `package_project`, `dynamic_project_options`, `run_vcpkg`, `target_link_system_libraries`, etc.
66

77
## Usage
88

@@ -15,10 +15,11 @@ cmake_minimum_required(VERSION 3.16)
1515
# If commented, the latest supported standard for your compiler is automatically set.
1616
# set(CMAKE_CXX_STANDARD 20)
1717
18-
# Add project_options v0.17.0
18+
# Add project_options v0.19.0
1919
# https://github.com/cpp-best-practices/project_options
20+
# Change the version in the following URL to update the package (watch the releases of the repository for future updates)
2021
include(FetchContent)
21-
FetchContent_Declare(_project_options URL https://github.com/cpp-best-practices/project_options/archive/refs/tags/v0.17.0.zip)
22+
FetchContent_Declare(_project_options URL https://github.com/cpp-best-practices/project_options/archive/refs/tags/v0.19.0.zip)
2223
FetchContent_MakeAvailable(_project_options)
2324
include(${_project_options_SOURCE_DIR}/Index.cmake)
2425
@@ -125,8 +126,8 @@ target_link_libraries(my_lib PRIVATE project_options project_warnings) # link pr
125126
126127
# Includes
127128
set(INCLUDE_DIR "include") # must be relative paths
128-
target_include_directories(my_lib INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${INCLUDE_DIR}>"
129-
"$<INSTALL_INTERFACE:./${CMAKE_INSTALL_INCLUDEDIR}>")
129+
target_include_directories(my_lib PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${INCLUDE_DIR}>"
130+
"$<INSTALL_INTERFACE:./${CMAKE_INSTALL_INCLUDEDIR}>")
130131
131132
# Find dependencies:
132133
set(DEPENDENCIES_CONFIGURED fmt Eigen3)
@@ -146,7 +147,7 @@ target_link_system_libraries(
146147
# Package the project
147148
package_project(
148149
TARGETS my_lib
149-
INTERFACE_INCLUDES ${INCLUDE_DIR}
150+
PUBLIC_INCLUDES ${INCLUDE_DIR}
150151
)
151152
```
152153

@@ -284,10 +285,11 @@ cmake_minimum_required(VERSION 3.16)
284285
# If commented, the latest supported standard for your compiler is automatically set.
285286
# set(CMAKE_CXX_STANDARD 20)
286287
287-
# Add project_options v0.17.0
288+
# Add project_options v0.19.0
288289
# https://github.com/cpp-best-practices/project_options
290+
# Change the version in the following URL to update the package (watch the releases of the repository for future updates)
289291
include(FetchContent)
290-
FetchContent_Declare(_project_options URL https://github.com/cpp-best-practices/project_options/archive/refs/tags/v0.17.0.zip)
292+
FetchContent_Declare(_project_options URL https://github.com/cpp-best-practices/project_options/archive/refs/tags/v0.19.0.zip)
291293
FetchContent_MakeAvailable(_project_options)
292294
include(${_project_options_SOURCE_DIR}/Index.cmake)
293295

Taskfile.yml

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,31 @@ tasks:
1313
- cd ./test/build && ctest -C Release --verbose
1414

1515
test_install:
16-
cmds:
16+
cmds:
1717
- task: test_release
1818
- cmake --install ./test/build --config Release --prefix ./install
1919
- cmake ./test_install -B ./test_install/build -DCMAKE_BUILD_TYPE:STRING=Release -G "Ninja Multi-Config" -DCMAKE_PREFIX_PATH:STRING={{.CWD}}/install;
2020
- cmake --build ./test_install/build --config Release
2121
- cd ./test_install/build && ctest -C Release --verbose
22-
vars:
23-
CWD:
24-
sh: git rev-parse --show-toplevel
22+
vars:
23+
CWD:
24+
sh: git rev-parse --show-toplevel
2525

26-
format:
27-
- clang-format -i ./test/src/*/*.cpp ./test/include/*/*.hpp ./test_install/*.cpp
28-
- cmake-format --in-place ./Index.cmake ./src/*.cmake ./test/CMakeLists.txt ./test_install/CMakeLists.txt
26+
lint:
27+
- |
28+
{{if eq OS "windows"}}
29+
powershell -c '$files=(git ls-files --exclude-standard); foreach ($file in $files) { if ((get-item $file).Extension -in ".cpp", ".hpp", ".c", ".cc", ".cxx", ".hxx", ".ixx") { clang-format -i -style=file $file } }'
30+
{{else}}
31+
git ls-files --exclude-standard | grep -E '\.(cpp|hpp|c|cc|cxx|hxx|ixx)$' | xargs clang-format -i -style=file
32+
{{end}}
33+
- |
34+
{{if eq OS "windows"}}
35+
powershell -c '$files=(git ls-files --exclude-standard); foreach ($file in $files) { $item=(get-item $file); if (($item.Name -eq "CMakeLists.txt") -or ($item.Extension -in ".cmake")) { cmake-format --in-place $file; cmake-lint $file --disabled-codes C0103 C0301 R0912 R0915 R0913 --suppress-decorations } }'
36+
{{else}}
37+
git ls-files --exclude-standard | grep -E '(CMakeLists\.txt)|(\.(cmake))$' | xargs cmake-format --in-place | xargs cmake-lint --disabled-codes C0103 C0301 R0912 R0915 R0913 --suppress-decorations
38+
{{end}}
2939
30-
lint: cmake-lint ./Index.cmake ./src/*.cmake
40+
- ~/vcpkg/vcpkg format-manifest ./test/vcpkg.json ./test_install/vcpkg.json
3141

3242
clean: |
3343
{{if eq OS "windows"}}

src/CompilerWarnings.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# from here:
1+
# Set the compiler warnings
22
#
3+
# https://clang.llvm.org/docs/DiagnosticsReference.html
34
# https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md
4-
55
function(
66
set_project_warnings
77
project_name

src/Conan.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Run Conan for dependency management
12
macro(run_conan)
23
# Download automatically, you can also just copy the conan.cmake file
34
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")

src/Doxygen.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ function(enable_doxygen DOXYGEN_THEME)
2929

3030
if("${DOXYGEN_THEME}" STREQUAL "awesome" OR "${DOXYGEN_THEME}" STREQUAL "awesome-sidebar")
3131
# use a modern doxygen theme
32-
# https://github.com/jothepro/doxygen-awesome-css v1.6.1
32+
# https://github.com/jothepro/doxygen-awesome-css v2.0.2
3333
FetchContent_Declare(_doxygen_theme
34-
URL https://github.com/jothepro/doxygen-awesome-css/archive/refs/tags/v1.6.1.zip)
34+
URL https://github.com/jothepro/doxygen-awesome-css/archive/refs/tags/v2.0.2.zip)
3535
FetchContent_MakeAvailable(_doxygen_theme)
3636
if("${DOXYGEN_THEME}" STREQUAL "awesome" OR "${DOXYGEN_THEME}" STREQUAL "awesome-sidebar")
3737
set(DOXYGEN_HTML_EXTRA_STYLESHEET "${_doxygen_theme_SOURCE_DIR}/doxygen-awesome.css")

src/DynamicProjectOptions.cmake

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
# * ENABLE_CLANG_TIDY: ON for Ninja/Makefiles
55
# * ENABLE_SANITIZER_UNDEFINED: ON for Compilers that support it
66
# * ENABLE_CPPCHECK: ON for Ninja/Makefiles
7-
7+
#
88
# For non-developer builds
99
# -DENABLE_DEVELOPER_MODE:BOOL=OFF
1010
# Is recommended
11-
11+
#
1212
# In developer mode, all features have options that show up in the CMake GUI tools
13-
13+
#
1414
# dynamic_project_options() macro enables all recommended defaults with appropriately
1515
# applied options from the GUI which are set
16-
16+
#
1717
# Any default can be overridden
1818
# set(<feature_name>_DEFAULT <value>) - set default for both user and developer modes
1919
# set(<feature_name>_DEVELOPER_DEFAULT <value>) - set default for developer mode
2020
# set(<feature_name>_USER_DEFAULT <value>) - set default for user mode
21-
21+
#
2222
macro(dynamic_project_options)
2323
option(ENABLE_DEVELOPER_MODE "Set up defaults for a developer of the project, and let developer change options" OFF)
2424
if(NOT ${ENABLE_DEVELOPER_MODE})

src/Index.cmake

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,24 @@ macro(project_options)
9595
include("${ProjectOptions_SRC_DIR}/StandardProjectSettings.cmake")
9696
include("${ProjectOptions_SRC_DIR}/Optimization.cmake")
9797

98+
# Link this 'library' to set the c++ standard / compile-time options requested
99+
add_library(project_options INTERFACE)
100+
98101
if(NOT
99-
"${ProjectOptions_IPO}"
102+
"${ProjectOptions_ENABLE_IPO}"
100103
STREQUAL
101104
"")
102105
message(WARNING "Deprecation: Use ENABLE_INTERPROCEDURAL_OPTIMIZATION instead of ENABLE_IPO")
103-
set(ProjectOptions_ENABLE_INTERPROCEDURAL_OPTIMIZATION ${ProjectOptions_IPO})
106+
set(ProjectOptions_ENABLE_INTERPROCEDURAL_OPTIMIZATION ${ProjectOptions_ENABLE_IPO})
104107
endif()
105108
if(${ProjectOptions_ENABLE_INTERPROCEDURAL_OPTIMIZATION})
106-
enable_interprocedural_optimization()
109+
enable_interprocedural_optimization(project_options)
107110
endif()
108111

109112
if(${ProjectOptions_ENABLE_NATIVE_OPTIMIZATION})
110-
enable_native_optimization()
113+
enable_native_optimization(project_options)
111114
endif()
112115

113-
# Link this 'library' to set the c++ standard / compile-time options requested
114-
add_library(project_options INTERFACE)
115-
116116
if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
117117
if(ProjectOptions_ENABLE_BUILD_WITH_TIME_TRACE)
118118
target_compile_options(project_options INTERFACE -ftime-trace)

src/Linker.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# TODO use function arguments instead of CMake cache variables and options
2+
3+
# Set the linker to use for the linking phase
14
macro(configure_linker project_name)
25
include(CheckCXXCompilerFlag)
36

src/Optimization.cmake

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1-
macro(enable_interprocedural_optimization)
1+
macro(enable_interprocedural_optimization project_name)
22
if(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
33
include(CheckIPOSupported)
44
check_ipo_supported(RESULT result OUTPUT output)
55
if(result)
6+
# If a static library of this project is used in another project that does not have `CMAKE_INTERPROCEDURAL_OPTIMIZATION` enabled, a linker error might happen.
7+
# TODO set this option in `package_project` function.
8+
message(
9+
STATUS
10+
"Interprocedural optimization is enabled. In other projects, linking with the compiled libraries of this project might require `set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)`"
11+
)
612
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
13+
set_target_properties(${project_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON)
714
else()
8-
message(SEND_ERROR "IPO is not supported: ${output}")
15+
message(WARNING "Interprocedural Optimization is not supported. Not using it. Here is the error log: ${output}")
916
endif()
1017
endif()
1118
endmacro()
1219

13-
macro(enable_native_optimization)
14-
message(STATUS "Enabling the optimizations specific to the current build machine (less portable)")
15-
if(MSVC)
16-
target_compile_options(${project_name} PRIVATE /arch:native)
17-
else()
18-
target_compile_options(${project_name} PRIVATE -march=native)
20+
macro(enable_native_optimization project_name)
21+
detect_architecture(_arch)
22+
if("${_arch}" STREQUAL "x64")
23+
message(STATUS "Enabling the optimizations specific to the current build machine (less portable)")
24+
if(MSVC)
25+
# TODO It seems it only accepts the exact instruction set like AVX https://docs.microsoft.com/en-us/cpp/build/reference/arch-x64
26+
# target_compile_options(${project_name} INTERFACE /arch:native)
27+
else()
28+
target_compile_options(${project_name} INTERFACE -march=native)
29+
endif()
1930
endif()
2031
endmacro()

0 commit comments

Comments
 (0)