From 5cca590e34fb2b17d7b7fa90ec88957dbce04435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Bobot?= Date: Mon, 27 Mar 2017 19:08:02 +0200 Subject: [PATCH] Don't use color feature for ocaml 4.02.3 --- src/ansi_color.ml | 8 +------- src/ansi_color.mli | 2 +- src/context.ml | 21 +++++++++++++++++++-- src/future.ml | 2 +- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/ansi_color.ml b/src/ansi_color.ml index cd89500e..060ef9f7 100644 --- a/src/ansi_color.ml +++ b/src/ansi_color.ml @@ -153,14 +153,8 @@ let strip_colors_for_stderr 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_ocaml_colors = lazy( +let setup_env_for_opam_colors = lazy( if Lazy.force stderr_supports_colors then begin - let value = - match Sys.getenv "OCAMLPARAM" with - | exception Not_found -> "color=always,_" - | s -> "color=always," ^ s - in - Unix.putenv "OCAMLPARAM" value; match Sys.getenv "OPAMCOLOR" with | exception Not_found -> Unix.putenv "OPAMCOLOR" "always" | _ -> () diff --git a/src/ansi_color.mli b/src/ansi_color.mli index 3e81798e..8a382134 100644 --- a/src/ansi_color.mli +++ b/src/ansi_color.mli @@ -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_ocaml_colors : unit Lazy.t +val setup_env_for_opam_colors : unit Lazy.t (** Strip colors in [not (Lazy.force stderr_supports_colors)] *) val strip_colors_for_stderr : string -> string diff --git a/src/context.ml b/src/context.ml index 235cecf2..ac21eaa2 100644 --- a/src/context.ml +++ b/src/context.ml @@ -251,6 +251,23 @@ let create ~(kind : Kind.t) ~path ~base_env ~env_extra ~name ~merlin ~use_findli let get_path var = Path.absolute (get var) in let stdlib_dir = get_path "standard_library" in 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 + let value = + match get_env env "OCAMLPARAM" with + | None -> "color=always,_" + | Some s -> "color=always," ^ s + in + extend_env ~env ~vars:((String_map.singleton "OCAMLPARAM" value)), + (String_map.add ~key:"OCAMLPARAM" ~data:value env_extra) + else + env,env_extra + in return { name ; kind @@ -280,7 +297,7 @@ let create ~(kind : Kind.t) ~path ~base_env ~env_extra ~name ~merlin ~use_findli ; stdlib_dir ; ocamlc_config = String_map.bindings ocamlc_config - ; version = get "version" + ; version ; ccomp_type = get "ccomp_type" ; bytecomp_c_compiler = get "bytecomp_c_compiler" ; bytecomp_c_libraries = get "bytecomp_c_libraries" @@ -317,7 +334,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_ocaml_colors; + Lazy.force Ansi_color.setup_env_for_opam_colors; Unix.environment ()) let default ?(merlin=true) ?(use_findlib=true) () = diff --git a/src/future.ml b/src/future.ml index 5330d841..7e99e9da 100644 --- a/src/future.ml +++ b/src/future.ml @@ -502,7 +502,7 @@ module Scheduler = struct go_rec cwd log t let go ?(log=Log.no_log) t = - Lazy.force Ansi_color.setup_env_for_ocaml_colors; + Lazy.force Ansi_color.setup_env_for_opam_colors; let cwd = Sys.getcwd () in go_rec cwd log t end