Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 1 | // Copyright 2017 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 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
| 18 | "os/exec" |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | type Sandbox string |
| 22 | |
| 23 | const ( |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 24 | noSandbox = "" |
| 25 | globalSandbox = "build/soong/ui/build/sandbox/darwin/global.sb" |
| 26 | dumpvarsSandbox = globalSandbox |
| 27 | soongSandbox = globalSandbox |
| 28 | katiSandbox = globalSandbox |
| 29 | ninjaSandbox = noSandbox |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | var sandboxExecPath string |
| 33 | |
| 34 | func init() { |
| 35 | if p, err := exec.LookPath("sandbox-exec"); err == nil { |
| 36 | sandboxExecPath = p |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func (c *Cmd) sandboxSupported() bool { |
| 41 | if c.Sandbox == "" { |
| 42 | return false |
| 43 | } else if sandboxExecPath == "" { |
| 44 | c.ctx.Verboseln("sandbox-exec not found, disabling sandboxing") |
| 45 | return false |
| 46 | } |
| 47 | return true |
| 48 | } |
| 49 | |
| 50 | func (c *Cmd) wrapSandbox() { |
| 51 | homeDir, _ := c.Environment.Get("HOME") |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 52 | outDir := absPath(c.ctx, c.config.OutDir()) |
| 53 | distDir := absPath(c.ctx, c.config.DistDir()) |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 54 | |
| 55 | c.Args[0] = c.Path |
| 56 | c.Path = sandboxExecPath |
| 57 | c.Args = append([]string{ |
| 58 | "sandbox-exec", "-f", string(c.Sandbox), |
| 59 | "-D", "HOME=" + homeDir, |
| 60 | "-D", "OUT_DIR=" + outDir, |
| 61 | "-D", "DIST_DIR=" + distDir, |
| 62 | }, c.Args...) |
| 63 | } |