Skip to content

Commit a616611

Browse files
committed
Don't set "unset" unless stricly necessary
"unset" is a special value cvd uses to let cvd_internal_start know a value in a vectorized flag is not set. This special value is not recognized by all versions of cvd_internal_start, so when the flag is unset for all instances cvd simply doesn't pass the flag to cvd_internal_start instead of passing something like "unset,unset". This allows using cvd environment configs with older android branches, albeit with some restrictions. For example, properties that could generate an "unset" must be either set in all instances or none.
1 parent 933bbb5 commit a616611

File tree

5 files changed

+53
-23
lines changed

5 files changed

+53
-23
lines changed

base/cvd/cuttlefish/host/commands/cvd/cli/parser/instance/cf_disk_configs.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
#include "cuttlefish/host/commands/cvd/cli/parser/instance/cf_disk_configs.h"
1717

18+
#include <algorithm>
1819
#include <string>
1920
#include <vector>
2021

@@ -31,6 +32,12 @@ using cvd::config::Instance;
3132
std::vector<std::string> GenerateDiskFlags(
3233
const EnvironmentSpecification& config) {
3334
std::vector<std::string> data_image_mbs;
35+
if (std::none_of(config.instances().cbegin(), config.instances().cend(),
36+
[](const auto& instance) {
37+
return instance.disk().has_blank_data_image_mb();
38+
})) {
39+
return {};
40+
}
3441
for (const auto& instance : config.instances()) {
3542
const auto& disk = instance.disk();
3643
if (disk.has_blank_data_image_mb()) {

base/cvd/cuttlefish/host/commands/cvd/cli/parser/instance/cf_graphics_configs.cpp

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "cuttlefish/host/commands/cvd/cli/parser/load_config.pb.h"
3535

3636
namespace cuttlefish {
37+
namespace {
3738

3839
using cvd::config::Display;
3940
using cvd::config::EnvironmentSpecification;
@@ -108,16 +109,33 @@ bool RecordScreen(const Instance& instance) {
108109
}
109110
}
110111

111-
std::string GpuMode(const Instance& instance) {
112-
if (instance.graphics().has_gpu_mode() &&
113-
!instance.graphics().gpu_mode().empty()) {
114-
return instance.graphics().gpu_mode();
115-
} else {
116-
// Use the instance default
117-
// https://github.com/google/android-cuttlefish/blob/c4f1643479f98bdc7310d281e81751188595233b/base/cvd/cuttlefish/host/commands/assemble_cvd/flags.cc#L948
118-
// See also b/406464352#comment7
119-
return "unset";
112+
std::optional<std::string> GpuMode(const Instance& instance) {
113+
if (!instance.graphics().has_gpu_mode() ||
114+
instance.graphics().gpu_mode().empty()) {
115+
return std::nullopt;
120116
}
117+
return instance.graphics().gpu_mode();
118+
}
119+
120+
std::optional<std::vector<std::string>> GpuModes(const EnvironmentSpecification& cfg) {
121+
std::vector<std::optional<std::string>> opts;
122+
for (const Instance& instance: cfg.instances()) {
123+
opts.emplace_back(GpuMode(instance));
124+
}
125+
if (std::none_of(opts.begin(), opts.end(),
126+
[](const auto& opt) { return opt.has_value(); })) {
127+
return std::nullopt;
128+
}
129+
std::vector<std::string> values;
130+
for (const std::optional<std::string>& opt: opts) {
131+
// Use the instance default
132+
// https://github.com/google/android-cuttlefish/blob/c4f1643479f98bdc7310d281e81751188595233b/base/cvd/cuttlefish/host/commands/assemble_cvd/flags.cc#L948
133+
// See also b/406464352#comment7
134+
values.emplace_back(opt.value_or("unset"));
135+
}
136+
return values;
137+
}
138+
121139
}
122140

123141
Result<std::vector<std::string>> GenerateGraphicsFlags(
@@ -128,7 +146,10 @@ Result<std::vector<std::string>> GenerateGraphicsFlags(
128146
flags.push_back(std::move(display_flag.value()));
129147
}
130148
flags.push_back(GenerateInstanceFlag("record_screen", cfg, RecordScreen));
131-
flags.push_back(GenerateInstanceFlag("gpu_mode", cfg, GpuMode));
149+
std::optional<std::vector<std::string>> gpu_modes = GpuModes(cfg);
150+
if (gpu_modes.has_value()) {
151+
flags.push_back(GenerateVecFlag("gpu_mode", gpu_modes.value()));
152+
}
132153
return flags;
133154
}
134155

base/cvd/cuttlefish/host/commands/cvd/cli/parser/instance/cf_vm_configs.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "cuttlefish/host/commands/cvd/cli/parser/instance/cf_vm_configs.h"
1717

1818
#include <cstdint>
19+
#include <optional>
1920
#include <string>
2021
#include <utility>
2122
#include <vector>
@@ -112,9 +113,10 @@ static std::string V4l2Proxy(const Instance& instance) {
112113
return crosvm.has_v4l2_proxy() ? crosvm.v4l2_proxy() : default_val;
113114
}
114115

115-
static Result<std::string> CustomConfigsFlagValue(const Instance& instance) {
116+
static Result<std::optional<std::string>> CustomConfigsFlagValue(
117+
const Instance& instance) {
116118
if (instance.vm().custom_actions().empty()) {
117-
return "unset";
119+
return std::nullopt;
118120
}
119121
std::vector<std::string> json_entries;
120122
for (const auto& action : instance.vm().custom_actions()) {
@@ -132,8 +134,11 @@ static Result<std::vector<std::string>> CustomConfigsFlags(
132134
const EnvironmentSpecification& cfg) {
133135
std::vector<std::string> ret;
134136
for (const auto& instance : cfg.instances()) {
135-
auto value = CF_EXPECT(CustomConfigsFlagValue(instance));
136-
ret.emplace_back(fmt::format("--custom_actions={}", value));
137+
std::optional<std::string> opt =
138+
CF_EXPECT(CustomConfigsFlagValue(instance));
139+
if (opt.has_value()) {
140+
ret.emplace_back(fmt::format("--custom_actions={}", opt.value()));
141+
}
137142
}
138143
return ret;
139144
}

base/cvd/cuttlefish/host/commands/cvd/cli/parser/instance/disk_configs_test.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ TEST(BootFlagsParserTest, ParseTwoInstancesBlankDataImageEmptyJson) {
4242
<< "Invalid Json string";
4343
auto serialized_data = LaunchCvdParserTester(json_configs);
4444
EXPECT_TRUE(serialized_data.ok()) << serialized_data.error().Trace();
45-
EXPECT_TRUE(
46-
FindConfig(*serialized_data, R"(--blank_data_image_mb=unset,unset)"))
47-
<< "blank_data_image_mb flag is missing or wrongly formatted";
45+
EXPECT_FALSE(
46+
FindConfig(*serialized_data, R"(--blank_data_image_mb=)"))
47+
<< "blank_data_image_mb flag is set";
4848
}
4949

5050
TEST(BootFlagsParserTest, ParseTwoInstancesBlankDataImagePartialJson) {

base/cvd/cuttlefish/host/commands/cvd/cli/parser/instance/vm_configs_test.cc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -942,8 +942,8 @@ TEST(VmFlagsParserTest, ParseTwoInstancesCustomActionsFlagEmptyJson) {
942942
<< "Invalid Json string";
943943
auto serialized_data = LaunchCvdParserTester(json_configs);
944944
EXPECT_TRUE(serialized_data.ok()) << serialized_data.error().Trace();
945-
EXPECT_TRUE(FindConfig(*serialized_data, R"(--custom_actions=unset)"))
946-
<< "custom_actions flag is missing or wrongly formatted";
945+
EXPECT_FALSE(FindConfig(*serialized_data, R"(--custom_actions=)"))
946+
<< "custom_actions flag is set";
947947
}
948948

949949
TEST(VmFlagsParserTest, ParseTwoInstancesCustomActionsFlagPartialJson) {
@@ -986,10 +986,7 @@ TEST(VmFlagsParserTest, ParseTwoInstancesCustomActionsFlagPartialJson) {
986986
custom_actions.emplace_back(flag.substr(sizeof(kPrefix) - 1));
987987
}
988988
}
989-
std::sort(custom_actions.begin(), custom_actions.end());
990-
991-
EXPECT_EQ(custom_actions.size(), 2);
992-
EXPECT_EQ(custom_actions[1], "unset");
989+
EXPECT_EQ(custom_actions.size(), 1);
993990

994991
Json::Value expected_actions;
995992
expected_actions[0]["device_states"][0]["lid_switch_open"] = false;

0 commit comments

Comments
 (0)