Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -c flag to force color output. #1268

Closed
wants to merge 1 commit into from
Closed

Conversation

altimin
Copy link

@altimin altimin commented Apr 12, 2017

No description provided.

@johandc
Copy link

johandc commented Jul 27, 2017

This is related to pull-request #1230

@ghost
Copy link

ghost commented Sep 11, 2017

Looks like it's missing this: https://github.com/ninja-build/ninja/pull/1230/files#diff-78294872cbf9d32f4f972288561fa718R1071

$ ninja -c
ninja: invalid option -- 'c'
usage: ninja [options] [targets...]

if targets are unspecified, builds the 'default' target (see manual).

options:
  --version  print ninja version ("1.8.1")

  -C DIR   change to DIR before doing anything else
  -f FILE  specify input build file [default=build.ninja]

  -j N     run N jobs in parallel [default=6, derived from CPUs available]
  -k N     keep going until N jobs fail [default=1]
  -l N     do not start new jobs if the load average is greater than N
  -n       dry run (don't run commands but act like they succeeded)
  -v       show all command lines while building

  -d MODE  enable debugging (use -d list to list modes)
  -t TOOL  run a subtool (use -t list to list subtools)
    terminates toplevel options; further flags are passed to the tool
  -w FLAG  adjust warnings (use -w list to list warnings)
  -c       force using color output

One more thing: it shouldn't return here https://github.com/ninja-build/ninja/pull/1268/files#diff-78294872cbf9d32f4f972288561fa718R1101

@ghost
Copy link

ghost commented Sep 11, 2017

click to expand

I tested the following to work, for me, while compiling chromium (with ninja):

modified to actually accept -c instead of fail
also don't return, but break instead! else other flags are ignored!

From 7d27df3873593f573515f3133ba7766d8d4fb262 Mon Sep 17 00:00:00 2001
From: Alexander Timin <[email protected]>
Date: Wed, 12 Apr 2017 14:11:48 +0100
Subject: [PATCH] Add -c flag to force color output.

---
 src/build.cc | 3 +--
 src/build.h  | 5 ++++-
 src/ninja.cc | 6 +++++-
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/build.cc b/src/build.cc
index a0c7ec882..f637ed15f 100644
--- a/src/build.cc
+++ b/src/build.cc
@@ -149,9 +149,8 @@ void BuildStatus::BuildEdgeFinished(Edge* edge,
     // (Launching subprocesses in pseudo ttys doesn't work because there are
     // only a few hundred available on some systems, and ninja can launch
     // thousands of parallel compile commands.)
-    // TODO: There should be a flag to disable escape code stripping.
     string final_output;
-    if (!printer_.is_smart_terminal())
+    if (!printer_.is_smart_terminal() && !config_.force_color_output)
       final_output = StripAnsiEscapeCodes(output);
     else
       final_output = output;
diff --git a/src/build.h b/src/build.h
index 66ce60767..0035a7cc2 100644
--- a/src/build.h
+++ b/src/build.h
@@ -124,7 +124,8 @@ struct CommandRunner {
 /// Options (e.g. verbosity, parallelism) passed to a build.
 struct BuildConfig {
   BuildConfig() : verbosity(NORMAL), dry_run(false), parallelism(1),
-                  failures_allowed(1), max_load_average(-0.0f) {}
+                  failures_allowed(1), max_load_average(-0.0f),
+                  force_color_output(false) {}
 
   enum Verbosity {
     NORMAL,
@@ -138,6 +139,8 @@ struct BuildConfig {
   /// The maximum load average we must not exceed. A negative value
   /// means that we do not have any limit.
   double max_load_average;
+  // Do not strip color marks even when writing to a non-terminal.
+  bool force_color_output;
 };
 
 /// Builder wraps the build process: starting commands, updating status.
diff --git a/src/ninja.cc b/src/ninja.cc
index 54de7b9f1..9661630af 100644
--- a/src/ninja.cc
+++ b/src/ninja.cc
@@ -212,7 +212,8 @@ void Usage(const BuildConfig& config) {
 "  -d MODE  enable debugging (use -d list to list modes)\n"
 "  -t TOOL  run a subtool (use -t list to list subtools)\n"
 "    terminates toplevel options; further flags are passed to the tool\n"
-"  -w FLAG  adjust warnings (use -w list to list warnings)\n",
+"  -w FLAG  adjust warnings (use -w list to list warnings)\n"
+"  -c       force using color output\n",
           kNinjaVersion, config.parallelism);
 }
 
@@ -1037,7 +1037,7 @@ int ReadFlags(int* argc, char*** argv,
 
   int opt;
   while (!options->tool &&
-         (opt = getopt_long(*argc, *argv, "d:f:j:k:l:nt:vw:C:h", kLongOptions,
+         (opt = getopt_long(*argc, *argv, "d:f:j:k:l:nct:vw:C:h", kLongOptions,
                             NULL)) != -1) {
     switch (opt) {
       case 'd':
@@ -1095,6 +1096,9 @@ int ReadFlags(int* argc, char*** argv,
       case OPT_VERSION:
         printf("%s\n", kNinjaVersion);
         return 0;
+      case 'c':
+        config->force_color_output = true;
+        break;
       case 'h':
       default:
         Usage(*config);

@johandc
Copy link

johandc commented Sep 23, 2017

Looks good

@nilsdeppe
Copy link

I would love to see this merged. Is it just the conflict that needs to be resolved?

@pseyfert
Copy link
Contributor

tiny nitpick:
shouldn't force using color output say instead something like preserve color output?
because what's relevant is not that ninja itself issues colorful output or colorizes uncolored output, but instead that the colors from gcc/clang/... do not get stripped off.

@pseyfert
Copy link
Contributor

pseyfert commented Feb 16, 2018

ignoring my previous comment, I resolved the conflict here (CI).

(edit: updated CI link)

@ghost
Copy link

ghost commented Feb 19, 2018

@pseyfert have you tested it to work though? if it doesn't work, check out my patch:
#1268 (comment)

@pseyfert
Copy link
Contributor

@xftroxgpx good point, patched, thanks.

@thughes thughes mentioned this pull request Apr 20, 2018
@GoaLitiuM
Copy link
Contributor

Any news on this? I'd really like to retain colored output from compilers when possible.
Other related PRs: #659, #1230

@jhasse
Copy link
Collaborator

jhasse commented Nov 13, 2018

Thanks for the PR!

I've implemented this using the env var CLICOLOR_FORCE instead. I think an env var makes more sense in this case since:

  • TERM=dumb and NINJA_STATUS are already used to influence the output, without a matching command line flag.
  • If ninja is called by other build system a change of command line flags would indicate that the output might differ (or more likely: They won't pass -c anyway, but the environment would be inherited).
  • CMake+Make already uses CLICOLOR_FORCE (for its own output though, as there isn't any stripping with Make) which makes switching the generator to Ninja easier.
  • Easy to set for CI system where this is often needed, as they don't come with a real terminal (Travis CI is the exception here).

If you don't want to use an env var it's also possible to use -v now btw. I must admit though that this is a little bit inconsistent.

Besides that I think that -c would be too similar to -C. Also other tools use different flags like --color, --ansi or -fdiagnostics-color.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants