Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Modules / CMakeFindPackageMode.cmake
1 # This file is executed by cmake when invoked with --find-package.
2 # It expects that the following variables are set using -D:
3 #   NAME = name of the package
4 #   COMPILER_ID = the CMake compiler ID for which the result is, i.e. GNU/Intel/Clang/MSVC, etc.
5 #   LANGUAGE = language for which the result will be used, i.e. C/CXX/Fortan/ASM
6 #   MODE = EXIST : only check for existance of the given package
7 #          COMPILE : print the flags needed for compiling an object file which uses the given package
8 #          LINK : print the flags needed for linking when using the given package
9 #   QUIET = if TRUE, don't print anything
10
11 #=============================================================================
12 # Copyright 2006-2011 Alexander Neundorf, <neundorf@kde.org>
13 #
14 # Distributed under the OSI-approved BSD License (the "License");
15 # see accompanying file Copyright.txt for details.
16 #
17 # This software is distributed WITHOUT ANY WARRANTY; without even the
18 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 # See the License for more information.
20 #=============================================================================
21 # (To distribute this file outside of CMake, substitute the full
22 #  License text for the above reference.)
23
24 if(NOT NAME)
25   message(FATAL_ERROR "Name of the package to be searched not specified. Set the CMake variable NAME, e.g. -DNAME=JPEG .")
26 endif()
27
28 if(NOT COMPILER_ID)
29   message(FATAL_ERROR "COMPILER_ID argument not specified. In doubt, use GNU.")
30 endif()
31
32 if(NOT LANGUAGE)
33   message(FATAL_ERROR "LANGUAGE argument not specified. Use C, CXX or Fortran.")
34 endif()
35
36 if(NOT MODE)
37   message(FATAL_ERROR "MODE argument not specified. Use either EXIST, COMPILE or LINK.")
38 endif()
39
40 # require the current version. If we don't do this, Platforms/CYGWIN.cmake complains because
41 # it doesn't know whether it should set WIN32 or not:
42 cmake_minimum_required(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION} )
43
44 macro(ENABLE_LANGUAGE)
45   # disable the enable_language() command, otherwise --find-package breaks on Windows.
46   # On Windows, enable_language(RC) is called in the platform files unconditionally.
47   # But in --find-package mode, we don't want (and can't) enable any language.
48 endmacro()
49
50 set(CMAKE_PLATFORM_INFO_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY})
51
52 include(CMakeDetermineSystem)
53
54 # short-cut some tests on Darwin, see Darwin-GNU.cmake:
55 if("${CMAKE_SYSTEM_NAME}" MATCHES Darwin  AND  "${COMPILER_ID}" MATCHES GNU)
56   set(CMAKE_${LANGUAGE}_SYSROOT_FLAG "")
57   set(CMAKE_${LANGUAGE}_OSX_DEPLOYMENT_TARGET_FLAG "")
58 endif()
59
60 # Also load the system specific file, which sets up e.g. the search paths.
61 # This makes the FIND_XXX() calls work much better
62 include(CMakeSystemSpecificInformation)
63
64 if(UNIX)
65
66   # try to guess whether we have a 64bit system, if it has not been set
67   # from the outside
68   if(NOT CMAKE_SIZEOF_VOID_P)
69     set(CMAKE_SIZEOF_VOID_P 4)
70     if(EXISTS /usr/lib64)
71       set(CMAKE_SIZEOF_VOID_P 8)
72     else()
73       # use the file utility to check whether itself is 64 bit:
74       find_program(FILE_EXECUTABLE file)
75       if(FILE_EXECUTABLE)
76         get_filename_component(FILE_ABSPATH "${FILE_EXECUTABLE}" ABSOLUTE)
77         execute_process(COMMAND "${FILE_ABSPATH}" "${FILE_ABSPATH}" OUTPUT_VARIABLE fileOutput ERROR_QUIET)
78         if("${fileOutput}" MATCHES "64-bit")
79           set(CMAKE_SIZEOF_VOID_P 8)
80         endif()
81       endif()
82     endif()
83   endif()
84
85   # guess Debian multiarch if it has not been set:
86   if(EXISTS /etc/debian_version)
87     if(NOT CMAKE_${LANGUAGE}_LANGUAGE_ARCHITECTURE )
88       file(GLOB filesInLib RELATIVE /lib /lib/*-linux-gnu* )
89       foreach(file ${filesInLib})
90         if("${file}" MATCHES "${CMAKE_LIBRARY_ARCHITECTURE_REGEX}")
91           set(CMAKE_${LANGUAGE}_LANGUAGE_ARCHITECTURE ${file})
92           break()
93         endif()
94       endforeach()
95     endif()
96   endif()
97
98 endif()
99
100 set(CMAKE_${LANGUAGE}_COMPILER "dummy")
101 set(CMAKE_${LANGUAGE}_COMPILER_ID "${COMPILER_ID}")
102 include(CMake${LANGUAGE}Information)
103
104
105 function(set_compile_flags_var _packageName)
106   string(TOUPPER "${_packageName}" PACKAGE_NAME)
107   # Check the following variables:
108   # FOO_INCLUDE_DIRS
109   # Foo_INCLUDE_DIRS
110   # FOO_INCLUDES
111   # Foo_INCLUDES
112   # FOO_INCLUDE_DIR
113   # Foo_INCLUDE_DIR
114   set(includes)
115   if(DEFINED ${_packageName}_INCLUDE_DIRS)
116     set(includes ${_packageName}_INCLUDE_DIRS)
117   elseif(DEFINED ${PACKAGE_NAME}_INCLUDE_DIRS)
118     set(includes ${PACKAGE_NAME}_INCLUDE_DIRS)
119   elseif(DEFINED ${_packageName}_INCLUDES)
120     set(includes ${_packageName}_INCLUDES)
121   elseif(DEFINED ${PACKAGE_NAME}_INCLUDES)
122     set(includes ${PACKAGE_NAME}_INCLUDES)
123   elseif(DEFINED ${_packageName}_INCLUDE_DIR)
124     set(includes ${_packageName}_INCLUDE_DIR)
125   elseif(DEFINED ${PACKAGE_NAME}_INCLUDE_DIR)
126     set(includes ${PACKAGE_NAME}_INCLUDE_DIR)
127   endif()
128
129   set(PACKAGE_INCLUDE_DIRS "${${includes}}" PARENT_SCOPE)
130
131   # Check the following variables:
132   # FOO_DEFINITIONS
133   # Foo_DEFINITIONS
134   set(definitions)
135   if(DEFINED ${_packageName}_DEFINITIONS)
136     set(definitions ${_packageName}_DEFINITIONS)
137   elseif(DEFINED ${PACKAGE_NAME}_DEFINITIONS)
138     set(definitions ${PACKAGE_NAME}_DEFINITIONS)
139   endif()
140
141   set(PACKAGE_DEFINITIONS  "${${definitions}}" )
142
143 endfunction()
144
145
146 function(set_link_flags_var _packageName)
147   string(TOUPPER "${_packageName}" PACKAGE_NAME)
148   # Check the following variables:
149   # FOO_LIBRARIES
150   # Foo_LIBRARIES
151   # FOO_LIBS
152   # Foo_LIBS
153   set(libs)
154   if(DEFINED ${_packageName}_LIBRARIES)
155     set(libs ${_packageName}_LIBRARIES)
156   elseif(DEFINED ${PACKAGE_NAME}_LIBRARIES)
157     set(libs ${PACKAGE_NAME}_LIBRARIES)
158   elseif(DEFINED ${_packageName}_LIBS)
159     set(libs ${_packageName}_LIBS)
160   elseif(DEFINED ${PACKAGE_NAME}_LIBS)
161     set(libs ${PACKAGE_NAME}_LIBS)
162   endif()
163
164   set(PACKAGE_LIBRARIES "${${libs}}" PARENT_SCOPE )
165
166 endfunction()
167
168
169 find_package("${NAME}" QUIET)
170
171 set(PACKAGE_FOUND FALSE)
172
173 string(TOUPPER "${NAME}" UPPERCASE_NAME)
174
175 if(${NAME}_FOUND  OR  ${UPPERCASE_NAME}_FOUND)
176   set(PACKAGE_FOUND TRUE)
177
178   if("${MODE}" STREQUAL "EXIST")
179     # do nothing
180   elseif("${MODE}" STREQUAL "COMPILE")
181     set_compile_flags_var(${NAME})
182   elseif("${MODE}" STREQUAL "LINK")
183     set_link_flags_var(${NAME})
184   else()
185     message(FATAL_ERROR "Invalid mode argument ${MODE} given.")
186   endif()
187
188 endif()
189
190 set(PACKAGE_QUIET ${SILENT} )