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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
// 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.
// The "srcs_for_embedded_tools" rule in the same package sets the line below to
// include runfiles.h from the correct path. Do not modify the line below.
#include "tools/cpp/runfiles/runfiles_src.h"
#ifdef _WIN32
#include <windows.h>
#else // not _WIN32
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#endif // _WIN32
#include <fstream>
#include <functional>
#include <map>
#include <sstream>
#include <vector>
#ifdef _WIN32
#include <memory>
#endif // _WIN32
namespace bazel {
namespace tools {
namespace cpp {
namespace runfiles {
using std::function;
using std::map;
using std::pair;
using std::string;
using std::vector;
namespace {
bool starts_with(const string& s, const char* prefix) {
if (!prefix || !*prefix) {
return true;
}
if (s.empty()) {
return false;
}
return s.find(prefix) == 0;
}
bool contains(const string& s, const char* substr) {
if (!substr || !*substr) {
return true;
}
if (s.empty()) {
return false;
}
return s.find(substr) != string::npos;
}
bool ends_with(const string& s, const string& suffix) {
if (suffix.empty()) {
return true;
}
if (s.empty()) {
return false;
}
return s.rfind(suffix) == s.size() - suffix.size();
}
bool IsReadableFile(const string& path) {
return std::ifstream(path).is_open();
}
bool IsDirectory(const string& path) {
#ifdef _WIN32
DWORD attrs = GetFileAttributesA(path.c_str());
return (attrs != INVALID_FILE_ATTRIBUTES) &&
(attrs & FILE_ATTRIBUTE_DIRECTORY);
#else
struct stat buf;
return stat(path.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode);
#endif
}
bool PathsFrom(const std::string& argv0, std::string runfiles_manifest_file,
std::string runfiles_dir, std::string* out_manifest,
std::string* out_directory);
bool PathsFrom(const std::string& argv0, std::string runfiles_manifest_file,
std::string runfiles_dir,
std::function<bool(const std::string&)> is_runfiles_manifest,
std::function<bool(const std::string&)> is_runfiles_directory,
std::string* out_manifest, std::string* out_directory);
bool ParseManifest(const string& path, map<string, string>* result,
string* error);
} // namespace
Runfiles* Runfiles::Create(const string& argv0,
const string& runfiles_manifest_file,
const string& runfiles_dir, string* error) {
string manifest, directory;
if (!PathsFrom(argv0, runfiles_manifest_file, runfiles_dir, &manifest,
&directory)) {
if (error) {
std::ostringstream err;
err << "ERROR: " << __FILE__ << "(" << __LINE__
<< "): cannot find runfiles (argv0=\"" << argv0 << "\")";
*error = err.str();
}
return nullptr;
}
const vector<pair<string, string> > envvars = {
{"RUNFILES_MANIFEST_FILE", manifest},
{"RUNFILES_DIR", directory},
// TODO(laszlocsomor): remove JAVA_RUNFILES once the Java launcher can
// pick up RUNFILES_DIR.
{"JAVA_RUNFILES", directory}};
map<string, string> runfiles;
if (!manifest.empty()) {
if (!ParseManifest(manifest, &runfiles, error)) {
return nullptr;
}
}
return new Runfiles(std::move(runfiles), std::move(directory),
std::move(envvars));
}
bool IsAbsolute(const string& path) {
if (path.empty()) {
return false;
}
char c = path.front();
return (c == '/' && (path.size() < 2 || path[1] != '/')) ||
(path.size() >= 3 &&
((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) &&
path[1] == ':' && (path[2] == '\\' || path[2] == '/'));
}
string GetEnv(const string& key) {
#ifdef _WIN32
DWORD size = ::GetEnvironmentVariableA(key.c_str(), nullptr, 0);
if (size == 0) {
return string(); // unset or empty envvar
}
std::unique_ptr<char[]> value(new char[size]);
::GetEnvironmentVariableA(key.c_str(), value.get(), size);
return value.get();
#else
char* result = getenv(key.c_str());
return (result == nullptr) ? string() : string(result);
#endif
}
string Runfiles::Rlocation(const string& path) const {
if (path.empty() || starts_with(path, "../") || contains(path, "/..") ||
starts_with(path, "./") || contains(path, "/./") ||
ends_with(path, "/.") || contains(path, "//")) {
return string();
}
if (IsAbsolute(path)) {
return path;
}
const auto value = runfiles_map_.find(path);
if (value != runfiles_map_.end()) {
return value->second;
}
if (!directory_.empty()) {
return directory_ + "/" + path;
}
return "";
}
namespace {
bool ParseManifest(const string& path, map<string, string>* result,
string* error) {
std::ifstream stm(path);
if (!stm.is_open()) {
if (error) {
std::ostringstream err;
err << "ERROR: " << __FILE__ << "(" << __LINE__
<< "): cannot open runfiles manifest \"" << path << "\"";
*error = err.str();
}
return false;
}
string line;
std::getline(stm, line);
size_t line_count = 1;
while (!line.empty()) {
string::size_type idx = line.find_first_of(' ');
if (idx == string::npos) {
if (error) {
std::ostringstream err;
err << "ERROR: " << __FILE__ << "(" << __LINE__
<< "): bad runfiles manifest entry in \"" << path << "\" line #"
<< line_count << ": \"" << line << "\"";
*error = err.str();
}
return false;
}
(*result)[line.substr(0, idx)] = line.substr(idx + 1);
std::getline(stm, line);
++line_count;
}
return true;
}
} // namespace
namespace testing {
bool TestOnly_PathsFrom(const string& argv0, string mf, string dir,
function<bool(const string&)> is_runfiles_manifest,
function<bool(const string&)> is_runfiles_directory,
string* out_manifest, string* out_directory) {
return PathsFrom(argv0, mf, dir, is_runfiles_manifest, is_runfiles_directory,
out_manifest, out_directory);
}
bool TestOnly_IsAbsolute(const string& path) { return IsAbsolute(path); }
} // namespace testing
Runfiles* Runfiles::Create(const string& argv0, string* error) {
return Runfiles::Create(argv0, GetEnv("RUNFILES_MANIFEST_FILE"),
GetEnv("RUNFILES_DIR"), error);
}
Runfiles* Runfiles::CreateForTest(std::string* error) {
return Runfiles::Create(std::string(), GetEnv("RUNFILES_MANIFEST_FILE"),
GetEnv("TEST_SRCDIR"), error);
}
namespace {
bool PathsFrom(const string& argv0, string mf, string dir, string* out_manifest,
string* out_directory) {
return PathsFrom(argv0, mf, dir,
[](const string& path) { return IsReadableFile(path); },
[](const string& path) { return IsDirectory(path); },
out_manifest, out_directory);
}
bool PathsFrom(const string& argv0, string mf, string dir,
function<bool(const string&)> is_runfiles_manifest,
function<bool(const string&)> is_runfiles_directory,
string* out_manifest, string* out_directory) {
out_manifest->clear();
out_directory->clear();
bool mfValid = is_runfiles_manifest(mf);
bool dirValid = is_runfiles_directory(dir);
if (!argv0.empty() && !mfValid && !dirValid) {
mf = argv0 + ".runfiles/MANIFEST";
dir = argv0 + ".runfiles";
mfValid = is_runfiles_manifest(mf);
dirValid = is_runfiles_directory(dir);
if (!mfValid) {
mf = argv0 + ".runfiles_manifest";
mfValid = is_runfiles_manifest(mf);
}
}
if (!mfValid && !dirValid) {
return false;
}
if (!mfValid) {
mf = dir + "/MANIFEST";
mfValid = is_runfiles_manifest(mf);
if (!mfValid) {
mf = dir + "_manifest";
mfValid = is_runfiles_manifest(mf);
}
}
if (!dirValid &&
(ends_with(mf, ".runfiles_manifest") || ends_with(mf, "/MANIFEST"))) {
static const size_t kSubstrLen = 9; // "_manifest" or "/MANIFEST"
dir = mf.substr(0, mf.size() - kSubstrLen);
dirValid = is_runfiles_directory(dir);
}
if (mfValid) {
*out_manifest = mf;
}
if (dirValid) {
*out_directory = dir;
}
return true;
}
} // namespace
} // namespace runfiles
} // namespace cpp
} // namespace tools
} // namespace bazel
|