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.
This commit is contained in:
Brad King 2012-08-02 11:53:25 -04:00
parent 796e33734d
commit 7e58e5bb68
9 changed files with 51 additions and 4 deletions

View File

@ -33,8 +33,9 @@ IF(NOT CMAKE_ASM${ASM_DIALECT}_COMPILER)
SET(CMAKE_ASM_COMPILER_ID "${CMAKE_CXX_COMPILER_ID}")
ELSE()
# List all default C and CXX compilers
SET(CMAKE_ASM${ASM_DIALECT}_COMPILER_LIST ${_CMAKE_TOOLCHAIN_PREFIX}gcc ${_CMAKE_TOOLCHAIN_PREFIX}cc cl bcc xlc
${_CMAKE_TOOLCHAIN_PREFIX}c++ ${_CMAKE_TOOLCHAIN_PREFIX}g++ CC aCC cl bcc xlC)
SET(CMAKE_ASM${ASM_DIALECT}_COMPILER_LIST
${_CMAKE_TOOLCHAIN_PREFIX}cc ${_CMAKE_TOOLCHAIN_PREFIX}gcc cl bcc xlc
CC ${_CMAKE_TOOLCHAIN_PREFIX}c++ ${_CMAKE_TOOLCHAIN_PREFIX}g++ aCC cl bcc xlC)
ENDIF()
ENDIF()
ELSE() # some specific assembler "dialect"

View File

@ -33,6 +33,12 @@
INCLUDE(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
# Load system-specific compiler preferences for this language.
INCLUDE(Platform/${CMAKE_SYSTEM_NAME}-C OPTIONAL)
IF(NOT CMAKE_C_COMPILER_NAMES)
SET(CMAKE_C_COMPILER_NAMES cc)
ENDIF()
IF(NOT CMAKE_C_COMPILER)
SET(CMAKE_C_COMPILER_INIT NOTFOUND)
@ -56,7 +62,7 @@ IF(NOT CMAKE_C_COMPILER)
# finally list compilers to try
IF(NOT CMAKE_C_COMPILER_INIT)
SET(CMAKE_C_COMPILER_LIST ${_CMAKE_TOOLCHAIN_PREFIX}gcc ${_CMAKE_TOOLCHAIN_PREFIX}cc cl bcc xlc clang)
SET(CMAKE_C_COMPILER_LIST ${_CMAKE_TOOLCHAIN_PREFIX}cc ${_CMAKE_TOOLCHAIN_PREFIX}gcc cl bcc xlc clang)
ENDIF()
_cmake_find_compiler(C)

View File

@ -32,6 +32,12 @@
INCLUDE(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
# Load system-specific compiler preferences for this language.
INCLUDE(Platform/${CMAKE_SYSTEM_NAME}-CXX OPTIONAL)
IF(NOT CMAKE_CXX_COMPILER_NAMES)
SET(CMAKE_CXX_COMPILER_NAMES CC)
ENDIF()
IF(NOT CMAKE_CXX_COMPILER)
SET(CMAKE_CXX_COMPILER_INIT NOTFOUND)
@ -55,7 +61,7 @@ IF(NOT CMAKE_CXX_COMPILER)
# finally list compilers to try
IF(NOT CMAKE_CXX_COMPILER_INIT)
SET(CMAKE_CXX_COMPILER_LIST ${_CMAKE_TOOLCHAIN_PREFIX}c++ ${_CMAKE_TOOLCHAIN_PREFIX}g++ CC aCC cl bcc xlC clang++)
SET(CMAKE_CXX_COMPILER_LIST CC ${_CMAKE_TOOLCHAIN_PREFIX}c++ ${_CMAKE_TOOLCHAIN_PREFIX}g++ aCC cl bcc xlC clang++)
ENDIF()
_cmake_find_compiler(CXX)

View File

@ -29,10 +29,16 @@ macro(_cmake_find_compiler lang)
list(APPEND CMAKE_${lang}_COMPILER_LIST
${_${lang}_COMPILER_NAMES_${CMAKE_${l}_COMPILER_ID}})
endforeach()
# 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)
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.

View File

@ -20,6 +20,10 @@
# as a default compiler
INCLUDE(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
INCLUDE(Platform/${CMAKE_SYSTEM_NAME}-Fortran OPTIONAL)
IF(NOT CMAKE_Fortran_COMPILER_NAMES)
SET(CMAKE_Fortran_COMPILER_NAMES f95)
ENDIF()
IF(NOT CMAKE_Fortran_COMPILER)
# prefer the environment variable CC

View File

@ -0,0 +1,7 @@
if(NOT CMAKE_CXX_COMPILER_NAMES)
set(CMAKE_CXX_COMPILER_NAMES c++)
endif()
# Exclude C++ compilers differing from C compiler only by case
# because this platform may have a case-insensitive filesystem.
set(CMAKE_CXX_COMPILER_EXCLUDE CC aCC xlC)

View File

@ -0,0 +1,7 @@
if(NOT CMAKE_CXX_COMPILER_NAMES)
set(CMAKE_CXX_COMPILER_NAMES c++)
endif()
# Exclude C++ compilers differing from C compiler only by case
# because this platform may have a case-insensitive filesystem.
set(CMAKE_CXX_COMPILER_EXCLUDE CC aCC xlC)

View File

@ -0,0 +1,3 @@
if(NOT CMAKE_CXX_COMPILER_NAMES)
set(CMAKE_CXX_COMPILER_NAMES c++)
endif()

View File

@ -0,0 +1,7 @@
if(NOT CMAKE_CXX_COMPILER_NAMES)
set(CMAKE_CXX_COMPILER_NAMES c++)
endif()
# Exclude C++ compilers differing from C compiler only by case
# because this platform may have a case-insensitive filesystem.
set(CMAKE_CXX_COMPILER_EXCLUDE CC aCC xlC)