blob: 43c5480c752279526bbb38077a4f6bf001de9a57 [file] [log] [blame]
Dan Willemsen269a8c72017-05-03 17:15:47 -07001// 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
15package build
16
17import (
18 "os/exec"
Dan Willemsen269a8c72017-05-03 17:15:47 -070019)
20
21type Sandbox string
22
23const (
Dan Willemsen63663c62019-01-02 12:24:44 -080024 noSandbox = ""
25 globalSandbox = "build/soong/ui/build/sandbox/darwin/global.sb"
26 dumpvarsSandbox = globalSandbox
27 soongSandbox = globalSandbox
28 katiSandbox = globalSandbox
29 ninjaSandbox = noSandbox
Dan Willemsen269a8c72017-05-03 17:15:47 -070030)
31
32var sandboxExecPath string
33
34func init() {
35 if p, err := exec.LookPath("sandbox-exec"); err == nil {
36 sandboxExecPath = p
37 }
38}
39
40func (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
50func (c *Cmd) wrapSandbox() {
51 homeDir, _ := c.Environment.Get("HOME")
Dan Willemsend9e8f0a2017-10-30 13:42:06 -070052 outDir := absPath(c.ctx, c.config.OutDir())
53 distDir := absPath(c.ctx, c.config.DistDir())
Dan Willemsen269a8c72017-05-03 17:15:47 -070054
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}