Skip to content

Commit dc847ca

Browse files
committed
build: CheckCacheVarValues CMake module file added
1 parent 45a19c8 commit dc847ca

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

src/CMakeLists.txt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ set(projectPrefix MP_UNITS_)
2727

2828
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
2929

30-
include(CheckCXXFeatureSupported)
3130
include(AddMPUnitsModule)
31+
include(CheckCacheVarValues)
32+
include(CheckCXXFeatureSupported)
3233
include(GNUInstallDirs)
3334

3435
# check if libc++ is being used
@@ -77,21 +78,12 @@ option(${projectPrefix}API_NO_CRTP "Enable class definitions without CRTP idiom"
7778
)
7879
option(${projectPrefix}API_FREESTANDING "Builds only freestanding part of the library" OFF)
7980
set(${projectPrefix}API_CONTRACTS GSL-LITE CACHE STRING "Enable contract checking")
81+
check_cache_var_values(API_CONTRACTS NONE GSL-LITE MS-GSL)
8082

8183
message(STATUS "${projectPrefix}API_STD_FORMAT: ${${projectPrefix}API_STD_FORMAT}")
8284
message(STATUS "${projectPrefix}API_NO_CRTP: ${${projectPrefix}API_NO_CRTP}")
8385
message(STATUS "${projectPrefix}API_FREESTANDING: ${${projectPrefix}API_FREESTANDING}")
84-
85-
function(cache_var_values name)
86-
set_property(CACHE ${projectPrefix}${name} PROPERTY STRINGS ${ARGN})
87-
if(NOT ${projectPrefix}${name} IN_LIST ARGN)
88-
message(FATAL_ERROR
89-
"Invalid value '${${projectPrefix}${name}}' provided for a cache variable ${projectPrefix}${name} (${ARGN} allowed)"
90-
)
91-
endif()
92-
message(STATUS "${projectPrefix}${name}: ${${projectPrefix}${name}}")
93-
endfunction()
94-
cache_var_values(API_CONTRACTS NONE GSL-LITE MS-GSL)
86+
message(STATUS "${projectPrefix}API_CONTRACTS: ${${projectPrefix}API_CONTRACTS}")
9587

9688
# validate options
9789
if(${projectPrefix}API_FREESTANDING AND NOT ${projectPrefix}API_CONTRACTS STREQUAL "NONE")

src/cmake/CheckCacheVarValues.cmake

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2018 Mateusz Pusz
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
cmake_minimum_required(VERSION 3.25)
24+
25+
function(check_cache_var_values name)
26+
set_property(CACHE ${projectPrefix}${name} PROPERTY STRINGS ${ARGN})
27+
if(NOT ${projectPrefix}${name} IN_LIST ARGN)
28+
message(FATAL_ERROR
29+
"Invalid value '${${projectPrefix}${name}}' provided for a cache variable ${projectPrefix}${name} (${ARGN} allowed)"
30+
)
31+
endif()
32+
endfunction()

0 commit comments

Comments
 (0)