summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2012-02-09 13:19:46 (GMT)
committerBrad King <brad.king@kitware.com>2012-02-09 13:19:46 (GMT)
commit3c6af5ff33a12b3c5603cba06e575fe62e234ce0 (patch)
treeefa679e8fc25dfd7e96bba7e31908aac11f848c2
parent59ecc2c8ac4f6e4e3e9b20a37763365060d8bb6d (diff)
parent5db99e8708c7d329620186e4cfb6e059f400dfcc (diff)
downloadCMake-3c6af5ff33a12b3c5603cba06e575fe62e234ce0.zip
CMake-3c6af5ff33a12b3c5603cba06e575fe62e234ce0.tar.gz
CMake-3c6af5ff33a12b3c5603cba06e575fe62e234ce0.tar.bz2
Merge branch 'add-CheckLanguage-module' into CMakeAddFortranSubdirectory
-rw-r--r--Modules/CheckLanguage.cmake65
-rw-r--r--Tests/CMakeOnly/CMakeLists.txt2
-rw-r--r--Tests/CMakeOnly/CheckLanguage/CMakeLists.txt22
3 files changed, 89 insertions, 0 deletions
diff --git a/Modules/CheckLanguage.cmake b/Modules/CheckLanguage.cmake
new file mode 100644
index 0000000..87a4018
--- /dev/null
+++ b/Modules/CheckLanguage.cmake
@@ -0,0 +1,65 @@
+# - Check if a language can be enabled
+# Usage:
+# check_language(<lang>)
+# where <lang> is a language that may be passed to enable_language()
+# such as "Fortran". If CMAKE_<lang>_COMPILER is already defined the
+# check does nothing. Otherwise it tries enabling the language in a
+# test project. The result is cached in CMAKE_<lang>_COMPILER as the
+# compiler that was found, or NOTFOUND if the language cannot be enabled.
+#
+# Example:
+# check_language(Fortran)
+# if(CMAKE_Fortran_COMPILER)
+# enable_language(Fortran)
+# else()
+# message(STATUS "No Fortran support")
+# endif()
+
+#=============================================================================
+# Copyright 2009-2012 Kitware, Inc.
+#
+# Distributed under the OSI-approved BSD License (the "License");
+# see accompanying file Copyright.txt for details.
+#
+# This software is distributed WITHOUT ANY WARRANTY; without even the
+# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the License for more information.
+#=============================================================================
+# (To distribute this file outside of CMake, substitute the full
+# License text for the above reference.)
+
+macro(check_language lang)
+ if(NOT DEFINED CMAKE_${lang}_COMPILER)
+ set(_desc "Looking for a ${lang} compiler")
+ message(STATUS ${_desc})
+ file(REMOVE_RECURSE ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang})
+ file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}/CMakeLists.txt"
+ "cmake_minimum_required(VERSION 2.8)
+project(Check${lang} ${lang})
+file(WRITE \"\${CMAKE_CURRENT_BINARY_DIR}/result.cmake\"
+ \"set(CMAKE_${lang}_COMPILER \\\"\${CMAKE_${lang}_COMPILER}\\\")\\n\"
+ )
+")
+ execute_process(
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}
+ COMMAND ${CMAKE_COMMAND} . -G ${CMAKE_GENERATOR}
+ OUTPUT_VARIABLE output
+ ERROR_VARIABLE output
+ RESULT_VARIABLE result
+ )
+ include(${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}/result.cmake OPTIONAL)
+ if(CMAKE_${lang}_COMPILER AND "${result}" STREQUAL "0")
+ file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
+ "${_desc} passed with the following output:\n"
+ "${output}\n")
+ else()
+ set(CMAKE_${lang}_COMPILER NOTFOUND)
+ file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
+ "${_desc} failed with the following output:\n"
+ "${output}\n")
+ endif()
+ message(STATUS "${_desc} - ${CMAKE_${lang}_COMPILER}")
+ set(CMAKE_${lang}_COMPILER "${CMAKE_${lang}_COMPILER}" CACHE FILEPATH "${lang} compiler")
+ mark_as_advanced(CMAKE_${lang}_COMPILER)
+ endif()
+endmacro()
diff --git a/Tests/CMakeOnly/CMakeLists.txt b/Tests/CMakeOnly/CMakeLists.txt
index 33c426a..4407efb 100644
--- a/Tests/CMakeOnly/CMakeLists.txt
+++ b/Tests/CMakeOnly/CMakeLists.txt
@@ -17,6 +17,8 @@ add_CMakeOnly_test(CheckCXXSymbolExists)
add_CMakeOnly_test(CheckCXXCompilerFlag)
+add_CMakeOnly_test(CheckLanguage)
+
add_CMakeOnly_test(AllFindModules)
add_CMakeOnly_test(TargetScope)
diff --git a/Tests/CMakeOnly/CheckLanguage/CMakeLists.txt b/Tests/CMakeOnly/CheckLanguage/CMakeLists.txt
new file mode 100644
index 0000000..f5336dc
--- /dev/null
+++ b/Tests/CMakeOnly/CheckLanguage/CMakeLists.txt
@@ -0,0 +1,22 @@
+cmake_minimum_required (VERSION 2.8)
+project(CheckLanguage NONE)
+include(CheckLanguage)
+
+set(langs )
+set(expect_C 1)
+set(expect_CXX 1)
+unset(expect_Fortran)
+set(expect_NoSuchLanguage 0)
+foreach(lang C CXX Fortran NoSuchLanguage)
+ check_language(${lang})
+ if(NOT DEFINED CMAKE_${lang}_COMPILER)
+ message(FATAL_ERROR "check_language(${lang}) did not set result")
+ endif()
+ if(DEFINED expect_${lang})
+ if(expect_${lang} AND NOT CMAKE_${lang}_COMPILER)
+ message(FATAL_ERROR "check_language(${lang}) should not fail!")
+ elseif(NOT expect_${lang} AND CMAKE_${lang}_COMPILER)
+ message(FATAL_ERROR "check_language(${lang}) should not succeed!")
+ endif()
+ endif()
+endforeach()