CMake/Modules/CMakeDetermineCompiler.cmake

116 lines
4.8 KiB
CMake
Raw Normal View History

#=============================================================================
# Copyright 2004-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(_cmake_find_compiler lang)
# Use already-enabled languages for reference.
get_property(_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
list(REMOVE_ITEM _languages "${lang}")
if(CMAKE_${lang}_COMPILER_INIT)
# Search only for the specified compiler.
set(CMAKE_${lang}_COMPILER_LIST "${CMAKE_${lang}_COMPILER_INIT}")
else()
# Re-order the compiler list with preferred vendors first.
set(_${lang}_COMPILER_LIST "${CMAKE_${lang}_COMPILER_LIST}")
set(CMAKE_${lang}_COMPILER_LIST "")
# Prefer vendors of compilers from reference languages.
foreach(l ${_languages})
list(APPEND CMAKE_${lang}_COMPILER_LIST
${_${lang}_COMPILER_NAMES_${CMAKE_${l}_COMPILER_ID}})
endforeach()
Prefer generic system compilers by default for C, C++, and Fortran Teach CMake to prefer the system default compiler automatically when no compiler is specified. By default use "cc" for C, "CC" for C++, and "f95" for Fortran. Load a new Platform/<os>-<lang>.cmake module to allow each platform to specify for each language its system compiler name(s) and/or exclude certain names. Create Platform/(CYGWIN|Darwin|Linux|Windows)-CXX.cmake modules to specify "c++" as the system C++ compiler name for these platforms. On systems that use case-insensitive filesystems exclude C++ compiler names that are distinguished from C compiler names only by case. This will change the default compiler selection for existing build scripts that do not specify a compiler when run on machines with separate system and GNU compilers both installed in the PATH. We do not make this change in default behavior lightly. However: (1) If a given build really needs specific compilers one should specify them explicitly e.g. by setting CC, CXX, and FC in the environment. (2) The motivating case is to prefer the system Clang on newer OS X systems over the older GNU compilers typically also installed. On such systems the names "cc" and "c++" link to Clang. This is the first platform known to CMake on which "c++" is not a GNU compiler. The old behavior selected "gcc" for C and "c++" C++ and therefore chooses GNU for C and Clang for C++ by default. The new behavior selects GNU or Clang consistently for both languages on older or newer OS X systems, respectively. (3) Other than the motivating OS X case the conditions under which the behavior changes do not tend to exist in default OS installations. They typically occur only on non-GNU systems with manually-installed GNU compilers. (4) The consequences of the new behavior are not dire. At worst the project fails to compile with the system compiler when it previously worked with the non-system GNU compiler. Such failure is easy to work around (see #1). In short this change creates a more sensible default behavior everywhere and fixes poor default behavior on a widely-used platform at the cost of a modest change in behavior in less-common conditions.
2012-08-02 19:53:25 +04:00
# Prefer vendors based on the platform.
list(APPEND CMAKE_${lang}_COMPILER_LIST ${CMAKE_${lang}_COMPILER_NAMES})
# Append the rest of the list and remove duplicates.
list(APPEND CMAKE_${lang}_COMPILER_LIST ${_${lang}_COMPILER_LIST})
unset(_${lang}_COMPILER_LIST)
list(REMOVE_DUPLICATES CMAKE_${lang}_COMPILER_LIST)
Prefer generic system compilers by default for C, C++, and Fortran Teach CMake to prefer the system default compiler automatically when no compiler is specified. By default use "cc" for C, "CC" for C++, and "f95" for Fortran. Load a new Platform/<os>-<lang>.cmake module to allow each platform to specify for each language its system compiler name(s) and/or exclude certain names. Create Platform/(CYGWIN|Darwin|Linux|Windows)-CXX.cmake modules to specify "c++" as the system C++ compiler name for these platforms. On systems that use case-insensitive filesystems exclude C++ compiler names that are distinguished from C compiler names only by case. This will change the default compiler selection for existing build scripts that do not specify a compiler when run on machines with separate system and GNU compilers both installed in the PATH. We do not make this change in default behavior lightly. However: (1) If a given build really needs specific compilers one should specify them explicitly e.g. by setting CC, CXX, and FC in the environment. (2) The motivating case is to prefer the system Clang on newer OS X systems over the older GNU compilers typically also installed. On such systems the names "cc" and "c++" link to Clang. This is the first platform known to CMake on which "c++" is not a GNU compiler. The old behavior selected "gcc" for C and "c++" C++ and therefore chooses GNU for C and Clang for C++ by default. The new behavior selects GNU or Clang consistently for both languages on older or newer OS X systems, respectively. (3) Other than the motivating OS X case the conditions under which the behavior changes do not tend to exist in default OS installations. They typically occur only on non-GNU systems with manually-installed GNU compilers. (4) The consequences of the new behavior are not dire. At worst the project fails to compile with the system compiler when it previously worked with the non-system GNU compiler. Such failure is easy to work around (see #1). In short this change creates a more sensible default behavior everywhere and fixes poor default behavior on a widely-used platform at the cost of a modest change in behavior in less-common conditions.
2012-08-02 19:53:25 +04:00
if(CMAKE_${lang}_COMPILER_EXCLUDE)
list(REMOVE_ITEM CMAKE_${lang}_COMPILER_LIST
${CMAKE_${lang}_COMPILER_EXCLUDE})
endif()
endif()
# Look for directories containing compilers of reference languages.
set(_${lang}_COMPILER_HINTS)
foreach(l ${_languages})
if(CMAKE_${l}_COMPILER AND IS_ABSOLUTE "${CMAKE_${l}_COMPILER}")
get_filename_component(_hint "${CMAKE_${l}_COMPILER}" PATH)
if(IS_DIRECTORY "${_hint}")
list(APPEND _${lang}_COMPILER_HINTS "${_hint}")
endif()
unset(_hint)
endif()
endforeach()
# Find the compiler.
if(_${lang}_COMPILER_HINTS)
# Prefer directories containing compilers of reference languages.
list(REMOVE_DUPLICATES _${lang}_COMPILER_HINTS)
find_program(CMAKE_${lang}_COMPILER
NAMES ${CMAKE_${lang}_COMPILER_LIST}
PATHS ${_${lang}_COMPILER_HINTS}
NO_DEFAULT_PATH
DOC "${lang} compiler")
endif()
find_program(CMAKE_${lang}_COMPILER NAMES ${CMAKE_${lang}_COMPILER_LIST} DOC "${lang} compiler")
if(CMAKE_${lang}_COMPILER_INIT AND NOT CMAKE_${lang}_COMPILER)
set_property(CACHE CMAKE_${lang}_COMPILER PROPERTY VALUE "${CMAKE_${lang}_COMPILER_INIT}")
endif()
unset(_${lang}_COMPILER_HINTS)
unset(_languages)
# Look for a make tool provided by Xcode
if(CMAKE_${lang}_COMPILER STREQUAL "CMAKE_${lang}_COMPILER-NOTFOUND" AND CMAKE_HOST_APPLE)
foreach(comp ${CMAKE_${lang}_COMPILER_LIST})
execute_process(COMMAND xcrun --find ${comp}
OUTPUT_VARIABLE _xcrun_out OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE _xcrun_err)
if(_xcrun_out)
set_property(CACHE CMAKE_${lang}_COMPILER PROPERTY VALUE "${_xcrun_out}")
break()
endif()
endforeach()
endif()
endmacro()
macro(_cmake_find_compiler_path lang)
if(CMAKE_${lang}_COMPILER)
# we only get here if CMAKE_${lang}_COMPILER was specified using -D or a pre-made CMakeCache.txt
# (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
# if CMAKE_${lang}_COMPILER is a list of length 2, use the first item as
# CMAKE_${lang}_COMPILER and the 2nd one as CMAKE_${lang}_COMPILER_ARG1
list(LENGTH CMAKE_${lang}_COMPILER _CMAKE_${lang}_COMPILER_LIST_LENGTH)
if("${_CMAKE_${lang}_COMPILER_LIST_LENGTH}" EQUAL 2)
list(GET CMAKE_${lang}_COMPILER 1 CMAKE_${lang}_COMPILER_ARG1)
list(GET CMAKE_${lang}_COMPILER 0 CMAKE_${lang}_COMPILER)
endif()
unset(_CMAKE_${lang}_COMPILER_LIST_LENGTH)
# find the compiler in the PATH if necessary
get_filename_component(_CMAKE_USER_${lang}_COMPILER_PATH "${CMAKE_${lang}_COMPILER}" PATH)
if(NOT _CMAKE_USER_${lang}_COMPILER_PATH)
find_program(CMAKE_${lang}_COMPILER_WITH_PATH NAMES ${CMAKE_${lang}_COMPILER})
if(CMAKE_${lang}_COMPILER_WITH_PATH)
set(CMAKE_${lang}_COMPILER ${CMAKE_${lang}_COMPILER_WITH_PATH})
get_property(_CMAKE_${lang}_COMPILER_CACHED CACHE CMAKE_${lang}_COMPILER PROPERTY TYPE)
if(_CMAKE_${lang}_COMPILER_CACHED)
set(CMAKE_${lang}_COMPILER "${CMAKE_${lang}_COMPILER}" CACHE STRING "${lang} compiler" FORCE)
endif()
unset(_CMAKE_${lang}_COMPILER_CACHED)
endif()
unset(CMAKE_${lang}_COMPILER_WITH_PATH CACHE)
endif()
endif()
endmacro()