Skip to content

Commit

Permalink
Expose buck metadata from high-level Buck API layer
Browse files Browse the repository at this point in the history
Reviewed By: kinto0

Differential Revision: D47579484

fbshipit-source-id: 7e8c5197b45ffd4d1a51b779aeb78eb5ae67ce75
  • Loading branch information
grievejia authored and facebook-github-bot committed Jul 20, 2023
1 parent 943519c commit f3e1cd3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
18 changes: 13 additions & 5 deletions source/buck/buck.mli
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ module Builder : sig

(** {1 Build} *)

(** The return type for incremental builds. It contains a build map, a list of buck targets that
(** The result type for incremental builds. It contains a build map, a list of buck targets that
are successfully included in the build, and a list of artifact files whose contents may be
altered by the build . *)
module IncrementalBuildResult : sig
Expand All @@ -555,6 +555,14 @@ module Builder : sig
}
end

(** A type representing the result of builds, along with some metadata about the build
(Buck2-only). *)
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). *)
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
and create a Python link tree at the given artifact root according to the build map. Return
the constructed build map along with a list of targets that are covered by the build map.
Expand All @@ -578,7 +586,7 @@ module Builder : sig
Note this API does not ensure the artifact root to be empty before the build starts. If
cleaness of the artifact directory is desirable, it is expected that the caller would take
care of that before its invocation. *)
val build : targets:string list -> t -> Interface.BuildResult.t Lwt.t
val build : targets:string list -> t -> build_result_t Lwt.t

(** Given a build map, create the corresponding Python link tree at the given artifact root
accordingly.
Expand Down Expand Up @@ -615,7 +623,7 @@ module Builder : sig
: old_build_map:BuildMap.t ->
targets:string list ->
t ->
IncrementalBuildResult.t Lwt.t
incremental_build_result_t Lwt.t

(** Given a list of normalized targets to build, fully construct a new build map for the targets
and incrementally update the Python link tree at the given artifact root according to how
Expand All @@ -633,7 +641,7 @@ module Builder : sig
: old_build_map:BuildMap.t ->
targets:Target.t list ->
t ->
IncrementalBuildResult.t Lwt.t
incremental_build_result_t Lwt.t

(** Given a list of normalized targets and changed/removed files, incrementally construct a new
build map for the targets and incrementally update the Python link tree at the given
Expand All @@ -653,7 +661,7 @@ module Builder : sig
changed_paths:PyrePath.t list ->
removed_paths:PyrePath.t list ->
t ->
IncrementalBuildResult.t Lwt.t
incremental_build_result_t Lwt.t

(** {1 Lookup} *)

Expand Down
38 changes: 26 additions & 12 deletions source/buck/builder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ module Classic = struct
}
end

type build_result_t = (Interface.BuildResult.t, string) Interface.WithMetadata.t

type incremental_build_result_t = (IncrementalBuildResult.t, string) Interface.WithMetadata.t

let restore ~source_root ~artifact_root build_map =
let open Lwt.Infix in
Artifacts.populate ~source_root ~artifact_root build_map
Expand All @@ -92,19 +96,19 @@ module Classic = struct


type t = {
build: string list -> Interface.BuildResult.t Lwt.t;
build: string list -> build_result_t Lwt.t;
restore: BuildMap.t -> unit Lwt.t;
full_incremental_build:
old_build_map:BuildMap.t -> string list -> IncrementalBuildResult.t Lwt.t;
old_build_map:BuildMap.t -> string list -> incremental_build_result_t Lwt.t;
incremental_build_with_normalized_targets:
old_build_map:BuildMap.t -> BuckTarget.t list -> IncrementalBuildResult.t Lwt.t;
old_build_map:BuildMap.t -> BuckTarget.t list -> incremental_build_result_t Lwt.t;
fast_incremental_build_with_normalized_targets:
old_build_map:BuildMap.t ->
old_build_map_index:BuildMap.Indexed.t ->
changed_paths:PyrePath.t list ->
removed_paths:PyrePath.t list ->
BuckTarget.t list ->
IncrementalBuildResult.t Lwt.t;
incremental_build_result_t Lwt.t;
lookup_source: index:BuildMap.Indexed.t -> PyrePath.t -> PyrePath.t option;
lookup_artifact: index:BuildMap.Indexed.t -> PyrePath.t -> PyrePath.t list;
identifier: string;
Expand All @@ -121,7 +125,7 @@ module Classic = struct
Artifacts.populate ~source_root ~artifact_root build_map
>>= function
| Result.Error message -> raise (LinkTreeConstructionError message)
| Result.Ok () -> Lwt.return build_result
| Result.Ok () -> Lwt.return (Interface.WithMetadata.create build_result)


let full_incremental_build ~interface ~source_root ~artifact_root ~old_build_map targets =
Expand All @@ -132,7 +136,9 @@ module Classic = struct
>>= fun { Interface.BuildResult.targets; build_map } ->
do_incremental_build ~source_root ~artifact_root ~old_build_map ~new_build_map:build_map ()
>>= fun changed_artifacts ->
Lwt.return { IncrementalBuildResult.targets; build_map; changed_artifacts }
Lwt.return
(Interface.WithMetadata.create
{ IncrementalBuildResult.targets; build_map; changed_artifacts })


let incremental_build_with_normalized_targets
Expand All @@ -147,7 +153,9 @@ module Classic = struct
>>= fun { Interface.BuildResult.targets; build_map } ->
do_incremental_build ~source_root ~artifact_root ~old_build_map ~new_build_map:build_map ()
>>= fun changed_artifacts ->
Lwt.return { IncrementalBuildResult.targets; build_map; changed_artifacts }
Lwt.return
(Interface.WithMetadata.create
{ IncrementalBuildResult.targets; build_map; changed_artifacts })


let compute_difference_from_removed_relative_paths ~build_map_index removed_paths =
Expand Down Expand Up @@ -264,28 +272,34 @@ module Classic = struct
let open Lwt.Infix in
update_artifacts ~source_root ~artifact_root difference
>>= fun changed_artifacts ->
Lwt.return { IncrementalBuildResult.targets; build_map; changed_artifacts }
Lwt.return
(Interface.WithMetadata.create
{ IncrementalBuildResult.targets; build_map; changed_artifacts })
end

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 = _ } ->
>>= 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.BuildResult.targets; build_map }
| 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 = _ } ->
>>= 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 { IncrementalBuildResult.targets; build_map; changed_artifacts }
Lwt.return
(Interface.WithMetadata.create
{ IncrementalBuildResult.targets; build_map; changed_artifacts }
?metadata)
end

