1// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package paths
16
17import "runtime"
18
19type PathConfig struct {
20	// Whether to create the symlink in the new PATH for this tool.
21	Symlink bool
22
23	// Whether to log about usages of this tool to the soong.log
24	Log bool
25
26	// Whether to exit with an error instead of invoking the underlying tool.
27	Error bool
28
29	// Whether we use a linux-specific prebuilt for this tool. On Darwin,
30	// we'll allow the host executable instead.
31	LinuxOnlyPrebuilt bool
32}
33
34var Allowed = PathConfig{
35	Symlink: true,
36	Log:     false,
37	Error:   false,
38}
39
40var Forbidden = PathConfig{
41	Symlink: false,
42	Log:     true,
43	Error:   true,
44}
45
46var Log = PathConfig{
47	Symlink: true,
48	Log:     true,
49	Error:   false,
50}
51
52// The configuration used if the tool is not listed in the config below.
53// Currently this will create the symlink, but log and error when it's used. In
54// the future, I expect the symlink to be removed, and this will be equivalent
55// to Forbidden.
56var Missing = PathConfig{
57	Symlink: true,
58	Log:     true,
59	Error:   true,
60}
61
62var LinuxOnlyPrebuilt = PathConfig{
63	Symlink:           false,
64	Log:               true,
65	Error:             true,
66	LinuxOnlyPrebuilt: true,
67}
68
69func GetConfig(name string) PathConfig {
70	if config, ok := Configuration[name]; ok {
71		return config
72	}
73	return Missing
74}
75
76var Configuration = map[string]PathConfig{
77	"bash":    Allowed,
78	"dd":      Allowed,
79	"diff":    Allowed,
80	"dlv":     Allowed,
81	"expr":    Allowed,
82	"fuser":   Allowed,
83	"getopt":  Allowed,
84	"git":     Allowed,
85	"hexdump": Allowed,
86	"jar":     Allowed,
87	"java":    Allowed,
88	"javap":   Allowed,
89	"lsof":    Allowed,
90	"openssl": Allowed,
91	"pstree":  Allowed,
92	"rsync":   Allowed,
93	"sh":      Allowed,
94	"tr":      Allowed,
95	"unzip":   Allowed,
96	"zip":     Allowed,
97
98	// Host toolchain is removed. In-tree toolchain should be used instead.
99	// GCC also can't find cc1 with this implementation.
100	"ar":         Forbidden,
101	"as":         Forbidden,
102	"cc":         Forbidden,
103	"clang":      Forbidden,
104	"clang++":    Forbidden,
105	"gcc":        Forbidden,
106	"g++":        Forbidden,
107	"ld":         Forbidden,
108	"ld.bfd":     Forbidden,
109	"ld.gold":    Forbidden,
110	"pkg-config": Forbidden,
111
112	// These are toybox tools that only work on Linux.
113	"pgrep": LinuxOnlyPrebuilt,
114	"pkill": LinuxOnlyPrebuilt,
115	"ps":    LinuxOnlyPrebuilt,
116}
117
118func init() {
119	if runtime.GOOS == "darwin" {
120		Configuration["sw_vers"] = Allowed
121		Configuration["xcrun"] = Allowed
122
123		// We don't have darwin prebuilts for some tools,
124		// so allow the host versions.
125		for name, config := range Configuration {
126			if config.LinuxOnlyPrebuilt {
127				Configuration[name] = Allowed
128			}
129		}
130	}
131}
132