On Wed, 2014-10-08 at 12:55 -0400, Brad King wrote:
> On 10/08/2014 12:15 PM, Paul Smith wrote:
> > Maybe we could add another valid value to --switch, like --switch=FORCE
> > which would always colorize.
> [snip]
> > The best option seems to be to add another flag to the "color" bitflag
> > variable that forces colorization always.
> 
> Yes, I think both of the above make sense.  If you want to work on
> it please read CONTRIBUTING.rst from the top of our source tree
> in Git ( http://cmake.org/cmake.git ) and come to the developers
> list with a proposal:

Hi all.  Attached please find a proposed patch for the above.  I still
think there's some slightly awkward redundancy between AssumeTTY and the
new ForceTTY, but I'm not sure these can actually be combined into one
flag... certainly not without reworking more of how AssumeTTY is
interpreted and used than I felt confident with.

I didn't try to allow "FORCE" to be case-insensitive.  It wouldn't be
hard, but I wasn't sure if it was worth it.  You must capitalize it as
the code is written today.  I found precedent for both options.

Also, the check for MAKE_TERMOUT might not be something you want to
keep... there's not a lot of precedent in the code, that I found, for
looking at other utilities' environment variables.  On the other hand
it's darn handy to have this "just work" without having to export
COLOR='$(if $(MAKE_TERMOUT),FORCE)' in your ~/.bashrc or whatever.
There's no "GNU Makefiles" generator, and I couldn't come up with a good
way to implement this in the generic Unix Makefiles generator.
From 82d2c63750c706e5f4f749a1288883934c725432 Mon Sep 17 00:00:00 2001
From: Paul Smith <p...@mad-scientist.net>
Date: Wed, 8 Oct 2014 14:18:14 -0400
Subject: [PATCH] cmake: Allow forced color output

Provide a new Terminal.h flag, Color_ForceTTY, which causes color output
always regardless of TTY/Console settings.  If --switch=FORCE is given
on the cmake command line, enable this flag.  Also check for the
MAKE_TERMOUT variable, set by GNU make 4.1+ when make is capturing
stdout which will eventually go to a terminal, and set Color_ForceTTY.
---
 Source/cmSystemTools.cxx   | 36 +++++++++++++++++++++++++-----------
 Source/cmcmd.cxx           |  8 +++++++-
 Source/kwsys/Terminal.c    |  8 +++++---
 Source/kwsys/Terminal.h.in | 17 +++++++++++------
 4 files changed, 48 insertions(+), 21 deletions(-)

diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx
index fbb4416..7c983c6 100644
--- a/Source/cmSystemTools.cxx
+++ b/Source/cmSystemTools.cxx
@@ -2290,31 +2290,45 @@ std::string const& cmSystemTools::GetCMakeRoot()
 void cmSystemTools::MakefileColorEcho(int color, const char* message,
                                       bool newline, bool enabled)
 {
+  if(!enabled)
+    {
+    // Color is disabled.  Print without color.
+    fprintf(stdout, "%s%s", message, newline? "\n" : "");
+    return;
+    }
+
   // On some platforms (an MSYS prompt) cmsysTerminal may not be able
   // to determine whether the stream is displayed on a tty.  In this
   // case it assumes no unless we tell it otherwise.  Since we want
   // color messages to be displayed for users we will assume yes.
   // However, we can test for some situations when the answer is most
-  // likely no.
-  int assumeTTY = cmsysTerminal_Color_AssumeTTY;
+  // likely no, and other cases where it should be forced to yes.
+  int flags = cmsysTerminal_Color_AssumeTTY;
+
   if(cmSystemTools::GetEnv("DART_TEST_FROM_DART") ||
      cmSystemTools::GetEnv("DASHBOARD_TEST_FROM_CTEST") ||
      cmSystemTools::GetEnv("CTEST_INTERACTIVE_DEBUG_MODE"))
     {
     // Avoid printing color escapes during dashboard builds.
-    assumeTTY = 0;
-    }
-
-  if(enabled)
-    {
-    cmsysTerminal_cfprintf(color | assumeTTY, stdout, "%s%s",
-                           message, newline? "\n" : "");
+    flags = 0;
     }
   else
     {
-    // Color is disabled.  Print without color.
-    fprintf(stdout, "%s%s", message, newline? "\n" : "");
+    // Newer versions of GNU make (4.0+) have the ability to separate the
+    // output of parallel builds so they don't interfere with each other.
+    // However this means commands invoked by make don't appear to have a TTY.
+    // GNU make 4.1+ exports an environment variable MAKE_TERMOUT which is
+    // non-empty if make thinks that it is printing _it's_ output to stdout.
+    // If this variable exists and is non-empty, then force color output.
+    const char* var = cmSystemTools::GetEnv("MAKE_TERMOUT");
+    if(var && var[0] != '\0')
+      {
+      flags |= cmsysTerminal_Color_ForceTTY;
+      }
     }
+
+  cmsysTerminal_cfprintf(color | flags, stdout, "%s%s",
+                         message, newline? "\n" : "");
 }
 #endif
 
diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx
index a0c67e0..af9b139 100644
--- a/Source/cmcmd.cxx
+++ b/Source/cmcmd.cxx
@@ -907,6 +907,7 @@ int cmcmd::ExecuteEchoColor(std::vector<std::string>& args)
 
   bool enabled = true;
   int color = cmsysTerminal_Color_Normal;
+  int force = 0;
   bool newline = true;
   for(unsigned int i=2; i < args.size(); ++i)
     {
@@ -920,6 +921,11 @@ int cmcmd::ExecuteEchoColor(std::vector<std::string>& args)
           {
           enabled = true;
           }
+        else if(strcmp(value.c_str(), "FORCE") == 0)
+          {
+          enabled = true;
+          force = cmsysTerminal_Color_ForceTTY;
+          }
         else
           {
           enabled = false;
@@ -977,7 +983,7 @@ int cmcmd::ExecuteEchoColor(std::vector<std::string>& args)
     else
       {
       // Color is enabled.  Print with the current color.
-      cmSystemTools::MakefileColorEcho(color, args[i].c_str(),
+      cmSystemTools::MakefileColorEcho(color|force, args[i].c_str(),
                                        newline, enabled);
       }
     }
diff --git a/Source/kwsys/Terminal.c b/Source/kwsys/Terminal.c
index e13003f..baaaf92 100644
--- a/Source/kwsys/Terminal.c
+++ b/Source/kwsys/Terminal.c
@@ -68,14 +68,16 @@ void kwsysTerminal_cfprintf(int color, FILE* stream, const char* format, ...)
 #if defined(KWSYS_TERMINAL_SUPPORT_CONSOLE)
   CONSOLE_SCREEN_BUFFER_INFO hOutInfo;
   HANDLE hOut = kwsysTerminalGetStreamHandle(stream);
-  if(GetConsoleScreenBufferInfo(hOut, &hOutInfo))
+  if(color & kwsysTerminal_Color_ForceTTY ||
+     GetConsoleScreenBufferInfo(hOut, &hOutInfo))
     {
     pipeIsConsole = 1;
     kwsysTerminalSetConsoleColor(hOut, &hOutInfo, stream, color);
     }
 #endif
-  if(!pipeIsConsole && kwsysTerminalStreamIsVT100(stream,
-                                                  default_vt100, default_tty))
+  if(color & kwsysTerminal_Color_ForceTTY ||
+     (!pipeIsConsole && kwsysTerminalStreamIsVT100(stream, default_vt100,
+                                                   default_tty)))
     {
     pipeIsVT100 = 1;
     kwsysTerminalSetVT100Color(stream, color);
diff --git a/Source/kwsys/Terminal.h.in b/Source/kwsys/Terminal.h.in
index 108cba0..941688c 100644
--- a/Source/kwsys/Terminal.h.in
+++ b/Source/kwsys/Terminal.h.in
@@ -49,6 +49,7 @@
 # define kwsysTerminal_Color_ForegroundBold    kwsys_ns(Terminal_Color_ForegroundBold)
 # define kwsysTerminal_Color_BackgroundBold    kwsys_ns(Terminal_Color_BackgroundBold)
 # define kwsysTerminal_Color_AssumeTTY         kwsys_ns(Terminal_Color_AssumeTTY)
+# define kwsysTerminal_Color_ForceTTY          kwsys_ns(Terminal_Color_ForceTTY)
 # define kwsysTerminal_Color_AssumeVT100       kwsys_ns(Terminal_Color_AssumeVT100)
 # define kwsysTerminal_Color_AttributeMask     kwsys_ns(Terminal_Color_AttributeMask)
 #endif
@@ -65,7 +66,9 @@ extern "C"
  * one background value may be given.
  *
  * Whether the a stream supports color is usually automatically
- * detected, but with two exceptions:
+ * detected, but with three exceptions:
+ *
+ *   - When the attribute Color_ForceTTY is set.
  *
  *   - When the stream is displayed in a terminal supporting VT100
  *   color but using an intermediate pipe for communication the
@@ -110,11 +113,12 @@ enum kwsysTerminal_Color_e
   kwsysTerminal_Color_BackgroundMask    = 0xF0,
 
   /* Attributes */
-  kwsysTerminal_Color_ForegroundBold = 0x100,
-  kwsysTerminal_Color_BackgroundBold = 0x200,
-  kwsysTerminal_Color_AssumeTTY      = 0x400,
-  kwsysTerminal_Color_AssumeVT100    = 0x800,
-  kwsysTerminal_Color_AttributeMask  = 0xF00
+  kwsysTerminal_Color_ForegroundBold = 0x0100,
+  kwsysTerminal_Color_BackgroundBold = 0x0200,
+  kwsysTerminal_Color_AssumeTTY      = 0x0400,
+  kwsysTerminal_Color_ForceTTY       = 0x0800,
+  kwsysTerminal_Color_AssumeVT100    = 0x1000,
+  kwsysTerminal_Color_AttributeMask  = 0x1F00
 };
 
 #if defined(__cplusplus)
@@ -151,6 +155,7 @@ enum kwsysTerminal_Color_e
 #  undef kwsysTerminal_Color_ForegroundBold
 #  undef kwsysTerminal_Color_BackgroundBold
 #  undef kwsysTerminal_Color_AssumeTTY
+#  undef kwsysTerminal_Color_ForceTTY
 #  undef kwsysTerminal_Color_AssumeVT100
 #  undef kwsysTerminal_Color_AttributeMask
 # endif
-- 
1.8.5.3

-- 

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