aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/runfiles/runfiles.h
blob: c15dec39e08f60672f36e46539b09d12103f6642 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Runfiles lookup library for Bazel-built C++ binaries and tests.
//
// TODO(laszlocsomor): add usage information and examples.

#ifndef BAZEL_SRC_TOOLS_RUNFILES_RUNFILES_H_
#define BAZEL_SRC_TOOLS_RUNFILES_RUNFILES_H_ 1

#include <string>

namespace bazel {
namespace runfiles {

class Runfiles {
 public:
  virtual ~Runfiles() {}

  // TODO(laszlocsomor): implement:
  //   Runfiles* Create(const string& argv0, string* error);
  // TODO(laszlocsomor): implement:
  //   vector<pair<string, string>> EnvVars();
  // TODO(laszlocsomor): implement:
  //   Runfiles* CreateManifestBased(const string& path, string* error);

  // Returns a new directory-based `Runfiles` instance.
  // Returns nullptr on error. If `error` is provided, the method prints an
  // error message into it.
  static Runfiles* CreateDirectoryBased(const std::string& directory_path,
                                        std::string* error = nullptr);

  // Returns the runtime path of a runfile.
  //
  // Runfiles are data-dependencies of Bazel-built binaries and tests.
  //
  // The returned path may not be valid. The caller should check the path's
  // validity and that the path exists.
  //
  // The function may return an empty string. In that case the caller can be
  // sure that the Runfiles object does not know about this data-dependency.
  //
  // Args:
  //   path: runfiles-root-relative path of the runfile; must not be empty and
  //     must not contain uplevel references.
  // Returns:
  //   the path to the runfile, which the caller should check for existence, or
  //   an empty string if the method doesn't know about this runfile
  virtual std::string Rlocation(const std::string& path) const = 0;

 protected:
  Runfiles() {}

 private:
  Runfiles(const Runfiles&) = delete;
  Runfiles(Runfiles&&) = delete;
  Runfiles& operator=(const Runfiles&) = delete;
  Runfiles& operator=(Runfiles&&) = delete;
};

// The "testing" namespace contains functions that allow unit testing the code.
// Do not use these outside of runfiles_test.cc, because they may change without
// notice.
namespace testing {

// For testing only.
// Returns true if `path` is an absolute Unix or Windows path.
// For Windows paths, this function does not regard drive-less absolute paths
// (i.e. absolute-on-current-drive, e.g. "\foo\bar") as absolute and returns
// false for these.
bool TestOnly_IsAbsolute(const std::string& path);

}  // namespace testing
}  // namespace runfiles
}  // namespace bazel

#endif  // BAZEL_SRC_TOOLS_RUNFILES_RUNFILES_H_