Skip to content

Commit 888e172

Browse files
committed
refactor: maintain sanitizers over vcpkg feature
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
1 parent 1a9711b commit 888e172

File tree

2 files changed

+124
-57
lines changed

2 files changed

+124
-57
lines changed

CMakeLists.txt

Lines changed: 99 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,97 +6,140 @@
66

77
cmake_minimum_required(VERSION 3.12)
88

9-
option(JAM_COMPATIBLE "Build compatible with JAM-codec" OFF)
10-
option(CUSTOM_CONFIG_SUPPORT "Support custom config of coder" OFF)
11-
option(BUILD_TESTS "Whether to include the test suite in build" OFF)
9+
# Select package manager
10+
if(PACKAGE_MANAGER)
11+
if(PACKAGE_MANAGER NOT MATCHES "^(hunter|vcpkg)$")
12+
message(FATAL_ERROR "PACKAGE_MANAGER must be set to 'hunter', 'vcpkg' or isn't set")
13+
endif()
14+
else()
15+
set(PACKAGE_MANAGER "hunter")
16+
if(CMAKE_TOOLCHAIN_FILE)
17+
get_filename_component(ACTUAL_NAME ${CMAKE_TOOLCHAIN_FILE} NAME)
18+
if(ACTUAL_NAME STREQUAL "vcpkg.cmake")
19+
message(STATUS "vcpkg will be used because vcpkg.cmake has found")
20+
set(PACKAGE_MANAGER "vcpkg")
21+
endif()
22+
endif()
23+
endif()
24+
message(STATUS "Selected package manager: ${PACKAGE_MANAGER}")
1225

13-
option(SCALE_ASAN "Build with address sanitizer" OFF)
14-
option(SCALE_UBSAN "Build with undefined behavior sanitizer" OFF)
15-
option(SCALE_TSAN "Build with thread sanitizer" OFF)
26+
# Default values of options
27+
set(JAM_COMPATIBLE_DEFAULT OFF)
28+
set(CUSTOM_CONFIG_SUPPORT_DEFAULT OFF)
29+
set(BUILD_TESTS_DEFAULT OFF)
30+
set(ASAN_DEFAULT OFF)
31+
set(TSAN_DEFAULT OFF)
32+
set(UBSAN_DEFAULT OFF)
33+
34+
# Adjust value by vcpkg manifest if any
35+
if(PACKAGE_MANAGER STREQUAL "vcpkg")
36+
if("jam-compatibility" IN_LIST VCPKG_MANIFEST_FEATURES)
37+
set(JAM_COMPATIBLE_DEFAULT ON)
38+
endif()
39+
if("configurable-coding" IN_LIST VCPKG_MANIFEST_FEATURES)
40+
set(CUSTOM_CONFIG_SUPPORT_DEFAULT ON)
41+
endif()
42+
if("scale-tests" IN_LIST VCPKG_MANIFEST_FEATURES)
43+
set(BUILD_TESTS_DEFAULT ON)
44+
endif()
45+
if("asan" IN_LIST VCPKG_MANIFEST_FEATURES)
46+
set(ASAN_DEFAULT ON)
47+
endif()
48+
if("tsan" IN_LIST VCPKG_MANIFEST_FEATURES)
49+
set(TSAN_DEFAULT ON)
50+
endif()
51+
if("ubsan" IN_LIST VCPKG_MANIFEST_FEATURES)
52+
set(UBSAN_DEFAULT ON)
53+
endif()
54+
endif()
1655

