Use iterator adapters in decl_class_parents
[hiphop-php.git] / hphp / util / build-info.cpp
blob6e497e949a6ddd7b306bc7d63c935debf297594a
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/util/build-info.h"
19 #include "hphp/util/embedded-data.h"
21 #include <folly/Conv.h>
22 #include <folly/Range.h>
24 #include <atomic>
25 #include <cstdlib>
26 #include <mutex>
27 #include <string>
28 #include <sys/types.h>
29 #include <unistd.h>
31 namespace HPHP {
32 ////////////////////////////////////////////////////////////////////////////////
34 namespace {
36 std::atomic<bool> inited;
37 std::mutex mtx;
38 std::string repoSchema;
39 std::string compiler;
40 std::string buildid;
41 std::string hhjsbabeltransform;
42 int64_t timestamp;
44 ////////////////////////////////////////////////////////////////////////////////
47 * Initializes the repo schema id and the compiler id from their special
48 * sections in the hhvm binary.
50 void readBuildInfo() {
51 if (inited.load(std::memory_order_acquire)) return;
52 std::unique_lock<std::mutex> lock(mtx);
53 if (inited.load(std::memory_order_acquire)) return;
55 auto const get = [&] (const char* section) -> std::string {
56 auto constexpr bad = "(UNKNOWN)";
58 embedded_data desc;
59 if (!get_embedded_data(section, &desc)) {
60 return bad;
63 auto const data = read_embedded_data(desc);
64 if (data.empty()) {
65 return bad;
68 return data;
71 if (auto const env_schema = getenv("HHVM_RUNTIME_REPO_SCHEMA")) {
72 repoSchema = env_schema;
73 } else {
74 repoSchema = get("repo_schema_id");
77 compiler = get("compiler_id");
78 buildid = get("build_id");
79 hhjsbabeltransform = get("hhjs_babel_transform");
81 try {
82 timestamp = std::stoll(get("compiler_ts"));
83 } catch(std::exception& e) {
84 timestamp = 0;
87 inited.store(true, std::memory_order_release);
90 template<typename ValueFn>
91 void replacePlaceholder(std::string& target,
92 const char* placeholder,
93 ValueFn&& value) {
94 auto const pos = target.find(placeholder);
95 if (pos == std::string::npos) return;
96 target.replace(pos, strlen(placeholder), value());
101 ////////////////////////////////////////////////////////////////////////////////
103 folly::StringPiece repoSchemaId() {
104 readBuildInfo();
105 return repoSchema;
108 folly::StringPiece compilerId() {
109 readBuildInfo();
110 return compiler;
113 int64_t compilerTimestamp() {
114 readBuildInfo();
115 return timestamp;
118 folly::StringPiece buildId() {
119 readBuildInfo();
120 return buildid;
123 folly::StringPiece hhjsBabelTransform() {
124 readBuildInfo();
125 return hhjsbabeltransform;
128 void replacePlaceholders(std::string& s) {
129 replacePlaceholder(s, "%{schema}", [] { return repoSchemaId().begin(); });
130 replacePlaceholder(s, "%{euid}", [] {
131 return folly::to<std::string>(geteuid());
133 replacePlaceholder(s, "%{user}", [] {
134 auto user = getenv("SUDO_USER");
135 if (user == nullptr) user = getenv("USER");
136 return user != nullptr ? user : "%{user}";
138 replacePlaceholder(s, "%{pid}", [] {
139 return folly::to<std::string>(getpid());
143 ////////////////////////////////////////////////////////////////////////////////