Re-order C/C++/Fortran compiler determination logic

Re-organize CMakeDetermine(C|CXX|Fortran)Compiler.cmake to search for
the compiler command-line tool only under generators for which it makes
sense.  For the Visual Studio generators we do not expect to find the
compiler tool from the environment, nor would we use the result anyway.

Furthermore, set CMAKE_${lang}_COMPILER_ID_TEST_FLAGS only when it has a
chance to be used.  Extract _CMAKE_TOOLCHAIN_LOCATION from the compiler
path after running the compiler id step so in the future that step can
help find the path to the compiler.
This commit is contained in:
Brad King 2012-08-15 15:53:04 -04:00
parent 2c7a451de0
commit b8b5c8342a
3 changed files with 227 additions and 236 deletions

View File

@ -39,7 +39,13 @@ if(NOT CMAKE_C_COMPILER_NAMES)
set(CMAKE_C_COMPILER_NAMES cc) set(CMAKE_C_COMPILER_NAMES cc)
endif() endif()
if(NOT CMAKE_C_COMPILER) if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
set(CMAKE_C_COMPILER_ID_RUN 1)
set(CMAKE_C_PLATFORM_ID "Windows")
set(CMAKE_C_COMPILER_ID "MSVC")
set(CMAKE_C_COMPILER "${CMAKE_GENERATOR_CC}")
else()
if(NOT CMAKE_C_COMPILER)
set(CMAKE_C_COMPILER_INIT NOTFOUND) set(CMAKE_C_COMPILER_INIT NOTFOUND)
# prefer the environment variable CC # prefer the environment variable CC
@ -67,7 +73,7 @@ if(NOT CMAKE_C_COMPILER)
_cmake_find_compiler(C) _cmake_find_compiler(C)
else() else()
# we only get here if CMAKE_C_COMPILER was specified using -D or a pre-made CMakeCache.txt # we only get here if CMAKE_C_COMPILER was specified using -D or a pre-made CMakeCache.txt
# (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
@ -93,22 +99,8 @@ else()
set(CMAKE_C_COMPILER ${CMAKE_C_COMPILER_WITH_PATH} CACHE STRING "C compiler" FORCE) set(CMAKE_C_COMPILER ${CMAKE_C_COMPILER_WITH_PATH} CACHE STRING "C compiler" FORCE)
endif() endif()
endif() endif()
endif() endif()
mark_as_advanced(CMAKE_C_COMPILER) mark_as_advanced(CMAKE_C_COMPILER)
if (NOT _CMAKE_TOOLCHAIN_LOCATION)
get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_C_COMPILER}" PATH)
endif ()
# Build a small source file to identify the compiler.
if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
set(CMAKE_C_COMPILER_ID_RUN 1)
set(CMAKE_C_PLATFORM_ID "Windows")
set(CMAKE_C_COMPILER_ID "MSVC")
endif()
if(NOT CMAKE_C_COMPILER_ID_RUN)
set(CMAKE_C_COMPILER_ID_RUN 1)
# Each entry in this list is a set of extra flags to try # Each entry in this list is a set of extra flags to try
# adding to the compile line to see if it helps produce # adding to the compile line to see if it helps produce
@ -120,6 +112,11 @@ if(NOT CMAKE_C_COMPILER_ID_RUN)
# Try enabling ANSI mode on HP. # Try enabling ANSI mode on HP.
"-Aa" "-Aa"
) )
endif()
# Build a small source file to identify the compiler.
if(NOT CMAKE_C_COMPILER_ID_RUN)
set(CMAKE_C_COMPILER_ID_RUN 1)
# Try to identify the compiler. # Try to identify the compiler.
set(CMAKE_C_COMPILER_ID) set(CMAKE_C_COMPILER_ID)
@ -139,6 +136,10 @@ if(NOT CMAKE_C_COMPILER_ID_RUN)
endif() endif()
endif() endif()
if (NOT _CMAKE_TOOLCHAIN_LOCATION)
get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_C_COMPILER}" PATH)
endif ()
# If we have a gcc cross compiler, they have usually some prefix, like # If we have a gcc cross compiler, they have usually some prefix, like
# e.g. powerpc-linux-gcc, arm-elf-gcc or i586-mingw32msvc-gcc, optionally # e.g. powerpc-linux-gcc, arm-elf-gcc or i586-mingw32msvc-gcc, optionally
# with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2). # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).

View File

@ -38,7 +38,13 @@ if(NOT CMAKE_CXX_COMPILER_NAMES)
set(CMAKE_CXX_COMPILER_NAMES CC) set(CMAKE_CXX_COMPILER_NAMES CC)
endif() endif()
if(NOT CMAKE_CXX_COMPILER) if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
set(CMAKE_CXX_COMPILER_ID_RUN 1)
set(CMAKE_CXX_PLATFORM_ID "Windows")
set(CMAKE_CXX_COMPILER_ID "MSVC")
set(CMAKE_CXX_COMPILER "${CMAKE_GENERATOR_CXX}")
else()
if(NOT CMAKE_CXX_COMPILER)
set(CMAKE_CXX_COMPILER_INIT NOTFOUND) set(CMAKE_CXX_COMPILER_INIT NOTFOUND)
# prefer the environment variable CXX # prefer the environment variable CXX
@ -65,13 +71,13 @@ if(NOT CMAKE_CXX_COMPILER)
endif() endif()
_cmake_find_compiler(CXX) _cmake_find_compiler(CXX)
else() else()
# we only get here if CMAKE_CXX_COMPILER was specified using -D or a pre-made CMakeCache.txt # we only get here if CMAKE_CXX_COMPILER was specified using -D or a pre-made CMakeCache.txt
# (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
# #
# if CMAKE_CXX_COMPILER is a list of length 2, use the first item as # if CMAKE_CXX_COMPILER is a list of length 2, use the first item as
# CMAKE_CXX_COMPILER and the 2nd one as CMAKE_CXX_COMPILER_ARG1 # CMAKE_CXX_COMPILER and the 2nd one as CMAKE_CXX_COMPILER_ARG1
list(LENGTH CMAKE_CXX_COMPILER _CMAKE_CXX_COMPILER_LIST_LENGTH) list(LENGTH CMAKE_CXX_COMPILER _CMAKE_CXX_COMPILER_LIST_LENGTH)
if("${_CMAKE_CXX_COMPILER_LIST_LENGTH}" EQUAL 2) if("${_CMAKE_CXX_COMPILER_LIST_LENGTH}" EQUAL 2)
@ -79,11 +85,11 @@ else()
list(GET CMAKE_CXX_COMPILER 0 CMAKE_CXX_COMPILER) list(GET CMAKE_CXX_COMPILER 0 CMAKE_CXX_COMPILER)
endif() endif()
# if a compiler was specified by the user but without path, # if a compiler was specified by the user but without path,
# now try to find it with the full path # now try to find it with the full path
# if it is found, force it into the cache, # if it is found, force it into the cache,
# if not, don't overwrite the setting (which was given by the user) with "NOTFOUND" # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
# if the CXX compiler already had a path, reuse it for searching the C compiler # if the CXX compiler already had a path, reuse it for searching the C compiler
get_filename_component(_CMAKE_USER_CXX_COMPILER_PATH "${CMAKE_CXX_COMPILER}" PATH) get_filename_component(_CMAKE_USER_CXX_COMPILER_PATH "${CMAKE_CXX_COMPILER}" PATH)
if(NOT _CMAKE_USER_CXX_COMPILER_PATH) if(NOT _CMAKE_USER_CXX_COMPILER_PATH)
find_program(CMAKE_CXX_COMPILER_WITH_PATH NAMES ${CMAKE_CXX_COMPILER}) find_program(CMAKE_CXX_COMPILER_WITH_PATH NAMES ${CMAKE_CXX_COMPILER})
@ -92,33 +98,8 @@ else()
set(CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER_WITH_PATH} CACHE STRING "CXX compiler" FORCE) set(CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER_WITH_PATH} CACHE STRING "CXX compiler" FORCE)
endif() endif()
endif() endif()
endif() endif()
mark_as_advanced(CMAKE_CXX_COMPILER) mark_as_advanced(CMAKE_CXX_COMPILER)
if (NOT _CMAKE_TOOLCHAIN_LOCATION)
get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_CXX_COMPILER}" PATH)
endif ()
# This block was used before the compiler was identified by building a
# source file. Unless g++ crashes when building a small C++
# executable this should no longer be needed.
#
# The g++ that comes with BeOS 5 segfaults if you run "g++ -E"
# ("gcc -E" is fine), which throws up a system dialog box that hangs cmake
# until the user clicks "OK"...so for now, we just assume it's g++.
# if(BEOS)
# set(CMAKE_COMPILER_IS_GNUCXX 1)
# set(CMAKE_COMPILER_IS_GNUCXX_RUN 1)
# endif()
# Build a small source file to identify the compiler.
if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
set(CMAKE_CXX_COMPILER_ID_RUN 1)
set(CMAKE_CXX_PLATFORM_ID "Windows")
set(CMAKE_CXX_COMPILER_ID "MSVC")
endif()
if(NOT CMAKE_CXX_COMPILER_ID_RUN)
set(CMAKE_CXX_COMPILER_ID_RUN 1)
# Each entry in this list is a set of extra flags to try # Each entry in this list is a set of extra flags to try
# adding to the compile line to see if it helps produce # adding to the compile line to see if it helps produce
@ -127,6 +108,11 @@ if(NOT CMAKE_CXX_COMPILER_ID_RUN)
# Try compiling to an object file only. # Try compiling to an object file only.
"-c" "-c"
) )
endif()
# Build a small source file to identify the compiler.
if(NOT CMAKE_CXX_COMPILER_ID_RUN)
set(CMAKE_CXX_COMPILER_ID_RUN 1)
# Try to identify the compiler. # Try to identify the compiler.
set(CMAKE_CXX_COMPILER_ID) set(CMAKE_CXX_COMPILER_ID)
@ -146,6 +132,10 @@ if(NOT CMAKE_CXX_COMPILER_ID_RUN)
endif() endif()
endif() endif()
if (NOT _CMAKE_TOOLCHAIN_LOCATION)
get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_CXX_COMPILER}" PATH)
endif ()
# if we have a g++ cross compiler, they have usually some prefix, like # if we have a g++ cross compiler, they have usually some prefix, like
# e.g. powerpc-linux-g++, arm-elf-g++ or i586-mingw32msvc-g++ , optionally # e.g. powerpc-linux-g++, arm-elf-g++ or i586-mingw32msvc-g++ , optionally
# with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2). # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).

View File

@ -25,7 +25,13 @@ if(NOT CMAKE_Fortran_COMPILER_NAMES)
set(CMAKE_Fortran_COMPILER_NAMES f95) set(CMAKE_Fortran_COMPILER_NAMES f95)
endif() endif()
if(NOT CMAKE_Fortran_COMPILER) if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
set(CMAKE_Fortran_COMPILER_ID_RUN 1)
set(CMAKE_Fortran_PLATFORM_ID "Windows")
set(CMAKE_Fortran_COMPILER_ID "Intel")
set(CMAKE_Fortran_COMPILER "${CMAKE_GENERATOR_FC}")
else()
if(NOT CMAKE_Fortran_COMPILER)
# prefer the environment variable CC # prefer the environment variable CC
if($ENV{FC} MATCHES ".+") if($ENV{FC} MATCHES ".+")
get_filename_component(CMAKE_Fortran_COMPILER_INIT $ENV{FC} PROGRAM PROGRAM_ARGS CMAKE_Fortran_FLAGS_ENV_INIT) get_filename_component(CMAKE_Fortran_COMPILER_INIT $ENV{FC} PROGRAM PROGRAM_ARGS CMAKE_Fortran_FLAGS_ENV_INIT)
@ -85,7 +91,7 @@ if(NOT CMAKE_Fortran_COMPILER)
_cmake_find_compiler(Fortran) _cmake_find_compiler(Fortran)
else() else()
# we only get here if CMAKE_Fortran_COMPILER was specified using -D or a pre-made CMakeCache.txt # we only get here if CMAKE_Fortran_COMPILER was specified using -D or a pre-made CMakeCache.txt
# (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
# if CMAKE_Fortran_COMPILER is a list of length 2, use the first item as # if CMAKE_Fortran_COMPILER is a list of length 2, use the first item as
@ -111,19 +117,8 @@ else()
CACHE STRING "Fortran compiler" FORCE) CACHE STRING "Fortran compiler" FORCE)
endif() endif()
endif() endif()
endif() endif()
mark_as_advanced(CMAKE_Fortran_COMPILER)
mark_as_advanced(CMAKE_Fortran_COMPILER)
# Build a small source file to identify the compiler.
if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
set(CMAKE_Fortran_COMPILER_ID_RUN 1)
set(CMAKE_Fortran_PLATFORM_ID "Windows")
set(CMAKE_Fortran_COMPILER_ID "Intel")
endif()
if(NOT CMAKE_Fortran_COMPILER_ID_RUN)
set(CMAKE_Fortran_COMPILER_ID_RUN 1)
# Each entry in this list is a set of extra flags to try # Each entry in this list is a set of extra flags to try
# adding to the compile line to see if it helps produce # adding to the compile line to see if it helps produce
@ -135,6 +130,11 @@ if(NOT CMAKE_Fortran_COMPILER_ID_RUN)
# Intel on windows does not preprocess by default. # Intel on windows does not preprocess by default.
"-fpp" "-fpp"
) )
endif()
# Build a small source file to identify the compiler.
if(NOT CMAKE_Fortran_COMPILER_ID_RUN)
set(CMAKE_Fortran_COMPILER_ID_RUN 1)
# Table of per-vendor compiler id flags with expected output. # Table of per-vendor compiler id flags with expected output.
list(APPEND CMAKE_Fortran_COMPILER_ID_VENDORS Compaq) list(APPEND CMAKE_Fortran_COMPILER_ID_VENDORS Compaq)