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 | #ifndef BAZEL_SRC_MAIN_CPP_WORKSPACE_LAYOUT_H_ |
| 16 | #define BAZEL_SRC_MAIN_CPP_WORKSPACE_LAYOUT_H_ |
| 17 | |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | namespace blaze { |
| 22 | |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 23 | // Provides methods to compute paths related to the workspace. |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 24 | class WorkspaceLayout { |
| 25 | public: |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 26 | virtual ~WorkspaceLayout() = default; |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 27 | |
Julio Merino | 69a8d72 | 2016-09-13 22:29:39 +0000 | [diff] [blame] | 28 | // Returns the directory to use for storing outputs. |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 29 | virtual std::string GetOutputRoot() const; |
Julio Merino | 69a8d72 | 2016-09-13 22:29:39 +0000 | [diff] [blame] | 30 | |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 31 | // Given the working directory, returns the nearest enclosing directory with a |
| 32 | // WORKSPACE file in it. If there is no such enclosing directory, returns "". |
| 33 | // |
| 34 | // E.g., if there was a WORKSPACE file in foo/bar/build_root: |
| 35 | // GetWorkspace('foo/bar') --> '' |
| 36 | // GetWorkspace('foo/bar/build_root') --> 'foo/bar/build_root' |
| 37 | // GetWorkspace('foo/bar/build_root/biz') --> 'foo/bar/build_root' |
| 38 | // |
| 39 | // The returned path is relative or absolute depending on whether cwd was |
| 40 | // relative or absolute. |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 41 | virtual std::string GetWorkspace(const std::string& cwd) const; |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 42 | |
philwo | a7831cc | 2018-02-01 09:07:39 -0800 | [diff] [blame] | 43 | // Given a result returned from GetWorkspace, returns a pretty workspace name |
| 44 | // than can e.g. be used in the process title of the Bazel server. |
| 45 | virtual std::string GetPrettyWorkspaceName( |
| 46 | const std::string& workspace) const; |
| 47 | |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 48 | // Returns if workspace is a valid build workspace. |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 49 | virtual bool InWorkspace(const std::string& workspace) const; |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 50 | |
ccalvarin | d8dfd78 | 2018-04-19 08:47:28 -0700 | [diff] [blame] | 51 | // Returns the path of the workspace rc file. |
| 52 | virtual std::string GetWorkspaceRcPath( |
Luis Fernando Pino Duque | d5e008c | 2016-12-08 16:59:16 +0000 | [diff] [blame] | 53 | const std::string& workspace, |
ajmichael | e1ed133 | 2018-02-07 11:11:03 -0800 | [diff] [blame] | 54 | const std::vector<std::string>& startup_args) const; |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 55 | |
| 56 | // Turn a %workspace%-relative import into its true name in the filesystem. |
| 57 | // path_fragment is modified in place. |
ajmichael | e1ed133 | 2018-02-07 11:11:03 -0800 | [diff] [blame] | 58 | // Unlike FindCandidateBlazercPaths, it is an error if no import file |
| 59 | // exists. |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 60 | virtual bool WorkspaceRelativizeRcFilePath(const std::string& workspace, |
| 61 | std::string* path_fragment) const; |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 62 | |
Laszlo Csomor | 74ffaf7 | 2016-11-24 12:17:20 +0000 | [diff] [blame] | 63 | static constexpr const char WorkspacePrefix[] = "%workspace%/"; |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 64 | static const int WorkspacePrefixLength = sizeof WorkspacePrefix - 1; |
| 65 | }; |
| 66 | |
| 67 | } // namespace blaze |
| 68 | |
| 69 | #endif // BAZEL_SRC_MAIN_CPP_WORKSPACE_LAYOUT_H_ |