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 "host/commands/cvd/server_command/host_tool_target.h"
18
19 #include <sys/stat.h>
20
21 #include <fruit/fruit.h>
22
23 #include "common/libs/utils/contains.h"
24 #include "common/libs/utils/files.h"
25 #include "common/libs/utils/result.h"
26 #include "common/libs/utils/subprocess.h"
27 #include "host/commands/cvd/common_utils.h"
28 #include "host/commands/cvd/server_command/flags_collector.h"
29
30 namespace cuttlefish {
31
Create(const std::string & artifacts_path,const OperationToBinsMap & supported_operations)32 Result<HostToolTarget> HostToolTarget::Create(
33 const std::string& artifacts_path,
34 const OperationToBinsMap& supported_operations) {
35 std::string bin_dir_path = ConcatToString(artifacts_path, "/bin");
36 std::unordered_map<std::string, OperationImplementation> op_to_impl_map;
37 for (const auto& [op, candidates] : supported_operations) {
38 for (const auto& bin_name : candidates) {
39 const auto bin_path = ConcatToString(bin_dir_path, "/", bin_name);
40 if (!FileExists(bin_path)) {
41 continue;
42 }
43 op_to_impl_map[op] = OperationImplementation{.bin_name_ = bin_name};
44 break;
45 }
46 }
47
48 for (auto& [op, op_impl] : op_to_impl_map) {
49 const std::string bin_path =
50 ConcatToString(artifacts_path, "/bin/", op_impl.bin_name_);
51 Command command(bin_path);
52 command.AddParameter("--helpxml");
53 // b/276497044
54 command.UnsetFromEnvironment(kAndroidHostOut);
55 command.AddEnvironmentVariable(kAndroidHostOut, artifacts_path);
56 command.UnsetFromEnvironment(kAndroidSoongHostOut);
57 command.AddEnvironmentVariable(kAndroidSoongHostOut, artifacts_path);
58
59 std::string xml_str;
60 RunWithManagedStdio(std::move(command), nullptr, std::addressof(xml_str),
61 nullptr);
62 auto flags_opt = CollectFlagsFromHelpxml(xml_str);
63 if (!flags_opt) {
64 LOG(ERROR) << bin_path << " --helpxml failed.";
65 continue;
66 }
67 auto flags = std::move(*flags_opt);
68 for (auto& flag : flags) {
69 op_impl.supported_flags_[flag->Name()] = std::move(flag);
70 }
71 }
72
73 struct stat for_dir_time_stamp;
74 time_t dir_time_stamp = 0;
75 // we get dir time stamp, as the runtime libraries might be updated
76 if (::stat(bin_dir_path.data(), &for_dir_time_stamp) == 0) {
77 // if stat failed, use the smallest possible value, which is 0
78 // in that way, the HostTool entry will be always updated on read request.
79 dir_time_stamp = for_dir_time_stamp.st_mtime;
80 }
81 return HostToolTarget(artifacts_path, dir_time_stamp,
82 std::move(op_to_impl_map));
83 }
84
HostToolTarget(const std::string & artifacts_path,const time_t dir_time_stamp,std::unordered_map<std::string,OperationImplementation> && op_to_impl_map)85 HostToolTarget::HostToolTarget(
86 const std::string& artifacts_path, const time_t dir_time_stamp,
87 std::unordered_map<std::string, OperationImplementation>&& op_to_impl_map)
88 : artifacts_path_(artifacts_path),
89 dir_time_stamp_(dir_time_stamp),
90 op_to_impl_map_(std::move(op_to_impl_map)) {}
91
IsDirty() const92 bool HostToolTarget::IsDirty() const {
93 std::string bin_path = ConcatToString(artifacts_path_, "/bin");
94 if (!DirectoryExists(bin_path)) {
95 return true;
96 }
97 struct stat for_dir_time_stamp;
98 if (::stat(bin_path.data(), &for_dir_time_stamp) != 0) {
99 return true;
100 }
101 return dir_time_stamp_ != for_dir_time_stamp.st_mtime;
102 }
103
GetFlagInfo(const FlagInfoRequest & request) const104 Result<FlagInfo> HostToolTarget::GetFlagInfo(
105 const FlagInfoRequest& request) const {
106 CF_EXPECT(Contains(op_to_impl_map_, request.operation_),
107 "Operation \"" << request.operation_ << "\" is not supported.");
108 auto& supported_flags =
109 op_to_impl_map_.at(request.operation_).supported_flags_;
110 CF_EXPECT(Contains(supported_flags, request.flag_name_));
111 const auto& flag_uniq_ptr = supported_flags.at(request.flag_name_);
112 FlagInfo copied(*flag_uniq_ptr);
113 return copied;
114 }
115
GetBinName(const std::string & operation) const116 Result<std::string> HostToolTarget::GetBinName(
117 const std::string& operation) const {
118 CF_EXPECT(Contains(op_to_impl_map_, operation),
119 "Operation \"" << operation << "\" is not supported by "
120 << "the host tool target object at "
121 << artifacts_path_);
122 return op_to_impl_map_.at(operation).bin_name_;
123 }
124
125 } // namespace cuttlefish
126