a21e65f265e0647c88192ea7b601b01e77ab059f
[platform/upstream/cmake.git] / Modules / CheckVariableExists.cmake
1 # - Check if the variable exists.
2 #  CHECK_VARIABLE_EXISTS(VAR VARIABLE)
3 #
4 #  VAR      - the name of the variable
5 #  VARIABLE - variable to store the result
6 #
7 # This macro is only for C variables.
8 #
9 # The following variables may be set before calling this macro to
10 # modify the way the check is run:
11 #
12 #  CMAKE_REQUIRED_FLAGS = string of compile command line flags
13 #  CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
14 #  CMAKE_REQUIRED_LIBRARIES = list of libraries to link
15
16 #=============================================================================
17 # Copyright 2002-2009 Kitware, Inc.
18 #
19 # Distributed under the OSI-approved BSD License (the "License");
20 # see accompanying file Copyright.txt for details.
21 #
22 # This software is distributed WITHOUT ANY WARRANTY; without even the
23 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 # See the License for more information.
25 #=============================================================================
26 # (To distribute this file outside of CMake, substitute the full
27 #  License text for the above reference.)
28
29
30
31 macro(CHECK_VARIABLE_EXISTS VAR VARIABLE)
32   if("${VARIABLE}" MATCHES "^${VARIABLE}$")
33     set(MACRO_CHECK_VARIABLE_DEFINITIONS
34       "-DCHECK_VARIABLE_EXISTS=${VAR} ${CMAKE_REQUIRED_FLAGS}")
35     message(STATUS "Looking for ${VAR}")
36     if(CMAKE_REQUIRED_LIBRARIES)
37       set(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES
38         LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
39     else()
40       set(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES)
41     endif()
42     try_compile(${VARIABLE}
43       ${CMAKE_BINARY_DIR}
44       ${CMAKE_ROOT}/Modules/CheckVariableExists.c
45       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
46       ${CHECK_VARIABLE_EXISTS_ADD_LIBRARIES}
47       CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_VARIABLE_DEFINITIONS}
48       OUTPUT_VARIABLE OUTPUT)
49     if(${VARIABLE})
50       set(${VARIABLE} 1 CACHE INTERNAL "Have variable ${VAR}")
51       message(STATUS "Looking for ${VAR} - found")
52       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
53         "Determining if the variable ${VAR} exists passed with the following output:\n"
54         "${OUTPUT}\n\n")
55     else()
56       set(${VARIABLE} "" CACHE INTERNAL "Have variable ${VAR}")
57       message(STATUS "Looking for ${VAR} - not found")
58       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
59         "Determining if the variable ${VAR} exists failed with the following output:\n"
60         "${OUTPUT}\n\n")
61     endif()
62   endif()
63 endmacro()