17-
set(MAX_AGGREGATE_FIELDS 20 CACHE STRING "Max number of aggregates fields (1..1000); for generation")
56+
# Init options
57+
option(JAM_COMPATIBLE "Build compatible with JAM-codec" ${JAM_COMPATIBLE_DEFAULT})
58+
option(CUSTOM_CONFIG_SUPPORT "Support custom config of coder" ${CUSTOM_CONFIG_SUPPORT_DEFAULT})
59+
option(BUILD_TESTS "Whether to include the test suite in build" ${BUILD_TESTS_DEFAULT})
60+
option(ASAN "Build tests with address sanitizer" ${ASAN_DEFAUL})
61+
option(TSAN "Build tests with thread sanitizer" ${TSAN_DEFAUL})
62+
option(UBSAN "Build tests with undefined behavior sanitizer" ${UBSAN_DEFAUL})
63+
if(ASAN OR TSAN OR UBSAN)
64+
if(NOT BUILD_TESTS)
65+
message(FATAL_ERROR "Sanitizer required but build of test is not set")
66+
endif()
67+
endif()
1868

19-
if (PACKAGE_MANAGER)
20-
if(PACKAGE_MANAGER NOT MATCHES "^(hunter|vcpkg)$")
21-
message(FATAL_ERROR "PACKAGE_MANAGER must be set to 'hunter', 'vcpkg' or isn't set")
22-
endif ()
23-
else ()
24-
set(PACKAGE_MANAGER "hunter")
25-
if (CMAKE_TOOLCHAIN_FILE)
26-
get_filename_component(ACTUAL_NAME ${CMAKE_TOOLCHAIN_FILE} NAME)
27-
if(ACTUAL_NAME STREQUAL "vcpkg.cmake")
28-
message(STATUS "vcpkg will be used because vcpkg.cmake has found")
29-
set(PACKAGE_MANAGER "vcpkg")
30-
endif ()
31-
endif ()
32-
endif ()
33-
message(STATUS "Selected package manager: ${PACKAGE_MANAGER}")
69+
# Adjust vcpkg features by custom defined option (for deploy possible dependencies)
70+
if(PACKAGE_MANAGER STREQUAL "vcpkg")
71+
if(BUILD_TESTS AND NOT "scale-tests" IN_LIST VCPKG_MANIFEST_FEATURES)
72+
list(APPEND VCPKG_MANIFEST_FEATURES "scale-tests")
73+
endif()
74+
endif()
3475

35-
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.27")
36-
cmake_policy(SET CMP0144 NEW)
37-
endif ()
76+
set(MAX_AGGREGATE_FIELDS 20 CACHE STRING "Max number of aggregates fields (1..1000); for generation")
3877

39-
if (PACKAGE_MANAGER STREQUAL "hunter")
40-
include("cmake/Hunter/init.cmake")
41-
endif ()
78+
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.27")
79+
cmake_policy(SET CMP0144 NEW)
80+
endif()
4281

43-
if(BUILD_TESTS)
44-
if (PACKAGE_MANAGER STREQUAL "vcpkg")
45-
list(APPEND VCPKG_MANIFEST_FEATURES scale-tests)
46-
endif()
82+
if(PACKAGE_MANAGER STREQUAL "hunter")
83+
include("cmake/Hunter/init.cmake")
84+
else()
85+
set(HUNTER_ENABLED OFF)
4786
endif()
4887

49-
project(Scale LANGUAGES CXX VERSION 2.0.0)
88+
project(Scale LANGUAGES CXX VERSION 2.0.1)
5089

5190
set(CMAKE_CXX_STANDARD 20)
5291
set(CMAKE_CXX_STANDARD_REQUIRED ON)
5392
set(CMAKE_CXX_EXTENSIONS OFF)
5493

5594
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
5695

57-
if (PACKAGE_MANAGER STREQUAL "hunter")
58-
hunter_add_package(Boost)
59-
find_package(Boost)
96+
if(PACKAGE_MANAGER STREQUAL "hunter")
97+
hunter_add_package(Boost)
98+
find_package(Boost)
6099
else()
61-
find_package(Boost CONFIG REQUIRED COMPONENTS endian multiprecision)
62-
endif ()
100+
find_package(Boost CONFIG REQUIRED COMPONENTS endian multiprecision)
101+
endif()
63102

