xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/util/runfiles_bazel.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2019 Google LLC
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 //     https://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 #include <cstdlib>
16 
17 #include "absl/strings/str_format.h"
18 #include "sandboxed_api/util/path.h"
19 #include "sandboxed_api/util/raw_logging.h"
20 #include "sandboxed_api/util/runfiles.h"
21 #include "tools/cpp/runfiles/runfiles.h"
22 
23 namespace sapi {
24 
GetDataDependencyFilePath(absl::string_view relative_path)25 std::string GetDataDependencyFilePath(absl::string_view relative_path) {
26   using bazel::tools::cpp::runfiles::Runfiles;
27 
28   static Runfiles* runfiles = []() {
29     std::string error;
30     auto* runfiles = Runfiles::Create(/*argv=*/"" /* unknown */, &error);
31     SAPI_RAW_CHECK(runfiles != nullptr, error.c_str());
32 
33     // Setup environment for child processes.
34     for (const auto& entry : runfiles->EnvVars()) {
35       setenv(entry.first.c_str(), entry.second.c_str(), 1 /* overwrite */);
36     }
37     return runfiles;
38   }();
39   return runfiles->Rlocation(std::string(relative_path));
40 }
41 
42 namespace internal {
43 
GetSapiDataDependencyFilePath(absl::string_view relative_path)44 std::string GetSapiDataDependencyFilePath(absl::string_view relative_path) {
45   return GetDataDependencyFilePath(file::JoinPath(
46       "com_google_sandboxed_api", "sandboxed_api", relative_path));
47 }
48 
49 }  // namespace internal
50 }  // namespace sapi
51