1// Copyright 2020 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 android
16
17import (
18	"fmt"
19	"os"
20)
21
22func init() {
23	// Stash the working directory in a private variable and then change the working directory
24	// to "/", which will prevent untracked accesses to files by Go Soong plugins. The
25	// SOONG_SANDBOX_SOONG_BUILD environment variable is set by soong_ui, and is not
26	// overrideable on the command line.
27
28	orig, err := os.Getwd()
29	if err != nil {
30		panic(fmt.Errorf("failed to get working directory: %s", err))
31	}
32	absSrcDir = orig
33
34	if getenv("SOONG_SANDBOX_SOONG_BUILD") == "true" {
35		err = os.Chdir("/")
36		if err != nil {
37			panic(fmt.Errorf("failed to change working directory to '/': %s", err))
38		}
39	}
40}
41
42// DO NOT USE THIS FUNCTION IN NEW CODE.
43// Deprecated: This function will be removed as soon as the existing use cases that use it have been
44// replaced.
45func AbsSrcDirForExistingUseCases() string {
46	return absSrcDir
47}
48