From 8d0b1ccac13389255a318422d38b246cf47d9ace Mon Sep 17 00:00:00 2001
From: Stephen Kelly <steveire@gmail.com>
Date: Tue, 15 Apr 2014 18:09:21 +0200
Subject: Features: FATAL_ERROR on compilers with no recorded features.

Users of the new target_compile_features command are expected to
check the existence of the CMAKE_CXX_COMPILE_FEATURES variable before
attempting to use it to require features.
---
 Source/cmMakefile.cxx                              | 24 ++++++++++++++++++++--
 .../NoSupportedCxxFeatures-result.txt              |  1 +
 .../NoSupportedCxxFeatures-stderr.txt              |  8 ++++++++
 .../CompileFeatures/NoSupportedCxxFeatures.cmake   |  3 +++
 .../NoSupportedCxxFeaturesGenex-result.txt         |  1 +
 .../NoSupportedCxxFeaturesGenex-stderr.txt         |  6 ++++++
 .../NoSupportedCxxFeaturesGenex.cmake              |  3 +++
 Tests/RunCMake/CompileFeatures/RunCMakeTest.cmake  | 11 ++++++++++
 .../CompileFeatures/generate_feature_list.cmake    |  4 ++++
 9 files changed, 59 insertions(+), 2 deletions(-)
 create mode 100644 Tests/RunCMake/CompileFeatures/NoSupportedCxxFeatures-result.txt
 create mode 100644 Tests/RunCMake/CompileFeatures/NoSupportedCxxFeatures-stderr.txt
 create mode 100644 Tests/RunCMake/CompileFeatures/NoSupportedCxxFeatures.cmake
 create mode 100644 Tests/RunCMake/CompileFeatures/NoSupportedCxxFeaturesGenex-result.txt
 create mode 100644 Tests/RunCMake/CompileFeatures/NoSupportedCxxFeaturesGenex-stderr.txt
 create mode 100644 Tests/RunCMake/CompileFeatures/NoSupportedCxxFeaturesGenex.cmake
 create mode 100644 Tests/RunCMake/CompileFeatures/generate_feature_list.cmake

diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx
index 6ec40fb..07cfe12 100644
--- a/Source/cmMakefile.cxx
+++ b/Source/cmMakefile.cxx
@@ -4604,8 +4604,28 @@ AddRequiredTargetFeature(cmTarget *target, const std::string& feature,
 
   if (!featuresKnown || !*featuresKnown)
     {
-    // We know of no features for the compiler at all.
-    return true;
+    cmOStringStream e;
+    if (error)
+      {
+      e << "no";
+      }
+    else
+      {
+      e << "No";
+      }
+    e << " known features for compiler\n\""
+      << this->GetDefinition("CMAKE_" + lang + "_COMPILER_ID")
+      << "\"\nversion "
+      << this->GetDefinition("CMAKE_" + lang + "_COMPILER_VERSION") << ".";
+    if (error)
+      {
+      *error = e.str();
+      }
+    else
+      {
+      this->IssueMessage(cmake::FATAL_ERROR, e.str());
+      }
+    return false;
     }
 
   std::vector<std::string> availableFeatures;
diff --git a/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeatures-result.txt b/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeatures-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeatures-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeatures-stderr.txt b/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeatures-stderr.txt
new file mode 100644
index 0000000..8b029ac
--- /dev/null
+++ b/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeatures-stderr.txt
@@ -0,0 +1,8 @@
+CMake Error at NoSupportedCxxFeatures.cmake:3 \(target_compile_features\):
+  target_compile_features no known features for compiler
+
+  "[^"]*"
+
+  version *[.0-9]+\.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)
diff --git a/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeatures.cmake b/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeatures.cmake
new file mode 100644
index 0000000..5121948
--- /dev/null
+++ b/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeatures.cmake
@@ -0,0 +1,3 @@
+
+add_library(no_features empty.cpp)
+target_compile_features(no_features PRIVATE cxx_constexpr)
diff --git a/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeaturesGenex-result.txt b/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeaturesGenex-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeaturesGenex-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeaturesGenex-stderr.txt b/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeaturesGenex-stderr.txt
new file mode 100644
index 0000000..d8366b2
--- /dev/null
+++ b/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeaturesGenex-stderr.txt
@@ -0,0 +1,6 @@
+CMake Error in CMakeLists.txt:
+  No known features for compiler
+
+  "[^"]*"
+
+  version *[.0-9]+\.
diff --git a/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeaturesGenex.cmake b/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeaturesGenex.cmake
new file mode 100644
index 0000000..490f187
--- /dev/null
+++ b/Tests/RunCMake/CompileFeatures/NoSupportedCxxFeaturesGenex.cmake
@@ -0,0 +1,3 @@
+
+add_library(no_features empty.cpp)
+target_compile_features(no_features PRIVATE $<1:cxx_constexpr>)
diff --git a/Tests/RunCMake/CompileFeatures/RunCMakeTest.cmake b/Tests/RunCMake/CompileFeatures/RunCMakeTest.cmake
index a6aeeee..43d4cb3 100644
--- a/Tests/RunCMake/CompileFeatures/RunCMakeTest.cmake
+++ b/Tests/RunCMake/CompileFeatures/RunCMakeTest.cmake
@@ -7,3 +7,14 @@ run_cmake(NotAFeature_OriginDebug)
 run_cmake(NotAFeature_OriginDebugGenex)
 run_cmake(NotAFeature_OriginDebugTransitive)
 run_cmake(NotAFeature_OriginDebug_target_compile_features)
+
+run_cmake(generate_feature_list)
+file(READ
+  "${RunCMake_BINARY_DIR}/generate_feature_list-build/features.txt"
+  FEATURES
+)
+
+if (NOT FEATURES)
+  run_cmake(NoSupportedCxxFeatures)
+  run_cmake(NoSupportedCxxFeaturesGenex)
+endif()
diff --git a/Tests/RunCMake/CompileFeatures/generate_feature_list.cmake b/Tests/RunCMake/CompileFeatures/generate_feature_list.cmake
new file mode 100644
index 0000000..2bbbd17
--- /dev/null
+++ b/Tests/RunCMake/CompileFeatures/generate_feature_list.cmake
@@ -0,0 +1,4 @@
+
+file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/features.txt"
+  "${CMAKE_CXX_COMPILE_FEATURES}"
+)
-- 
cgit v0.12