Hi Brad,
please find attached a first attempt to implement OPTIONAL for
find_package. It is supposed to suppress all warnings and indicate the
negative result by a single line of output. Unfortunately, I still get a
warning and I could figure out how to simply print a message without the
warning. Could you give me some hints and comment on my patch?

Thanks
Christoph

-- 
[..] Mathematicians are like theologians: we regard
existence as the prime attribute of what we study.
But unlike theologians, we need not always rely upon
faith alone.                      [Lawrence Evans]

Am 10.10.2016 um 14:51 schrieb Brad King:
> On 10/09/2016 03:24 PM, Christoph Grüninger wrote:
>> * or loosing the output of tests and feature summary, as both tests are 
>> QUIET.
>>
>> I'd prefer to write the error message to the CMakeError.log and reduce
>> the output to one line. I think, the current behavior must be considered
>> a bug.
> 
> The default behavior evolved over many years to satisfy many use cases and
> give people the information they need to resolve real problems.  It is not
> going to change.  However, if you'd like to propose additional options to
> these interfaces to get the behavior you want then that would be fine.
> Perhaps find_package could gain an ``OPTIONAL`` switch.  I'm not very
> familiar with feature_summary so I'm not sure what that would need off the
> top of my head.
> 
> -Brad
>From efebb5e08dacb5eedc9cd2c809f2a937a156de8c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Christoph=20Gr=C3=83=C2=BCninger?=
 <gruen...@dune-project.org>
Date: Tue, 25 Apr 2017 06:41:31 +0200
Subject: [PATCH] cmFindPackageCommand: Add OPTIONAL

If OPTIONAL is set and the package is not found, just a single
line of output is written and despite QUIET the package appears
in the feature summary.
---
 Help/command/find_package.rst   | 18 ++++++++++++------
 Source/cmFindPackageCommand.cxx | 24 ++++++++++++++++++++++--
 Source/cmFindPackageCommand.h   |  1 +
 3 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/Help/command/find_package.rst b/Help/command/find_package.rst
index 60a77b85e..61b31ac46 100644
--- a/Help/command/find_package.rst
+++ b/Help/command/find_package.rst
@@ -5,8 +5,8 @@ Load settings for an external project.
 
 ::
 
