1// Copyright 2019 The Chromium OS Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5package main 6 7import ( 8 "errors" 9 "os" 10 "os/exec" 11 "path/filepath" 12) 13 14// Note: We keep this code in python as golang has no builtin 15// shlex function. 16const bisectPythonCommand = ` 17import bisect_driver 18import shlex 19import sys 20 21def ExpandArgs(args, target): 22 for arg in args: 23 if arg[0] == '@': 24 with open(arg[1:], 'r', encoding='utf-8') as f: 25 ExpandArgs(shlex.split(f.read()), target) 26 else: 27 target.append(arg) 28 return target 29 30stage = sys.argv[1] 31dir = sys.argv[2] 32execargs = ExpandArgs(sys.argv[3:], []) 33 34sys.exit(bisect_driver.bisect_driver(stage, dir, execargs)) 35` 36 37func getBisectStage(env env) string { 38 value, _ := env.getenv("BISECT_STAGE") 39 return value 40} 41 42func calcBisectCommand(env env, cfg *config, bisectStage string, compilerCmd *command) (*command, error) { 43 bisectDir, _ := env.getenv("BISECT_DIR") 44 if bisectDir == "" { 45 if cfg.isAndroidWrapper { 46 homeDir, ok := env.getenv("HOME") 47 if !ok { 48 return nil, errors.New("$HOME is not set") 49 } 50 bisectDir = filepath.Join(homeDir, "ANDROID_BISECT") 51 } else { 52 bisectDir = "/tmp/sysroot_bisect" 53 } 54 } 55 absCompilerPath := getAbsCmdPath(env, compilerCmd) 56 pythonPath, err := exec.LookPath(os.Args[0]) 57 if err != nil { 58 return nil, err 59 } 60 pythonPath, err = filepath.EvalSymlinks(pythonPath) 61 if err != nil { 62 return nil, err 63 } 64 pythonPath = filepath.Dir(pythonPath) 65 return &command{ 66 Path: "/usr/bin/env", 67 Args: append([]string{ 68 "python3", 69 "-c", 70 bisectPythonCommand, 71 bisectStage, 72 bisectDir, 73 absCompilerPath, 74 }, compilerCmd.Args...), 75 EnvUpdates: append(compilerCmd.EnvUpdates, "PYTHONPATH="+pythonPath), 76 }, nil 77} 78