blob: 1b85c905d485e360d97093a662291f32c72ac771 [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
Anton Hanssonda4972f2020-01-08 09:48:18 +000019#include <android-base/file.h>
20#include <android-base/logging.h>
21#include <android-base/properties.h>
Anton Hanssonb1a217c2020-11-30 11:43:05 +000022#include <android-modules-utils/sdk_level.h>
Anton Hanssone4fd55c2020-12-29 17:46:30 +000023#include <dirent.h>
24#include <sys/stat.h>
25
26#include <algorithm>
27#include <iostream>
28#include <vector>
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**) {
Anton Hanssone4fd55c2020-12-29 17:46:30 +000035 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;
Anton Hanssonda4972f2020-01-08 09:48:18 +000047 }
Anton Hanssone4fd55c2020-12-29 17:46:30 +000048 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);
Anton Hanssonda4972f2020-01-08 09:48:18 +000052 }
Anton Hanssone4fd55c2020-12-29 17:46:30 +000053 }
Anton Hanssonda4972f2020-01-08 09:48:18 +000054
Anton Hanssone4fd55c2020-12-29 17:46:30 +000055 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;
Anton Hanssonda4972f2020-01-08 09:48:18 +000061 }
Anton Hanssone4fd55c2020-12-29 17:46:30 +000062 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);
Anton Hanssonda4972f2020-01-08 09:48:18 +000072
Anton Hanssone4fd55c2020-12-29 17:46:30 +000073 if (!android::base::SetProperty("build.version.extensions.r", prop_value)) {
74 LOG(ERROR) << "failed to set r sdk_info prop";
75 return EXIT_FAILURE;
76 }
77 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;
Anton Hanssonda4972f2020-01-08 09:48:18 +000081 }
Anton Hanssone4fd55c2020-12-29 17:46:30 +000082 }
Anton Hanssonda4972f2020-01-08 09:48:18 +000083
Anton Hanssone4fd55c2020-12-29 17:46:30 +000084 LOG(INFO) << "Extension version is " << prop_value;
85 return EXIT_SUCCESS;
Anton Hanssonda4972f2020-01-08 09:48:18 +000086}