Skip to content

Commit

Permalink
Rename Buck-related interfaces to eliminate reference to V2
Browse files Browse the repository at this point in the history
Reviewed By: rchen152

Differential Revision: D64935429

fbshipit-source-id: 35c1655aec93fe2a5606a36310d4a460d0d3904f
  • Loading branch information
grievejia authored and facebook-github-bot committed Oct 25, 2024
1 parent 0370410 commit 35f5edd
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 183 deletions.
75 changes: 34 additions & 41 deletions source/buck/buck.mli
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@
communicates with Buck (e.g. how to shell out to the [buck] executable, how cli arguments are
passed, how subprocess stdout/stderr are handled, etc.).
- The middle abstraction layer is {!module:Interface}, which handles how one or more [buck]
invocations can be organized to perform higher-level tasks (e.g. normalize targets, load
source databases, etc.).
invocations can be organized to perform higher-level tasks (e.g. load source databases).
- The highest abstraction layer is {!module:Builder}, which coordinates one or more [buck] tasks
as well as Pyre's own link tree building tasks to offer push-button build state management.
(e.g. "build this project", "incrementally update my build", etc.).*)
Expand Down Expand Up @@ -330,22 +329,19 @@ module Raw : sig
type t = ?mode:string -> ?isolation_prefix:string -> string list -> Output.t Lwt.t
end

(** This module contains APIs specific to Buck2 *)
module V2 : sig
type t
type t

(** Create an instance of [t] based on system-installed Buck2. The [additional_log_size]
parameter controls how many lines of Buck2 log to preserve when an {!BuckError} is raised.
By default, the size is set to 0, which means no additional log will be kept. *)
val create : ?additional_log_size:int -> unit -> t
(** Create an instance of [t] based on system-installed Buck2. The [additional_log_size] parameter
controls how many lines of Buck2 log to preserve when an {!BuckError} is raised. By default,
the size is set to 0, which means no additional log will be kept. *)
val create : ?additional_log_size:int -> unit -> t

(** Create an instance of [t] from custom [bxl] behavior. Useful for unit testing. *)
val create_for_testing : bxl:Command.t -> unit -> t
(** Create an instance of [t] from custom [bxl] behavior. Useful for unit testing. *)
val create_for_testing : bxl:Command.t -> unit -> t

(** Shell out to `buck2 bxl` with the given cli arguments. Returns the content of stdout. If the
return code is not 0, raise [BuckError]. *)
val bxl : t -> Command.t
end
(** Shell out to `buck2 bxl` with the given cli arguments. Returns the content of stdout. If the
return code is not 0, raise [BuckError]. *)
val bxl : t -> Command.t
end

(** This module contains high-level interfaces for invoking [buck] as an external tool. It relies on
Expand Down Expand Up @@ -375,13 +371,12 @@ module Interface : sig
val create : ?metadata:'metadata -> 'data -> ('data, 'metadata) t
end

(** This module contains APIs specific to Buck2 *)
module V2 : sig
(** This module contains APIs specific eager Buck building *)
module Eager : sig
type t

(** Create an instance of [t] from an instance of {!Raw.V2.t} and some buck options. Interfaces
created this way is only compatible with Buck2.*)
val create : ?mode:string -> ?isolation_prefix:string -> ?bxl_builder:string -> Raw.V2.t -> t
(** Create an instance of [t] from an instance of {!Raw.t} and some buck options. *)
val create : ?mode:string -> ?isolation_prefix:string -> ?bxl_builder:string -> Raw.t -> t

(** Create an instance of [t] from custom [construct_build_map] behavior. Useful for unit
testing. *)
Expand All @@ -406,16 +401,15 @@ module Interface : sig

(** This module contains APIs specific to lazy Buck building.
Lazy building is only supported for Buck2. The APIs are very simliar to those of
{!Interface.V2}, except that targets to build are not specified upfront for the lazy case.
Instead, the lazy builder is only given a set of source paths to construct the build map on,
and the builder itself needs to figure out what are the corresponding targets for these source
paths. *)
The APIs are very similar to those of {!Interface.Eager}, except that targets to build are not
specified upfront for the lazy case. Instead, the lazy builder is only given a set of source
paths to construct the build map on, and the builder itself needs to figure out what are the
corresponding targets for these source paths. *)
module Lazy : sig
type t

