Reviewed By: ngorogiannis Differential Revision: D20302009 fbshipit-source-id: a7c12a044master
parent
f8a490d477
commit
74e54575fa
@ -1,70 +0,0 @@
|
||||
# Copyright (c) Facebook, Inc. and its affiliates.
|
||||
#
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
import logging
|
||||
import os
|
||||
import subprocess
|
||||
import traceback
|
||||
|
||||
import util
|
||||
from inferlib import config, utils
|
||||
|
||||
MODULE_NAME = 'ndk-build/clang'
|
||||
MODULE_DESCRIPTION = '''Run analysis of code built with ndk-build
|
||||
|
||||
Analysis examples:
|
||||
infer -- ndk-build'''
|
||||
LANG = ['clang']
|
||||
|
||||
ALIASED_COMMANDS = ['clang', 'clang++', 'cc', 'gcc', 'g++']
|
||||
|
||||
def gen_instance(*args):
|
||||
return NdkBuildCapture(*args)
|
||||
|
||||
|
||||
create_argparser = util.base_argparser(MODULE_DESCRIPTION, MODULE_NAME)
|
||||
|
||||
|
||||
class NdkBuildCapture():
|
||||
def __init__(self, args, cmd):
|
||||
cmd = [
|
||||
cmd[0],
|
||||
'NDK_TOOLCHAIN_VERSION=clang',
|
||||
'TARGET_CC=clang',
|
||||
'TARGET_CXX=clang',
|
||||
'TARGET_LD=ld',
|
||||
] + cmd[1:]
|
||||
self.args = args
|
||||
self.cmd = cmd
|
||||
command_name = os.path.basename(cmd[0])
|
||||
if command_name in ALIASED_COMMANDS:
|
||||
# remove absolute paths for these commands as we want to
|
||||
# substitue our own wrappers instead using a PATH trick
|
||||
cmd[0] = command_name
|
||||
|
||||
def get_envvars(self):
|
||||
env_vars = utils.read_env()
|
||||
wrappers_path = config.WRAPPERS_DIRECTORY
|
||||
# INFER_RESULTS_DIR and INFER_OLD_PATH are used by javac wrapper only
|
||||
env_vars['INFER_OLD_PATH'] = env_vars['PATH']
|
||||
env_vars['PATH'] = '{wrappers}{sep}{path}'.format(
|
||||
wrappers=wrappers_path,
|
||||
sep=os.pathsep,
|
||||
path=env_vars['PATH'],
|
||||
)
|
||||
env_vars['INFER_RESULTS_DIR'] = self.args.infer_out
|
||||
return env_vars
|
||||
|
||||
def capture(self):
|
||||
try:
|
||||
env = utils.encode_env(self.get_envvars())
|
||||
cmd = map(utils.encode, self.cmd)
|
||||
logging.info('Running command %s with env:\n%s' % (cmd, env))
|
||||
subprocess.check_call(cmd, env=env)
|
||||
return os.EX_OK
|
||||
except subprocess.CalledProcessError as exc:
|
||||
if self.args.debug:
|
||||
traceback.print_exc()
|
||||
return exc.returncode
|
@ -0,0 +1,47 @@
|
||||
(*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*)
|
||||
open! IStd
|
||||
module L = Logging
|
||||
|
||||
let capture ~build_cmd =
|
||||
let raw_prog, ndk_args = (List.hd_exn build_cmd, List.tl_exn build_cmd) in
|
||||
(* env vars *)
|
||||
let path_opt = Sys.getenv "PATH" in
|
||||
let new_env =
|
||||
[ ("INFER_OLD_PATH", Option.value path_opt ~default:"")
|
||||
; ("PATH", Option.fold path_opt ~init:Config.wrappers_dir ~f:(Printf.sprintf "%s:%s"))
|
||||
; ("INFER_RESULTS_DIR", Config.results_dir) ]
|
||||
in
|
||||
(* args *)
|
||||
let raw_prog_name = Filename.basename raw_prog in
|
||||
let prog =
|
||||
if List.exists Config.clang_exe_aliases ~f:(String.equal raw_prog_name) then raw_prog_name
|
||||
else raw_prog
|
||||
in
|
||||
let args =
|
||||
"NDK_TOOLCHAIN_VERSION=clang" :: "TARGET_CC=clang" :: "TARGET_CXX=clang" :: "TARGET_LD=ld"
|
||||
:: ndk_args
|
||||
in
|
||||
(* run ndk_build *)
|
||||
L.(debug Capture Verbose)
|
||||
"Running command %s %s with env %s@." prog (String.concat ~sep:" " args)
|
||||
(List.to_string ~f:(fun (a, b) -> a ^ "=" ^ b) new_env) ;
|
||||
let {Unix.Process_info.stdin; stdout; stderr; pid} =
|
||||
Unix.create_process_env ~prog ~args ~env:(`Extend new_env) ()
|
||||
in
|
||||
let ndk_stderr = Unix.in_channel_of_descr stderr in
|
||||
Utils.with_channel_in ndk_stderr ~f:(L.progress "NDKBUILD: %s@.") ;
|
||||
Unix.close stdin ;
|
||||
Unix.close stdout ;
|
||||
In_channel.close ndk_stderr ;
|
||||
match Unix.waitpid pid with
|
||||
| Ok () ->
|
||||
()
|
||||
| Error _ as err ->
|
||||
L.die ExternalError "ndkbuild capture failed to execute: %s"
|
||||
(Unix.Exit_or_signal.to_string_hum err)
|
||||
()
|
@ -0,0 +1,10 @@
|
||||
(*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*)
|
||||
|
||||
open! IStd
|
||||
|
||||
val capture : build_cmd:string list -> unit
|
Loading…
Reference in new issue