ENH: Enabling color makefile support using cmsysTerminal_cfprintf. Support for color is automatically detected when messages are printed. Also made color scheme more readable on both black and white backgrounds. This option can be enabled by setting CMAKE_COLOR_MAKEFILE to true in the project.

This commit is contained in:
Brad King 2006-04-26 21:31:39 -04:00
parent 92ea0a077c
commit 4494c29078
12 changed files with 153 additions and 47 deletions

View File

@ -24,7 +24,7 @@ cmGlobalBorlandMakefileGenerator::cmGlobalBorlandMakefileGenerator()
this->EmptyRuleHackDepends = "NUL"; this->EmptyRuleHackDepends = "NUL";
this->FindMakeProgramFile = "CMakeBorlandFindMake.cmake"; this->FindMakeProgramFile = "CMakeBorlandFindMake.cmake";
this->ForceUnixPaths = false; this->ForceUnixPaths = false;
this->ToolSupportsColorVT100 = false; this->ToolSupportsColor = true;
} }

View File

@ -35,7 +35,7 @@ cmGlobalGenerator::cmGlobalGenerator()
this->ForceUnixPaths = false; this->ForceUnixPaths = false;
// By default do not try to support color. // By default do not try to support color.
this->ToolSupportsColorVT100 = false; this->ToolSupportsColor = false;
} }
cmGlobalGenerator::~cmGlobalGenerator() cmGlobalGenerator::~cmGlobalGenerator()

View File

@ -127,7 +127,7 @@ public:
static int s_TryCompileTimeout; static int s_TryCompileTimeout;
bool GetForceUnixPaths() {return this->ForceUnixPaths;} bool GetForceUnixPaths() {return this->ForceUnixPaths;}
bool GetToolSupportsColorVT100() { return this->ToolSupportsColorVT100; } bool GetToolSupportsColor() { return this->ToolSupportsColor; }
///! return the language for the given extension ///! return the language for the given extension
const char* GetLanguageFromExtension(const char* ext); const char* GetLanguageFromExtension(const char* ext);
///! is an extension to be ignored ///! is an extension to be ignored
@ -204,7 +204,7 @@ protected:
std::vector<std::string> depends, bool depends_on_all = false); std::vector<std::string> depends, bool depends_on_all = false);
bool ForceUnixPaths; bool ForceUnixPaths;
bool ToolSupportsColorVT100; bool ToolSupportsColor;
cmStdString FindMakeProgramFile; cmStdString FindMakeProgramFile;
cmStdString ConfiguredFilesPath; cmStdString ConfiguredFilesPath;
cmake *CMakeInstance; cmake *CMakeInstance;

View File

@ -28,7 +28,7 @@ cmGlobalKdevelopGenerator::cmGlobalKdevelopGenerator()
// This type of makefile always requires unix style paths // This type of makefile always requires unix style paths
this->ForceUnixPaths = true; this->ForceUnixPaths = true;
this->FindMakeProgramFile = "CMakeUnixFindMake.cmake"; this->FindMakeProgramFile = "CMakeUnixFindMake.cmake";
this->ToolSupportsColorVT100 = false; this->ToolSupportsColor = false;
} }
///! Create a local generator appropriate to this Global Generator ///! Create a local generator appropriate to this Global Generator

View File

@ -23,7 +23,7 @@ cmGlobalMSYSMakefileGenerator::cmGlobalMSYSMakefileGenerator()
{ {
this->FindMakeProgramFile = "CMakeMSYSFindMake.cmake"; this->FindMakeProgramFile = "CMakeMSYSFindMake.cmake";
this->ForceUnixPaths = true; this->ForceUnixPaths = true;
this->ToolSupportsColorVT100 = true; this->ToolSupportsColor = true;
} }
std::string std::string

View File