(** Create an instance of [t] from an instance of {!Raw.V2.t} and some buck options. *)
val create : ?mode:string -> ?isolation_prefix:string -> bxl_builder:string -> Raw.V2.t -> t
(** Create an instance of [t] from an instance of {!Raw.t} and some buck options. *)
val create : ?mode:string -> ?isolation_prefix:string -> bxl_builder:string -> Raw.t -> t

(** Create an instance of [t] from custom [construct_build_map] behavior. Useful for unit
testing. *)
Expand Down Expand Up @@ -446,14 +440,14 @@ module Builder : sig

(** This module contains APIs specific to classical, non-lazy Buck building, where the targets
that need to be built are specified upfront. *)
module Classic : sig
module Eager : sig
type t

(** {1 Creation} *)

(** Create an instance of {!Builder.Classic.t} from an instance of {!Interface.V2.t} and some
buck options. Builders created with way are only compatible with Buck2. *)
val create_v2 : source_root:PyrePath.t -> artifact_root:PyrePath.t -> Interface.V2.t -> t
(** Create an instance of {!Builder.Eager.t} from an instance of {!Interface.Eager.t} and some
buck options. *)
val create : source_root:PyrePath.t -> artifact_root:PyrePath.t -> Interface.Eager.t -> t

(** {1 Build} *)

Expand All @@ -468,12 +462,11 @@ module Builder : sig
}
end

(** A type representing the result of builds, along with some metadata about the build
(Buck2-only). *)
(** A type representing the result of builds, along with some metadata about the build. *)
type build_result_t = (Interface.BuildResult.t, string) Interface.WithMetadata.t

(** A type representing the result of incremental builds, along with some metadata about the
build (Buck2-only). *)
build. *)
type incremental_build_result_t = (IncrementalBuildResult.t, string) Interface.WithMetadata.t

(** Given a list of buck target specificaitons to build, construct a build map for the targets
Expand Down Expand Up @@ -505,7 +498,7 @@ module Builder : sig
accordingly.
In most cases, downstream clients are discouraged from invoking this API.
{!val:Builder.Classic.build} should be used instead, since it does not force the clients to
{!val:Builder.Eager.build} should be used instead, since it does not force the clients to
construct a build map by themselves and risk having the build map and the link tree
inconsistent with what the target specification says. The only scenario where it makes sense
to prefer {!val:restore} over {!val:build} is saved state loading: in that case, we already
Expand Down Expand Up @@ -601,17 +594,17 @@ module Builder : sig

(** This module contains APIs specific to lazy Buck building.
Lazy building is only supported for Buck2. The APIs are very simliar to those of {!Builder},
except that targets to build are not specified upfront for the lazy case. Instead, the lazy
builder is only given a set of source paths to construct the build map on, and the builder
itself needs to figure out what are the corresponding targets for these source paths. *)
The APIs are very similar to those of {!Builder.Eager}, except that targets to build are not
specified upfront for the lazy case. Instead, the lazy builder is only given a set of source
paths to construct the build map on, and the builder itself needs to figure out what are the
corresponding targets for these source paths. *)
module Lazy : sig
type t

(** {1 Creation} *)

(** Create an instance of [Builder.t] from an instance of {!Interface.Lazy.t} and some buck
options. Builders created this way are only compatible with Buck2. *)
(** Create an instance of [Builder.Lazy.t] from an instance of {!Interface.Lazy.t} and some buck
options. *)
val create : source_root:PyrePath.t -> artifact_root:PyrePath.t -> Interface.Lazy.t -> t

(** {1 Build} *)
Expand Down
59 changes: 29 additions & 30 deletions source/buck/builder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ let do_lookup_artifact ~index ~source_root ~artifact_root path =
|> List.map ~f:(fun relative -> PyrePath.create_relative ~root:artifact_root ~relative)


module Classic = struct
module Eager = struct
module IncrementalBuildResult = struct
type t = {
build_map: BuildMap.t;
Expand Down Expand Up @@ -114,32 +114,31 @@ module Classic = struct
identifier: string;
}

module V2 = struct
let build ~interface ~source_root ~artifact_root targets =
let open Lwt.Infix in
Interface.V2.construct_build_map interface targets
>>= fun { Interface.WithMetadata.data = build_map; metadata } ->
Log.info "Constructing Python link-tree for type checking...";
Artifacts.populate ~source_root ~artifact_root build_map
>>= function
| Result.Error message -> raise (LinkTreeConstructionError message)
| Result.Ok () ->
Lwt.return Interface.(WithMetadata.create { BuildResult.targets; build_map } ?metadata)


