use new hugify method only in fb builds
[hiphop-php.git] / hphp / util / build-info.cpp
blob068583666365a7d639fc0ebe096c4cdaace90393
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/Range.h>
22 #include <atomic>
23 #include <mutex>
24 #include <string>
26 namespace HPHP {
27 ////////////////////////////////////////////////////////////////////////////////
29 namespace {
31 std::atomic<bool> inited;
32 std::mutex mtx;
33 std::string repoSchema;
34 std::string compiler;
35 std::string buildid;
36 std::string hhjsbabeltransform;
38 ////////////////////////////////////////////////////////////////////////////////
41 * Initializes the repo schema id and the compiler id from their special
42 * sections in the hhvm binary.
44 void readBuildInfo() {
45 if (inited.load(std::memory_order_acquire)) return;
46 std::unique_lock<std::mutex> lock(mtx);
47 if (inited.load(std::memory_order_acquire)) return;
49 auto const get = [&] (const char* section) -> std::string {
50 auto constexpr bad = "(UNKNOWN)";
52 embedded_data desc;
53 if (!get_embedded_data(section, &desc)) {
54 return bad;
57 auto const data = read_embedded_data(desc);
58 if (data.empty()) {
59 return bad;
62 return data;
65 if (auto const env_schema = getenv("HHVM_RUNTIME_REPO_SCHEMA")) {
66 repoSchema = env_schema;
67 } else {
68 repoSchema = get("repo_schema_id");
71 compiler = get("compiler_id");
72 buildid = get("build_id");
73 hhjsbabeltransform = get("hhjs_babel_transform");
75 inited.store(true, std::memory_order_release);
80 ////////////////////////////////////////////////////////////////////////////////
82 folly::StringPiece repoSchemaId() {
83 readBuildInfo();
84 return repoSchema;
87 folly::StringPiece compilerId() {
88 readBuildInfo();
89 return compiler;
92 folly::StringPiece buildId() {
93 readBuildInfo();
94 return buildid;
97 folly::StringPiece hhjsBabelTransform() {
98 readBuildInfo();
99 return hhjsbabeltransform;
102 const char* kSchemaPlaceholder = "%{schema}";
104 std::string insertSchema(const char* path) {
105 assert(strstr(repoSchemaId().begin(), kSchemaPlaceholder) == nullptr);
106 std::string result = path;
107 size_t idx;
108 if ((idx = result.find(kSchemaPlaceholder)) != std::string::npos) {
109 result.replace(idx, strlen(kSchemaPlaceholder), repoSchemaId().begin());
111 return result;
114 ////////////////////////////////////////////////////////////////////////////////