Add logging for comparison behaviors
[hiphop-php.git] / hphp / util / build-info.cpp
blob0cdb81c6cf517480bf6705e6eae8c2197d757aba
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;
43 ////////////////////////////////////////////////////////////////////////////////
46 * Initializes the repo schema id and the compiler id from their special
47 * sections in the hhvm binary.
49 void readBuildInfo() {
50 if (inited.load(std::memory_order_acquire)) return;
51 std::unique_lock<std::mutex> lock(mtx);
52 if (inited.load(std::memory_order_acquire)) return;
54 auto const get = [&] (const char* section) -> std::string {
55 auto constexpr bad = "(UNKNOWN)";
57 embedded_data desc;
58 if (!get_embedded_data(section, &desc)) {
59 return bad;
62 auto const data = read_embedded_data(desc);
63 if (data.empty()) {
64 return bad;
67 return data;
70 if (auto const env_schema = getenv("HHVM_RUNTIME_REPO_SCHEMA")) {
71 repoSchema = env_schema;
72 } else {
73 repoSchema = get("repo_schema_id");
76 compiler = get("compiler_id");
77 buildid = get("build_id");
78 hhjsbabeltransform = get("hhjs_babel_transform");
80 inited.store(true, std::memory_order_release);
83 template<typename ValueFn>
84 void replacePlaceholder(std::string& target,
85 const char* placeholder,
86 ValueFn&& value) {
87 auto const pos = target.find(placeholder);
88 if (pos == std::string::npos) return;
89 target.replace(pos, strlen(placeholder), value());
94 ////////////////////////////////////////////////////////////////////////////////
96 folly::StringPiece repoSchemaId() {
97 readBuildInfo();
98 return repoSchema;
101 folly::StringPiece compilerId() {
102 readBuildInfo();
103 return compiler;
106 folly::StringPiece buildId() {
107 readBuildInfo();
108 return buildid;
111 folly::StringPiece hhjsBabelTransform() {
112 readBuildInfo();
113 return hhjsbabeltransform;
116 void replacePlaceholders(std::string& s) {
117 replacePlaceholder(s, "%{schema}", [] { return repoSchemaId().begin(); });
118 replacePlaceholder(s, "%{euid}", [] {
119 return folly::to<std::string>(geteuid());
121 replacePlaceholder(s, "%{user}", [] {
122 auto user = getenv("SUDO_USER");
123 if (user == nullptr) user = getenv("USER");
124 return user != nullptr ? user : "%{user}";
128 ////////////////////////////////////////////////////////////////////////////////