Skip to content

Commit 3ef8062

Browse files
authored
Merge pull request #8 from rigtorp/build/simplify-cmake-and-ci
Modernize and simplify CMake build and CI
2 parents 6af3605 + 705dea0 commit 3ef8062

13 files changed

Lines changed: 198 additions & 273 deletions

File tree

‎.github/workflows/ccpp.yml‎

Lines changed: 0 additions & 72 deletions
This file was deleted.

‎.github/workflows/ci.yml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [master, main]
6+
pull_request:
7+
8+
jobs:
9+
ci:
10+
uses: rigtorp/.github/.github/workflows/cpp-ci.yml@master
11+
with:
12+
cxx-standards: '["14", "17", "20"]'
13+
run-sanitizers: true
14+
enable-windows: true
15+
enable-macos: true
16+
warnings-as-errors: true

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.idea/
33
.vscode/
44
build/
5-
cmake-build-*
5+
cmake-build-*
6+
CMakeUserPresets.json

‎CMakeLists.txt‎

Lines changed: 83 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,106 @@
1-
cmake_minimum_required(VERSION 3.20)
1+
cmake_minimum_required(VERSION 3.24...4.3)
22

3-
project(HashMap VERSION 1.0.0 LANGUAGES CXX)
4-
5-
include(GNUInstallDirs)
6-
include(CMakePackageConfigHelpers)
7-
8-
set(HASHMAP_TOP_LEVEL OFF)
9-
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
10-
set(HASHMAP_TOP_LEVEL ON)
11-
endif()
12-
13-
option(HASHMAP_BUILD_TESTS "Build HashMap tests" ${HASHMAP_TOP_LEVEL})
14-
option(HASHMAP_BUILD_EXAMPLES "Build HashMap examples" ${HASHMAP_TOP_LEVEL})
15-
set(HASHMAP_BENCHMARK_DEFAULT ${HASHMAP_TOP_LEVEL})
16-
if(WIN32)
17-
set(HASHMAP_BENCHMARK_DEFAULT OFF)
18-
endif()
19-
option(HASHMAP_BUILD_BENCHMARKS "Build the POSIX benchmark" ${HASHMAP_BENCHMARK_DEFAULT})
20-
option(HASHMAP_INSTALL "Generate HashMap install rules" ${HASHMAP_TOP_LEVEL})
21-
option(HASHMAP_WARNINGS_AS_ERRORS "Treat HashMap executable warnings as errors" OFF)
3+
project(HashMap
4+
VERSION 1.0.0
5+
HOMEPAGE_URL "https://github.com/rigtorp/HashMap"
6+
LANGUAGES CXX
7+
)
228

9+
# -----------------------------------------------------------------------------
10+
# Library Target
11+
# -----------------------------------------------------------------------------
2312
add_library(HashMap INTERFACE)
2413
add_library(HashMap::HashMap ALIAS HashMap)
2514
target_compile_features(HashMap INTERFACE cxx_std_14)
26-
target_include_directories(HashMap INTERFACE
27-
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
28-
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
15+
target_sources(HashMap INTERFACE
16+
FILE_SET HEADERS
17+
BASE_DIRS include
18+
FILES include/rigtorp/HashMap.h
19+
)
20+
21+
# -----------------------------------------------------------------------------
22+
# Tests, Examples & Benchmarks (Top-Level Only)
23+
# -----------------------------------------------------------------------------
24+
include(CTest)
2925

30-
function(hashmap_executable target source)
26+
if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING)
27+
function(hashmap_executable target source)
3128
add_executable(${target} ${source})
3229
target_link_libraries(${target} PRIVATE HashMap::HashMap)
33-
set_target_properties(${target} PROPERTIES CXX_EXTENSIONS OFF)
34-
if(MSVC)
35-
target_compile_options(${target} PRIVATE /permissive- /W4)
36-
if(HASHMAP_WARNINGS_AS_ERRORS)
37-
target_compile_options(${target} PRIVATE /WX)
38-
endif()
39-
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
40-
target_compile_options(${target} PRIVATE -Wall -Wextra -Wpedantic)
41-
if(HASHMAP_WARNINGS_AS_ERRORS)
42-
target_compile_options(${target} PRIVATE -Werror)
43-
endif()
44-
endif()
45-
endfunction()
46-
47-
if(HASHMAP_BUILD_TESTS)
48-
include(CTest)
49-
if(BUILD_TESTING)
50-
hashmap_executable(HashMapTest src/HashMapTest.cpp)
51-
add_test(NAME HashMapTest COMMAND HashMapTest)
52-
endif()
53-
endif()
30+
set_target_properties(${target} PROPERTIES
31+
CXX_EXTENSIONS OFF
32+
COMPILE_WARNING_AS_ERROR ON
33+
)
34+
target_compile_options(${target} PRIVATE
35+
"$<$<CXX_COMPILER_ID:MSVC>:/permissive-;/W4;/w14242>"
36+
"$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wall;-Wextra;-Wpedantic;-Wshadow;-Wconversion;-Wsign-conversion;-Wold-style-cast;-Wundef>"
37+
)
38+
endfunction()
5439

