blob: 60407d49f1a92f59eefac4be7a911a5e7704477a [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 (
Dan Willemsen59fdf962017-07-24 22:26:54 -070025 noSandbox = ""
26 globalSandbox = "build/soong/ui/build/sandbox/darwin/global.sb"
27 makeSandbox = globalSandbox
28 soongSandbox = globalSandbox
29 katiSandbox = globalSandbox
30 katiCleanSpecSandbox = globalSandbox
Dan Willemsen269a8c72017-05-03 17:15:47 -070031)
32
33var sandboxExecPath string
34
35func init() {
36 if p, err := exec.LookPath("sandbox-exec"); err == nil {
37 sandboxExecPath = p
38 }
39}
40
41func (c *Cmd) sandboxSupported() bool {
42 if c.Sandbox == "" {
43 return false
44 } else if sandboxExecPath == "" {
45 c.ctx.Verboseln("sandbox-exec not found, disabling sandboxing")
46 return false
47 }
48 return true
49}
50
51func (c *Cmd) wrapSandbox() {
52 homeDir, _ := c.Environment.Get("HOME")
53 outDir, err := filepath.Abs(c.config.OutDir())
54 if err != nil {
55 c.ctx.Fatalln("Failed to get absolute path of OUT_DIR:", err)
56 }
57 distDir, err := filepath.Abs(c.config.DistDir())
58 if err != nil {
59 c.ctx.Fatalln("Failed to get absolute path of DIST_DIR:", err)
60 }
61
62 c.Args[0] = c.Path
63 c.Path = sandboxExecPath
64 c.Args = append([]string{
65 "sandbox-exec", "-f", string(c.Sandbox),
66 "-D", "HOME=" + homeDir,
67 "-D", "OUT_DIR=" + outDir,
68 "-D", "DIST_DIR=" + distDir,
69 }, c.Args...)
70}