-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathRunfilesPath.cc
71 lines (58 loc) · 2.1 KB
/
RunfilesPath.cc
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
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#include <fmt/format.h>
#include "boost/filesystem.hpp"
namespace fs = boost::filesystem;
namespace compiler_gym::util {
namespace {
static const char* UNKNOWN_USER_NAME = "unknown";
inline std::string getUser() {
const char* base = std::getenv("USER");
return base ? base : UNKNOWN_USER_NAME;
}
} // namespace
fs::path getRunfilesPath(const std::string& relPath) {
const char* base = std::getenv("COMPILER_GYM_RUNFILES");
if (base) {
return fs::path(base) / relPath;
} else {
return fs::path(relPath);
}
}
fs::path getSiteDataPath(const std::string& relPath) {
// NOTE(cummins): This function has a matching implementation in the Python
// sources, compiler_gym.util.runfiles_path.get_site_data_path().
// Any change to behavior here must be reflected in the Python version.
const char* force = std::getenv("COMPILER_GYM_SITE_DATA");
if (force) {
return fs::path(force) / relPath;
}
const char* home = std::getenv("HOME");
if (home) {
return fs::path(home) / ".local/share/compiler_gym" / relPath;
} else {
// $HOME may not be set under testing conditions. In this case, use a
// throwaway directory.
return fs::temp_directory_path() / fmt::format("compiler_gym_{}", getUser()) / relPath;
}
}
fs::path getCacheRootPath() {
// NOTE(cummins): This function has a related implementation in the Python
// sources, compiler_gym.util.runfiles_path.get_cache_path(). Any change to
// behavior here must be reflected in the Python version.
const char* force = std::getenv("COMPILER_GYM_CACHE");
if (force) {
return fs::path(force);
}
const char* home = std::getenv("HOME");
if (home) {
return fs::path(home) / ".local/cache/compiler_gym";
} else {
// $HOME may not be set under testing conditions. In this case, use a
// throwaway directory.
return fs::temp_directory_path() / fmt::format("compiler_gym_{}", getUser());
}
}
} // namespace compiler_gym::util