blob: 89c9adfcab9f0ba946aa524fc8bfe043d1e39fab [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>
Anton Hanssonb1a217c2020-11-30 11:43:05 +000028#include <android-modules-utils/sdk_level.h>
Anton Hanssonda4972f2020-01-08 09:48:18 +000029
Baligh Uddina1fed012020-05-22 16:48:01 +000030#include "packages/modules/SdkExtensions/derive_sdk/sdk.pb.h"
Anton Hanssonda4972f2020-01-08 09:48:18 +000031
32using com::android::sdkext::proto::SdkVersion;
33
34int main(int, char**) {
35 std::unique_ptr<DIR, decltype(&closedir)> apex(opendir("/apex"), closedir);
36 if (!apex) {
37 LOG(ERROR) << "Could not read /apex";
38 return EXIT_FAILURE;
39 }
40 struct dirent* de;
41 std::vector<std::string> paths;
42 while ((de = readdir(apex.get()))) {
43 std::string name = de->d_name;
44 if (name[0] == '.' || name.find('@') != std::string::npos) {
45 // Skip <name>@<ver> dirs, as they are bind-mounted to <name>
46 continue;
47 }
48 std::string path = "/apex/" + name + "/etc/sdkinfo.binarypb";
49 struct stat statbuf;
50 if (stat(path.c_str(), &statbuf) == 0) {
51 paths.push_back(path);
52 }
53 }
54
55 std::vector<int> versions;
56 for (const auto& path : paths) {
57 std::string contents;
58 if (!android::base::ReadFileToString(path, &contents, true)) {
59 LOG(ERROR) << "failed to read " << path;
60 continue;
61 }
62 SdkVersion sdk_version;
63 if (!sdk_version.ParseFromString(contents)) {
64 LOG(ERROR) << "failed to parse " << path;
65 continue;
66 }
67 LOG(INFO) << "Read version " << sdk_version.version() << " from " << path;
68 versions.push_back(sdk_version.version());
69 }
70 auto itr = std::min_element(versions.begin(), versions.end());
71 std::string prop_value = itr == versions.end() ? "0" : std::to_string(*itr);
72
Anton Hansson6e7f3282020-02-07 19:05:33 +000073 if (!android::base::SetProperty("build.version.extensions.r", prop_value)) {
Anton Hanssonb1a217c2020-11-30 11:43:05 +000074 LOG(ERROR) << "failed to set r sdk_info prop";
Anton Hanssonda4972f2020-01-08 09:48:18 +000075 return EXIT_FAILURE;
76 }
Anton Hanssonb1a217c2020-11-30 11:43:05 +000077 if (android::modules::sdklevel::IsAtLeastS()) {
78 if (!android::base::SetProperty("build.version.extensions.s", prop_value)) {
79 LOG(ERROR) << "failed to set s sdk_info prop";
80 return EXIT_FAILURE;
81 }
82 }
Anton Hanssonda4972f2020-01-08 09:48:18 +000083
Anton Hanssonb1a217c2020-11-30 11:43:05 +000084 LOG(INFO) << "Extension version is " << prop_value;
Anton Hanssonda4972f2020-01-08 09:48:18 +000085 return EXIT_SUCCESS;
86}