From 869904271ba0e8b855043894360b5dc7fe1e3b56 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 26 Jun 2017 13:48:21 -0400 Subject: [PATCH 1/2] Tests: Enable languages explicitly in RunCMake.target_compile_features Enable C or CXX (or nothing) as needed in each test case. This will allow us to add test cases that do not enable CXX. --- Tests/RunCMake/target_compile_features/CMakeLists.txt | 2 +- Tests/RunCMake/target_compile_features/alias_target-stderr.txt | 2 +- Tests/RunCMake/target_compile_features/alias_target.cmake | 1 + .../RunCMake/target_compile_features/imported_target-stderr.txt | 2 +- Tests/RunCMake/target_compile_features/imported_target.cmake | 1 + Tests/RunCMake/target_compile_features/invalid_args-stderr.txt | 2 +- Tests/RunCMake/target_compile_features/invalid_args.cmake | 1 + .../invalid_args_on_interface-stderr.txt | 2 +- .../target_compile_features/invalid_args_on_interface.cmake | 1 + .../target_compile_features/no_matching_c_feature-stderr.txt | 2 +- .../target_compile_features/no_matching_c_feature.cmake | 1 + .../target_compile_features/no_matching_cxx_feature-stderr.txt | 2 +- .../target_compile_features/no_matching_cxx_feature.cmake | 1 + Tests/RunCMake/target_compile_features/no_target-stderr.txt | 2 +- Tests/RunCMake/target_compile_features/no_target.cmake | 1 + .../RunCMake/target_compile_features/not_a_c_feature-stderr.txt | 2 +- Tests/RunCMake/target_compile_features/not_a_c_feature.cmake | 1 + .../target_compile_features/not_a_cxx_feature-stderr.txt | 2 +- Tests/RunCMake/target_compile_features/not_a_cxx_feature.cmake | 1 + .../RunCMake/target_compile_features/not_enough_args-stderr.txt | 2 +- Tests/RunCMake/target_compile_features/not_enough_args.cmake | 1 + .../RunCMake/target_compile_features/utility_target-stderr.txt | 2 +- 22 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Tests/RunCMake/target_compile_features/CMakeLists.txt b/Tests/RunCMake/target_compile_features/CMakeLists.txt index 3482e6baf2..2897109554 100644 --- a/Tests/RunCMake/target_compile_features/CMakeLists.txt +++ b/Tests/RunCMake/target_compile_features/CMakeLists.txt @@ -1,3 +1,3 @@ cmake_minimum_required(VERSION 3.0) -project(${RunCMake_TEST} CXX) +project(${RunCMake_TEST} NONE) include(${RunCMake_TEST}.cmake) diff --git a/Tests/RunCMake/target_compile_features/alias_target-stderr.txt b/Tests/RunCMake/target_compile_features/alias_target-stderr.txt index 417bf62aae..5ebe170c36 100644 --- a/Tests/RunCMake/target_compile_features/alias_target-stderr.txt +++ b/Tests/RunCMake/target_compile_features/alias_target-stderr.txt @@ -1,4 +1,4 @@ -CMake Error at alias_target.cmake:4 \(target_compile_features\): +CMake Error at alias_target.cmake:[0-9]+ \(target_compile_features\): target_compile_features can not be used on an ALIAS target. Call Stack \(most recent call first\): CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/target_compile_features/alias_target.cmake b/Tests/RunCMake/target_compile_features/alias_target.cmake index d35ddba541..6fcdadaa59 100644 --- a/Tests/RunCMake/target_compile_features/alias_target.cmake +++ b/Tests/RunCMake/target_compile_features/alias_target.cmake @@ -1,3 +1,4 @@ +enable_language(CXX) add_executable(main empty.cpp) add_executable(Alias::Main ALIAS main) diff --git a/Tests/RunCMake/target_compile_features/imported_target-stderr.txt b/Tests/RunCMake/target_compile_features/imported_target-stderr.txt index c6ff5ec216..7a07427a00 100644 --- a/Tests/RunCMake/target_compile_features/imported_target-stderr.txt +++ b/Tests/RunCMake/target_compile_features/imported_target-stderr.txt @@ -1,4 +1,4 @@ -CMake Error at imported_target.cmake:3 \(target_compile_features\): +CMake Error at imported_target.cmake:[0-9]+ \(target_compile_features\): Cannot specify compile features for imported target "main". Call Stack \(most recent call first\): CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/target_compile_features/imported_target.cmake b/Tests/RunCMake/target_compile_features/imported_target.cmake index e248c2feea..e886ce9224 100644 --- a/Tests/RunCMake/target_compile_features/imported_target.cmake +++ b/Tests/RunCMake/target_compile_features/imported_target.cmake @@ -1,3 +1,4 @@ +enable_language(CXX) add_library(main INTERFACE IMPORTED) target_compile_features(main INTERFACE cxx_delegating_constructors) diff --git a/Tests/RunCMake/target_compile_features/invalid_args-stderr.txt b/Tests/RunCMake/target_compile_features/invalid_args-stderr.txt index bd5b7b9d54..9917be77a1 100644 --- a/Tests/RunCMake/target_compile_features/invalid_args-stderr.txt +++ b/Tests/RunCMake/target_compile_features/invalid_args-stderr.txt @@ -1,4 +1,4 @@ -CMake Error at invalid_args.cmake:3 \(target_compile_features\): +CMake Error at invalid_args.cmake:[0-9]+ \(target_compile_features\): target_compile_features called with invalid arguments Call Stack \(most recent call first\): CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/target_compile_features/invalid_args.cmake b/Tests/RunCMake/target_compile_features/invalid_args.cmake index 1a7fb3749a..cc051a445d 100644 --- a/Tests/RunCMake/target_compile_features/invalid_args.cmake +++ b/Tests/RunCMake/target_compile_features/invalid_args.cmake @@ -1,3 +1,4 @@ +enable_language(CXX) add_executable(main empty.cpp) target_compile_features(main INVALID cxx_delegating_constructors) diff --git a/Tests/RunCMake/target_compile_features/invalid_args_on_interface-stderr.txt b/Tests/RunCMake/target_compile_features/invalid_args_on_interface-stderr.txt index c30209a28b..37089983a3 100644 --- a/Tests/RunCMake/target_compile_features/invalid_args_on_interface-stderr.txt +++ b/Tests/RunCMake/target_compile_features/invalid_args_on_interface-stderr.txt @@ -1,4 +1,4 @@ -CMake Error at invalid_args_on_interface.cmake:3 \(target_compile_features\): +CMake Error at invalid_args_on_interface.cmake:[0-9]+ \(target_compile_features\): target_compile_features may only be set INTERFACE properties on INTERFACE targets Call Stack \(most recent call first\): diff --git a/Tests/RunCMake/target_compile_features/invalid_args_on_interface.cmake b/Tests/RunCMake/target_compile_features/invalid_args_on_interface.cmake index 324d0f3846..49d2d827dd 100644 --- a/Tests/RunCMake/target_compile_features/invalid_args_on_interface.cmake +++ b/Tests/RunCMake/target_compile_features/invalid_args_on_interface.cmake @@ -1,3 +1,4 @@ +enable_language(CXX) add_library(main INTERFACE) target_compile_features(main PRIVATE cxx_delegating_constructors) diff --git a/Tests/RunCMake/target_compile_features/no_matching_c_feature-stderr.txt b/Tests/RunCMake/target_compile_features/no_matching_c_feature-stderr.txt index 96b959cf52..1875d1209e 100644 --- a/Tests/RunCMake/target_compile_features/no_matching_c_feature-stderr.txt +++ b/Tests/RunCMake/target_compile_features/no_matching_c_feature-stderr.txt @@ -1,4 +1,4 @@ -CMake Error at no_matching_c_feature.cmake:[0-9][0-9]? \((target_compile_features|message)\): +CMake Error at no_matching_c_feature.cmake:[0-9]+ \((target_compile_features|message)\): The compiler feature "gnu_c_dummy" is not known to C compiler "GNU" diff --git a/Tests/RunCMake/target_compile_features/no_matching_c_feature.cmake b/Tests/RunCMake/target_compile_features/no_matching_c_feature.cmake index a44caf29a3..0e93b41ffc 100644 --- a/Tests/RunCMake/target_compile_features/no_matching_c_feature.cmake +++ b/Tests/RunCMake/target_compile_features/no_matching_c_feature.cmake @@ -1,3 +1,4 @@ +enable_language(CXX) if (NOT ";${CMAKE_C_COMPILE_FEATURES};" MATCHES ";gnu_c_typeof;") # Simulate passing the test. diff --git a/Tests/RunCMake/target_compile_features/no_matching_cxx_feature-stderr.txt b/Tests/RunCMake/target_compile_features/no_matching_cxx_feature-stderr.txt index f976dfeab7..90d41c9576 100644 --- a/Tests/RunCMake/target_compile_features/no_matching_cxx_feature-stderr.txt +++ b/Tests/RunCMake/target_compile_features/no_matching_cxx_feature-stderr.txt @@ -1,4 +1,4 @@ -CMake Error at no_matching_cxx_feature.cmake:[0-9][0-9]? \((target_compile_features|message)\): +CMake Error at no_matching_cxx_feature.cmake:[0-9]+ \((target_compile_features|message)\): The compiler feature "[^"]+" is not known to CXX compiler "[^"]*" diff --git a/Tests/RunCMake/target_compile_features/no_matching_cxx_feature.cmake b/Tests/RunCMake/target_compile_features/no_matching_cxx_feature.cmake index ab1fd76088..4ee3445e85 100644 --- a/Tests/RunCMake/target_compile_features/no_matching_cxx_feature.cmake +++ b/Tests/RunCMake/target_compile_features/no_matching_cxx_feature.cmake @@ -1,3 +1,4 @@ +enable_language(CXX) if (NOT ";${CMAKE_CXX_COMPILE_FEATURES};" MATCHES ";gnu_cxx_typeof;" AND NOT ";${CMAKE_CXX_COMPILE_FEATURES};" MATCHES ";msvc_cxx_sealed;" ) diff --git a/Tests/RunCMake/target_compile_features/no_target-stderr.txt b/Tests/RunCMake/target_compile_features/no_target-stderr.txt index 323ba7a901..65974b4d6c 100644 --- a/Tests/RunCMake/target_compile_features/no_target-stderr.txt +++ b/Tests/RunCMake/target_compile_features/no_target-stderr.txt @@ -1,4 +1,4 @@ -CMake Error at no_target.cmake:2 \(target_compile_features\): +CMake Error at no_target.cmake:[0-9]+ \(target_compile_features\): Cannot specify compile features for target "main" which is not built by this project. Call Stack \(most recent call first\): diff --git a/Tests/RunCMake/target_compile_features/no_target.cmake b/Tests/RunCMake/target_compile_features/no_target.cmake index 3f0afe273f..a514de86a7 100644 --- a/Tests/RunCMake/target_compile_features/no_target.cmake +++ b/Tests/RunCMake/target_compile_features/no_target.cmake @@ -1,2 +1,3 @@ +enable_language(CXX) target_compile_features(main INTERFACE cxx_delegating_constructors) diff --git a/Tests/RunCMake/target_compile_features/not_a_c_feature-stderr.txt b/Tests/RunCMake/target_compile_features/not_a_c_feature-stderr.txt index 6dd00f359a..493c5826c6 100644 --- a/Tests/RunCMake/target_compile_features/not_a_c_feature-stderr.txt +++ b/Tests/RunCMake/target_compile_features/not_a_c_feature-stderr.txt @@ -1,4 +1,4 @@ -CMake Error at not_a_c_feature.cmake:3 \(target_compile_features\): +CMake Error at not_a_c_feature.cmake:[0-9]+ \(target_compile_features\): target_compile_features specified unknown feature "c_not_a_feature" for target "main". Call Stack \(most recent call first\): diff --git a/Tests/RunCMake/target_compile_features/not_a_c_feature.cmake b/Tests/RunCMake/target_compile_features/not_a_c_feature.cmake index 0420698bc9..f5d70d0935 100644 --- a/Tests/RunCMake/target_compile_features/not_a_c_feature.cmake +++ b/Tests/RunCMake/target_compile_features/not_a_c_feature.cmake @@ -1,3 +1,4 @@ +enable_language(C) add_executable(main empty.c) target_compile_features(main diff --git a/Tests/RunCMake/target_compile_features/not_a_cxx_feature-stderr.txt b/Tests/RunCMake/target_compile_features/not_a_cxx_feature-stderr.txt index efa2bad5f8..3dbf0e6549 100644 --- a/Tests/RunCMake/target_compile_features/not_a_cxx_feature-stderr.txt +++ b/Tests/RunCMake/target_compile_features/not_a_cxx_feature-stderr.txt @@ -1,4 +1,4 @@ -CMake Error at not_a_cxx_feature.cmake:3 \(target_compile_features\): +CMake Error at not_a_cxx_feature.cmake:[0-9]+ \(target_compile_features\): target_compile_features specified unknown feature "cxx_not_a_feature" for target "main". Call Stack \(most recent call first\): diff --git a/Tests/RunCMake/target_compile_features/not_a_cxx_feature.cmake b/Tests/RunCMake/target_compile_features/not_a_cxx_feature.cmake index 0207b724de..bc3a8c4f68 100644 --- a/Tests/RunCMake/target_compile_features/not_a_cxx_feature.cmake +++ b/Tests/RunCMake/target_compile_features/not_a_cxx_feature.cmake @@ -1,3 +1,4 @@ +enable_language(CXX) add_executable(main empty.cpp) target_compile_features(main diff --git a/Tests/RunCMake/target_compile_features/not_enough_args-stderr.txt b/Tests/RunCMake/target_compile_features/not_enough_args-stderr.txt index 2f8d812431..c0c2efac48 100644 --- a/Tests/RunCMake/target_compile_features/not_enough_args-stderr.txt +++ b/Tests/RunCMake/target_compile_features/not_enough_args-stderr.txt @@ -1,4 +1,4 @@ -CMake Error at not_enough_args.cmake:3 \(target_compile_features\): +CMake Error at not_enough_args.cmake:[0-9]+ \(target_compile_features\): target_compile_features called with incorrect number of arguments Call Stack \(most recent call first\): CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/target_compile_features/not_enough_args.cmake b/Tests/RunCMake/target_compile_features/not_enough_args.cmake index 95612300ad..f5b719863c 100644 --- a/Tests/RunCMake/target_compile_features/not_enough_args.cmake +++ b/Tests/RunCMake/target_compile_features/not_enough_args.cmake @@ -1,3 +1,4 @@ +enable_language(CXX) add_executable(main empty.cpp) target_compile_features(main) diff --git a/Tests/RunCMake/target_compile_features/utility_target-stderr.txt b/Tests/RunCMake/target_compile_features/utility_target-stderr.txt index d23905995e..ff03310d13 100644 --- a/Tests/RunCMake/target_compile_features/utility_target-stderr.txt +++ b/Tests/RunCMake/target_compile_features/utility_target-stderr.txt @@ -1,4 +1,4 @@ -CMake Error at utility_target.cmake:4 \(target_compile_features\): +CMake Error at utility_target.cmake:[0-9]+ \(target_compile_features\): target_compile_features called with non-compilable target type Call Stack \(most recent call first\): CMakeLists.txt:3 \(include\) From e03a1b3b6185e36a5905db3d2551c9575fb074aa Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 26 Jun 2017 13:52:26 -0400 Subject: [PATCH 2/2] target_compile_features: Do not crash on non-enabled language Fixes: #17011 --- Source/cmMakefile.cxx | 21 +++++++++++++++++-- .../RunCMakeTest.cmake | 1 + .../cxx_not_enabled-result.txt | 1 + .../cxx_not_enabled-stderr.txt | 4 ++++ .../cxx_not_enabled.cmake | 2 ++ 5 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 Tests/RunCMake/target_compile_features/cxx_not_enabled-result.txt create mode 100644 Tests/RunCMake/target_compile_features/cxx_not_enabled-stderr.txt create mode 100644 Tests/RunCMake/target_compile_features/cxx_not_enabled.cmake diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index 802a427a66..608b18a46a 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -4190,6 +4190,23 @@ bool cmMakefile::CompileFeatureKnown(cmTarget const* target, const char* cmMakefile::CompileFeaturesAvailable(const std::string& lang, std::string* error) const { + if (!this->GlobalGenerator->GetLanguageEnabled(lang)) { + std::ostringstream e; + if (error) { + e << "cannot"; + } else { + e << "Cannot"; + } + e << " use features from non-enabled language " << lang; + if (error) { + *error = e.str(); + } else { + this->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(), + this->Backtrace); + } + return CM_NULLPTR; + } + const char* featuresKnown = this->GetDefinition("CMAKE_" + lang + "_COMPILE_FEATURES"); @@ -4201,9 +4218,9 @@ const char* cmMakefile::CompileFeaturesAvailable(const std::string& lang, e << "No"; } e << " known features for " << lang << " compiler\n\"" - << this->GetDefinition("CMAKE_" + lang + "_COMPILER_ID") + << this->GetSafeDefinition("CMAKE_" + lang + "_COMPILER_ID") << "\"\nversion " - << this->GetDefinition("CMAKE_" + lang + "_COMPILER_VERSION") << "."; + << this->GetSafeDefinition("CMAKE_" + lang + "_COMPILER_VERSION") << "."; if (error) { *error = e.str(); } else { diff --git a/Tests/RunCMake/target_compile_features/RunCMakeTest.cmake b/Tests/RunCMake/target_compile_features/RunCMakeTest.cmake index 33faf2bbb8..1f67f11a09 100644 --- a/Tests/RunCMake/target_compile_features/RunCMakeTest.cmake +++ b/Tests/RunCMake/target_compile_features/RunCMakeTest.cmake @@ -11,3 +11,4 @@ run_cmake(not_a_cxx_feature) run_cmake(no_matching_cxx_feature) run_cmake(not_a_c_feature) run_cmake(no_matching_c_feature) +run_cmake(cxx_not_enabled) diff --git a/Tests/RunCMake/target_compile_features/cxx_not_enabled-result.txt b/Tests/RunCMake/target_compile_features/cxx_not_enabled-result.txt new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/Tests/RunCMake/target_compile_features/cxx_not_enabled-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/target_compile_features/cxx_not_enabled-stderr.txt b/Tests/RunCMake/target_compile_features/cxx_not_enabled-stderr.txt new file mode 100644 index 0000000000..4f707c7b9a --- /dev/null +++ b/Tests/RunCMake/target_compile_features/cxx_not_enabled-stderr.txt @@ -0,0 +1,4 @@ +^CMake Error at cxx_not_enabled.cmake:[0-9]+ \(target_compile_features\): + target_compile_features cannot use features from non-enabled language CXX +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\)$ diff --git a/Tests/RunCMake/target_compile_features/cxx_not_enabled.cmake b/Tests/RunCMake/target_compile_features/cxx_not_enabled.cmake new file mode 100644 index 0000000000..b7e91196e4 --- /dev/null +++ b/Tests/RunCMake/target_compile_features/cxx_not_enabled.cmake @@ -0,0 +1,2 @@ +add_executable(main empty.c) +target_compile_features(main PRIVATE cxx_decltype)