-  find_package(<package> [version] [EXACT] [QUIET] [MODULE]
-               [REQUIRED] [[COMPONENTS] [components...]]
+  find_package(<package> [version] [EXACT] [OPTIONAL] [QUIET]
+               [MODULE] [REQUIRED] [[COMPONENTS] [components...]]
                [OPTIONAL_COMPONENTS components...]
                [NO_POLICY_SCOPE])
 
@@ -14,7 +14,10 @@ Finds and loads settings from an external project.  ``<package>_FOUND``
 will be set to indicate whether the package was found.  When the
 package is found package-specific information is provided through
 variables and :ref:`Imported Targets` documented by the package itself.  The
-``QUIET`` option disables messages if the package cannot be found.  The
+``OPTIONAL`` option reduces the lengthy warning message to a single
+line of output if the package cannot be found.  The ``QUIET`` option
+disables messages if the package cannot be found and suppresses the
+feature summary for this package.  The
 ``MODULE`` option disables the second signature documented below.  The
 ``REQUIRED`` option stops processing with an error message if the package
 cannot be found.
@@ -54,7 +57,7 @@ option is not given the command proceeds to Config mode.
 
 The complete Config mode command signature is::
 
-  find_package(<package> [version] [EXACT] [QUIET]
+  find_package(<package> [version] [EXACT] [OPTIONAL] [QUIET]
                [REQUIRED] [[COMPONENTS] [components...]]
                [CONFIG|NO_MODULE]
                [NO_POLICY_SCOPE]
@@ -100,8 +103,11 @@ version are stored in the cmake variable ``<package>_CONSIDERED_CONFIGS``,
 the associated versions in ``<package>_CONSIDERED_VERSIONS``.
 
 If the package configuration file cannot be found CMake will generate
-an error describing the problem unless the ``QUIET`` argument is
-specified.  If ``REQUIRED`` is specified and the package is not found a
+an error describing the problem.  When the ``OPTIONAL`` argument is
+specified, the error is reduced to a single line containing the package
+status.  When the ``QUIET`` argument is specified, the error and
+possible feature summary entries are suppressed.
+If ``REQUIRED`` is specified and the package is not found a
 fatal error is generated and the configure step stops executing.  If
 ``<package>_DIR`` has been set to a directory not containing a
 configuration file CMake will ignore it and search from scratch.
diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx
index 17fa92ccd..2f899a38f 100644
--- a/Source/cmFindPackageCommand.cxx
+++ b/Source/cmFindPackageCommand.cxx
@@ -83,6 +83,7 @@ void cmFindPackageCommand::Sort(std::vector<std::string>::iterator begin,
 cmFindPackageCommand::cmFindPackageCommand()
 {
   this->CMakePathName = "PACKAGE";
+  this->Optional = false;
   this->Quiet = false;
   this->Required = false;
   this->NoUserRegistry = false;
@@ -242,7 +243,10 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args,
   std::set<unsigned int> configArgs;
   std::set<unsigned int> moduleArgs;
   for (unsigned int i = 1; i < args.size(); ++i) {
-    if (args[i] == "QUIET") {
+    if (args[i] == "OPTIONAL") {
+      this->Optional = true;
+      doing = DoingNone;
+    } else if (args[i] == "QUIET") {
       this->Quiet = true;
       doing = DoingNone;
     } else if (args[i] == "EXACT") {
@@ -546,6 +550,14 @@ void cmFindPackageCommand::SetModuleVariables(const std::string& components)
   std::string components_var = this->Name + "_FIND_COMPONENTS";
   this->AddFindDefinition(components_var, components.c_str());
 
+  if (this->Optional) {
+    // Tell the module that is about to be read that it should find
+    // an optional package.
+    std::string optional = this->Name;
+    optional += "_FIND_OPTIONAL";
+    this->AddFindDefinition(optional, "1");
+  }
+
   if (this->Quiet) {
     // Tell the module that is about to be read that it should find
     // quietly.
@@ -727,7 +739,7 @@ bool cmFindPackageCommand::HandlePackageMode()
     }
   }
 
-  if (result && !found && (!this->Quiet || this->Required)) {
+  if (result && !found && ((!this->Quiet && !this->Optional) || this->Required)) {
     // The variable is not set.
     std::ostringstream e;
     std::ostringstream aw;
@@ -827,6 +839,14 @@ bool cmFindPackageCommand::HandlePackageMode()
       this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, aw.str());
     }
   }
+  else if (result && !found && !this->Quiet && this->Optional) {
+      std::ostringstream aw;
+      aw << "Could not find " << this->Name
+         << ". (No " << this->Name
+         << ".cmake or " << this->Name
+         << "-config.cmake)\n";
+      this->Makefile->IssueMessage(cmake::MESSAGE, aw.str());
+  }
 
   // Set a variable marking whether the package was found.
   this->Makefile->AddDefinition(foundVar, found ? "1" : "0");
diff --git a/Source/cmFindPackageCommand.h b/Source/cmFindPackageCommand.h
index c42eccee3..376a9cbba 100644
--- a/Source/cmFindPackageCommand.h
+++ b/Source/cmFindPackageCommand.h
@@ -161,6 +161,7 @@ private:
   unsigned int VersionFoundTweak;
   unsigned int VersionFoundCount;
   KWIML_INT_uint64_t RequiredCMakeVersion;
+  bool Optional;
   bool Quiet;
   bool Required;
   bool UseConfigFiles;
-- 
2.12.0

-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake-developers

Reply via email to