64-
if (PACKAGE_MANAGER STREQUAL "hunter")
65-
hunter_add_package(qtils)
66-
endif ()
103+
if(PACKAGE_MANAGER STREQUAL "hunter")
104+
hunter_add_package(qtils)
105+
endif()
67106
find_package(qtils CONFIG REQUIRED)
68107

69-
SET(JAM_COMPATIBILITY_ENABLED "${JAM_COMPATIBLE}")
108+
set(JAM_COMPATIBILITY_ENABLED "${JAM_COMPATIBLE}")
70109
set(CUSTOM_CONFIG_ENABLED "${CUSTOM_CONFIG_SUPPORT}")
71110
configure_file("${CMAKE_SOURCE_DIR}/include/scale/definitions.hpp.in" "${CMAKE_BINARY_DIR}/include/scale/definitions.hpp")
72111

73-
if (SCALE_ASAN)
74-
add_compile_options(-fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer)
75-
add_link_options(-fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer)
112+
if(ASAN)
113+
message(STATUS "Address sanitizer will be used")
114+
add_compile_options(-fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer)
115+
add_link_options(-fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer)
76116
endif()
77-
if (SCALE_UBSAN)
78-
add_compile_options(-fsanitize=undefined -fno-omit-frame-pointer)
79-
add_link_options(-fsanitize=undefined -fno-omit-frame-pointer)
117+
if(TSAN)
118+
message(STATUS "Thread sanitizer will be used")
119+
add_compile_options(-fsanitize=thread -fno-omit-frame-pointer)
120+
add_link_options(-fsanitize=thread -fno-omit-frame-pointer)
80121
endif()
81-
if (SCALE_TSAN)
82-
add_compile_options(-fsanitize=thread -fno-omit-frame-pointer)
83-
add_link_options(-fsanitize=thread -fno-omit-frame-pointer)
122+
if(UBSAN)
123+
message(STATUS "Undefined behavior sanitizer will be used")
124+
add_compile_options(-fsanitize=undefined -fno-omit-frame-pointer)
125+
add_link_options(-fsanitize=undefined -fno-omit-frame-pointer)
84126
endif()
85127

86128
add_subdirectory(src)
87129

88-
if (BUILD_TESTS)
89-
enable_testing()
90-
add_subdirectory(test ${CMAKE_BINARY_DIR}/test_bin)
91-
endif ()
130+
if(BUILD_TESTS)
131+
enable_testing()
132+
add_subdirectory(test ${CMAKE_BINARY_DIR}/test_bin)
133+
endif()
92134

93135
###############################################################################
94136
# INSTALLATION
95137
###############################################################################
96138

97139
include(GNUInstallDirs)
98140

99-
install(TARGETS scale EXPORT scaleConfig
141+
install(
142+
TARGETS scale EXPORT scaleConfig
100143
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
101144
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
102145
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}

vcpkg.json

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
{
22
"name": "scale",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"dependencies": [
55
"qtils",
66
"boost-multiprecision",
77
"boost-endian"
88
],
99
"features": {
10+
"jam-compatibility": {
11+
"description": "Encoding/decoding compatible with JAM"
12+
},
13+
"configurable-coding": {
14+
"description": "Enable configurable encoding/decoding"
15+
},
1016
"scale-tests": {
1117
"description": "Test of scale encoding/decoding",
1218
"dependencies": [
1319
"gtest"
1420
]
21+
},
22+
"asan": {
23+
"description": "Test of scale encoding/decoding with address sanitizer",
24+
"dependencies": [
25+
"scale-tests"
26+
]
27+
},
28+
"ubsan": {
29+
"description": "Test of scale encoding/decoding with undefined behavior sanitizer",
30+
"dependencies": [
31+
"scale-tests"
32+
]
33+
},
34+
"tsan": {
35+
"description": "Test of scale encoding/decoding with threads sanitizer",
36+
"dependencies": [
37+
"scale-tests"
38+
]
1539
}
1640
}
1741
}

0 commit comments

Comments
 (0)