| 1 |
# @HEADER |
| 2 |
# ***************************************************************************** |
| 3 |
# TriBITS: Tribal Build, Integrate, and Test System |
| 4 |
# |
| 5 |
# Copyright 2013-2016 NTESS and the TriBITS contributors. |
| 6 |
# SPDX-License-Identifier: BSD-3-Clause |
| 7 |
# ***************************************************************************** |
| 8 |
# @HEADER |
| 9 |
|
| 10 |
|
| 11 |
# @FUNCTION: assert_defined() |
| 12 |
# |
| 13 |
# Assert that a variable is defined and if not call ``message(SEND_ERROR |
| 14 |
# ...)``. |
| 15 |
# |
| 16 |
# Usage:: |
| 17 |
# |
| 18 |
# assert_defined(<varName>) |
| 19 |
# |
| 20 |
# This is used to get around the problem of CMake not asserting the |
| 21 |
# dereferencing of undefined variables. For example, how does one know if one |
| 22 |
# did not misspell the name of a variable in an if statement like:: |
| 23 |
# |
| 24 |
# if (SOME_VARBLE) |
| 25 |
# ... |
| 26 |
# endif() |
| 27 |
# |
| 28 |
# ? |
| 29 |
# |
| 30 |
# If one misspelled the variable ``SOME_VARBLE`` (which is likely in this |
| 31 |
# case), then the if statement will always be false! To avoid this problem |
| 32 |
# when one always expects that a variable is explicitly set, instead do:: |
| 33 |
# |
| 34 |
# assert_defined(SOME_VARBLE) |
| 35 |
# if (SOME_VARBLE) |
| 36 |
# ... |
| 37 |
# endif() |
| 38 |
# |
| 39 |
# Now if one misspells this variable, then CMake will asset and stop |
| 40 |
# processing. This is not a perfect solution since one can misspell the |
| 41 |
# variable name in the following if statement but typically one would always |
| 42 |
# just copy and paste between the two statements so these names are always the |
| 43 |
# same. This is the best that can be done in CMake unfortunately to catch |
| 44 |
# usage of misspelled undefined variables. |
| 45 |
# |
| 46 |
function(assert_defined VARS) |
| 47 |
foreach(VAR ${VARS}) |
| 48 |
if(NOT DEFINED ${VAR}) |
| 49 |
message(SEND_ERROR "Error, the variable ${VAR} is not defined!") |
| 50 |
endif() |
| 51 |
endforeach() |
| 52 |
endfunction() |
| 53 |
|
| 54 |
# ToDo: The VARS arg This really needs to be replaced with ${ARGV}. I fear |
| 55 |
# that only the first arg passed in is asserted. However, to change this now |
| 56 |
# is breaking backward compatibility. |