Use OCAML_COLOR and stop using OCAMLPARAM when possible

This commit is contained in:
Jeremie Dimino 2017-05-10 16:31:44 +01:00
parent a573643b1e
commit 76db4d5641
4 changed files with 21 additions and 15 deletions

View File

@ -150,14 +150,18 @@ let strip_colors_for_stderr s =
else
strip s
(* We redirect the output of all commands, so by default the compiler will disable
colors. Since we support colors in the output of commands, we force it via OCAMLPARAM
if stderr supports colors. *)
let setup_env_for_opam_colors = lazy(
(* We redirect the output of all commands, so by default the various tools will disable
colors. Since we support colors in the output of commands, we force it via specific
environment variables if stderr supports colors. *)
let setup_env_for_colors = lazy(
if Lazy.force stderr_supports_colors then begin
match Sys.getenv "OPAMCOLOR" with
| exception Not_found -> Unix.putenv "OPAMCOLOR" "always"
| _ -> ()
let set var value =
match Sys.getenv with
| exception Not_found -> Unix.putenv var value
| _ -> ()
in
set "OPAMCOLOR" "always";
set "OCAML_COLOR" "always";
end
)

View File

@ -2,7 +2,7 @@ val colorize : key:string -> string -> string
val strip : string -> string
val stderr_supports_colors : bool Lazy.t
val setup_env_for_opam_colors : unit Lazy.t
val setup_env_for_colors : unit Lazy.t
(** Strip colors in [not (Lazy.force stderr_supports_colors)] *)
val strip_colors_for_stderr : string -> string

View File

@ -264,11 +264,13 @@ let create ~(kind : Kind.t) ~path ~base_env ~env_extra ~name ~merlin ~use_findli
let natdynlink_supported = Path.exists (Path.relative stdlib_dir "dynlink.cmxa") in
let version = get "version" in
let env,env_extra =
(* We redirect the output of all commands, so by default the compiler will disable
colors. Since we support colors in the output of commands, we force it via OCAMLPARAM
if stderr supports colors. *)
let ocaml_version = Scanf.sscanf version "%u.%u.%u" (fun a b c -> a, b, c) in
if Lazy.force Ansi_color.stderr_supports_colors && ocaml_version > (4, 02, 3) then
(* See comment in ansi_color.ml for setup_env_for_colors. For OCaml < 4.05,
OCAML_COLOR is not supported so we use OCAMLPARAM. OCaml 4.02 doesn't support
'color' in OCAMLPARAM, so we just don't force colors with 4.02. *)
let ocaml_version = Scanf.sscanf version "%u.%u" (fun a b -> a, b) in
if Lazy.force Ansi_color.stderr_supports_colors
&& ocaml_version > (4, 02)
&& ocaml_version < (4, 05) then
let value =
match get_env env "OCAMLPARAM" with
| None -> "color=always,_"
@ -369,7 +371,7 @@ let create ~(kind : Kind.t) ~path ~base_env ~env_extra ~name ~merlin ~use_findli
let opam_config_var t var = opam_config_var ~env:t.env ~cache:t.opam_var_cache var
let initial_env = lazy (
Lazy.force Ansi_color.setup_env_for_opam_colors;
Lazy.force Ansi_color.setup_env_for_colors;
Unix.environment ())
let default ?(merlin=true) ?(use_findlib=true) () =

View File

@ -620,7 +620,7 @@ module Scheduler = struct
go_rec cwd log t
let go ?(log=Log.no_log) t =
Lazy.force Ansi_color.setup_env_for_opam_colors;
Lazy.force Ansi_color.setup_env_for_colors;
Log.info log ("Workspace root: " ^ !Clflags.workspace_root);
let cwd = Sys.getcwd () in
go_rec cwd log t