summaryrefslogtreecommitdiffstats
path: root/Modules/CMakeDetermineCXXCompiler.cmake
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2012-08-15 19:53:04 (GMT)
committerBrad King <brad.king@kitware.com>2012-08-22 19:14:33 (GMT)
commitb8b5c8342a2a2de5f57f9dd83435060b4eb825a8 (patch)
tree6b327fd1375d9aef519d7bfa563edb31eeb1cf11 /Modules/CMakeDetermineCXXCompiler.cmake
parent2c7a451de003782b1ba5e47ab377f33f1fa5c4e5 (diff)
downloadCMake-b8b5c8342a2a2de5f57f9dd83435060b4eb825a8.zip
CMake-b8b5c8342a2a2de5f57f9dd83435060b4eb825a8.tar.gz
CMake-b8b5c8342a2a2de5f57f9dd83435060b4eb825a8.tar.bz2
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.
Diffstat (limited to 'Modules/CMakeDetermineCXXCompiler.cmake')
-rw-r--r--Modules/CMakeDetermineCXXCompiler.cmake130
1 files changed, 60 insertions, 70 deletions
diff --git a/Modules/CMakeDetermineCXXCompiler.cmake b/Modules/CMakeDetermineCXXCompiler.cmake
index 898947a..6ed2c68 100644
--- a/Modules/CMakeDetermineCXXCompiler.cmake
+++ b/Modules/CMakeDetermineCXXCompiler.cmake
@@ -38,87 +38,68 @@ if(NOT CMAKE_CXX_COMPILER_NAMES)
set(CMAKE_CXX_COMPILER_NAMES CC)
endif()
-if(NOT CMAKE_CXX_COMPILER)
- set(CMAKE_CXX_COMPILER_INIT NOTFOUND)
-
- # prefer the environment variable CXX
- if($ENV{CXX} MATCHES ".+")
- get_filename_component(CMAKE_CXX_COMPILER_INIT $ENV{CXX} PROGRAM PROGRAM_ARGS CMAKE_CXX_FLAGS_ENV_INIT)
- if(CMAKE_CXX_FLAGS_ENV_INIT)
- set(CMAKE_CXX_COMPILER_ARG1 "${CMAKE_CXX_FLAGS_ENV_INIT}" CACHE STRING "First argument to 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)
+
+ # prefer the environment variable CXX
+ if($ENV{CXX} MATCHES ".+")
+ get_filename_component(CMAKE_CXX_COMPILER_INIT $ENV{CXX} PROGRAM PROGRAM_ARGS CMAKE_CXX_FLAGS_ENV_INIT)
+ if(CMAKE_CXX_FLAGS_ENV_INIT)
+ set(CMAKE_CXX_COMPILER_ARG1 "${CMAKE_CXX_FLAGS_ENV_INIT}" CACHE STRING "First argument to CXX compiler")
+ endif()
+ if(NOT EXISTS ${CMAKE_CXX_COMPILER_INIT})
+ message(FATAL_ERROR "Could not find compiler set in environment variable CXX:\n$ENV{CXX}.\n${CMAKE_CXX_COMPILER_INIT}")
+ endif()
endif()
- if(NOT EXISTS ${CMAKE_CXX_COMPILER_INIT})
- message(FATAL_ERROR "Could not find compiler set in environment variable CXX:\n$ENV{CXX}.\n${CMAKE_CXX_COMPILER_INIT}")
+
+ # next prefer the generator specified compiler
+ if(CMAKE_GENERATOR_CXX)
+ if(NOT CMAKE_CXX_COMPILER_INIT)
+ set(CMAKE_CXX_COMPILER_INIT ${CMAKE_GENERATOR_CXX})
+ endif()
endif()
- endif()
- # next prefer the generator specified compiler
- if(CMAKE_GENERATOR_CXX)
+ # finally list compilers to try
if(NOT CMAKE_CXX_COMPILER_INIT)
- set(CMAKE_CXX_COMPILER_INIT ${CMAKE_GENERATOR_CXX})
+ set(CMAKE_CXX_COMPILER_LIST CC ${_CMAKE_TOOLCHAIN_PREFIX}c++ ${_CMAKE_TOOLCHAIN_PREFIX}g++ aCC cl bcc xlC clang++)
endif()
- endif()
-
- # finally list compilers to try
- if(NOT CMAKE_CXX_COMPILER_INIT)
- set(CMAKE_CXX_COMPILER_LIST CC ${_CMAKE_TOOLCHAIN_PREFIX}c++ ${_CMAKE_TOOLCHAIN_PREFIX}g++ aCC cl bcc xlC clang++)
- endif()
- _cmake_find_compiler(CXX)
-else()
+ _cmake_find_compiler(CXX)
+ else()
-# 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
-#
-# 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
+ # 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
+ #
+ # 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
- list(LENGTH CMAKE_CXX_COMPILER _CMAKE_CXX_COMPILER_LIST_LENGTH)
- if("${_CMAKE_CXX_COMPILER_LIST_LENGTH}" EQUAL 2)
- list(GET CMAKE_CXX_COMPILER 1 CMAKE_CXX_COMPILER_ARG1)
- list(GET CMAKE_CXX_COMPILER 0 CMAKE_CXX_COMPILER)
- endif()
+ list(LENGTH CMAKE_CXX_COMPILER _CMAKE_CXX_COMPILER_LIST_LENGTH)
+ if("${_CMAKE_CXX_COMPILER_LIST_LENGTH}" EQUAL 2)
+ list(GET CMAKE_CXX_COMPILER 1 CMAKE_CXX_COMPILER_ARG1)
+ list(GET CMAKE_CXX_COMPILER 0 CMAKE_CXX_COMPILER)
+ endif()
-# if a compiler was specified by the user but without path,
-# now try to find it with the full path
-# 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 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)
- if(NOT _CMAKE_USER_CXX_COMPILER_PATH)
- find_program(CMAKE_CXX_COMPILER_WITH_PATH NAMES ${CMAKE_CXX_COMPILER})
- mark_as_advanced(CMAKE_CXX_COMPILER_WITH_PATH)
- if(CMAKE_CXX_COMPILER_WITH_PATH)
- set(CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER_WITH_PATH} CACHE STRING "CXX compiler" FORCE)
+ # if a compiler was specified by the user but without path,
+ # now try to find it with the full path
+ # 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 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)
+ if(NOT _CMAKE_USER_CXX_COMPILER_PATH)
+ find_program(CMAKE_CXX_COMPILER_WITH_PATH NAMES ${CMAKE_CXX_COMPILER})
+ mark_as_advanced(CMAKE_CXX_COMPILER_WITH_PATH)
+ if(CMAKE_CXX_COMPILER_WITH_PATH)
+ set(CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER_WITH_PATH} CACHE STRING "CXX compiler" FORCE)
+ endif()
endif()
endif()
-endif()
-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)
+ mark_as_advanced(CMAKE_CXX_COMPILER)
# Each entry in this list is a set of extra flags to try
# 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.
"-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.
set(CMAKE_CXX_COMPILER_ID)
@@ -146,6 +132,10 @@ if(NOT CMAKE_CXX_COMPILER_ID_RUN)
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
# 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).