1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <build/version.h>
18
19 #ifdef __ANDROID__
20 #include <sys/system_properties.h>
21 #endif
22
23 namespace android {
24 namespace build {
25
26 #define PLACEHOLDER "SOONG BUILD NUMBER PLACEHOLDER"
27
28 extern "C" {
29 char soong_build_number[128] = PLACEHOLDER;
30 }
31
32 #ifdef __ANDROID__
33
GetBuildNumber()34 std::string GetBuildNumber() {
35 if (strcmp(PLACEHOLDER, soong_build_number) != 0) {
36 return soong_build_number;
37 }
38
39 const prop_info* pi = __system_property_find("ro.build.version.incremental");
40 if (pi == nullptr) return "";
41
42 std::string property_value;
43 __system_property_read_callback(pi,
44 [](void* cookie, const char*, const char* value, unsigned) {
45 auto property_value = reinterpret_cast<std::string*>(cookie);
46 *property_value = value;
47 },
48 &property_value);
49
50 return property_value;
51 }
52
53 #else
54
GetBuildNumber()55 std::string GetBuildNumber() {
56 return soong_build_number;
57 }
58
59 #endif
60 } // namespace build
61 } // namespace android
62