blob: e9b66c38a643a65d639c071ae5bdc5441c814155 [file] [log] [blame]
Julio Merino211a95c2016-08-29 11:01:35 +00001// Copyright 2016 The Bazel Authors. 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.
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000014
Julio Merino211a95c2016-08-29 11:01:35 +000015#include "src/main/cpp/workspace_layout.h"
16
17#include <assert.h>
Julio Merino211a95c2016-08-29 11:01:35 +000018
Julio Merino69a8d722016-09-13 22:29:39 +000019#include "src/main/cpp/blaze_util_platform.h"
Julio Merino211a95c2016-08-29 11:01:35 +000020#include "src/main/cpp/util/file.h"
Laszlo Csomor9c951962016-11-10 13:31:27 +000021#include "src/main/cpp/util/file_platform.h"
ccalvarinac69da02018-06-05 15:27:26 -070022#include "src/main/cpp/util/path.h"
23#include "src/main/cpp/util/path_platform.h"
Julio Merino211a95c2016-08-29 11:01:35 +000024
25namespace blaze {
26
Thiago Farina80bb0f22016-10-17 15:57:13 +000027using std::string;
Julio Merino211a95c2016-08-29 11:01:35 +000028using std::vector;
29
Yun Peng0af987b2019-11-11 07:19:54 -080030static const char kWorkspaceDotBazelMarker[] = "WORKSPACE.bazel";
Julio Merino211a95c2016-08-29 11:01:35 +000031static const char kWorkspaceMarker[] = "WORKSPACE";
32
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000033string WorkspaceLayout::GetOutputRoot() const {
Julio Merino69a8d722016-09-13 22:29:39 +000034 return blaze::GetOutputRoot();
35}
36
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000037bool WorkspaceLayout::InWorkspace(const string &workspace) const {
Samuel Giddins9c624012020-04-02 23:29:45 -070038 auto workspaceDotBazelPath =
39 blaze_util::JoinPath(workspace, kWorkspaceDotBazelMarker);
40 auto workspacePath = blaze_util::JoinPath(workspace, kWorkspaceMarker);
41 return (blaze_util::PathExists(workspaceDotBazelPath) &&
42 !blaze_util::IsDirectory(workspaceDotBazelPath)) ||
43 (blaze_util::PathExists(workspacePath) &&
44 !blaze_util::IsDirectory(workspacePath));
Julio Merino211a95c2016-08-29 11:01:35 +000045}
46
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000047string WorkspaceLayout::GetWorkspace(const string &cwd) const {
Julio Merino211a95c2016-08-29 11:01:35 +000048 assert(!cwd.empty());
49 string workspace = cwd;
50
51 do {
Thiago Farinaaf25cea2016-09-28 08:59:38 +000052 if (InWorkspace(workspace)) {
Julio Merino211a95c2016-08-29 11:01:35 +000053 return workspace;
54 }
55 workspace = blaze_util::Dirname(workspace);
Laszlo Csomor760f7862016-12-19 15:46:47 +000056 } while (!workspace.empty() && !blaze_util::IsRootDirectory(workspace));
Julio Merino211a95c2016-08-29 11:01:35 +000057 return "";
58}
59
philwoa7831cc2018-02-01 09:07:39 -080060string WorkspaceLayout::GetPrettyWorkspaceName(
61 const std::string& workspace) const {
62 // e.g. A Bazel server process running in ~/src/myproject (where there's a
63 // ~/src/myproject/WORKSPACE file) will appear in ps(1) as "bazel(myproject)".
64 return blaze_util::Basename(workspace);
65}
66
ccalvarind8dfd782018-04-19 08:47:28 -070067std::string WorkspaceLayout::GetWorkspaceRcPath(
68 const std::string &workspace,
69 const std::vector<std::string> &startup_args) const {
ccalvarinbccf9c62018-06-20 15:05:23 -070070 // TODO(b/36168162): Rename and remove the tools/ prefix. See
71 // https://github.com/bazelbuild/bazel/issues/4502#issuecomment-372697374
72 // for the final set of bazelrcs we want to have.
ccalvarind8dfd782018-04-19 08:47:28 -070073 return blaze_util::JoinPath(workspace, "tools/bazel.rc");
Julio Merino211a95c2016-08-29 11:01:35 +000074}
75
76bool WorkspaceLayout::WorkspaceRelativizeRcFilePath(const string &workspace,
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000077 string *path_fragment)
78 const {
Julio Merino211a95c2016-08-29 11:01:35 +000079 // Strip off the "%workspace%/" prefix and prepend the true workspace path.
80 // In theory this could use alternate search paths for blazerc files.
81 path_fragment->assign(
82 blaze_util::JoinPath(workspace,
83 path_fragment->substr(WorkspacePrefixLength)));
84 return true;
85}
86
87} // namespace blaze