Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 1 | // 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 Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 14 | |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 15 | #include "src/main/cpp/workspace_layout.h" |
| 16 | |
| 17 | #include <assert.h> |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 18 | |
Julio Merino | 69a8d72 | 2016-09-13 22:29:39 +0000 | [diff] [blame] | 19 | #include "src/main/cpp/blaze_util_platform.h" |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 20 | #include "src/main/cpp/util/file.h" |
Laszlo Csomor | 9c95196 | 2016-11-10 13:31:27 +0000 | [diff] [blame] | 21 | #include "src/main/cpp/util/file_platform.h" |
ccalvarin | ac69da0 | 2018-06-05 15:27:26 -0700 | [diff] [blame] | 22 | #include "src/main/cpp/util/path.h" |
| 23 | #include "src/main/cpp/util/path_platform.h" |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 24 | |
| 25 | namespace blaze { |
| 26 | |
Thiago Farina | 80bb0f2 | 2016-10-17 15:57:13 +0000 | [diff] [blame] | 27 | using std::string; |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 28 | using std::vector; |
| 29 | |
Yun Peng | 0af987b | 2019-11-11 07:19:54 -0800 | [diff] [blame] | 30 | static const char kWorkspaceDotBazelMarker[] = "WORKSPACE.bazel"; |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 31 | static const char kWorkspaceMarker[] = "WORKSPACE"; |
| 32 | |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 33 | string WorkspaceLayout::GetOutputRoot() const { |
Julio Merino | 69a8d72 | 2016-09-13 22:29:39 +0000 | [diff] [blame] | 34 | return blaze::GetOutputRoot(); |
| 35 | } |
| 36 | |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 37 | bool WorkspaceLayout::InWorkspace(const string &workspace) const { |
Samuel Giddins | 9c62401 | 2020-04-02 23:29:45 -0700 | [diff] [blame] | 38 | 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 Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 47 | string WorkspaceLayout::GetWorkspace(const string &cwd) const { |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 48 | assert(!cwd.empty()); |
| 49 | string workspace = cwd; |
| 50 | |
| 51 | do { |
Thiago Farina | af25cea | 2016-09-28 08:59:38 +0000 | [diff] [blame] | 52 | if (InWorkspace(workspace)) { |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 53 | return workspace; |
| 54 | } |
| 55 | workspace = blaze_util::Dirname(workspace); |
Laszlo Csomor | 760f786 | 2016-12-19 15:46:47 +0000 | [diff] [blame] | 56 | } while (!workspace.empty() && !blaze_util::IsRootDirectory(workspace)); |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 57 | return ""; |
| 58 | } |
| 59 | |
philwo | a7831cc | 2018-02-01 09:07:39 -0800 | [diff] [blame] | 60 | string 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 | |
ccalvarin | d8dfd78 | 2018-04-19 08:47:28 -0700 | [diff] [blame] | 67 | std::string WorkspaceLayout::GetWorkspaceRcPath( |
| 68 | const std::string &workspace, |
| 69 | const std::vector<std::string> &startup_args) const { |
ccalvarin | bccf9c6 | 2018-06-20 15:05:23 -0700 | [diff] [blame] | 70 | // 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. |
ccalvarin | d8dfd78 | 2018-04-19 08:47:28 -0700 | [diff] [blame] | 73 | return blaze_util::JoinPath(workspace, "tools/bazel.rc"); |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | bool WorkspaceLayout::WorkspaceRelativizeRcFilePath(const string &workspace, |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 77 | string *path_fragment) |
| 78 | const { |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 79 | // 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 |