[buck integration] Support passing targets by file to buck in both the compilation database integration and the flavors integration

Reviewed By: jvillard

Differential Revision: D5763855

fbshipit-source-id: 0e3e367
master
Dulma Churchill 7 years ago committed by Facebook Github Bot
parent 8cee4fefd5
commit 5f9c020570

@ -82,7 +82,7 @@ let get_dependency_targets_and_add_flavors targets ~depth =
^ build_deps_string targets ^ ")\"" ) ]
in
let buck_query_cmd = String.concat buck_query ~sep:" " in
Logging.(debug Linters Medium) "*** Executing command:@\n*** %s@." buck_query_cmd ;
Logging.(debug Linters Quiet) "*** Executing command:@\n*** %s@." buck_query_cmd ;
let output, exit_or_signal = Utils.with_process_in buck_query_cmd In_channel.input_lines in
match exit_or_signal with
| Error _ as status
@ -92,3 +92,29 @@ let get_dependency_targets_and_add_flavors targets ~depth =
| Ok ()
-> List.map output ~f:(fun name ->
string_of_target (add_flavor_to_target {name; flavors= Config.append_buck_flavors}) )
(** Given a list of arguments return the extended list of arguments where
the args in a file have been extracted *)
let inline_argument_files buck_args =
let expand_buck_arg buck_arg =
if String.is_prefix ~prefix:"@" buck_arg then
let file_name = String.chop_prefix_exn ~prefix:"@" buck_arg in
if Sys.file_exists file_name <> `Yes then [buck_arg]
(* Arguments that start with @ could mean something different than an arguments file in buck. *)
else
let expanded_args =
try Utils.with_file_in file_name ~f:In_channel.input_lines
with exn ->
Logging.die UserError "Could not read from file '%s': %a@." file_name Exn.pp exn
in
expanded_args
else [buck_arg]
in
List.concat_map ~f:expand_buck_arg buck_args
let store_targets_in_file buck_targets =
let file = Filename.temp_file "buck_targets_" ".txt" in
let write_args outc = Out_channel.output_string outc (String.concat ~sep:"\n" buck_targets) in
Utils.with_file_out file ~f:write_args |> ignore ;
L.(debug Capture Quiet) "Buck targets options stored in file '%s'@\n" file ;
Printf.sprintf "@%s" file

@ -26,3 +26,10 @@ val add_flavors_to_buck_command : string list -> string list
val get_dependency_targets_and_add_flavors : string list -> depth:int option -> string list
(** Runs buck query to get the dependency targets of the given targets
[get_dependency_targets args] = targets with dependent targets, other args *)
val inline_argument_files : string list -> string list
(** Given a list of arguments to buck, return the extended list of arguments where
the args in a file have been extracted *)
val store_targets_in_file : string list -> string
(** Given a list of buck targets, stores them in a file and returns the file name *)

@ -64,16 +64,10 @@ let run_compilation_database compilation_database should_capture_file =
Process.run_jobs_in_parallel ~fail_on_failed_job jobs_stack
(run_compilation_file compilation_database) job_to_string
let buck_targets_in_file buck_targets =
let file = Filename.temp_file "buck_targets_" ".txt" in
let write_args outc = Out_channel.output_string outc (String.concat ~sep:"\n" buck_targets) in
Utils.with_file_out file ~f:write_args |> ignore ;
L.(debug Capture Quiet) "Buck targets options stored in file %s@\n" file ;
Printf.sprintf "@%s" file
(** Computes the compilation database files. *)
let get_compilation_database_files_buck ~prog ~args =
let targets, no_targets = List.partition_tf ~f:Buck.is_target_string args in
let all_buck_args = Buck.inline_argument_files args in
let targets, no_targets = List.partition_tf ~f:Buck.is_target_string all_buck_args in
let targets =
match Config.buck_compilation_database with
| Some Deps depth
@ -84,8 +78,10 @@ let get_compilation_database_files_buck ~prog ~args =
match no_targets with
| "build" :: _
-> (
let targets_in_file = buck_targets_in_file targets in
let targets_in_file = Buck.store_targets_in_file targets in
let build_args = no_targets @ ["--config"; "*//cxx.pch_enabled=false"; targets_in_file] in
Logging.(debug Linters Quiet)
"Processed buck command is : 'buck %s'@\n" (String.concat ~sep:" " build_args) ;
Process.create_process_and_wait ~prog ~args:build_args ;
let buck_targets_shell =
[prog; "targets"; "--show-output"; targets_in_file] |> Utils.shell_escape_command

@ -330,7 +330,16 @@ let capture ~changed_files = function
(Option.to_list (Sys.getenv CLOpt.args_env_var) @ ["--buck"])
in
Unix.putenv ~key:CLOpt.args_env_var ~data:infer_args_with_buck ;
Buck.add_flavors_to_buck_command build_cmd
let all_buck_args = Buck.inline_argument_files build_cmd in
let targets, no_targets =
List.partition_tf ~f:Buck.is_target_string all_buck_args
in
let targets_with_flavor = Buck.add_flavors_to_buck_command targets in
let targets_in_file = Buck.store_targets_in_file targets_with_flavor in
let updated_buck_cmd = no_targets @ [targets_in_file] in
Logging.(debug Capture Quiet)
"Processed buck command '%s'@\n" (String.concat ~sep:" " updated_buck_cmd) ;
updated_buck_cmd
else build_cmd ) )
in
run_command ~prog:infer_py ~args

@ -24,4 +24,4 @@ infer-out/report.json: $(CLANG_DEPS) $(SOURCES)
NO_BUCKD=1 \
$(INFER_BIN) -a $(ANALYZER) --stats $(INFER_OPTIONS) -o $(CURDIR)/$(@D) \
--buck-compilation-database no-deps \
-- $(BUCK) build --no-cache '//clang_compilation_database:Hel lo#x86_64')
-- $(BUCK) build --no-cache '//clang_compilation_database:Hel lo#x86_64' @clang_compilation_database/buck_target_hello_test.txt)

@ -1,4 +1,5 @@
clang_compilation_database/hello.cpp, test0, 2, NULL_DEREFERENCE, [start of procedure test0()]
clang_compilation_database/hello.cpp, test1, 2, NULL_DEREFERENCE, [start of procedure test1(),start of procedure deref1()]
clang_compilation_database/hello.cpp, test2, 2, NULL_DEREFERENCE, [start of procedure test2(),start of procedure deref2()]
clang_compilation_database/hello_test.c, hello_test, 2, NULL_DEREFERENCE, [start of procedure hello_test()]
clang_compilation_database/lib3.h, test, 1, NULL_DEREFERENCE, [start of procedure test(),start of procedure deref3()]

@ -9,7 +9,7 @@ TESTS_DIR = ../..
ROOT_DIR = $(TESTS_DIR)/../..
ANALYZER = infer
BUCK_TARGET = //src:hello
BUCK_TARGET = //src:hello @buck_target.txt
SOURCES = $(wildcard src/hello.c)
OBJECTS = buck-out/gen/src/hello\#compile-hello.c.o1f717d69,default/hello.c.o
INFER_OPTIONS = --report-custom-error --developer-mode --project-root $(TESTS_DIR) --no-keep-going

@ -1,2 +1,3 @@
src/hello.c, test, 2, NULL_DEREFERENCE, [start of procedure test()]
src/hello2.c, test2, 2, NULL_DEREFERENCE, [start of procedure test2()]
src/hello3.c, test3, 2, NULL_DEREFERENCE, [start of procedure test3()]

@ -4,3 +4,10 @@ cxx_library(
'hello.c', 'hello2.c',
],
)
cxx_library(
name = 'hello3',
srcs = [
'hello3.c',
],
)

@ -0,0 +1,15 @@
/*
* Copyright (c) 2015 - present Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include <stdlib.h>
void test3() {
int* s = NULL;
*s = 42;
}

@ -7,3 +7,10 @@ cxx_library(
'-std=c++11',
],
)
cxx_library(
name = 'hello_test',
srcs = [
'hello_test.c',
],
)

@ -0,0 +1,15 @@
/*
* Copyright (c) 2015 - present Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include <stdlib.h>
void hello_test() {
int* s = NULL;
*s = 42;
}
Loading…
Cancel
Save