let create ~source_root ~artifact_root interface =
Expand Down
17 changes: 13 additions & 4 deletions source/server/buildSystem.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ let create_for_testing
module ClassicBuckBuilder = Buck.Builder.Classic
module WithMetadata = Buck.Interface.WithMetadata
module BuckBuildSystem = struct
module State = struct
Expand Down Expand Up @@ -77,7 +78,11 @@ module BuckBuildSystem = struct
let create_from_scratch ~builder ~targets () =
let open Lwt.Infix in
ClassicBuckBuilder.build builder ~targets
>>= fun { Buck.Interface.BuildResult.targets = normalized_targets; build_map } ->
>>= fun Buck.Interface.
{
WithMetadata.data = { BuildResult.targets = normalized_targets; build_map };
metadata = _;
} ->
Lwt.return (create ~targets ~builder ~normalized_targets ~build_map ())
Expand Down Expand Up @@ -176,9 +181,13 @@ module BuckBuildSystem = struct
let rebuild_and_update_state rebuild () =
rebuild state.builder
>>= fun {
ClassicBuckBuilder.IncrementalBuildResult.targets = normalized_targets;
build_map;
changed_artifacts;
WithMetadata.data =
{
ClassicBuckBuilder.IncrementalBuildResult.targets = normalized_targets;
build_map;
changed_artifacts;
};
metadata = _;
} ->
State.update ~normalized_targets ~build_map state;
Lwt.return changed_artifacts
Expand Down

0 comments on commit f3e1cd3

Please sign in to comment.