aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/bisect_flag.go
blob: adfa8b03ac8da33b598a5aea000c632c628f5271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package main

import (
	"errors"
	"os"
	"os/exec"
	"path/filepath"
)

// Note: We keep this code in python as golang has no builtin
// shlex function.
const bisectPythonCommand = `
import bisect_driver
import shlex
import sys

def ExpandArgs(args, target):
	for arg in args:
		if arg[0] == '@':
			with open(arg[1:], 'r', encoding='utf-8') as f:
				ExpandArgs(shlex.split(f.read()), target)
		else:
			target.append(arg)
	return target

stage = sys.argv[1]
dir = sys.argv[2]
execargs = ExpandArgs(sys.argv[3:], [])

sys.exit(bisect_driver.bisect_driver(stage, dir, execargs))
`

func getBisectStage(env env) string {
	value, _ := env.getenv("BISECT_STAGE")
	return value
}

func calcBisectCommand(env env, cfg *config, bisectStage string, compilerCmd *command) (*command, error) {
	bisectDir, _ := env.getenv("BISECT_DIR")
	if bisectDir == "" {
		if cfg.isAndroidWrapper {
			homeDir, ok := env.getenv("HOME")
			if !ok {
				return nil, errors.New("$HOME is not set")
			}
			bisectDir = filepath.Join(homeDir, "ANDROID_BISECT")
		} else {
			bisectDir = "/tmp/sysroot_bisect"
		}
	}
	absCompilerPath := getAbsCmdPath(env, compilerCmd)
	pythonPath, err := exec.LookPath(os.Args[0])
	if err != nil {
		return nil, err
	}
	pythonPath, err = filepath.EvalSymlinks(pythonPath)
	if err != nil {
		return nil, err
	}
	pythonPath = filepath.Dir(pythonPath)
	return &command{
		Path: "/usr/bin/env",
		Args: append([]string{
			"python3",
			"-c",
			bisectPythonCommand,
			bisectStage,
			bisectDir,
			absCompilerPath,
		}, compilerCmd.Args...),
		EnvUpdates: append(compilerCmd.EnvUpdates, "PYTHONPATH="+pythonPath),
	}, nil
}