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	"patch":   Allowed,
92	"pstree":  Allowed,
93	"rsync":   Allowed,
94	"sh":      Allowed,
95	"tr":      Allowed,
96	"unzip":   Allowed,
97	"zip":     Allowed,
98
99	// Host toolchain is removed. In-tree toolchain should be used instead.
100	// GCC also can't find cc1 with this implementation.
101	"ar":         Forbidden,
102	"as":         Forbidden,
103	"cc":         Forbidden,
104	"clang":      Forbidden,
105	"clang++":    Forbidden,
106	"gcc":        Forbidden,
107	"g++":        Forbidden,
108	"ld":         Forbidden,
109	"ld.bfd":     Forbidden,
110	"ld.gold":    Forbidden,
111	"pkg-config": Forbidden,
112
113	// These are toybox tools that only work on Linux.
114	"pgrep": LinuxOnlyPrebuilt,
115	"pkill": LinuxOnlyPrebuilt,
116	"ps":    LinuxOnlyPrebuilt,
117}
118
119func init() {
120	if runtime.GOOS == "darwin" {
121		Configuration["sw_vers"] = Allowed
122		Configuration["xcrun"] = Allowed
123
124		// We don't have darwin prebuilts for some tools,
125		// so allow the host versions.
126		for name, config := range Configuration {
127			if config.LinuxOnlyPrebuilt {
128				Configuration[name] = Allowed
129			}
130		}
131	}
132}
133