[buck-clang] don't special case for targets

Summary:
There is a lot of subtlety in our parsing of buck targets on the command line, that is then just thrown away. Push this one level up, getting rid of the special case where in Clang mode if we only have "normal" targets we don't resolve them.

Also introduce a proper variant for buck target types.

Reviewed By: skcho

Differential Revision: D22160490

fbshipit-source-id: 500c1b12c
master
Nikos Gorogiannis 4 years ago committed by Facebook GitHub Bot
parent 70a432e795
commit 6a74b967ac

@ -87,6 +87,12 @@ module Target = struct
add_flavor_internal target "infer-capture-all" add_flavor_internal target "infer-capture-all"
end end
type buck_argument =
| NotATarget of string
| AliasTarget of string
| PatternTarget of string
| NormalTarget of string
let parse_target_string = let parse_target_string =
let alias_target_regexp = Str.regexp "^[^/:]+\\(#.*\\)?$" in let alias_target_regexp = Str.regexp "^[^/:]+\\(#.*\\)?$" in
let pattern_target_regexp = Str.regexp "^[^/]*//\\(\\.\\.\\.\\|.*\\(:\\|/\\.\\.\\.\\)\\)$" in let pattern_target_regexp = Str.regexp "^[^/]*//\\(\\.\\.\\.\\|.*\\(:\\|/\\.\\.\\.\\)\\)$" in
@ -94,13 +100,13 @@ let parse_target_string =
let noname_target_regexp = Str.regexp "^[^/]*//.*$" in let noname_target_regexp = Str.regexp "^[^/]*//.*$" in
let parse_with_retry s ~retry = let parse_with_retry s ~retry =
(* do not consider --buck-options as targets *) (* do not consider --buck-options as targets *)
if String.equal s "" || Char.equal s.[0] '-' || Char.equal s.[0] '@' then `NotATarget s if String.equal s "" || Char.equal s.[0] '-' || Char.equal s.[0] '@' then NotATarget s
else if Str.string_match alias_target_regexp s 0 then `AliasTarget s else if Str.string_match alias_target_regexp s 0 then AliasTarget s
else if Str.string_match pattern_target_regexp s 0 then `PatternTarget s else if Str.string_match pattern_target_regexp s 0 then PatternTarget s
else if Str.string_match normal_target_regexp s 0 then `NormalTarget s else if Str.string_match normal_target_regexp s 0 then NormalTarget s
else if Str.string_match noname_target_regexp s 0 then else if Str.string_match noname_target_regexp s 0 then
let name = String.split s ~on:'/' |> List.last_exn in let name = String.split s ~on:'/' |> List.last_exn in
`NormalTarget (F.sprintf "%s:%s" s name) NormalTarget (F.sprintf "%s:%s" s name)
else retry s else retry s
in in
fun s -> fun s ->
@ -271,15 +277,9 @@ let resolve_pattern_targets (buck_mode : BuckMode.t) targets =
else Fn.id else Fn.id
type parsed_args = type parsed_args = {rev_not_targets: string list; targets: string list}
{ rev_not_targets': string list
; normal_targets: string list
; alias_targets: string list
; pattern_targets: string list }
let empty_parsed_args =
{rev_not_targets'= []; normal_targets= []; alias_targets= []; pattern_targets= []}
let empty_parsed_args = {rev_not_targets= []; targets= []}
let split_buck_command buck_cmd = let split_buck_command buck_cmd =
match buck_cmd with match buck_cmd with
@ -326,39 +326,27 @@ let parse_command_and_targets (buck_mode : BuckMode.t) original_buck_args =
parsed_args parsed_args
| param :: arg :: args when List.mem ~equal:String.equal parameters_with_argument param -> | param :: arg :: args when List.mem ~equal:String.equal parameters_with_argument param ->
parse_cmd_args parse_cmd_args
{parsed_args with rev_not_targets'= arg :: param :: parsed_args.rev_not_targets'} {parsed_args with rev_not_targets= arg :: param :: parsed_args.rev_not_targets}
args args
| target :: args -> | target :: args ->
let parsed_args = let parsed_args =
match parse_target_string target with match parse_target_string target with
| `NotATarget s -> | NotATarget s ->
{parsed_args with rev_not_targets'= s :: parsed_args.rev_not_targets'} {parsed_args with rev_not_targets= s :: parsed_args.rev_not_targets}
| `NormalTarget t -> | NormalTarget t | AliasTarget t | PatternTarget t ->
{parsed_args with normal_targets= t :: parsed_args.normal_targets} {parsed_args with targets= t :: parsed_args.targets}
| `AliasTarget a ->
{parsed_args with alias_targets= a :: parsed_args.alias_targets}
| `PatternTarget p ->
{parsed_args with pattern_targets= p :: parsed_args.pattern_targets}
in in
parse_cmd_args parsed_args args parse_cmd_args parsed_args args
in in
let parsed_args = parse_cmd_args empty_parsed_args args in let parsed_args = parse_cmd_args empty_parsed_args args in
let targets = let targets = resolve_pattern_targets buck_mode parsed_args.targets in
match (buck_mode, parsed_args) with
| ClangFlavors, {pattern_targets= []; alias_targets= []; normal_targets} ->
normal_targets
| ( (ClangFlavors | CombinedGenrule | JavaGenruleMaster | ClangCompilationDB _)
, {pattern_targets; alias_targets; normal_targets} ) ->
pattern_targets |> List.rev_append alias_targets |> List.rev_append normal_targets
|> resolve_pattern_targets buck_mode
in
let targets = let targets =
Option.value_map ~default:targets Option.value_map ~default:targets
~f:(fun re -> List.filter ~f:(fun tgt -> not (Str.string_match re tgt 0)) targets) ~f:(fun re -> List.filter ~f:(fun tgt -> not (Str.string_match re tgt 0)) targets)
buck_targets_blacklist_regexp buck_targets_blacklist_regexp
in in
ScubaLogging.log_count ~label:"buck_targets" ~value:(List.length targets) ; ScubaLogging.log_count ~label:"buck_targets" ~value:(List.length targets) ;
(command, parsed_args.rev_not_targets', targets) (command, parsed_args.rev_not_targets, targets)
let rec exceed_length ~max = function let rec exceed_length ~max = function

Loading…
Cancel
Save