let full_incremental_build ~interface ~source_root ~artifact_root ~old_build_map targets =
let open Lwt.Infix in
Interface.V2.construct_build_map interface targets
>>= fun { Interface.WithMetadata.data = build_map; metadata } ->
do_incremental_build ~source_root ~artifact_root ~old_build_map ~new_build_map:build_map ()
>>= fun changed_artifacts ->
Lwt.return
(Interface.WithMetadata.create
{ IncrementalBuildResult.targets; build_map; changed_artifacts }
?metadata)
end
let build ~interface ~source_root ~artifact_root targets =
let open Lwt.Infix in
Interface.Eager.construct_build_map interface targets
>>= fun { Interface.WithMetadata.data = build_map; metadata } ->
Log.info "Constructing Python link-tree for type checking...";
Artifacts.populate ~source_root ~artifact_root build_map
>>= function
| Result.Error message -> raise (LinkTreeConstructionError message)
| Result.Ok () ->
Lwt.return Interface.(WithMetadata.create { BuildResult.targets; build_map } ?metadata)


let create_v2 ~source_root ~artifact_root interface =
let full_incremental_build ~interface ~source_root ~artifact_root ~old_build_map targets =
let open Lwt.Infix in
Interface.Eager.construct_build_map interface targets
>>= fun { Interface.WithMetadata.data = build_map; metadata } ->
do_incremental_build ~source_root ~artifact_root ~old_build_map ~new_build_map:build_map ()
>>= fun changed_artifacts ->
Lwt.return
(Interface.WithMetadata.create
{ IncrementalBuildResult.targets; build_map; changed_artifacts }
?metadata)


let create ~source_root ~artifact_root interface =
let fast_incremental_build_with_normalized_targets
~old_build_map
~old_build_map_index:_
Expand All @@ -149,14 +148,14 @@ module Classic = struct
=
(* NOTE: The same query we relied on to optimize incremental build in Buck1 does not exist in
Buck2. For now, fallback to a less optimized rebuild approach. *)
V2.full_incremental_build ~interface ~source_root ~artifact_root ~old_build_map targets
full_incremental_build ~interface ~source_root ~artifact_root ~old_build_map targets
in
{
build = V2.build ~interface ~source_root ~artifact_root;
build = build ~interface ~source_root ~artifact_root;
restore = restore ~source_root ~artifact_root;
full_incremental_build = V2.full_incremental_build ~interface ~source_root ~artifact_root;
full_incremental_build = full_incremental_build ~interface ~source_root ~artifact_root;
incremental_build_with_normalized_targets =
V2.full_incremental_build ~interface ~source_root ~artifact_root;
full_incremental_build ~interface ~source_root ~artifact_root;
fast_incremental_build_with_normalized_targets;
lookup_source = lookup_source ~source_root ~artifact_root;
lookup_artifact = lookup_artifact ~source_root ~artifact_root;
Expand Down
10 changes: 5 additions & 5 deletions source/buck/interface.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module WithMetadata = struct
let create ?metadata data = { data; metadata }
end

module V2 = struct
module Eager = struct
type t = { construct_build_map: string list -> (BuildMap.t, string) WithMetadata.t Lwt.t }

let create_for_testing ~construct_build_map () = { construct_build_map }
Expand Down Expand Up @@ -152,7 +152,7 @@ module V2 = struct
List.bind target_patterns ~f:(fun target ->
["--target"; Stdlib.Format.sprintf "%s" target]);
]
|> Raw.V2.bxl ?mode ?isolation_prefix raw
|> Raw.bxl ?mode ?isolation_prefix raw


let warn_on_conflict
Expand Down Expand Up @@ -224,8 +224,8 @@ module Lazy = struct


let parse_bxl_output bxl_output =
V2.parse_merged_sourcedb_path_json bxl_output
|> V2.parse_merged_sourcedb_json
Eager.parse_merged_sourcedb_path_json bxl_output
|> Eager.parse_merged_sourcedb_json
|> parse_merged_sourcedb


Expand All @@ -247,7 +247,7 @@ module Lazy = struct
["--"];
List.bind target_patterns ~f:(fun source_path -> ["--source"; source_path]);
]
|> Raw.V2.bxl ?mode ?isolation_prefix raw
|> Raw.bxl ?mode ?isolation_prefix raw
>>= fun { Raw.Command.Output.stdout; _ } -> Lwt.return (parse_bxl_output stdout)


Expand Down
Loading

0 comments on commit 35f5edd

Please sign in to comment.