55-
if(HASHMAP_BUILD_EXAMPLES)
56-
hashmap_executable(HashMapExample src/HashMapExample.cpp)
57-
endif()
40+
hashmap_executable(HashMapExample src/HashMapExample.cpp)
5841

59-
if(HASHMAP_BUILD_BENCHMARKS)
60-
if(WIN32)
61-
message(FATAL_ERROR "HashMapBenchmark requires POSIX; disable HASHMAP_BUILD_BENCHMARKS on Windows")
62-
endif()
42+
hashmap_executable(HashMapTest src/HashMapTest.cpp)
43+
add_test(NAME HashMapTest COMMAND HashMapTest)
6344

64-
option(HASHMAP_BENCHMARK_WITH_ABSL "Use installed Abseil when available" ON)
65-
option(HASHMAP_BENCHMARK_WITH_SPARSEHASH "Use installed sparsehash when available" ON)
45+
if(NOT WIN32)
6646
hashmap_executable(HashMapBenchmark src/HashMapBenchmark.cpp)
6747
target_compile_features(HashMapBenchmark PRIVATE cxx_std_17)
6848
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64|amd64)$")
69-
target_compile_options(HashMapBenchmark PRIVATE -msse4.2)
49+
target_compile_options(HashMapBenchmark PRIVATE -msse4.2)
7050
endif()
7151

