blob: 3827b874062deb0082431cd228a37f09e9518f31 [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#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
21namespace blaze {
22
Julio Merino211a95c2016-08-29 11:01:35 +000023// Provides methods to compute paths related to the workspace.
Julio Merino211a95c2016-08-29 11:01:35 +000024class WorkspaceLayout {
25 public:
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000026 virtual ~WorkspaceLayout() = default;
Julio Merino211a95c2016-08-29 11:01:35 +000027
Julio Merino69a8d722016-09-13 22:29:39 +000028 // Returns the directory to use for storing outputs.
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000029 virtual std::string GetOutputRoot() const;
Julio Merino69a8d722016-09-13 22:29:39 +000030
Julio Merino211a95c2016-08-29 11:01:35 +000031 // 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 Merinoe3e3bfa2016-12-08 22:22:12 +000041 virtual std::string GetWorkspace(const std::string& cwd) const;
Julio Merino211a95c2016-08-29 11:01:35 +000042
philwoa7831cc2018-02-01 09:07:39 -080043 // 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 Merino211a95c2016-08-29 11:01:35 +000048 // Returns if workspace is a valid build workspace.
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000049 virtual bool InWorkspace(const std::string& workspace) const;
Julio Merino211a95c2016-08-29 11:01:35 +000050
ccalvarind8dfd782018-04-19 08:47:28 -070051 // Returns the path of the workspace rc file.
52 virtual std::string GetWorkspaceRcPath(
Luis Fernando Pino Duqued5e008c2016-12-08 16:59:16 +000053 const std::string& workspace,
ajmichaele1ed1332018-02-07 11:11:03 -080054 const std::vector<std::string>& startup_args) const;
Julio Merino211a95c2016-08-29 11:01:35 +000055
56 // Turn a %workspace%-relative import into its true name in the filesystem.
57 // path_fragment is modified in place.
ajmichaele1ed1332018-02-07 11:11:03 -080058 // Unlike FindCandidateBlazercPaths, it is an error if no import file
59 // exists.
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000060 virtual bool WorkspaceRelativizeRcFilePath(const std::string& workspace,
61 std::string* path_fragment) const;
Julio Merino211a95c2016-08-29 11:01:35 +000062
Laszlo Csomor74ffaf72016-11-24 12:17:20 +000063 static constexpr const char WorkspacePrefix[] = "%workspace%/";
Julio Merino211a95c2016-08-29 11:01:35 +000064 static const int WorkspacePrefixLength = sizeof WorkspacePrefix - 1;
65};
66
67} // namespace blaze
68
69#endif // BAZEL_SRC_MAIN_CPP_WORKSPACE_LAYOUT_H_