1 /*
2 * Copyright (C) 2023 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 #include <android-base/file.h>
18 #include <gflags/gflags.h>
19
20 #include <stdio.h>
21 #include <fstream>
22 #include <string>
23
24 #include "common/libs/utils/files.h"
25 #include "common/libs/utils/json.h"
26 #include "host/commands/assemble_cvd/flags_defaults.h"
27 #include "host/commands/cvd/parser/cf_configs_common.h"
28 #include "host/commands/cvd/parser/cf_configs_instances.h"
29 #include "host/commands/cvd/parser/launch_cvd_parser.h"
30 #include "host/commands/cvd/parser/launch_cvd_templates.h"
31
32 namespace cuttlefish {
33
GenerateNumInstancesFlag(const Json::Value & root)34 std::string GenerateNumInstancesFlag(const Json::Value& root) {
35 int num_instances = root["instances"].size();
36 LOG(DEBUG) << "num_instances = " << num_instances;
37 std::string result = "--num_instances=" + std::to_string(num_instances);
38 return result;
39 }
40
GenerateCommonGflag(const Json::Value & root,const std::string & gflag_name,const std::string & json_flag)41 std::string GenerateCommonGflag(const Json::Value& root,
42 const std::string& gflag_name,
43 const std::string& json_flag) {
44 std::stringstream buff;
45 // Append Header
46 buff << "--" << gflag_name << "=" << root[json_flag].asString();
47 return buff.str();
48 }
49
GenerateCfFlags(const Json::Value & root)50 std::vector<std::string> GenerateCfFlags(const Json::Value& root) {
51 std::vector<std::string> result;
52 result.emplace_back(GenerateNumInstancesFlag(root));
53 result.emplace_back(GenerateCommonGflag(root, "netsim_bt", "netsim_bt"));
54
55 result = MergeResults(result, GenerateInstancesFlags(root["instances"]));
56 return result;
57 }
58
InitCvdConfigs(Json::Value & root)59 void InitCvdConfigs(Json::Value& root) {
60 // Handle common flags
61 if (!root.isMember("netsim_bt")) {
62 root["netsim_bt"] = CF_DEFAULTS_NETSIM_BT;
63 }
64 // Handle instances flags
65 InitInstancesConfigs(root["instances"]);
66 }
67
ParseLaunchCvdConfigs(Json::Value & root)68 std::vector<std::string> ParseLaunchCvdConfigs(Json::Value& root) {
69 ExtractLaunchTemplates(root["instances"]);
70 InitCvdConfigs(root);
71 return GenerateCfFlags(root);
72 }
73
74 } // namespace cuttlefish
75