Skip to content

Commit d3bb8f7

Browse files
committed
Add AndroidBuild implementation based on misc_info.txt
Bug: b/471081721
1 parent affca6c commit d3bb8f7

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed

base/cvd/cuttlefish/host/commands/assemble_cvd/android_build/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,16 @@ cf_cc_library(
6464
"@protobuf",
6565
],
6666
)
67+
68+
cf_cc_library(
69+
name = "misc_info_metadata",
70+
srcs = ["misc_info_metadata.cc"],
71+
hdrs = ["misc_info_metadata.h"],
72+
deps = [
73+
"//cuttlefish/host/commands/assemble_cvd/android_build",
74+
"//cuttlefish/result",
75+
"@abseil-cpp//absl/strings",
76+
"@abseil-cpp//absl/strings:str_format",
77+
"@fmt",
78+
],
79+
)
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//
2+
// Copyright (C) 2025 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+
#include "cuttlefish/host/commands/assemble_cvd/android_build/misc_info_metadata.h"
17+
18+
#include <functional>
19+
#include <map>
20+
#include <memory>
21+
#include <ostream>
22+
#include <set>
23+
#include <string>
24+
#include <string_view>
25+
#include <utility>
26+
27+
#include "absl/strings/match.h"
28+
#include "absl/strings/str_cat.h"
29+
#include "absl/strings/str_split.h"
30+
#include "fmt/ostream.h"
31+
32+
#include "cuttlefish/host/commands/assemble_cvd/android_build/android_build.h"
33+
#include "cuttlefish/result/result.h"
34+
35+
namespace cuttlefish {
36+
namespace {
37+
38+
class MetadataFromMiscInfo : public AndroidBuild {
39+
public:
40+
MetadataFromMiscInfo(std::map<std::string, std::string> misc_info)
41+
: misc_info_(std::move(misc_info)) {}
42+
43+
Result<std::set<std::string, std::less<void>>> SystemPartitions() override {
44+
return CF_EXPECT(PartitionsMatchingGroup("system"));
45+
}
46+
47+
Result<std::set<std::string, std::less<void>>> VendorPartitions() override {
48+
return CF_EXPECT(PartitionsMatchingGroup("vendor"));
49+
}
50+
51+
Result<std::set<std::string, std::less<void>>> LogicalPartitions() override {
52+
std::set<std::string, std::less<void>> partitions;
53+
partitions.merge(CF_EXPECT(SystemPartitions()));
54+
partitions.merge(CF_EXPECT(VendorPartitions()));
55+
return partitions;
56+
}
57+
58+
private:
59+
Result<std::set<std::string, std::less<void>>> PartitionsMatchingGroup(
60+
std::string_view match) {
61+
std::string groups_key = "super_partition_groups";
62+
auto groups_it = misc_info_.find(groups_key);
63+
CF_EXPECTF(groups_it != misc_info_.end(), "Could not find entry for '{}'",
64+
groups_key);
65+
66+
std::string_view matching_group;
67+
for (std::string_view group :
68+
absl::StrSplit(groups_it->second, " ", absl::SkipEmpty())) {
69+
if (absl::StrContains(group, match)) {
70+
matching_group = group;
71+
}
72+
}
73+
74+
CF_EXPECTF(!matching_group.empty(), "No '{}' group", match);
75+
76+
std::string key = absl::StrCat("super_", matching_group, "_partition_list");
77+
78+
auto group_it = misc_info_.find(key);
79+
CF_EXPECTF(group_it != misc_info_.end(), "Could not find entry for '{}'",
80+
key);
81+
82+
return absl::StrSplit(group_it->second, " ", absl::SkipEmpty());
83+
}
84+
85+
std::ostream& Format(std::ostream& out) const override {
86+
out << "MetadataFromMiscInfo { ";
87+
for (auto& [key, value] : misc_info_) {
88+
fmt::print(out, "'{}' => '{}', ", key, value);
89+
}
90+
return out << "}";
91+
}
92+
93+
std::map<std::string, std::string> misc_info_;
94+
};
95+
96+
} // namespace
97+
98+
Result<std::unique_ptr<AndroidBuild>> AndroidBuildFromMiscInfo(
99+
std::map<std::string, std::string> misc_info) {
100+
auto build = std::make_unique<MetadataFromMiscInfo>(std::move(misc_info));
101+
102+
std::set<std::string, std::less<void>> system =
103+
CF_EXPECT(build->SystemPartitions());
104+
CF_EXPECT(!system.empty());
105+
106+
std::set<std::string, std::less<void>> vendor =
107+
CF_EXPECT(build->VendorPartitions());
108+
CF_EXPECT(!vendor.empty());
109+
110+
return build;
111+
}
112+
113+
} // namespace cuttlefish
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Copyright (C) 2025 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+
#pragma once
17+
18+
#include <map>
19+
#include <memory>
20+
#include <string>
21+
22+
#include "cuttlefish/host/commands/assemble_cvd/android_build/android_build.h"
23+
#include "cuttlefish/result/result.h"
24+
25+
namespace cuttlefish {
26+
27+
/**
28+
* Reports partition information from the key-value pairs in a `misc_info.txt`
29+
* file.
30+
*
31+
* Although `misc_info.txt` does not contain any image files, it does have a
32+
* complete list of the logical partitions that are intended to be present in a
33+
* complete `super.img` file, as well as the division between "system" and
34+
* "vendor" side logical partitions.
35+
*/
36+
Result<std::unique_ptr<AndroidBuild>> AndroidBuildFromMiscInfo(
37+
std::map<std::string, std::string>);
38+
39+
} // namespace cuttlefish

0 commit comments

Comments
 (0)