From ec1d3bc0b64550307c23ceae7ef542124b68e2e5 Mon Sep 17 00:00:00 2001 From: Fred Baksik Date: Wed, 15 Jul 2020 15:03:45 -0400 Subject: cmake: avoid exception when printing "changed variables" message If the changed cache variable was a list then this processing may attempt to access beyond the last item in the list. Instead skip printing the non-existing value and backup one to finish the loop. --- Source/cmake.cxx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 45fa44b..aa8298f 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -1390,8 +1390,13 @@ int cmake::HandleDeleteCacheVariables(const std::string& var) save.key = *i; warning << *i << "= "; i++; - save.value = *i; - warning << *i << "\n"; + if (i != argsSplit.end()) { + save.value = *i; + warning << *i << "\n"; + } else { + warning << "\n"; + i -= 1; + } cmProp existingValue = this->State->GetCacheEntryValue(save.key); if (existingValue) { save.type = this->State->GetCacheEntryType(save.key); -- cgit v0.12 From 6f1af899db4e93c960145dae12ebaacb308ea1f0 Mon Sep 17 00:00:00 2001 From: Fred Baksik Date: Wed, 15 Jul 2020 15:03:45 -0400 Subject: Toolchain: Capture all arguments from CMAKE__COMPILER Capture CMAKE__COMPILER_ARG1 from CMAKE__COMPILER in the same fashion that it is from $ENV{}. Since get_filename_component() returns a single string of all the arguments from $ENV{}, a single string of arguments will be constructed from the items contained in CMAKE__COMPILER. Fixes #20089 --- Modules/CMakeDetermineASMCompiler.cmake | 2 +- Modules/CMakeDetermineCCompiler.cmake | 2 +- Modules/CMakeDetermineCUDACompiler.cmake | 2 +- Modules/CMakeDetermineCXXCompiler.cmake | 2 +- Modules/CMakeDetermineCompiler.cmake | 13 +++++-------- Modules/CMakeDetermineFortranCompiler.cmake | 2 +- Modules/CMakeDetermineJavaCompiler.cmake | 2 +- Modules/CMakeDetermineOBJCCompiler.cmake | 15 ++++++--------- Modules/CMakeDetermineOBJCXXCompiler.cmake | 15 ++++++--------- Modules/CMakeDetermineRCCompiler.cmake | 2 +- Modules/CMakeDetermineSwiftCompiler.cmake | 2 +- 11 files changed, 25 insertions(+), 34 deletions(-) diff --git a/Modules/CMakeDetermineASMCompiler.cmake b/Modules/CMakeDetermineASMCompiler.cmake index bc8b86b..a3e5a12 100644 --- a/Modules/CMakeDetermineASMCompiler.cmake +++ b/Modules/CMakeDetermineASMCompiler.cmake @@ -11,7 +11,7 @@ if(NOT CMAKE_ASM${ASM_DIALECT}_COMPILER) if(NOT $ENV{ASM${ASM_DIALECT}} STREQUAL "") get_filename_component(CMAKE_ASM${ASM_DIALECT}_COMPILER_INIT $ENV{ASM${ASM_DIALECT}} PROGRAM PROGRAM_ARGS CMAKE_ASM${ASM_DIALECT}_FLAGS_ENV_INIT) if(CMAKE_ASM${ASM_DIALECT}_FLAGS_ENV_INIT) - set(CMAKE_ASM${ASM_DIALECT}_COMPILER_ARG1 "${CMAKE_ASM${ASM_DIALECT}_FLAGS_ENV_INIT}" CACHE STRING "First argument to ASM${ASM_DIALECT} compiler") + set(CMAKE_ASM${ASM_DIALECT}_COMPILER_ARG1 "${CMAKE_ASM${ASM_DIALECT}_FLAGS_ENV_INIT}" CACHE STRING "Arguments to ASM${ASM_DIALECT} compiler") endif() if(NOT EXISTS ${CMAKE_ASM${ASM_DIALECT}_COMPILER_INIT}) message(FATAL_ERROR "Could not find compiler set in environment variable ASM${ASM_DIALECT}:\n$ENV{ASM${ASM_DIALECT}}.") diff --git a/Modules/CMakeDetermineCCompiler.cmake b/Modules/CMakeDetermineCCompiler.cmake index 86683d1..5aa7dd8 100644 --- a/Modules/CMakeDetermineCCompiler.cmake +++ b/Modules/CMakeDetermineCCompiler.cmake @@ -43,7 +43,7 @@ else() if(NOT $ENV{CC} STREQUAL "") get_filename_component(CMAKE_C_COMPILER_INIT $ENV{CC} PROGRAM PROGRAM_ARGS CMAKE_C_FLAGS_ENV_INIT) if(CMAKE_C_FLAGS_ENV_INIT) - set(CMAKE_C_COMPILER_ARG1 "${CMAKE_C_FLAGS_ENV_INIT}" CACHE STRING "First argument to C compiler") + set(CMAKE_C_COMPILER_ARG1 "${CMAKE_C_FLAGS_ENV_INIT}" CACHE STRING "Arguments to C compiler") endif() if(NOT EXISTS ${CMAKE_C_COMPILER_INIT}) message(FATAL_ERROR "Could not find compiler set in environment variable CC:\n$ENV{CC}.") diff --git a/Modules/CMakeDetermineCUDACompiler.cmake b/Modules/CMakeDetermineCUDACompiler.cmake index 0c586f5..daca382 100644 --- a/Modules/CMakeDetermineCUDACompiler.cmake +++ b/Modules/CMakeDetermineCUDACompiler.cmake @@ -19,7 +19,7 @@ else() if(NOT $ENV{CUDACXX} STREQUAL "") get_filename_component(CMAKE_CUDA_COMPILER_INIT $ENV{CUDACXX} PROGRAM PROGRAM_ARGS CMAKE_CUDA_FLAGS_ENV_INIT) if(CMAKE_CUDA_FLAGS_ENV_INIT) - set(CMAKE_CUDA_COMPILER_ARG1 "${CMAKE_CUDA_FLAGS_ENV_INIT}" CACHE STRING "First argument to CXX compiler") + set(CMAKE_CUDA_COMPILER_ARG1 "${CMAKE_CUDA_FLAGS_ENV_INIT}" CACHE STRING "Arguments to CXX compiler") endif() if(NOT EXISTS ${CMAKE_CUDA_COMPILER_INIT}) message(FATAL_ERROR "Could not find compiler set in environment variable CUDACXX:\n$ENV{CUDACXX}.\n${CMAKE_CUDA_COMPILER_INIT}") diff --git a/Modules/CMakeDetermineCXXCompiler.cmake b/Modules/CMakeDetermineCXXCompiler.cmake index 662b831..e7e294f 100644 --- a/Modules/CMakeDetermineCXXCompiler.cmake +++ b/Modules/CMakeDetermineCXXCompiler.cmake @@ -42,7 +42,7 @@ else() if(NOT $ENV{CXX} STREQUAL "") 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") + set(CMAKE_CXX_COMPILER_ARG1 "${CMAKE_CXX_FLAGS_ENV_INIT}" CACHE STRING "Arguments 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}") diff --git a/Modules/CMakeDetermineCompiler.cmake b/Modules/CMakeDetermineCompiler.cmake index 7cd7c47..142e61e 100644 --- a/Modules/CMakeDetermineCompiler.cmake +++ b/Modules/CMakeDetermineCompiler.cmake @@ -107,14 +107,11 @@ 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) + # if CMAKE_${lang}_COMPILER is a list, use the first item as + # CMAKE_${lang}_COMPILER and the rest as CMAKE_${lang}_COMPILER_ARG1 + set(CMAKE_${lang}_COMPILER_ARG1 "${CMAKE_${lang}_COMPILER}") + list(POP_FRONT CMAKE_${lang}_COMPILER_ARG1 CMAKE_${lang}_COMPILER) + list(JOIN CMAKE_${lang}_COMPILER_ARG1 " " CMAKE_${lang}_COMPILER_ARG1) # find the compiler in the PATH if necessary get_filename_component(_CMAKE_USER_${lang}_COMPILER_PATH "${CMAKE_${lang}_COMPILER}" PATH) diff --git a/Modules/CMakeDetermineFortranCompiler.cmake b/Modules/CMakeDetermineFortranCompiler.cmake index 5f5a70a..8a57408 100644 --- a/Modules/CMakeDetermineFortranCompiler.cmake +++ b/Modules/CMakeDetermineFortranCompiler.cmake @@ -26,7 +26,7 @@ else() if(NOT $ENV{FC} STREQUAL "") get_filename_component(CMAKE_Fortran_COMPILER_INIT $ENV{FC} PROGRAM PROGRAM_ARGS CMAKE_Fortran_FLAGS_ENV_INIT) if(CMAKE_Fortran_FLAGS_ENV_INIT) - set(CMAKE_Fortran_COMPILER_ARG1 "${CMAKE_Fortran_FLAGS_ENV_INIT}" CACHE STRING "First argument to Fortran compiler") + set(CMAKE_Fortran_COMPILER_ARG1 "${CMAKE_Fortran_FLAGS_ENV_INIT}" CACHE STRING "Arguments to Fortran compiler") endif() if(EXISTS ${CMAKE_Fortran_COMPILER_INIT}) else() diff --git a/Modules/CMakeDetermineJavaCompiler.cmake b/Modules/CMakeDetermineJavaCompiler.cmake index 3092bb5..db456c0 100644 --- a/Modules/CMakeDetermineJavaCompiler.cmake +++ b/Modules/CMakeDetermineJavaCompiler.cmake @@ -11,7 +11,7 @@ if(NOT CMAKE_Java_COMPILER) if(NOT $ENV{JAVA_COMPILER} STREQUAL "") get_filename_component(CMAKE_Java_COMPILER_INIT $ENV{JAVA_COMPILER} PROGRAM PROGRAM_ARGS CMAKE_Java_FLAGS_ENV_INIT) if(CMAKE_Java_FLAGS_ENV_INIT) - set(CMAKE_Java_COMPILER_ARG1 "${CMAKE_Java_FLAGS_ENV_INIT}" CACHE STRING "First argument to Java compiler") + set(CMAKE_Java_COMPILER_ARG1 "${CMAKE_Java_FLAGS_ENV_INIT}" CACHE STRING "Arguments to Java compiler") endif() if(NOT EXISTS ${CMAKE_Java_COMPILER_INIT}) message(SEND_ERROR "Could not find compiler set in environment variable JAVA_COMPILER:\n$ENV{JAVA_COMPILER}.") diff --git a/Modules/CMakeDetermineOBJCCompiler.cmake b/Modules/CMakeDetermineOBJCCompiler.cmake index 11b47fd..709eb25 100644 --- a/Modules/CMakeDetermineOBJCCompiler.cmake +++ b/Modules/CMakeDetermineOBJCCompiler.cmake @@ -39,7 +39,7 @@ else() if($ENV{${var}} MATCHES ".+") get_filename_component(CMAKE_OBJC_COMPILER_INIT $ENV{${var}} PROGRAM PROGRAM_ARGS CMAKE_OBJC_FLAGS_ENV_INIT) if(CMAKE_OBJC_FLAGS_ENV_INIT) - set(CMAKE_OBJC_COMPILER_ARG1 "${CMAKE_OBJC_FLAGS_ENV_INIT}" CACHE STRING "First argument to Objective-C compiler") + set(CMAKE_OBJC_COMPILER_ARG1 "${CMAKE_OBJC_FLAGS_ENV_INIT}" CACHE STRING "Arguments to Objective-C compiler") endif() if(NOT EXISTS ${CMAKE_OBJC_COMPILER_INIT}) message(FATAL_ERROR "Could not find compiler set in environment variable ${var}:\n $ENV{${var}}") @@ -65,14 +65,11 @@ else() else() # we only get here if CMAKE_OBJC_COMPILER was specified using -D or a pre-made CMakeCache.txt # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE - # if CMAKE_OBJC_COMPILER is a list of length 2, use the first item as - # CMAKE_OBJC_COMPILER and the 2nd one as CMAKE_OBJC_COMPILER_ARG1 - - list(LENGTH CMAKE_OBJC_COMPILER _CMAKE_OBJC_COMPILER_LIST_LENGTH) - if("${_CMAKE_OBJC_COMPILER_LIST_LENGTH}" EQUAL 2) - list(GET CMAKE_OBJC_COMPILER 1 CMAKE_OBJC_COMPILER_ARG1) - list(GET CMAKE_OBJC_COMPILER 0 CMAKE_OBJC_COMPILER) - endif() + # if CMAKE_OBJC_COMPILER is a list, use the first item as + # CMAKE_OBJC_COMPILER and the rest as CMAKE_OBJC_COMPILER_ARG1 + set(CMAKE_OBJC_COMPILER_ARG1 "${CMAKE_OBJC_COMPILER}") + list(POP_FRONT CMAKE_OBJC_COMPILER_ARG1 CMAKE_OBJC_COMPILER) + list(JOIN CMAKE_OBJC_COMPILER_ARG1 " " CMAKE_OBJC_COMPILER_ARG1) # if a compiler was specified by the user but without path, # now try to find it with the full path diff --git a/Modules/CMakeDetermineOBJCXXCompiler.cmake b/Modules/CMakeDetermineOBJCXXCompiler.cmake index 99ad6c3..ffd0091 100644 --- a/Modules/CMakeDetermineOBJCXXCompiler.cmake +++ b/Modules/CMakeDetermineOBJCXXCompiler.cmake @@ -41,7 +41,7 @@ else() if($ENV{${var}} MATCHES ".+") get_filename_component(CMAKE_OBJCXX_COMPILER_INIT $ENV{${var}} PROGRAM PROGRAM_ARGS CMAKE_OBJCXX_FLAGS_ENV_INIT) if(CMAKE_OBJCXX_FLAGS_ENV_INIT) - set(CMAKE_OBJCXX_COMPILER_ARG1 "${CMAKE_OBJCXX_FLAGS_ENV_INIT}" CACHE STRING "First argument to Objective-C++ compiler") + set(CMAKE_OBJCXX_COMPILER_ARG1 "${CMAKE_OBJCXX_FLAGS_ENV_INIT}" CACHE STRING "Arguments to Objective-C++ compiler") endif() if(NOT EXISTS ${CMAKE_OBJCXX_COMPILER_INIT}) message(FATAL_ERROR "Could not find compiler set in environment variable ${var}:\n $ENV{${var}}") @@ -67,14 +67,11 @@ else() else() # we only get here if CMAKE_OBJCXX_COMPILER was specified using -D or a pre-made CMakeCache.txt # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE - # if CMAKE_OBJCXX_COMPILER is a list of length 2, use the first item as - # CMAKE_OBJCXX_COMPILER and the 2nd one as CMAKE_OBJCXX_COMPILER_ARG1 - - list(LENGTH CMAKE_OBJCXX_COMPILER _CMAKE_OBJCXX_COMPILER_LIST_LENGTH) - if("${_CMAKE_OBJCXX_COMPILER_LIST_LENGTH}" EQUAL 2) - list(GET CMAKE_OBJCXX_COMPILER 1 CMAKE_OBJCXX_COMPILER_ARG1) - list(GET CMAKE_OBJCXX_COMPILER 0 CMAKE_OBJCXX_COMPILER) - endif() + # if CMAKE_OBJCXX_COMPILER is a list, use the first item as + # CMAKE_OBJCXX_COMPILER and the rest as CMAKE_OBJCXX_COMPILER_ARG1 + set(CMAKE_OBJCXX_COMPILER_ARG1 "${CMAKE_OBJCXX_COMPILER}") + list(POP_FRONT CMAKE_OBJCXX_COMPILER_ARG1 CMAKE_OBJCXX_COMPILER) + list(JOIN CMAKE_OBJCXX_COMPILER_ARG1 " " CMAKE_OBJCXX_COMPILER_ARG1) # if a compiler was specified by the user but without path, # now try to find it with the full path diff --git a/Modules/CMakeDetermineRCCompiler.cmake b/Modules/CMakeDetermineRCCompiler.cmake index 8801e16..f8d55a5 100644 --- a/Modules/CMakeDetermineRCCompiler.cmake +++ b/Modules/CMakeDetermineRCCompiler.cmake @@ -13,7 +13,7 @@ if(NOT CMAKE_RC_COMPILER) if(NOT $ENV{RC} STREQUAL "") get_filename_component(CMAKE_RC_COMPILER_INIT $ENV{RC} PROGRAM PROGRAM_ARGS CMAKE_RC_FLAGS_ENV_INIT) if(CMAKE_RC_FLAGS_ENV_INIT) - set(CMAKE_RC_COMPILER_ARG1 "${CMAKE_RC_FLAGS_ENV_INIT}" CACHE STRING "First argument to RC compiler") + set(CMAKE_RC_COMPILER_ARG1 "${CMAKE_RC_FLAGS_ENV_INIT}" CACHE STRING "Arguments to RC compiler") endif() if(EXISTS ${CMAKE_RC_COMPILER_INIT}) else() diff --git a/Modules/CMakeDetermineSwiftCompiler.cmake b/Modules/CMakeDetermineSwiftCompiler.cmake index 688133f..aaad560 100644 --- a/Modules/CMakeDetermineSwiftCompiler.cmake +++ b/Modules/CMakeDetermineSwiftCompiler.cmake @@ -27,7 +27,7 @@ elseif("${CMAKE_GENERATOR}" MATCHES "^Ninja") PROGRAM_ARGS CMAKE_Swift_FLAGS_ENV_INIT) if(CMAKE_Swift_FLAGS_ENV_INIT) set(CMAKE_Swift_COMPILER_ARG1 "${CMAKE_Swift_FLAGS_ENV_INIT}" CACHE - STRING "First argument to the Swift compiler") + STRING "Arguments to the Swift compiler") endif() if(NOT EXISTS ${CMAKE_Swift_COMPILER_INIT}) message(FATAL_ERROR "Could not find compiler set in environment variable SWIFTC\n$ENV{SWIFTC}.\n${CMAKE_Swift_COMPILER_INIT}") -- cgit v0.12 From 12ba89e142c3db629e6e201a409b4c08bb169bdc Mon Sep 17 00:00:00 2001 From: Fred Baksik Date: Wed, 15 Jul 2020 15:03:45 -0400 Subject: Toolchain: Make `/path/comp;-argn' behave the same as 'comp;-argn' When using `cmake ... -DCMAKE_C_COMPILER=gcc;-pipe` first invocation of CMake worked correctly. When using `cmake ... -DCMAKE_C_COMPILER=/path/to/gcc;-pipe` first invocation of CMake detected a change to CMAKE_C_COMPILER, printed "You have changed variables" message, and re-ran the initial compiler tests after configuration was complete and before generation of the project files. The difference was due to the cache being forced updated with the new value of CMAKE_C_COMPILER so that the comparison check passes. --- Modules/CMakeDetermineCompiler.cmake | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Modules/CMakeDetermineCompiler.cmake b/Modules/CMakeDetermineCompiler.cmake index 142e61e..a1da441 100644 --- a/Modules/CMakeDetermineCompiler.cmake +++ b/Modules/CMakeDetermineCompiler.cmake @@ -114,6 +114,7 @@ macro(_cmake_find_compiler_path lang) list(JOIN CMAKE_${lang}_COMPILER_ARG1 " " CMAKE_${lang}_COMPILER_ARG1) # find the compiler in the PATH if necessary + # if compiler (and arguments) comes from cache then synchronize cache with updated CMAKE__COMPILER 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}) @@ -126,6 +127,12 @@ macro(_cmake_find_compiler_path lang) unset(_CMAKE_${lang}_COMPILER_CACHED) endif() unset(CMAKE_${lang}_COMPILER_WITH_PATH CACHE) + elseif (EXISTS ${CMAKE_${lang}_COMPILER}) + 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() endif() endmacro() -- cgit v0.12 From ca899af3e2d5af3d79d345322b300c348065f560 Mon Sep 17 00:00:00 2001 From: Fred Baksik Date: Wed, 15 Jul 2020 15:03:45 -0400 Subject: Toolchain: Handle repeated invocations of CMake with -DCMAKE_C_COMPILER Repeated invocations of `cmake ... -DCMAKE_C_COMPILER=gcc` should not trigger a "You have changed variables" message even though the cache value of CMAKE_C_COMPILER changes from '/path/to/gcc' to 'gcc'. Make repeated invocations of `cmake ... -DCMAKE_C_COMPILER=gcc;-pipe` not trigger the warning by comparing the compiler name portion of the list to the compiler being used. --- Source/cmGlobalGenerator.cxx | 8 ++++++++ Source/cmRulePlaceholderExpander.cxx | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx index 5c07e31..b227599 100644 --- a/Source/cmGlobalGenerator.cxx +++ b/Source/cmGlobalGenerator.cxx @@ -235,6 +235,14 @@ void cmGlobalGenerator::ResolveLanguageCompiler(const std::string& lang, } cmProp cname = this->GetCMakeInstance()->GetState()->GetInitializedCacheValue(langComp); + + // Split compiler from arguments + std::vector cnameArgVec; + if (cname && !cname->empty()) { + cmExpandList(*cname, cnameArgVec); + cname = &cnameArgVec.front(); + } + std::string changeVars; if (cname && !optional) { std::string cnameString; diff --git a/Source/cmRulePlaceholderExpander.cxx b/Source/cmRulePlaceholderExpander.cxx index 254131b..4ff81c1 100644 --- a/Source/cmRulePlaceholderExpander.cxx +++ b/Source/cmRulePlaceholderExpander.cxx @@ -261,7 +261,7 @@ std::string cmRulePlaceholderExpander::ExpandRuleVariable( this->VariableMappings["CMAKE_" + compIt->second + "_COMPILE_OPTIONS_SYSROOT"]; - // if there is a required first argument to the compiler add it + // if there are required arguments to the compiler add it // to the compiler string if (!compilerArg1.empty()) { ret += " "; -- cgit v0.12 From deec2f587cd0bd08ee6866d06b9600dee98af910 Mon Sep 17 00:00:00 2001 From: Fred Baksik Date: Wed, 15 Jul 2020 15:03:45 -0400 Subject: Toolchain: Take CMAKE__FLAGS_INIT into account during compiler detection Fixes: #20040 --- Modules/CMakeDetermineCompilerId.cmake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Modules/CMakeDetermineCompilerId.cmake b/Modules/CMakeDetermineCompilerId.cmake index 832b18d..d7a35e1 100644 --- a/Modules/CMakeDetermineCompilerId.cmake +++ b/Modules/CMakeDetermineCompilerId.cmake @@ -14,8 +14,10 @@ function(CMAKE_DETERMINE_COMPILER_ID lang flagvar src) # Make sure user-specified compiler flags are used. if(CMAKE_${lang}_FLAGS) set(CMAKE_${lang}_COMPILER_ID_FLAGS ${CMAKE_${lang}_FLAGS}) - else() + elseif(DEFINED ENV{${flagvar}}) set(CMAKE_${lang}_COMPILER_ID_FLAGS $ENV{${flagvar}}) + else(CMAKE_${lang}_FLAGS_INIT) + set(CMAKE_${lang}_COMPILER_ID_FLAGS ${CMAKE_${lang}_FLAGS_INIT}) endif() string(REPLACE " " ";" CMAKE_${lang}_COMPILER_ID_FLAGS_LIST "${CMAKE_${lang}_COMPILER_ID_FLAGS}") -- cgit v0.12 From db486da2655c49ac9f6d770fd7d4475aa5354b9f Mon Sep 17 00:00:00 2001 From: Fred Baksik Date: Wed, 15 Jul 2020 15:03:46 -0400 Subject: Toolchain: Update documentation for initial compiler flags --- Help/envvar/ASM_DIALECT.rst | 8 ++++++++ Help/envvar/CC.rst | 8 ++++++++ Help/envvar/CSFLAGS.rst | 2 +- Help/envvar/CUDACXX.rst | 8 ++++++++ Help/envvar/CXX.rst | 8 ++++++++ Help/envvar/FC.rst | 8 ++++++++ Help/envvar/RC.rst | 8 ++++++++ Help/envvar/SWIFTC.rst | 8 ++++++++ Help/release/dev/compiler_flags.rst | 9 +++++++++ Help/variable/CMAKE_LANG_COMPILER.rst | 25 +++++++++++++++++++++++++ 10 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 Help/release/dev/compiler_flags.rst diff --git a/Help/envvar/ASM_DIALECT.rst b/Help/envvar/ASM_DIALECT.rst index 768d8fe..a606280 100644 --- a/Help/envvar/ASM_DIALECT.rst +++ b/Help/envvar/ASM_DIALECT.rst @@ -14,3 +14,11 @@ in the cache as :variable:`CMAKE_ASM_COMPILER _COMPILER>`. For subsequent configuration runs, the environment variable will be ignored in favor of :variable:`CMAKE_ASM_COMPILER _COMPILER>`. + +.. note:: + Options that are required to make the compiler work correctly can be included; + they can not be changed. + +.. code-block:: console + + $ export ASM="custom-compiler --arg1 --arg2" diff --git a/Help/envvar/CC.rst b/Help/envvar/CC.rst index 7d2b870..4c970b4 100644 --- a/Help/envvar/CC.rst +++ b/Help/envvar/CC.rst @@ -11,3 +11,11 @@ value for ``CC`` is stored in the cache as :variable:`CMAKE_C_COMPILER _COMPILER>`. For any configuration run (including the first), the environment variable will be ignored if the :variable:`CMAKE_C_COMPILER _COMPILER>` variable is defined. + +.. note:: + Options that are required to make the compiler work correctly can be included; + they can not be changed. + +.. code-block:: console + + $ export CC="custom-compiler --arg1 --arg2" diff --git a/Help/envvar/CSFLAGS.rst b/Help/envvar/CSFLAGS.rst index e8c0a11..d875209 100644 --- a/Help/envvar/CSFLAGS.rst +++ b/Help/envvar/CSFLAGS.rst @@ -5,7 +5,7 @@ CSFLAGS .. include:: ENV_VAR.txt -Preferred executable for compiling ``CSharp`` language files. Will only be +Default compilation flags to be used when compiling ``CSharp`` files. Will only be used by CMake on the first configuration to determine ``CSharp`` default compilation flags, after which the value for ``CSFLAGS`` is stored in the cache as :variable:`CMAKE_CSharp_FLAGS _FLAGS>`. For any configuration diff --git a/Help/envvar/CUDACXX.rst b/Help/envvar/CUDACXX.rst index 1affab6..69471b0 100644 --- a/Help/envvar/CUDACXX.rst +++ b/Help/envvar/CUDACXX.rst @@ -11,3 +11,11 @@ value for ``CUDA`` is stored in the cache as :variable:`CMAKE_CUDA_COMPILER _COMPILER>`. For any configuration run (including the first), the environment variable will be ignored if the :variable:`CMAKE_CUDA_COMPILER _COMPILER>` variable is defined. + +.. note:: + Options that are required to make the compiler work correctly can be included; + they can not be changed. + +.. code-block:: console + + $ export CUDACXX="custom-compiler --arg1 --arg2" diff --git a/Help/envvar/CXX.rst b/Help/envvar/CXX.rst index df927d5..908a521 100644 --- a/Help/envvar/CXX.rst +++ b/Help/envvar/CXX.rst @@ -11,3 +11,11 @@ value for ``CXX`` is stored in the cache as :variable:`CMAKE_CXX_COMPILER _COMPILER>`. For any configuration run (including the first), the environment variable will be ignored if the :variable:`CMAKE_CXX_COMPILER _COMPILER>` variable is defined. + +.. note:: + Options that are required to make the compiler work correctly can be included; + they can not be changed. + +.. code-block:: console + + $ export CXX="custom-compiler --arg1 --arg2" diff --git a/Help/envvar/FC.rst b/Help/envvar/FC.rst index f1c26b0..dcee02d 100644 --- a/Help/envvar/FC.rst +++ b/Help/envvar/FC.rst @@ -12,3 +12,11 @@ which the value for ``Fortran`` is stored in the cache as configuration run (including the first), the environment variable will be ignored if the :variable:`CMAKE_Fortran_COMPILER _COMPILER>` variable is defined. + +.. note:: + Options that are required to make the compiler work correctly can be included; + they can not be changed. + +.. code-block:: console + + $ export FC="custom-compiler --arg1 --arg2" diff --git a/Help/envvar/RC.rst b/Help/envvar/RC.rst index 3ec1788..adfaecc 100644 --- a/Help/envvar/RC.rst +++ b/Help/envvar/RC.rst @@ -11,3 +11,11 @@ value for ``RC`` is stored in the cache as :variable:`CMAKE_RC_COMPILER _COMPILER>`. For any configuration run (including the first), the environment variable will be ignored if the :variable:`CMAKE_RC_COMPILER _COMPILER>` variable is defined. + +.. note:: + Options that are required to make the compiler work correctly can be included; + they can not be changed. + +.. code-block:: console + + $ export RC="custom-compiler --arg1 --arg2" diff --git a/Help/envvar/SWIFTC.rst b/Help/envvar/SWIFTC.rst index ce01ca2..896e156 100644 --- a/Help/envvar/SWIFTC.rst +++ b/Help/envvar/SWIFTC.rst @@ -11,3 +11,11 @@ value for ``SWIFTC`` is stored in the cache as :variable:`CMAKE_Swift_COMPILER _COMPILER>`. For any configuration run (including the first), the environment variable will be ignored if the :variable:`CMAKE_Swift_COMPILER _COMPILER>` variable is defined. + +.. note:: + Options that are required to make the compiler work correctly can be included; + they can not be changed. + +.. code-block:: console + + $ export SWIFTC="custom-compiler --arg1 --arg2" diff --git a/Help/release/dev/compiler_flags.rst b/Help/release/dev/compiler_flags.rst new file mode 100644 index 0000000..7138e80 --- /dev/null +++ b/Help/release/dev/compiler_flags.rst @@ -0,0 +1,9 @@ +compiler_flags +----------------- + +* The :variable:`CMAKE__COMPILER` variable may now be used to + store "mandatory" compiler flags like the :envvar:`CC` and other environment variables. + +* The :variable:`CMAKE__FLAGS_INIT` variable will now be considered during + the compiler indentification check if other sources like :variable:`CMAKE__FLAGS` + or :envvar:`CFLAGS` are not set. diff --git a/Help/variable/CMAKE_LANG_COMPILER.rst b/Help/variable/CMAKE_LANG_COMPILER.rst index 89df495..e694b33 100644 --- a/Help/variable/CMAKE_LANG_COMPILER.rst +++ b/Help/variable/CMAKE_LANG_COMPILER.rst @@ -5,3 +5,28 @@ The full path to the compiler for ``LANG``. This is the command that will be used as the ```` compiler. Once set, you can not change this variable. + +Usage +^^^^^ + +This variable can be set by the user during the first time a build tree is configured. + +If a non-full path value is supplied then CMake will resolve the full path of +the compiler. + +The variable could be set in a user supplied toolchain file or via `-D` on the command line. + +.. note:: + Options that are required to make the compiler work correctly can be included + as items in a list; they can not be changed. + +.. code-block:: cmake + + #set within user supplied toolchain file + set(CMAKE_C_COMPILER /full/path/to/qcc --arg1 --arg2) + +or + +.. code-block:: console + + $ cmake ... -DCMAKE_C_COMPILER='qcc;--arg1;--arg2' -- cgit v0.12 From f76c20da6395c0830ac0de667c105b0b29f84c7a Mon Sep 17 00:00:00 2001 From: Fred Baksik Date: Mon, 20 Jul 2020 09:45:32 -0400 Subject: Toolchain: Test compiler initial settings --- Tests/RunCMake/CMakeLists.txt | 1 + Tests/RunCMake/CompilerArgs/C.cmake | 3 ++ Tests/RunCMake/CompilerArgs/CMakeLists.txt | 3 ++ Tests/RunCMake/CompilerArgs/CXX.cmake | 3 ++ Tests/RunCMake/CompilerArgs/FindCCompiler.cmake | 2 + Tests/RunCMake/CompilerArgs/FindCXXCompiler.cmake | 2 + Tests/RunCMake/CompilerArgs/RunCMakeTest.cmake | 58 +++++++++++++++++++++++ Tests/RunCMake/CompilerArgs/main.c | 10 ++++ Tests/RunCMake/CompilerArgs/main.cxx | 10 ++++ Tests/RunCMake/CompilerArgs/toolchain.cmake.in | 1 + 10 files changed, 93 insertions(+) create mode 100644 Tests/RunCMake/CompilerArgs/C.cmake create mode 100644 Tests/RunCMake/CompilerArgs/CMakeLists.txt create mode 100644 Tests/RunCMake/CompilerArgs/CXX.cmake create mode 100644 Tests/RunCMake/CompilerArgs/FindCCompiler.cmake create mode 100644 Tests/RunCMake/CompilerArgs/FindCXXCompiler.cmake create mode 100644 Tests/RunCMake/CompilerArgs/RunCMakeTest.cmake create mode 100644 Tests/RunCMake/CompilerArgs/main.c create mode 100644 Tests/RunCMake/CompilerArgs/main.cxx create mode 100644 Tests/RunCMake/CompilerArgs/toolchain.cmake.in diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 36b016f..5c35040 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -624,6 +624,7 @@ if("${CMAKE_GENERATOR}" MATCHES "Make|Ninja") set_property(TEST RunCMake.CompilerLauncher APPEND PROPERTY LABELS "CUDA") add_RunCMake_test(ctest_labels_for_subprojects) + add_RunCMake_test(CompilerArgs) endif() set(cpack_tests diff --git a/Tests/RunCMake/CompilerArgs/C.cmake b/Tests/RunCMake/CompilerArgs/C.cmake new file mode 100644 index 0000000..96b004b --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/C.cmake @@ -0,0 +1,3 @@ +enable_language(C) +set(CMAKE_VERBOSE_MAKEFILE TRUE) +add_executable(main main.c) diff --git a/Tests/RunCMake/CompilerArgs/CMakeLists.txt b/Tests/RunCMake/CompilerArgs/CMakeLists.txt new file mode 100644 index 0000000..18dfd26 --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.2) +project(${RunCMake_TEST} NONE) +include(${RunCMake_TEST}.cmake) diff --git a/Tests/RunCMake/CompilerArgs/CXX.cmake b/Tests/RunCMake/CompilerArgs/CXX.cmake new file mode 100644 index 0000000..3d2ee00 --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/CXX.cmake @@ -0,0 +1,3 @@ +enable_language(CXX) +set(CMAKE_VERBOSE_MAKEFILE TRUE) +add_executable(main main.cxx) diff --git a/Tests/RunCMake/CompilerArgs/FindCCompiler.cmake b/Tests/RunCMake/CompilerArgs/FindCCompiler.cmake new file mode 100644 index 0000000..aeaaf7f --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/FindCCompiler.cmake @@ -0,0 +1,2 @@ +enable_language(C) +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/C_comp.cmake" "set(temp_CMAKE_C_COMPILER \"${CMAKE_C_COMPILER}\")\n") diff --git a/Tests/RunCMake/CompilerArgs/FindCXXCompiler.cmake b/Tests/RunCMake/CompilerArgs/FindCXXCompiler.cmake new file mode 100644 index 0000000..663ac83 --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/FindCXXCompiler.cmake @@ -0,0 +1,2 @@ +enable_language(CXX) +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/CXX_comp.cmake" "set(temp_CMAKE_CXX_COMPILER \"${CMAKE_CXX_COMPILER}\")\n") diff --git a/Tests/RunCMake/CompilerArgs/RunCMakeTest.cmake b/Tests/RunCMake/CompilerArgs/RunCMakeTest.cmake new file mode 100644 index 0000000..9e5a18a --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/RunCMakeTest.cmake @@ -0,0 +1,58 @@ +include(RunCMake) + +function(find_compiler lang) + # Detect the compiler in use in the current environment. + run_cmake(Find${lang}Compiler) + # Use the detected compiler + include(${RunCMake_BINARY_DIR}/Find${lang}Compiler-build/${lang}_comp.cmake) + if(NOT temp_CMAKE_${lang}_COMPILER) + message(FATAL_ERROR "FindCompiler provided no compiler!") + endif() + # Create a toolchain file + set(__test_compiler_var CMAKE_${lang}_COMPILER) + set(__test_compiler "${temp_CMAKE_${lang}_COMPILER}") + configure_file(${RunCMake_SOURCE_DIR}/toolchain.cmake.in + ${RunCMake_BINARY_DIR}/Find${lang}Compiler-build/toolchain_${lang}_comp.cmake @ONLY) +endfunction() + +function(run_compiler_env lang) + # Use the correct compiler + include(${RunCMake_BINARY_DIR}/Find${lang}Compiler-build/${lang}_comp.cmake) + + # Use a single build tree for tests without cleaning. + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${lang}-env-build) + set(RunCMake_TEST_NO_CLEAN 1) + file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}") + file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}") + + # Set the compiler + if(lang STREQUAL "C") + set(ENV{CC} "'${temp_CMAKE_${lang}_COMPILER}' -DFOO1 -DFOO2") + else() + set(ENV{${lang}} "'${temp_CMAKE_${lang}_COMPILER}' -DFOO1 -DFOO2") + endif() + + run_cmake(${lang}) + run_cmake_command(${lang}-Build ${CMAKE_COMMAND} --build . ${verbose_args}) +endfunction() + +function(run_compiler_tc lang) + # Use a single build tree for tests without cleaning. + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${lang}-tc-build) + set(RunCMake_TEST_NO_CLEAN 1) + file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}") + file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}") + + set(RunCMake_TEST_OPTIONS + -DCMAKE_TOOLCHAIN_FILE=${RunCMake_BINARY_DIR}/Find${lang}Compiler-build/toolchain_${lang}_comp.cmake) + run_cmake(${lang}) + run_cmake_command(${lang}-Build ${CMAKE_COMMAND} --build . ${verbose_args}) +endfunction() + +set(langs C CXX) + +foreach(lang ${langs}) + find_compiler(${lang}) + run_compiler_env(${lang}) + run_compiler_tc(${lang}) +endforeach() diff --git a/Tests/RunCMake/CompilerArgs/main.c b/Tests/RunCMake/CompilerArgs/main.c new file mode 100644 index 0000000..b526135 --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/main.c @@ -0,0 +1,10 @@ +#ifndef FOO1 +# error Missing FOO1 +#endif +#ifndef FOO2 +# error Missing FOO2 +#endif +int main(void) +{ + return 0; +} diff --git a/Tests/RunCMake/CompilerArgs/main.cxx b/Tests/RunCMake/CompilerArgs/main.cxx new file mode 100644 index 0000000..db90e93 --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/main.cxx @@ -0,0 +1,10 @@ +#ifndef FOO1 +# error Missing FOO1 +#endif +#ifndef FOO2 +# error Missing FOO2 +#endif +int main() +{ + return 0; +} diff --git a/Tests/RunCMake/CompilerArgs/toolchain.cmake.in b/Tests/RunCMake/CompilerArgs/toolchain.cmake.in new file mode 100644 index 0000000..ff77639 --- /dev/null +++ b/Tests/RunCMake/CompilerArgs/toolchain.cmake.in @@ -0,0 +1 @@ +set(@__test_compiler_var@ "@__test_compiler@" -DFOO1 -DFOO2) -- cgit v0.12