From 4fa0844e42b91b9355c1875d131d99bc58d6a9ee Mon Sep 17 00:00:00 2001 From: Jeremie Dimino Date: Thu, 2 Mar 2017 18:21:19 +0000 Subject: [PATCH] Remove build-package in favor of --only-packages --- README.org | 6 ++++- bin/main.ml | 62 ++++++++++++++++++++++------------------------- doc/jbuild | 1 - doc/manual.org | 28 +++++++++++++++++++++ src/ansi_color.ml | 16 ++++++------ src/gen_rules.ml | 19 +++++++++------ src/gen_rules.mli | 2 +- src/import.ml | 11 ++++++++- src/main.ml | 10 ++++++-- src/main.mli | 2 +- 10 files changed, 102 insertions(+), 55 deletions(-) diff --git a/README.org b/README.org index a3ea9b08..ae285de1 100644 --- a/README.org +++ b/README.org @@ -51,7 +51,11 @@ packages. When building via [[https://opam.ocaml.org/][opam]], it is able to cor libraries that were previously installed even if they are already present in the source tree. -The magic invocation is =jbuilder build-package =. +The magic invocation is: + +#+begin_src sh +$ jbuilder build --only-packages @install +#+end_src *** Building against several configurations at once diff --git a/bin/main.ml b/bin/main.ml index 7b2d027e..9c368462 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -17,6 +17,7 @@ type common = ; workspace_file : string option ; root : string ; target_prefix : string + ; only_packages : String_set.t option } let prefix_target common s = common.target_prefix ^ s @@ -34,8 +35,10 @@ let set_common c = module Main = struct include Jbuilder.Main - let setup ?only_package ?filter_out_optional_stanzas_with_missing_deps common = - setup ?workspace_file:common.workspace_file ?only_package + let setup ?filter_out_optional_stanzas_with_missing_deps common = + setup + ?workspace_file:common.workspace_file + ?only_packages:common.only_packages ?filter_out_optional_stanzas_with_missing_deps () end @@ -104,8 +107,16 @@ let help_secs = ] let common = - let make concurrency debug_rules debug_dep_path debug_findlib dev_mode - workspace_file root = + let make + concurrency + only_packages + debug_rules + debug_dep_path + debug_findlib + dev_mode + workspace_file + root + = let root, to_cwd = match root with | Some dn -> (dn, []) @@ -119,6 +130,9 @@ let common = ; workspace_file ; root ; target_prefix = String.concat ~sep:"" (List.map to_cwd ~f:(sprintf "%s/")) + ; only_packages = + Option.map only_packages + ~f:(fun s -> String_set.of_list (String.split s ~on:',')) } in let docs = copts_sect in @@ -129,6 +143,16 @@ let common = ~doc:{|Run no more than $(i,JOBS) commands simultaneously.|} ) in + let only_packages = + Arg.(value + & opt (some string) None + & info ["only-packages"] ~docs ~docv:"PACKAGES" + ~doc:{|Ignore stanzas referring to a package that is not in $(b,PACKAGES). + $(b,PACKAGES) is a coma-separated list of package name. You need to + use this option in your $(i,.opam) file if your project + contains several packages.|} + ) + in let drules = Arg.(value & flag @@ -173,6 +197,7 @@ let common = in Term.(const make $ concurrency + $ only_packages $ drules $ ddep_path $ dfindlib @@ -210,34 +235,6 @@ let resolve_package_install setup pkg = | Error () -> die "Unknown package %s!%s" pkg (hint pkg (String_map.keys setup.packages)) -let build_package common pkg = - Future.Scheduler.go ~log:(create_log ()) - (Main.setup common ~only_package:pkg >>= fun setup -> - do_build setup [resolve_package_install setup pkg]) - -let build_package = - let doc = "Build a single package in release mode." in - let man = - [ `S "DESCRIPTION" - ; `P {|This command is meant to be used in $(i,.opam) files. - It builds the given package as if all the definitions in - the source tree that are for another package didn't existed. - |} - ; `P {|More precisely, this is what you should use in your $(i,.opam) file:|} - ; `Pre {| build: ["jbuilder" "build-package" "" "-j" jobs|} - ; `Blocks help_secs - ] - in - let name_ = Arg.info [] ~docv:"PACKAGE-NAME" in - let go common pkg = - set_common common; - build_package common pkg - in - ( Term.(const go - $ common - $ Arg.(required & pos 0 (some string) None name_)) - , Term.info "build-package" ~doc ~man) - type target = | File of Path.t | Alias of Path.t * Alias.t @@ -558,7 +555,6 @@ let exec = let all = [ installed_libraries - ; build_package ; external_lib_deps ; build_targets ; runtest diff --git a/doc/jbuild b/doc/jbuild index 0a5d5c3f..87ce0bb5 100644 --- a/doc/jbuild +++ b/doc/jbuild @@ -4,7 +4,6 @@ open StdLabels let commands = [ "build" - ; "build-package" ; "external-lib-deps" ; "install" ; "installed-libraries" diff --git a/doc/manual.org b/doc/manual.org index a40cec6f..c58995bd 100644 --- a/doc/manual.org +++ b/doc/manual.org @@ -1021,6 +1021,34 @@ So for instance: =foo= build context - =jbuilder build @runtest= will run the tests for all build contexts +** Restricting the set of packages + +You can restrict the set of packages from your workspace that Jbuilder +can see with the =--only-packages= option: + +#+begin_src sh +$ jbuilder build --only-packages pkg1,pkg2,... @install +#+end_src + +This option acts as if you went through all the jbuild files and +commented out the stanzas refering to a package that is not in the +list given to =jbuilder=. + +** Invocation from opam + +You should set the =build:= field of your =.opam= file as +follow: + +#+begin_src +build: [["jbuilder" "build" "--only-packages" "" "--root" "." "-j" jobs @install]] +#+end_src + +This has the following effects: +- it tells jbuilder to build everything that is installable and to + ignore packages other than == defined in your project +- it sets the root to prevent jbuilder from looking it up +- it uses whatever concurrency option opam provides + ** Workspace configuration By default, a workspace has only one build context named =default= diff --git a/src/ansi_color.ml b/src/ansi_color.ml index 609a9623..93f7696b 100644 --- a/src/ansi_color.ml +++ b/src/ansi_color.ml @@ -179,14 +179,14 @@ let styles_of_tag = function let setup_err_formatter_colors () = let open Format in if Lazy.force stderr_supports_colors then begin - let ppf = err_formatter in - let funcs = pp_get_formatter_tag_functions ppf () in - pp_set_mark_tags ppf true; - pp_set_formatter_tag_functions ppf - { funcs with - mark_close_tag = (fun _ -> ansi_escape_of_styles [Reset]) - ; mark_open_tag = (fun tag -> ansi_escape_of_styles (styles_of_tag tag)) - } + List.iter [err_formatter; die_ppf] ~f:(fun ppf -> + let funcs = pp_get_formatter_tag_functions ppf () in + pp_set_mark_tags ppf true; + pp_set_formatter_tag_functions ppf + { funcs with + mark_close_tag = (fun _ -> ansi_escape_of_styles [Reset]) + ; mark_open_tag = (fun tag -> ansi_escape_of_styles (styles_of_tag tag)) + }) end let output_filename = [Bold; Foreground Green] diff --git a/src/gen_rules.ml b/src/gen_rules.ml index 9ec84569..12c094db 100644 --- a/src/gen_rules.ml +++ b/src/gen_rules.ml @@ -1806,7 +1806,7 @@ module Gen(P : Params) = struct end let gen ~contexts ?(filter_out_optional_stanzas_with_missing_deps=true) - ?only_package conf = + ?only_packages conf = let open Future in let { Jbuild_load. file_tree; tree; jbuilds; packages } = conf in let module Common = struct @@ -1816,23 +1816,28 @@ let gen ~contexts ?(filter_out_optional_stanzas_with_missing_deps=true) ~f:(fun ~key:_ ~data:{ Package. path; _ } acc -> Path.Set.add path acc) let file_tree = file_tree - let packages = packages + let packages = + match only_packages with + | None -> packages + | Some pkgs -> + String_map.filter packages ~f:(fun _ { Package.name; _ } -> + String_set.mem name pkgs) let filter_out_optional_stanzas_with_missing_deps = filter_out_optional_stanzas_with_missing_deps end in List.map contexts ~f:(fun context -> Jbuild_load.Jbuilds.eval ~context jbuilds >>| fun stanzas -> let stanzas = - match only_package with + match only_packages with | None -> stanzas - | Some pkg -> + | Some pkgs -> List.map stanzas ~f:(fun (dir, stanzas) -> (dir, List.filter stanzas ~f:(fun stanza -> match (stanza : Stanza.t) with - | Library { public = Some { package; _ }; _ } -> - package = pkg - | Install { package = Some name; _ } -> name = pkg + | Library { public = Some { package; _ }; _ } + | Install { package = Some package; _ } -> + String_set.mem package pkgs | _ -> true))) in let module M = diff --git a/src/gen_rules.mli b/src/gen_rules.mli index b8e46f8f..4f17b76f 100644 --- a/src/gen_rules.mli +++ b/src/gen_rules.mli @@ -3,7 +3,7 @@ open! Import val gen : contexts:Context.t list -> ?filter_out_optional_stanzas_with_missing_deps:bool (** default: true *) - -> ?only_package:string + -> ?only_packages:String_set.t -> Jbuild_load.conf -> (Build_interpret.Rule.t list * (* Evaluated jbuilds per context names *) diff --git a/src/import.ml b/src/import.ml index bac55fbd..99dd8229 100644 --- a/src/import.ml +++ b/src/import.ml @@ -341,7 +341,16 @@ let lines_of_file fn = with_file_in fn ~f:input_lines let write_file fn data = with_file_out fn ~f:(fun oc -> output_string oc data) exception Fatal_error of string -let die fmt = ksprintf (fun msg -> raise (Fatal_error msg)) fmt +let die_buf = Buffer.create 128 +let die_ppf (* Referenced in Ansi_color *) = Format.formatter_of_buffer die_buf +let die fmt = + Format.kfprintf + (fun ppf -> + Format.pp_print_flush ppf (); + let s = Buffer.contents die_buf in + Buffer.clear die_buf; + raise (Fatal_error s)) + die_ppf fmt let warn fmt = ksprintf (fun msg -> diff --git a/src/main.ml b/src/main.ml index 8506f3f8..0e5ae0d6 100644 --- a/src/main.ml +++ b/src/main.ml @@ -15,8 +15,14 @@ let package_install_file { packages; _ } pkg = let setup ?filter_out_optional_stanzas_with_missing_deps ?workspace ?(workspace_file="jbuild-workspace") - ?only_package () = + ?only_packages () = let conf = Jbuild_load.load () in + Option.iter only_packages ~f:(fun set -> + String_set.iter set ~f:(fun pkg -> + if not (String_map.mem pkg conf.packages) then + die "@{Error@}: I don't know about package %s \ + (passed through --only-packages)%s" + pkg (hint pkg (String_map.keys conf.packages)))); let workspace = match workspace with | Some w -> w @@ -34,7 +40,7 @@ let setup ?filter_out_optional_stanzas_with_missing_deps Context.create_for_opam ~name ~switch ?root ~merlin ())) >>= fun contexts -> Gen_rules.gen conf ~contexts - ?only_package + ?only_packages ?filter_out_optional_stanzas_with_missing_deps >>= fun (rules, stanzas) -> let build_system = Build_system.create ~contexts ~file_tree:conf.file_tree ~rules in diff --git a/src/main.mli b/src/main.mli index 4f39ebe5..30be0b8d 100644 --- a/src/main.mli +++ b/src/main.mli @@ -15,7 +15,7 @@ val setup : ?filter_out_optional_stanzas_with_missing_deps:bool -> ?workspace:Workspace.t -> ?workspace_file:string - -> ?only_package:string + -> ?only_packages:String_set.t -> unit -> setup Future.t val external_lib_deps