blob: 7122927a629b56a7d6ca538bea9272282fa68a87 [file] [log] [blame]
Dan Willemsen18490112018-05-25 16:30:04 -07001// 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 build
16
17import (
18 "fmt"
19 "io/ioutil"
20 "os"
Dan Willemsen4e2456b2019-10-03 16:45:58 -070021 "os/exec"
Dan Willemsen18490112018-05-25 16:30:04 -070022 "path/filepath"
Dan Willemsen417be1f2018-10-30 23:18:54 -070023 "runtime"
Dan Willemsen18490112018-05-25 16:30:04 -070024 "strings"
25
26 "github.com/google/blueprint/microfactory"
27
28 "android/soong/ui/build/paths"
Nan Zhang17f27672018-12-12 16:01:49 -080029 "android/soong/ui/metrics"
Dan Willemsen18490112018-05-25 16:30:04 -070030)
31
32func parsePathDir(dir string) []string {
33 f, err := os.Open(dir)
34 if err != nil {
35 return nil
36 }
37 defer f.Close()
38
39 if s, err := f.Stat(); err != nil || !s.IsDir() {
40 return nil
41 }
42
43 infos, err := f.Readdir(-1)
44 if err != nil {
45 return nil
46 }
47
48 ret := make([]string, 0, len(infos))
49 for _, info := range infos {
50 if m := info.Mode(); !m.IsDir() && m&0111 != 0 {
51 ret = append(ret, info.Name())
52 }
53 }
54 return ret
55}
56
Dan Willemsen4e2456b2019-10-03 16:45:58 -070057// A "lite" version of SetupPath used for dumpvars, or other places that need
58// minimal overhead (but at the expense of logging).
59func SetupLitePath(ctx Context, config Config) {
60 if config.pathReplaced {
61 return
62 }
63
64 ctx.BeginTrace(metrics.RunSetupTool, "litepath")
65 defer ctx.EndTrace()
66
67 origPath, _ := config.Environment().Get("PATH")
68 myPath, _ := config.Environment().Get("TMPDIR")
69 myPath = filepath.Join(myPath, "path")
70 ensureEmptyDirectoriesExist(ctx, myPath)
71
72 os.Setenv("PATH", origPath)
73 for name, pathConfig := range paths.Configuration {
74 if !pathConfig.Symlink {
75 continue
76 }
77
78 origExec, err := exec.LookPath(name)
79 if err != nil {
80 continue
81 }
82 origExec, err = filepath.Abs(origExec)
83 if err != nil {
84 continue
85 }
86
87 err = os.Symlink(origExec, filepath.Join(myPath, name))
88 if err != nil {
89 ctx.Fatalln("Failed to create symlink:", err)
90 }
91 }
92
93 myPath, _ = filepath.Abs(myPath)
94
95 prebuiltsPath, _ := filepath.Abs("prebuilts/build-tools/path/" + runtime.GOOS + "-x86")
96 myPath = prebuiltsPath + string(os.PathListSeparator) + myPath
97
98 config.Environment().Set("PATH", myPath)
99 config.pathReplaced = true
100}
101
Dan Willemsen18490112018-05-25 16:30:04 -0700102func SetupPath(ctx Context, config Config) {
103 if config.pathReplaced {
104 return
105 }
106
Nan Zhang17f27672018-12-12 16:01:49 -0800107 ctx.BeginTrace(metrics.RunSetupTool, "path")
Dan Willemsen18490112018-05-25 16:30:04 -0700108 defer ctx.EndTrace()
109
110 origPath, _ := config.Environment().Get("PATH")
111 myPath := filepath.Join(config.OutDir(), ".path")
112 interposer := myPath + "_interposer"
113
114 var cfg microfactory.Config
115 cfg.Map("android/soong", "build/soong")
116 cfg.TrimPath, _ = filepath.Abs(".")
117 if _, err := microfactory.Build(&cfg, interposer, "android/soong/cmd/path_interposer"); err != nil {
118 ctx.Fatalln("Failed to build path interposer:", err)
119 }
120
121 if err := ioutil.WriteFile(interposer+"_origpath", []byte(origPath), 0777); err != nil {
122 ctx.Fatalln("Failed to write original path:", err)
123 }
124
125 entries, err := paths.LogListener(ctx.Context, interposer+"_log")
126 if err != nil {
127 ctx.Fatalln("Failed to listen for path logs:", err)
128 }
129
130 go func() {
131 for log := range entries {
132 curPid := os.Getpid()
133 for i, proc := range log.Parents {
134 if proc.Pid == curPid {
135 log.Parents = log.Parents[i:]
136 break
137 }
138 }
139 procPrints := []string{
140 "See https://android.googlesource.com/platform/build/+/master/Changes.md#PATH_Tools for more information.",
141 }
142 if len(log.Parents) > 0 {
143 procPrints = append(procPrints, "Process tree:")
144 for i, proc := range log.Parents {
145 procPrints = append(procPrints, fmt.Sprintf("%s→ %s", strings.Repeat(" ", i), proc.Command))
146 }
147 }
148
149 config := paths.GetConfig(log.Basename)
150 if config.Error {
151 ctx.Printf("Disallowed PATH tool %q used: %#v", log.Basename, log.Args)
152 for _, line := range procPrints {
153 ctx.Println(line)
154 }
155 } else {
156 ctx.Verbosef("Unknown PATH tool %q used: %#v", log.Basename, log.Args)
157 for _, line := range procPrints {
158 ctx.Verboseln(line)
159 }
160 }
161 }
162 }()
163
164 ensureEmptyDirectoriesExist(ctx, myPath)
165
166 var execs []string
167 for _, pathEntry := range filepath.SplitList(origPath) {
168 if pathEntry == "" {
169 // Ignore the current directory
170 continue
171 }
172 // TODO(dwillemsen): remove path entries under TOP? or anything
173 // that looks like an android source dir? They won't exist on
174 // the build servers, since they're added by envsetup.sh.
175 // (Except for the JDK, which is configured in ui/build/config.go)
176
177 execs = append(execs, parsePathDir(pathEntry)...)
178 }
179
Dan Willemsen347ba752020-05-01 16:29:00 -0700180 if config.Environment().IsEnvTrue("TEMPORARY_DISABLE_PATH_RESTRICTIONS") {
181 ctx.Fatalln("TEMPORARY_DISABLE_PATH_RESTRICTIONS was a temporary migration method, and is now obsolete.")
182 }
183
Dan Willemsen18490112018-05-25 16:30:04 -0700184 for _, name := range execs {
Dan Willemsen347ba752020-05-01 16:29:00 -0700185 if !paths.GetConfig(name).Symlink {
Dan Willemsen18490112018-05-25 16:30:04 -0700186 continue
187 }
188
189 err := os.Symlink("../.path_interposer", filepath.Join(myPath, name))
190 // Intentionally ignore existing files -- that means that we
191 // just created it, and the first one should win.
192 if err != nil && !os.IsExist(err) {
193 ctx.Fatalln("Failed to create symlink:", err)
194 }
195 }
196
197 myPath, _ = filepath.Abs(myPath)
Dan Willemsen417be1f2018-10-30 23:18:54 -0700198
Dan Willemsen91219732019-02-14 20:00:56 -0800199 // We put some prebuilts in $PATH, since it's infeasible to add dependencies for all of
200 // them.
Dan Willemsen733547d2019-02-14 20:11:26 -0800201 prebuiltsPath, _ := filepath.Abs("prebuilts/build-tools/path/" + runtime.GOOS + "-x86")
202 myPath = prebuiltsPath + string(os.PathListSeparator) + myPath
Dan Willemsen417be1f2018-10-30 23:18:54 -0700203
Dan Willemsen18490112018-05-25 16:30:04 -0700204 config.Environment().Set("PATH", myPath)
205 config.pathReplaced = true
206}