52+
option(HASHMAP_BENCHMARK_WITH_ABSL "Use installed Abseil when available" ON)
7253
if(HASHMAP_BENCHMARK_WITH_ABSL)
73-
find_package(absl CONFIG QUIET)
74-
if(TARGET absl::flat_hash_map)
75-
target_link_libraries(HashMapBenchmark PRIVATE absl::flat_hash_map)
76-
target_compile_definitions(HashMapBenchmark PRIVATE HASHMAP_BENCHMARK_HAS_ABSL)
77-
endif()
54+
find_package(absl CONFIG QUIET)
55+
if(TARGET absl::flat_hash_map)
56+
target_link_libraries(HashMapBenchmark PRIVATE absl::flat_hash_map)
57+
target_compile_definitions(HashMapBenchmark PRIVATE HASHMAP_BENCHMARK_HAS_ABSL)
58+
endif()
7859
endif()
60+
61+
option(HASHMAP_BENCHMARK_WITH_SPARSEHASH "Use installed sparsehash when available" ON)
7962
if(HASHMAP_BENCHMARK_WITH_SPARSEHASH)
80-
find_path(HASHMAP_SPARSEHASH_INCLUDE_DIR NAMES google/dense_hash_map)
81-
mark_as_advanced(HASHMAP_SPARSEHASH_INCLUDE_DIR)
82-
if(HASHMAP_SPARSEHASH_INCLUDE_DIR)
83-
target_include_directories(HashMapBenchmark SYSTEM PRIVATE "${HASHMAP_SPARSEHASH_INCLUDE_DIR}")
84-
target_compile_definitions(HashMapBenchmark PRIVATE HASHMAP_BENCHMARK_HAS_SPARSEHASH)
85-
endif()
86-
endif()
87-
if(HASHMAP_BUILD_TESTS AND BUILD_TESTING)
88-
add_test(NAME HashMapBenchmarkSmoke COMMAND HashMapBenchmark -t 4 -c 1000 -i 1000)
63+
find_path(HASHMAP_SPARSEHASH_INCLUDE_DIR NAMES google/dense_hash_map)
64+
mark_as_advanced(HASHMAP_SPARSEHASH_INCLUDE_DIR)
65+
if(HASHMAP_SPARSEHASH_INCLUDE_DIR)
66+
target_include_directories(HashMapBenchmark SYSTEM PRIVATE "${HASHMAP_SPARSEHASH_INCLUDE_DIR}")
67+
target_compile_definitions(HashMapBenchmark PRIVATE HASHMAP_BENCHMARK_HAS_SPARSEHASH)
68+
endif()
8969
endif()
70+
71+
add_test(NAME HashMapBenchmarkSmoke COMMAND HashMapBenchmark -t 4 -c 1000 -i 1000)
72+
endif()
9073
endif()
9174

92-
if(HASHMAP_INSTALL)
93-
set(HASHMAP_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/HashMap")
94-
configure_package_config_file(cmake/HashMapConfig.cmake.in
95-
"${CMAKE_CURRENT_BINARY_DIR}/HashMapConfig.cmake"
96-
INSTALL_DESTINATION "${HASHMAP_INSTALL_CMAKEDIR}")
97-
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/HashMapConfigVersion.cmake"
98-
VERSION ${PROJECT_VERSION}
99-
COMPATIBILITY SameMajorVersion
100-
ARCH_INDEPENDENT)
101-
102-
install(TARGETS HashMap EXPORT HashMapTargets)
103-
install(DIRECTORY include/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
104-
install(EXPORT HashMapTargets FILE HashMapTargets.cmake
105-
NAMESPACE HashMap:: DESTINATION "${HASHMAP_INSTALL_CMAKEDIR}")
106-
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/HashMapConfig.cmake"
107-
"${CMAKE_CURRENT_BINARY_DIR}/HashMapConfigVersion.cmake"
108-
DESTINATION "${HASHMAP_INSTALL_CMAKEDIR}")
109-
export(EXPORT HashMapTargets FILE "${CMAKE_CURRENT_BINARY_DIR}/HashMapTargets.cmake"
110-
NAMESPACE HashMap::)
75+
# -----------------------------------------------------------------------------
76+
# Installation & Package Export
77+
# -----------------------------------------------------------------------------
78+
if(PROJECT_IS_TOP_LEVEL)
79+
include(GNUInstallDirs)
80+
include(CMakePackageConfigHelpers)
81+
82+
export(
83+
TARGETS HashMap
84+
NAMESPACE HashMap::
85+
FILE HashMapConfig.cmake
86+
)
87+
88+
write_basic_package_version_file(
89+
"${CMAKE_CURRENT_BINARY_DIR}/HashMapConfigVersion.cmake"
90+
COMPATIBILITY SameMajorVersion
91+
ARCH_INDEPENDENT
92+
)
93+
94+
install(TARGETS HashMap EXPORT HashMapConfig FILE_SET HEADERS)
95+
96+
install(
97+
EXPORT HashMapConfig
98+
NAMESPACE HashMap::
99+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/HashMap"
100+
)
101+
102+
install(
103+
FILES "${CMAKE_CURRENT_BINARY_DIR}/HashMapConfigVersion.cmake"
104+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/HashMap"
105+
)
111106
endif()