@ -22,7 +22,7 @@ cmGlobalMinGWMakefileGenerator::cmGlobalMinGWMakefileGenerator()
{ {
this->FindMakeProgramFile = "CMakeMinGWFindMake.cmake"; this->FindMakeProgramFile = "CMakeMinGWFindMake.cmake";
this->ForceUnixPaths = true; this->ForceUnixPaths = true;
this->ToolSupportsColorVT100 = false; this->ToolSupportsColor = true;
} }
void cmGlobalMinGWMakefileGenerator::EnableLanguage(std::vector<std::string>const& l, void cmGlobalMinGWMakefileGenerator::EnableLanguage(std::vector<std::string>const& l,

View File

@ -22,7 +22,7 @@ cmGlobalNMakeMakefileGenerator::cmGlobalNMakeMakefileGenerator()
{ {
this->FindMakeProgramFile = "CMakeNMakeFindMake.cmake"; this->FindMakeProgramFile = "CMakeNMakeFindMake.cmake";
this->ForceUnixPaths = false; this->ForceUnixPaths = false;
this->ToolSupportsColorVT100 = false; this->ToolSupportsColor = true;
} }
void cmGlobalNMakeMakefileGenerator::EnableLanguage(std::vector<std::string>const& l, void cmGlobalNMakeMakefileGenerator::EnableLanguage(std::vector<std::string>const& l,

View File

@ -26,7 +26,7 @@ cmGlobalUnixMakefileGenerator3::cmGlobalUnixMakefileGenerator3()
// This type of makefile always requires unix style paths // This type of makefile always requires unix style paths
this->ForceUnixPaths = true; this->ForceUnixPaths = true;
this->FindMakeProgramFile = "CMakeUnixFindMake.cmake"; this->FindMakeProgramFile = "CMakeUnixFindMake.cmake";
this->ToolSupportsColorVT100 = true; this->ToolSupportsColor = true;
} }
void cmGlobalUnixMakefileGenerator3 void cmGlobalUnixMakefileGenerator3

View File

@ -22,7 +22,7 @@ cmGlobalWatcomWMakeGenerator::cmGlobalWatcomWMakeGenerator()
{ {
this->FindMakeProgramFile = "CMakeFindWMake.cmake"; this->FindMakeProgramFile = "CMakeFindWMake.cmake";
this->ForceUnixPaths = false; this->ForceUnixPaths = false;
this->ToolSupportsColorVT100 = false; this->ToolSupportsColor = true;
this->EmptyCommandsHack = "@cd ."; this->EmptyCommandsHack = "@cd .";
} }

View File

@ -35,28 +35,6 @@
#include <memory> // auto_ptr #include <memory> // auto_ptr
#include <queue> #include <queue>
#define CMAKE_VT100_NORMAL "\33[0m"
#define CMAKE_VT100_BOLD "\33[1m"
#define CMAKE_VT100_UNDERLINE "\33[4m"
#define CMAKE_VT100_BLINK "\33[5m"
#define CMAKE_VT100_INVERSE "\33[7m"
#define CMAKE_VT100_FRONT_BLACK "\33[30m"
#define CMAKE_VT100_FRONT_RED "\33[31m"
#define CMAKE_VT100_FRONT_GREEN "\33[32m"
#define CMAKE_VT100_FRONT_YELLOW "\33[33m"
#define CMAKE_VT100_FRONT_BLUE "\33[34m"
#define CMAKE_VT100_FRONT_MAGENTA "\33[35m"
#define CMAKE_VT100_FRONT_CYAN "\33[36m"
#define CMAKE_VT100_FRONT_WHITE "\33[37m"
#define CMAKE_VT100_BACK_BLACK "\33[40m"
#define CMAKE_VT100_BACK_RED "\33[41m"
#define CMAKE_VT100_BACK_GREEN "\33[42m"
#define CMAKE_VT100_BACK_YELLOW "\33[43m"
#define CMAKE_VT100_BACK_BLUE "\33[44m"
#define CMAKE_VT100_BACK_MAGENTA "\33[45m"
#define CMAKE_VT100_BACK_CYAN "\33[46m"
#define CMAKE_VT100_BACK_WHITE "\33[47m"
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
cmLocalUnixMakefileGenerator3::cmLocalUnixMakefileGenerator3() cmLocalUnixMakefileGenerator3::cmLocalUnixMakefileGenerator3()
{ {
@ -876,32 +854,33 @@ cmLocalUnixMakefileGenerator3::AppendEcho(std::vector<std::string>& commands,
EchoColor color) EchoColor color)
{ {
// Choose the color for the text. // Choose the color for the text.
std::string prefix; std::string color_name;
if(this->GlobalGenerator->GetToolSupportsColorVT100() && if(this->GlobalGenerator->GetToolSupportsColor() &&
this->Makefile->IsOn("CMAKE_COLOR_MAKEFILE")) this->Makefile->IsOn("CMAKE_COLOR_MAKEFILE"))
{ {
// See cmake::ExecuteEchoColor in cmake.cxx for these options.
// This color set is readable on both black and white backgrounds.
switch(color) switch(color)
{ {
case EchoNormal: case EchoNormal:
break; break;
case EchoDepend: case EchoDepend:
prefix = CMAKE_VT100_FRONT_MAGENTA; color_name = "--magenta --bold ";
break; break;
case EchoBuild: case EchoBuild:
prefix = CMAKE_VT100_FRONT_GREEN; color_name = "--green ";
break; break;
case EchoLink: case EchoLink:
prefix = CMAKE_VT100_FRONT_RED; color_name = "--red --bold ";
break; break;
case EchoGenerate: case EchoGenerate:
prefix = CMAKE_VT100_FRONT_BLUE; color_name = "--blue --bold ";
break; break;
case EchoGlobal: case EchoGlobal:
prefix = CMAKE_VT100_FRONT_CYAN; color_name = "--cyan ";
break; break;
} }
} }
std::string suffix = prefix.empty()? "" : CMAKE_VT100_NORMAL;
// Echo one line at a time. // Echo one line at a time.
std::string line; std::string line;
@ -914,18 +893,30 @@ cmLocalUnixMakefileGenerator3::AppendEcho(std::vector<std::string>& commands,
if(*c != '\0' || !line.empty()) if(*c != '\0' || !line.empty())
{ {
// Add a command to echo this line. // Add a command to echo this line.
std::string cmd = "@echo "; std::string cmd;
if(color_name.empty())
{
// Use the native echo command.
cmd = "@echo ";
if(this->EchoNeedsQuote) if(this->EchoNeedsQuote)
{ {
cmd += "\""; cmd += "\"";
} }
cmd += prefix;
cmd += line; cmd += line;
cmd += suffix;
if(this->EchoNeedsQuote) if(this->EchoNeedsQuote)
{ {
cmd += "\""; cmd += "\"";
} }
}
else
{
// Use cmake to echo the text in color.
cmd = "@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) ";
cmd += color_name;
cmd += "\"";
cmd += line;
cmd += "\"";
}
commands.push_back(cmd); commands.push_back(cmd);
} }

View File

@ -24,6 +24,8 @@
#include "cmFileTimeComparison.h" #include "cmFileTimeComparison.h"
#include "cmGeneratedFileStream.h" #include "cmGeneratedFileStream.h"
#include <cmsys/Terminal.h>
#if defined(CMAKE_BUILD_WITH_CMAKE) #if defined(CMAKE_BUILD_WITH_CMAKE)
# include "cmDependsFortran.h" // For -E cmake_copy_f90_mod callback. # include "cmDependsFortran.h" // For -E cmake_copy_f90_mod callback.
# include "cmVariableWatch.h" # include "cmVariableWatch.h"
@ -1005,6 +1007,12 @@ int cmake::ExecuteCMakeCommand(std::vector<std::string>& args)
return 1; return 1;
} }
// Internal CMake color makefile support.
else if (args[1] == "cmake_echo_color" )
{
return cmake::ExecuteEchoColor(args);
}
// Tar files // Tar files
else if (args[1] == "tar" && args.size() > 3) else if (args[1] == "tar" && args.size() > 3)
{ {
@ -2344,3 +2352,108 @@ void cmake::GenerateGraphViz(const char* fileName)
str << "}" << std::endl; str << "}" << std::endl;
} }
//----------------------------------------------------------------------------
int cmake::ExecuteEchoColor(std::vector<std::string>& args)
{
// The arguments are
// argv[0] == <cmake-executable>
// argv[1] == cmake_echo_color
// 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;
if(cmSystemTools::GetEnv("DART_TEST_FROM_DART") ||
cmSystemTools::GetEnv("DASHBOARD_TEST_FROM_CTEST"))
{
// Avoid printing color escapes during dashboard builds.
assumeTTY = 0;
}
bool enabled = true;
int color = cmsysTerminal_Color_Normal;
bool newline = true;
for(unsigned int i=2; i < args.size(); ++i)
{
if(args[i].find("--switch=") == 0)
{
// Enable or disable color based on the switch value.
std::string value = args[i].substr(9);
if(!value.empty())
{
if(cmSystemTools::IsOn(value.c_str()))
{
enabled = true;
}
else
{
enabled = false;
}
}
}
else if(args[i] == "--normal")
{
color = cmsysTerminal_Color_Normal;
}
else if(args[i] == "--black")
{
color = cmsysTerminal_Color_ForegroundBlack;
}
else if(args[i] == "--red")
{
color = cmsysTerminal_Color_ForegroundRed;
}
else if(args[i] == "--green")
{
color = cmsysTerminal_Color_ForegroundGreen;
}
else if(args[i] == "--yellow")
{
color = cmsysTerminal_Color_ForegroundYellow;
}
else if(args[i] == "--blue")
{
color = cmsysTerminal_Color_ForegroundBlue;
}
else if(args[i] == "--magenta")
{
color = cmsysTerminal_Color_ForegroundMagenta;
}
else if(args[i] == "--cyan")
{
color = cmsysTerminal_Color_ForegroundCyan;
}
else if(args[i] == "--white")
{
color = cmsysTerminal_Color_ForegroundWhite;
}
else if(args[i] == "--bold")
{
color |= cmsysTerminal_Color_ForegroundBold;
}
else if(args[i] == "--no-newline")
{
newline = false;
}
else if(args[i] == "--newline")
{
newline = true;
}
else if(enabled)
{
// Color is enabled. Print with the current color.
cmsysTerminal_cfprintf(color | assumeTTY, stdout, "%s%s",
args[i].c_str(), newline? "\n" : "");
}
else
{
// Color is disabled. Print without color.
fprintf(stdout, "%s%s", args[i].c_str(), newline? "\n" : "");
}
}
return 0;
}

View File

@ -316,6 +316,8 @@ protected:
void GenerateGraphViz(const char* fileName); void GenerateGraphViz(const char* fileName);
static int ExecuteEchoColor(std::vector<std::string>& args);
cmVariableWatch* VariableWatch; cmVariableWatch* VariableWatch;
private: private: