From ef6be544a375a91fadc28749976cd48424a2a8ed Mon Sep 17 00:00:00 2001 From: Jeremie Dimino Date: Fri, 5 May 2017 11:21:46 +0100 Subject: [PATCH] Keep the package context after parsing --- bin/main.ml | 2 +- src/gen_rules.ml | 5 +++-- src/gen_rules.mli | 3 ++- src/jbuild_load.ml | 26 +++++++++----------------- src/jbuild_load.mli | 3 ++- src/jbuild_types.ml | 23 +++++++++++------------ src/main.ml | 4 ++-- src/main.mli | 3 ++- src/super_context.ml | 6 ++++-- src/super_context.mli | 3 ++- 10 files changed, 38 insertions(+), 40 deletions(-) diff --git a/bin/main.ml b/bin/main.ml index b5dd404c..8d19b7dc 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -450,7 +450,7 @@ let external_lib_deps = (Build_system.all_lib_deps_by_context setup.build_system targets) ~f:(fun ~key:context_name ~data:lib_deps acc -> let internals = - Jbuild_types.Stanza.lib_names + Jbuild_types.Stanzas.lib_names (match String_map.find context_name setup.Main.stanzas with | None -> assert false | Some x -> x) diff --git a/src/gen_rules.ml b/src/gen_rules.ml index 6e26860e..8568cf76 100644 --- a/src/gen_rules.ml +++ b/src/gen_rules.ml @@ -576,7 +576,7 @@ module Gen(P : Params) = struct | Stanza | +-----------------------------------------------------------------+ *) - let rules { SC.Dir_with_jbuild. src_dir; ctx_dir; stanzas } = + let rules { SC.Dir_with_jbuild. src_dir; ctx_dir; stanzas; pkgs = _ } = (* Interpret user rules and other simple stanzas first in order to populate the known target table, which is needed for guessing the list of modules. *) List.iter stanzas ~f:(fun stanza -> @@ -907,8 +907,9 @@ let gen ~contexts ?(filter_out_optional_stanzas_with_missing_deps=true) match only_packages with | None -> stanzas | Some pkgs -> - List.map stanzas ~f:(fun (dir, stanzas) -> + List.map stanzas ~f:(fun (dir, pkgs_ctx, stanzas) -> (dir, + pkgs_ctx, List.filter stanzas ~f:(fun stanza -> match (stanza : Stanza.t) with | Library { public = Some { package; _ }; _ } diff --git a/src/gen_rules.mli b/src/gen_rules.mli index 13c2615c..9f28e66a 100644 --- a/src/gen_rules.mli +++ b/src/gen_rules.mli @@ -1,4 +1,5 @@ open! Import +open Jbuild_types val gen : contexts:Context.t list @@ -7,4 +8,4 @@ val gen -> Jbuild_load.conf -> (Build_interpret.Rule.t list * (* Evaluated jbuilds per context names *) - (Path.t * Jbuild_types.Stanzas.t) list String_map.t) Future.t + (Path.t * Pkgs.t * Stanzas.t) list String_map.t) Future.t diff --git a/src/jbuild_load.ml b/src/jbuild_load.ml index 93c07bd8..06d7515a 100644 --- a/src/jbuild_load.ml +++ b/src/jbuild_load.ml @@ -3,13 +3,12 @@ open Jbuild_types module Jbuilds = struct type script = - { dir : Path.t - ; visible_packages : Package.t String_map.t - ; closest_packages : Package.t list + { dir : Path.t + ; pkgs : Pkgs.t } type one = - | Literal of Path.t * Stanza.t list + | Literal of (Path.t * Pkgs.t * Stanza.t list) | Script of script type t = one list @@ -66,12 +65,8 @@ end let eval jbuilds ~(context : Context.t) = let open Future in List.map jbuilds ~f:(function - | Literal (path, stanzas) -> - return (path, stanzas) - | Script { dir - ; visible_packages - ; closest_packages - } -> + | Literal x -> return x + | Script { dir; pkgs = pkgs_ctx } -> let file = Path.relative dir "jbuild" in let generated_jbuild = Path.append (Path.relative generated_jbuilds_dir context.name) file @@ -123,7 +118,7 @@ end Did you forgot to call [Jbuild_plugin.V*.send]?" (Path.to_string file); let sexps = Sexp_load.many (Path.to_string generated_jbuild) in - return (dir, Stanzas.parse sexps ~visible_packages ~closest_packages)) + return (dir, pkgs_ctx, Stanzas.parse pkgs_ctx sexps)) |> Future.all end @@ -136,15 +131,12 @@ type conf = let load ~dir ~visible_packages ~closest_packages = let file = Path.relative dir "jbuild" in + let pkgs = { Pkgs. visible_packages; closest_packages } in match Sexp_load.many_or_ocaml_script (Path.to_string file) with | Sexps sexps -> - Jbuilds.Literal (dir, Stanzas.parse sexps ~visible_packages ~closest_packages) + Jbuilds.Literal (dir, pkgs, Stanzas.parse pkgs sexps) | Ocaml_script -> - Script - { dir - ; visible_packages - ; closest_packages - } + Script { dir; pkgs } let load ?(extra_ignored_subtrees=Path.Set.empty) () = let ftree = File_tree.load Path.root in diff --git a/src/jbuild_load.mli b/src/jbuild_load.mli index f2b2290c..5ba5fe8f 100644 --- a/src/jbuild_load.mli +++ b/src/jbuild_load.mli @@ -1,9 +1,10 @@ open Import +open Jbuild_types module Jbuilds : sig type t - val eval : t -> context:Context.t -> (Path.t * Jbuild_types.Stanzas.t) list Future.t + val eval : t -> context:Context.t -> (Path.t * Pkgs.t * Stanzas.t) list Future.t end type conf = diff --git a/src/jbuild_types.ml b/src/jbuild_types.ml index 3c50eb58..6d8a96fc 100644 --- a/src/jbuild_types.ml +++ b/src/jbuild_types.ml @@ -862,22 +862,12 @@ module Stanza = struct let select : Jbuild_version.t -> Pkgs.t -> t list Sexp.Of_sexp.t = function | V1 -> v1 | Vjs -> v1 - - let lib_names ts = - List.fold_left ts ~init:String_set.empty ~f:(fun acc (_, stanzas) -> - List.fold_left stanzas ~init:acc ~f:(fun acc -> function - | Library lib -> - String_set.add lib.name - (match lib.public with - | None -> acc - | Some { name; _ } -> String_set.add name acc) - | _ -> acc)) end module Stanzas = struct type t = Stanza.t list - let parse sexps ~visible_packages ~closest_packages = + let parse pkgs sexps = let versions, sexps = List.partition_map sexps ~f:(function | List (loc, [Atom (_, "jbuild_version"); ver]) -> @@ -891,6 +881,15 @@ module Stanzas = struct | _ :: (_, loc) :: _ -> Loc.fail loc "jbuild_version specified too many times" in - let pkgs = { Pkgs. visible_packages; closest_packages } in List.concat_map sexps ~f:(Stanza.select version pkgs) + + let lib_names ts = + List.fold_left ts ~init:String_set.empty ~f:(fun acc (_, _, stanzas) -> + List.fold_left stanzas ~init:acc ~f:(fun acc -> function + | Stanza.Library lib -> + String_set.add lib.name + (match lib.public with + | None -> acc + | Some { name; _ } -> String_set.add name acc) + | _ -> acc)) end diff --git a/src/main.ml b/src/main.ml index 8199e83a..2e9f8fa3 100644 --- a/src/main.ml +++ b/src/main.ml @@ -3,7 +3,7 @@ open Future type setup = { build_system : Build_system.t - ; stanzas : (Path.t * Jbuild_types.Stanzas.t) list String_map.t + ; stanzas : (Path.t * Jbuild_types.Pkgs.t * Jbuild_types.Stanzas.t) list String_map.t ; contexts : Context.t list ; packages : Package.t String_map.t } @@ -69,7 +69,7 @@ let external_lib_deps ?log ~packages () = match String_map.find "default" setup.stanzas with | None -> die "You need to set a default context to use external-lib-deps" | Some stanzas -> - let internals = Jbuild_types.Stanza.lib_names stanzas in + let internals = Jbuild_types.Stanzas.lib_names stanzas in Path.Map.map (Build_system.all_lib_deps setup.build_system install_files) ~f:(String_map.filter ~f:(fun name _ -> diff --git a/src/main.mli b/src/main.mli index 1fb9213a..6638754d 100644 --- a/src/main.mli +++ b/src/main.mli @@ -1,9 +1,10 @@ open! Import +open Jbuild_types type setup = { build_system : Build_system.t ; (* Evaluated jbuilds per context names *) - stanzas : (Path.t * Jbuild_types.Stanzas.t) list String_map.t + stanzas : (Path.t * Pkgs.t * Stanzas.t) list String_map.t ; contexts : Context.t list ; packages : Package.t String_map.t } diff --git a/src/super_context.ml b/src/super_context.ml index 0aefdff4..0503b525 100644 --- a/src/super_context.ml +++ b/src/super_context.ml @@ -6,6 +6,7 @@ module Dir_with_jbuild = struct { src_dir : Path.t ; ctx_dir : Path.t ; stanzas : Stanzas.t + ; pkgs : Pkgs.t } end @@ -60,11 +61,12 @@ let create = let stanzas = List.map stanzas - ~f:(fun (dir, stanzas) -> + ~f:(fun (dir, pkgs, stanzas) -> { Dir_with_jbuild. src_dir = dir ; ctx_dir = Path.append context.build_dir dir - ; stanzas + ; stanzas + ; pkgs }) in let internal_libraries = diff --git a/src/super_context.mli b/src/super_context.mli index 59d4c2f4..7390f880 100644 --- a/src/super_context.mli +++ b/src/super_context.mli @@ -14,6 +14,7 @@ module Dir_with_jbuild : sig { src_dir : Path.t ; ctx_dir : Path.t (** [_build/context-name/src_dir] *) ; stanzas : Stanzas.t + ; pkgs : Pkgs.t } end @@ -25,7 +26,7 @@ val create -> dirs_with_dot_opam_files:Path.Set.t -> file_tree:File_tree.t -> packages:Package.t String_map.t - -> stanzas:(Path.t * Stanzas.t) list + -> stanzas:(Path.t * Pkgs.t * Stanzas.t) list -> filter_out_optional_stanzas_with_missing_deps:bool -> t