blob: 54c145c5c0afc425d1be944640b1562b7e711956 [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"
19 "path/filepath"
20)
21
22type Sandbox string
23
24const (
25 noSandbox = ""
26 globalSandbox = "build/soong/ui/build/sandbox/darwin/global.sb"
27 makeSandbox = globalSandbox
28 soongSandbox = globalSandbox
29 katiSandbox = globalSandbox
30)
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")
52 outDir, err := filepath.Abs(c.config.OutDir())
53 if err != nil {
54 c.ctx.Fatalln("Failed to get absolute path of OUT_DIR:", err)
55 }
56 distDir, err := filepath.Abs(c.config.DistDir())
57 if err != nil {
58 c.ctx.Fatalln("Failed to get absolute path of DIST_DIR:", err)
59 }
60
61 c.Args[0] = c.Path
62 c.Path = sandboxExecPath
63 c.Args = append([]string{
64 "sandbox-exec", "-f", string(c.Sandbox),
65 "-D", "HOME=" + homeDir,
66 "-D", "OUT_DIR=" + outDir,
67 "-D", "DIST_DIR=" + distDir,
68 }, c.Args...)
69}