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 #ifdef __ANDROID_VENDOR__
40 const prop_info* pi = __system_property_find("ro.vendor.build.version.incremental");
41 #else
42 const prop_info* pi = __system_property_find("ro.build.version.incremental");
43 #endif
44 if (pi == nullptr) return "";
45
46 std::string property_value;
47 __system_property_read_callback(pi,
48 [](void* cookie, const char*, const char* value, unsigned) {
49 auto property_value = reinterpret_cast<std::string*>(cookie);
50 *property_value = value;
51 },
52 &property_value);
53
54 return property_value;
55 }
56
57 #else
58
GetBuildNumber()59 std::string GetBuildNumber() {
60 return soong_build_number;
61 }
62
63 #endif
64 } // namespace build
65 } // namespace android
66