From 3fd14a8f2893fb137001922bb1b4f182212ad35e Mon Sep 17 00:00:00 2001 From: Jeremie Dimino Date: Thu, 23 Feb 2017 18:46:12 +0000 Subject: [PATCH] Factorize jbuilds a bit --- src/gen_rules.ml | 52 +++++++-------- src/jbuild_types.ml | 154 ++++++++++++++++++++++---------------------- src/lib_db.ml | 2 +- 3 files changed, 100 insertions(+), 108 deletions(-) diff --git a/src/gen_rules.ml b/src/gen_rules.ml index 370d6688..76064020 100644 --- a/src/gen_rules.ml +++ b/src/gen_rules.ml @@ -25,7 +25,7 @@ module Ocaml_flags = struct ; specific : string list Mode.Dict.t } - let make ~flags ~ocamlc_flags ~ocamlopt_flags = + let make { Buildable. flags; ocamlc_flags; ocamlopt_flags; _ } = let eval = Ordered_set_lang.eval_with_standard in { common = eval flags ~standard:default_flags ; specific = @@ -38,9 +38,7 @@ module Ocaml_flags = struct let get_for_cm t ~cm_kind = get t (Mode.of_cm_kind cm_kind) - let default = - let std = Ordered_set_lang.standard in - make ~flags:std ~ocamlc_flags:std ~ocamlopt_flags:std + let default = make (Sexp.Of_sexp.record Buildable.t (List [])) end let default_c_flags = g () @@ -928,13 +926,10 @@ module Gen(P : Params) = struct let library_rules (lib : Library.t) ~dir ~all_modules ~files = let dep_kind = if lib.optional then Build.Optional else Required in - let flags = - Ocaml_flags.make - ~flags:lib.flags - ~ocamlc_flags:lib.ocamlc_flags - ~ocamlopt_flags:lib.ocamlopt_flags + let flags = Ocaml_flags.make lib.buildable in + let modules = + parse_modules ~dir ~all_modules ~modules_written_by_user:lib.buildable.modules in - let modules = parse_modules ~dir ~all_modules ~modules_written_by_user:lib.modules in let main_module_name = String.capitalize_ascii lib.name in let modules = String_map.map modules ~f:(fun (m : Module.t) -> @@ -965,8 +960,8 @@ module Gen(P : Params) = struct in (* Preprocess before adding the alias module as it doesn't need preprocessing *) let modules = - pped_modules ~dir ~dep_kind ~modules ~preprocess:lib.preprocess - ~preprocessor_deps:lib.preprocessor_deps + pped_modules ~dir ~dep_kind ~modules ~preprocess:lib.buildable.preprocess + ~preprocessor_deps:lib.buildable.preprocessor_deps ~lib_name:(Some lib.name) in let modules = @@ -991,14 +986,14 @@ module Gen(P : Params) = struct let requires = requires ~dir ~dep_kind ~item:lib.name - ~libraries:lib.libraries - ~preprocess:lib.preprocess + ~libraries:lib.buildable.libraries + ~preprocess:lib.buildable.preprocess ~virtual_deps:lib.virtual_deps in setup_runtime_deps ~dir ~dep_kind ~item:lib.name - ~libraries:lib.libraries + ~libraries:lib.buildable.libraries ~ppx_runtime_libraries:lib.ppx_runtime_libraries; - List.iter (Lib_db.select_rules ~dir lib.libraries) ~f:add_rule; + List.iter (Lib_db.select_rules ~dir lib.buildable.libraries) ~f:add_rule; build_modules ~flags ~dir ~dep_graph ~modules ~requires ~alias_module; Option.iter alias_module ~f:(fun m -> @@ -1119,13 +1114,10 @@ module Gen(P : Params) = struct let executables_rules (exes : Executables.t) ~dir ~all_modules = let dep_kind = Build.Required in - let flags = - Ocaml_flags.make - ~flags:exes.flags - ~ocamlc_flags:exes.ocamlc_flags - ~ocamlopt_flags:exes.ocamlopt_flags + let flags = Ocaml_flags.make exes.buildable in + let modules = + parse_modules ~dir ~all_modules ~modules_written_by_user:exes.buildable.modules in - let modules = parse_modules ~dir ~all_modules ~modules_written_by_user:exes.modules in let modules = String_map.map modules ~f:(fun (m : Module.t) -> { m with obj_name = obj_name_of_basename m.ml_fname }) @@ -1136,19 +1128,21 @@ module Gen(P : Params) = struct name (Path.to_string dir)); *) let modules = - pped_modules ~dir ~dep_kind ~modules ~preprocess:exes.preprocess - ~preprocessor_deps:[] ~lib_name:None + pped_modules ~dir ~dep_kind ~modules + ~preprocess:exes.buildable.preprocess + ~preprocessor_deps:exes.buildable.preprocessor_deps + ~lib_name:None in let item = List.hd exes.names in let dep_graph = ocamldep_rules ~dir ~item ~modules ~alias_module:None in let requires = requires ~dir ~dep_kind ~item - ~libraries:exes.libraries - ~preprocess:exes.preprocess + ~libraries:exes.buildable.libraries + ~preprocess:exes.buildable.preprocess ~virtual_deps:[] in - List.iter (Lib_db.select_rules ~dir exes.libraries) ~f:add_rule; + List.iter (Lib_db.select_rules ~dir exes.buildable.libraries) ~f:add_rule; build_modules ~flags ~dir ~dep_graph ~modules ~requires ~alias_module:None; @@ -1394,8 +1388,8 @@ module Gen(P : Params) = struct | Ocamllex conf -> List.map conf.names ~f:(fun name -> name ^ ".ml") | Ocamlyacc conf -> List.concat_map conf.names ~f:(fun name -> [ name ^ ".ml"; name ^ ".mli" ]) - | Library { libraries; _ } | Executables { libraries; _ } -> - List.filter_map libraries ~f:(function + | Library { buildable; _ } | Executables { buildable; _ } -> + List.filter_map buildable.libraries ~f:(function | Direct _ -> None | Select s -> Some s.result_fn) | _ -> []) diff --git a/src/jbuild_types.ml b/src/jbuild_types.ml index 0957730c..44ea858d 100644 --- a/src/jbuild_types.ml +++ b/src/jbuild_types.ml @@ -285,16 +285,9 @@ end let field_osl name = field name Ordered_set_lang.t ~default:Ordered_set_lang.standard -let field_modules = - field "modules" (fun s -> Ordered_set_lang.(map (t s)) ~f:String.capitalize_ascii) - ~default:Ordered_set_lang.standard - let field_oslu name = field name Ordered_set_lang.Unexpanded.t ~default:Ordered_set_lang.Unexpanded.standard -let field_pp name = - field name Preprocess_map.t ~default:Preprocess_map.default - module Js_of_ocaml = struct type t = { flags : string list @@ -369,6 +362,55 @@ module Lib_dep = struct let direct s = Direct s end +module Buildable = struct + type t = + { modules : Ordered_set_lang.t + ; libraries : Lib_dep.t list + ; preprocess : Preprocess_map.t + ; preprocessor_deps : Dep_conf.t list + ; flags : Ordered_set_lang.t + ; ocamlc_flags : Ordered_set_lang.t + ; ocamlopt_flags : Ordered_set_lang.t + } + + let t = + field "preprocess" Preprocess_map.t ~default:Preprocess_map.default + >>= fun preprocess -> + field "preprocessor_deps" (list Dep_conf.t) ~default:[] + >>= fun preprocessor_deps -> + field "modules" (fun s -> Ordered_set_lang.(map (t s)) ~f:String.capitalize_ascii) + ~default:Ordered_set_lang.standard + >>= fun modules -> + field "extra_disabled_warnings" (list int) ~default:[] + >>= fun extra_disabled_warnings -> + field "libraries" (list Lib_dep.t) ~default:[] + >>= fun libraries -> + field_osl "flags" >>= fun flags -> + field_osl "ocamlc_flags" >>= fun ocamlc_flags -> + field_osl "ocamlopt_flags" >>= fun ocamlopt_flags -> + let flags = + if Ordered_set_lang.is_standard flags && extra_disabled_warnings <> [] then + Ordered_set_lang.append flags + (Ordered_set_lang.t + (List [ Atom "-w" + ; Atom + (String.concat ~sep:"" + (List.map extra_disabled_warnings ~f:(sprintf "-%d"))) + ])) + else + flags + in + return + { preprocess + ; preprocessor_deps + ; modules + ; libraries + ; flags + ; ocamlc_flags + ; ocamlopt_flags + } +end + module Library = struct module Kind = struct type t = @@ -389,11 +431,9 @@ module Library = struct ; public_name : string option ; synopsis : string option ; install_c_headers : string list - ; libraries : Lib_dep.t list ; ppx_runtime_libraries : string list ; modes : Mode.t list ; kind : Kind.t - ; modules : Ordered_set_lang.t ; c_flags : Ordered_set_lang.Unexpanded.t ; c_names : string list ; cxx_flags : Ordered_set_lang.Unexpanded.t @@ -401,59 +441,46 @@ module Library = struct ; includes : String_with_vars.t list ; library_flags : String_with_vars.t list ; c_library_flags : Ordered_set_lang.Unexpanded.t - ; preprocess : Preprocess_map.t - ; preprocessor_deps : Dep_conf.t list ; self_build_stubs_archive : string option ; js_of_ocaml : Js_of_ocaml.t option ; virtual_deps : string list ; wrapped : bool ; optional : bool - ; flags : Ordered_set_lang.t - ; ocamlc_flags : Ordered_set_lang.t - ; ocamlopt_flags : Ordered_set_lang.t + ; buildable : Buildable.t } let t = record (ignore_fields ["inline_tests"; "skip_from_default"; "lint"] >>= fun () -> - field "name" library_name >>= fun name -> - field_o "public_name" string >>= fun public_name -> - field_o "synopsis" string >>= fun synopsis -> - field "install_c_headers" (list string) ~default:[] >>= fun install_c_headers -> - field "libraries" (list Lib_dep.t) ~default:[] >>= fun libraries -> - field "ppx_runtime_libraries" (list string) ~default:[] >>= fun ppx_runtime_libraries -> - field_modules >>= fun modules -> - field_oslu "c_flags" >>= fun c_flags -> - field_oslu "cxx_flags" >>= fun cxx_flags -> - field "c_names" (list string) ~default:[] >>= fun c_names -> - field "cxx_names" (list string) ~default:[] >>= fun cxx_names -> - field "library_flags" (list String_with_vars.t) ~default:[] >>= fun library_flags -> - field "c_libraries" (list string) ~default:[] >>= fun c_libraries -> - field_oslu "c_library_flags" >>= fun c_library_flags -> - field_pp "preprocess" >>= fun preprocess -> - field "preprocessor_deps" (list Dep_conf.t) ~default:[] >>= fun preprocessor_deps -> - field "self_build_stubs_archive" (option string) ~default:None >>= fun self_build_stubs_archive -> - field_o "js_of_ocaml" Js_of_ocaml.t >>= fun js_of_ocaml -> - field "virtual_deps" (list string) ~default:[] >>= fun virtual_deps -> - field "modes" (list Mode.t) ~default:Mode.all >>= fun modes -> - field "includes" (list String_with_vars.t) ~default:[] >>= fun includes -> - field "kind" Kind.t ~default:Kind.Normal >>= fun kind -> - field "wrapped" bool ~default:true >>= fun wrapped -> - field_b "optional" >>= fun optional -> - field_osl "flags" >>= fun flags -> - field_osl "ocamlc_flags" >>= fun ocamlc_flags -> - field_osl "ocamlopt_flags" >>= fun ocamlopt_flags -> - field "extra_disabled_warnings" (list int) ~default:[] >>= fun extra_disabled_warnings -> + Buildable.t >>= fun buildable -> + field "name" library_name >>= fun name -> + field_o "public_name" string >>= fun public_name -> + field_o "synopsis" string >>= fun synopsis -> + field "install_c_headers" (list string) ~default:[] >>= fun install_c_headers -> + field "ppx_runtime_libraries" (list string) ~default:[] >>= fun ppx_runtime_libraries -> + field_oslu "c_flags" >>= fun c_flags -> + field_oslu "cxx_flags" >>= fun cxx_flags -> + field "c_names" (list string) ~default:[] >>= fun c_names -> + field "cxx_names" (list string) ~default:[] >>= fun cxx_names -> + field "library_flags" (list String_with_vars.t) ~default:[] >>= fun library_flags -> + field "c_libraries" (list string) ~default:[] >>= fun c_libraries -> + field_oslu "c_library_flags" >>= fun c_library_flags -> + field "self_build_stubs_archive" (option string) ~default:None >>= fun self_build_stubs_archive -> + field_o "js_of_ocaml" Js_of_ocaml.t >>= fun js_of_ocaml -> + field "virtual_deps" (list string) ~default:[] >>= fun virtual_deps -> + field "modes" (list Mode.t) ~default:Mode.all >>= fun modes -> + field "includes" (list String_with_vars.t) ~default:[] >>= fun includes -> + field "kind" Kind.t ~default:Kind.Normal >>= fun kind -> + field "wrapped" bool ~default:true >>= fun wrapped -> + field_b "optional" >>= fun optional -> return { name ; public_name ; synopsis ; install_c_headers - ; libraries ; ppx_runtime_libraries ; modes ; kind - ; modules ; c_names ; c_flags ; cxx_names @@ -465,26 +492,12 @@ module Library = struct (Ordered_set_lang.Unexpanded.t (Sexp.To_sexp.(list string (List.map c_libraries ~f:((^) "-l"))))) c_library_flags - ; preprocess - ; preprocessor_deps ; self_build_stubs_archive ; js_of_ocaml ; virtual_deps ; wrapped ; optional - ; flags = - if Ordered_set_lang.is_standard flags && extra_disabled_warnings <> [] then - Ordered_set_lang.append flags - (Ordered_set_lang.t - (List [ Atom "-w" - ; Atom - (String.concat ~sep:"" - (List.map extra_disabled_warnings ~f:(sprintf "-%d"))) - ])) - else - flags - ; ocamlc_flags - ; ocamlopt_flags + ; buildable }) let has_stubs t = @@ -496,7 +509,7 @@ module Library = struct Path.relative dir (sprintf "lib%s_stubs%s" t.name ext_lib) let all_lib_deps t = - List.map t.virtual_deps ~f:(fun s -> Lib_dep.Direct s) @ t.libraries + List.map t.virtual_deps ~f:(fun s -> Lib_dep.Direct s) @ t.buildable.libraries end module Executables = struct @@ -505,13 +518,8 @@ module Executables = struct ; object_public_name : string option ; synopsis : string option ; link_executables : bool - ; libraries : Lib_dep.t list ; link_flags : string list - ; modules : Ordered_set_lang.t - ; preprocess : Preprocess_map.t - ; flags : Ordered_set_lang.t - ; ocamlc_flags : Ordered_set_lang.t - ; ocamlopt_flags : Ordered_set_lang.t + ; buildable : Buildable.t } let t = @@ -519,29 +527,19 @@ module Executables = struct (ignore_fields ["js_of_ocaml"; "only_shared_object"; "review_help"; "skip_from_default"] >>= fun () -> + Buildable.t >>= fun buildable -> field "names" (list string) >>= fun names -> field_o "object_public_name" string >>= fun object_public_name -> field_o "synopsis" string >>= fun synopsis -> field "link_executables" bool ~default:true >>= fun link_executables -> - field "libraries" (list Lib_dep.t) >>= fun libraries -> field "link_flags" (list string) ~default:[] >>= fun link_flags -> - field_modules >>= fun modules -> - field_pp "preprocess" >>= fun preprocess -> - field_osl "flags" >>= fun flags -> - field_osl "ocamlc_flags" >>= fun ocamlc_flags -> - field_osl "ocamlopt_flags" >>= fun ocamlopt_flags -> return { names ; object_public_name ; synopsis ; link_executables - ; libraries ; link_flags - ; modules - ; preprocess - ; flags - ; ocamlc_flags - ; ocamlopt_flags + ; buildable }) end diff --git a/src/lib_db.ml b/src/lib_db.ml index fa8e44fc..d5d8f793 100644 --- a/src/lib_db.ml +++ b/src/lib_db.ml @@ -25,7 +25,7 @@ module Local_closure = Top_closure.Make(String)(struct type t = Lib.Internal.t let key ((_, lib) : t) = lib.name let deps ((_, lib) : Lib.Internal.t) graph = - List.concat_map lib.libraries ~f:(fun dep -> + List.concat_map lib.buildable.libraries ~f:(fun dep -> List.filter_map (Lib_dep.to_lib_names dep) ~f:(find_internal graph)) end)