blob: 6fb7ef43416e2ed7209391f9a8c5ac80f261a28b [file] [log] [blame]
Anton Hanssonda4972f2020-01-08 09:48:18 +00001/*
2 * Copyright (C) 2019 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#define LOG_TAG "derive_sdk"
18
19#include <algorithm>
20#include <dirent.h>
21#include <iostream>
22#include <sys/stat.h>
23#include <vector>
24
25#include <android-base/file.h>
26#include <android-base/logging.h>
27#include <android-base/properties.h>
28
29#include "frameworks/base/apex/sdkextensions/derive_sdk/sdk.pb.h"
30
31using com::android::sdkext::proto::SdkVersion;
32
33int main(int, char**) {
34 std::unique_ptr<DIR, decltype(&closedir)> apex(opendir("/apex"), closedir);
35 if (!apex) {
36 LOG(ERROR) << "Could not read /apex";
37 return EXIT_FAILURE;
38 }
39 struct dirent* de;
40 std::vector<std::string> paths;
41 while ((de = readdir(apex.get()))) {
42 std::string name = de->d_name;
43 if (name[0] == '.' || name.find('@') != std::string::npos) {
44 // Skip <name>@<ver> dirs, as they are bind-mounted to <name>
45 continue;
46 }
47 std::string path = "/apex/" + name + "/etc/sdkinfo.binarypb";
48 struct stat statbuf;
49 if (stat(path.c_str(), &statbuf) == 0) {
50 paths.push_back(path);
51 }
52 }
53
54 std::vector<int> versions;
55 for (const auto& path : paths) {
56 std::string contents;
57 if (!android::base::ReadFileToString(path, &contents, true)) {
58 LOG(ERROR) << "failed to read " << path;
59 continue;
60 }
61 SdkVersion sdk_version;
62 if (!sdk_version.ParseFromString(contents)) {
63 LOG(ERROR) << "failed to parse " << path;
64 continue;
65 }
66 LOG(INFO) << "Read version " << sdk_version.version() << " from " << path;
67 versions.push_back(sdk_version.version());
68 }
69 auto itr = std::min_element(versions.begin(), versions.end());
70 std::string prop_value = itr == versions.end() ? "0" : std::to_string(*itr);
71
72 if (!android::base::SetProperty("ro.build.version.extensions.r", prop_value)) {
73 LOG(ERROR) << "failed to set sdk_info prop";
74 return EXIT_FAILURE;
75 }
76
77 LOG(INFO) << "R extension version is " << prop_value;
78 return EXIT_SUCCESS;
79}