From 4bacf48a89f08fb2990b0181b10250d25e815b3c Mon Sep 17 00:00:00 2001 From: Etienne Millon Date: Fri, 3 Aug 2018 09:05:01 +0000 Subject: [PATCH] Extract predicates for compiler versions Instead of comparing on the version numbers, add some predicates in a new `Ocaml_version` module that describe the compiler behavior. Signed-off-by: Etienne Millon --- src/context.ml | 18 +++++++++--------- src/context.mli | 2 +- src/gen_rules.ml | 10 ++++++---- src/module_compilation.ml | 6 +++--- src/ocaml_version.ml | 19 +++++++++++++++++++ src/ocaml_version.mli | 20 ++++++++++++++++++++ 6 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 src/ocaml_version.ml create mode 100644 src/ocaml_version.mli diff --git a/src/context.ml b/src/context.ml index e25cb971..86e49ea8 100644 --- a/src/context.ml +++ b/src/context.ml @@ -50,7 +50,7 @@ type t = ; natdynlink_supported : bool ; ocaml_config : Ocaml_config.t ; version_string : string - ; version : int * int * int + ; version : Ocaml_version.t ; stdlib_dir : Path.t ; ccomp_type : string ; c_compiler : string @@ -274,16 +274,16 @@ let create ~(kind : Kind.t) ~path ~env ~env_nodes ~name ~merlin ~targets ocaml_config_ok_exn (Ocaml_config.Vars.of_lines lines >>= Ocaml_config.make)) >>= fun (findlib_path, ocfg) -> - let version = Ocaml_config.version ocfg in + let version = Ocaml_version.of_ocaml_config ocfg in let env = - (* 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. *) + (* See comment in ansi_color.ml for setup_env_for_colors. + For versions where OCAML_COLOR is not supported, but 'color' is in + OCAMLPARAM, use the latter. + If 'color' is not supported, we just don't force colors with 4.02. *) if !Clflags.capture_outputs && Lazy.force Colors.stderr_supports_colors - && version >= (4, 03, 0) - && version < (4, 05, 0) then + && Ocaml_version.supports_color_in_ocamlparam version + && not (Ocaml_version.supports_ocaml_color version) then let value = match Env.get env "OCAMLPARAM" with | None -> "color=always,_" @@ -332,8 +332,8 @@ let create ~(kind : Kind.t) ~path ~env ~env_nodes ~name ~merlin ~targets in let stdlib_dir = Path.of_string (Ocaml_config.standard_library ocfg) in let natdynlink_supported = Ocaml_config.natdynlink_supported ocfg in - let version = Ocaml_config.version ocfg in let version_string = Ocaml_config.version_string ocfg in + let version = Ocaml_version.of_ocaml_config ocfg in let arch_sixtyfour = Ocaml_config.word_size ocfg = 64 in Fiber.return { name diff --git a/src/context.mli b/src/context.mli index 954434c0..a3ac39d2 100644 --- a/src/context.mli +++ b/src/context.mli @@ -90,7 +90,7 @@ type t = ; ocaml_config : Ocaml_config.t ; version_string : string - ; version : int * int * int + ; version : Ocaml_version.t ; stdlib_dir : Path.t ; ccomp_type : string ; c_compiler : string diff --git a/src/gen_rules.ml b/src/gen_rules.ml index 8d7deee0..bd77fcfd 100644 --- a/src/gen_rules.ml +++ b/src/gen_rules.ml @@ -17,7 +17,8 @@ module Gen(P : Install_rules.Params) = struct let sctx = P.sctx let ctx = SC.context sctx - let opaque = ctx.profile = "dev" && ctx.version >= (4, 03, 0) + let opaque = + ctx.profile = "dev" && Ocaml_version.supports_opaque_for_mli ctx.version (* +-----------------------------------------------------------------+ | Library stuff | @@ -143,10 +144,11 @@ module Gen(P : Install_rules.Params) = struct ])); dst - (* In 4.02, the compiler reads the cmi for module alias even with - [-w -49 -no-alias-deps], so we must sandbox the build of the + (* If the compiler reads the cmi for module alias even with + [-w -49 -no-alias-deps], we must sandbox the build of the alias module since the modules it references are built after. *) - let alias_module_build_sandbox = ctx.version < (4, 03, 0) + let alias_module_build_sandbox = + Ocaml_version.always_reads_alias_cmi ctx.version let library_rules (lib : Library.t) ~dir_contents ~dir ~scope ~compile_info ~dir_kind = diff --git a/src/module_compilation.ml b/src/module_compilation.ml index 6d6e2b0f..a8167001 100644 --- a/src/module_compilation.ml +++ b/src/module_compilation.ml @@ -95,10 +95,10 @@ let build_cm cctx ?sandbox ?(dynlink=true) ~dep_graphs ~cm_kind (m : Module.t) = in let dir, no_keep_locs = if CC.no_keep_locs cctx && cm_kind = Cmi then begin - if ctx.version < (4, 03, 0) then - (obj_dir, Arg_spec.As []) + if Ocaml_version.supports_no_keep_locs ctx.version then + (ctx.build_dir, Arg_spec.As ["-no-keep-locs"]) else - (ctx.build_dir, As ["-no-keep-locs"]) + (obj_dir, As []) end else (ctx.build_dir, As []) in diff --git a/src/ocaml_version.ml b/src/ocaml_version.ml new file mode 100644 index 00000000..21ad5a56 --- /dev/null +++ b/src/ocaml_version.ml @@ -0,0 +1,19 @@ +type t = int * int * int + +let of_ocaml_config ocfg = + Ocaml_config.version ocfg + +let supports_no_keep_locs version = + version >= (4, 03, 0) + +let supports_opaque_for_mli version = + version >= (4, 03, 0) + +let always_reads_alias_cmi version = + version < (4, 03, 0) + +let supports_color_in_ocamlparam version = + version >= (4, 03, 0) + +let supports_ocaml_color version = + version >= (4, 05, 0) diff --git a/src/ocaml_version.mli b/src/ocaml_version.mli new file mode 100644 index 00000000..f6106864 --- /dev/null +++ b/src/ocaml_version.mli @@ -0,0 +1,20 @@ +(** Version numbers for ocamlc and ocamlopt *) +type t + +val of_ocaml_config : Ocaml_config.t -> t + +(** Does this support [-no-keep-locs]? *) +val supports_no_keep_locs : t -> bool + +(** Does this support [-opaque] for [.mli] files? *) +val supports_opaque_for_mli : t -> bool + +(** Does it read the [.cmi] file of module alias + even when [-no-alias-deps] is passed? *) +val always_reads_alias_cmi : t -> bool + +(** Does this support ['color'] in [OCAMLPARAM]? *) +val supports_color_in_ocamlparam : t -> bool + +(** Does this support [OCAML_COLOR]? *) +val supports_ocaml_color : t -> bool