‎CMakePresets.json‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"version": 3,
3+
"cmakeMinimumRequired": {
4+
"major": 3,
5+
"minor": 24,
6+
"patch": 0
7+
},
8+
"configurePresets": [
9+
{
10+
"name": "base",
11+
"hidden": true,
12+
"binaryDir": "${sourceDir}/build/${presetName}",
13+
"cacheVariables": {
14+
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
15+
}
16+
},
17+
{
18+
"name": "debug",
19+
"displayName": "Debug",
20+
"inherits": "base",
21+
"cacheVariables": {
22+
"CMAKE_BUILD_TYPE": "Debug"
23+
}
24+
},
25+
{
26+
"name": "release",
27+
"displayName": "Release",
28+
"inherits": "base",
29+
"cacheVariables": {
30+
"CMAKE_BUILD_TYPE": "Release"
31+
}
32+
},
33+
{
34+
"name": "asan",
35+
"displayName": "Debug (ASan + UBSan)",
36+
"inherits": "debug",
37+
"cacheVariables": {
38+
"CMAKE_CXX_FLAGS": "-fsanitize=address,undefined -fno-omit-frame-pointer"
39+
}
40+
},
41+
{
42+
"name": "tsan",
43+
"displayName": "Debug (TSan)",
44+
"inherits": "debug",
45+
"cacheVariables": {
46+
"CMAKE_CXX_FLAGS": "-fsanitize=thread -fno-omit-frame-pointer"
47+
}
48+
}
49+
],
50+
"buildPresets": [
51+
{
52+
"name": "base",
53+
"hidden": true
54+
},
55+
{ "name": "debug", "inherits": "base", "configurePreset": "debug" },
56+
{ "name": "release", "inherits": "base", "configurePreset": "release" },
57+
{ "name": "asan", "inherits": "base", "configurePreset": "asan" },
58+
{ "name": "tsan", "inherits": "base", "configurePreset": "tsan" }
59+
],
60+
"testPresets": [
61+
{
62+
"name": "base",
63+
"hidden": true,
64+
"output": { "outputOnFailure": true }
65+
},
66+
{ "name": "debug", "inherits": "base", "configurePreset": "debug" },
67+
{ "name": "release", "inherits": "base", "configurePreset": "release" },
68+
{ "name": "asan", "inherits": "base", "configurePreset": "asan" },
69+
{ "name": "tsan", "inherits": "base", "configurePreset": "tsan" }
70+
]
71+
}

‎README.md‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ The rest of the member functions are implemented as for
7979
## Build and install
8080
8181
The header-only `HashMap::HashMap` CMake target requires C++14. Configure with
82-
CMake 3.20 or newer:
82+
CMake 3.24 or newer:
8383
8484
```sh
8585
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
@@ -89,15 +89,8 @@ cmake --install build --prefix /path/to/install
8989
```
9090

9191
Standalone builds enable tests and examples, plus the benchmark on Linux and
92-
macOS. These are disabled by default when included with `add_subdirectory`.
93-
94-
| CMake option | Standalone default | Purpose |
95-
| --- | --- | --- |
96-
| `HASHMAP_BUILD_TESTS` | `ON` | Build tests when `BUILD_TESTING` is also enabled |
97-
| `HASHMAP_BUILD_EXAMPLES` | `ON` | Build the example |
98-
| `HASHMAP_BUILD_BENCHMARKS` | `ON` on POSIX, `OFF` on Windows | Build the benchmark |
99-
| `HASHMAP_INSTALL` | `ON` | Generate installation and package files |
100-
| `HASHMAP_WARNINGS_AS_ERRORS` | `OFF` | Treat warnings in project executables as errors |
92+
macOS. These are disabled by default when included with `add_subdirectory` or
93+
when `BUILD_TESTING=OFF`.
10194

10295
To consume an installed package:
10396

‎cmake/HashMapConfig.cmake.in‎

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)