Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
| 5 | #include "rs_bindings_from_cc/cmdline.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <iterator> |
| 9 | #include <string> |
| 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 13 | #include "absl/flags/flag.h" |
| 14 | #include "absl/strings/str_cat.h" |
| 15 | #include "absl/strings/substitute.h" |
Marco Poletti | c61bcc4 | 2022-04-08 12:54:30 -0700 | [diff] [blame] | 16 | #include "common/status_macros.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 17 | #include "llvm/Support/JSON.h" |
Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 18 | |
| 19 | ABSL_FLAG(bool, do_nothing, false, |
| 20 | "if set to true the tool will produce empty files " |
Googler | 6c3de12 | 2022-03-28 11:40:41 +0000 | [diff] [blame] | 21 | "(useful for testing Bazel integration)"); |
Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 22 | ABSL_FLAG(std::string, rs_out, "", |
| 23 | "output path for the Rust source file with bindings"); |
| 24 | ABSL_FLAG(std::string, cc_out, "", |
| 25 | "output path for the C++ source file with bindings implementation"); |
| 26 | ABSL_FLAG(std::string, ir_out, "", |
| 27 | "(optional) output path for the JSON IR. If not present, the JSON IR " |
| 28 | "will not be dumped."); |
Lukasz Anforowicz | dd90770 | 2022-05-06 09:24:07 -0700 | [diff] [blame] | 29 | ABSL_FLAG(std::string, crubit_support_path, "", |
| 30 | "path to a the crubit/rs_bindings/support directory in a format that " |
| 31 | "should be used in the #include directives inside the generated .cc " |
| 32 | "files."); |
Lukasz Anforowicz | d7d68f0 | 2022-05-26 07:41:02 -0700 | [diff] [blame] | 33 | ABSL_FLAG(std::string, rustfmt_exe_path, "", |
| 34 | "Path to a rustfmt executable that will be used to format the " |
| 35 | ".rs files generated by the tool."); |
Lukasz Anforowicz | 54ff318 | 2022-05-06 07:17:58 -0700 | [diff] [blame] | 36 | ABSL_FLAG(std::string, rustfmt_config_path, "", |
| 37 | "(optional) path to a rustfmt.toml file that should replace the " |
| 38 | "default formatting of the .rs files generated by the tool."); |
Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 39 | ABSL_FLAG(std::vector<std::string>, public_headers, std::vector<std::string>(), |
| 40 | "public headers of the cc_library this tool should generate bindings " |
| 41 | "for, in a format suitable for usage in google3-relative quote " |
| 42 | "include (#include \"\")."); |
| 43 | ABSL_FLAG(std::string, targets_and_headers, std::string(), |
| 44 | "Information about which headers belong to which targets, encoded as " |
| 45 | "a JSON array. For example: " |
| 46 | "[\n" |
| 47 | " {\n" |
| 48 | " \"t\": \"//foo/bar:baz\",\n" |
| 49 | " \"h\": [\"foo/bar/header1.h\", \"foo/bar/header2.h\"]\n" |
| 50 | " },\n" |
| 51 | "...\n" |
| 52 | "]"); |
Devin Jeanpierre | 96bf0bd | 2022-10-04 20:32:15 -0700 | [diff] [blame] | 53 | ABSL_FLAG(std::vector<std::string>, extra_rs_srcs, std::vector<std::string>(), |
| 54 | "Additional Rust source files to include into the crate."); |
| 55 | ABSL_FLAG(std::vector<std::string>, srcs_to_scan_for_instantiations, |
| 56 | std::vector<std::string>(), |
Marcel Hlopko | 18bb5d2 | 2022-05-09 04:23:43 -0700 | [diff] [blame] | 57 | "[template instantiation mode only] all Rust source files of a crate " |
| 58 | "for which we are instantiating templates."); |
| 59 | ABSL_FLAG(std::string, instantiations_out, "", |
| 60 | "[template instantiation mode only] output path for the JSON file " |
| 61 | "with mapping from a template instantiation to a generated Rust " |
| 62 | "struct name. This file is used by cc_template! macro expansion."); |
Rosica Dejanovska | abe406f | 2022-09-02 07:06:50 -0700 | [diff] [blame] | 63 | ABSL_FLAG(std::string, namespaces_out, "", |
| 64 | "(optional) output path for the JSON file containing the target's" |
| 65 | "namespace hierarchy."); |
Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 66 | |
Marcel Hlopko | f15e8ce | 2022-04-08 08:46:09 -0700 | [diff] [blame] | 67 | namespace crubit { |
Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 68 | |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 69 | namespace { |
| 70 | |
| 71 | struct TargetAndHeaders { |
| 72 | std::string target; |
| 73 | std::vector<std::string> headers; |
| 74 | }; |
| 75 | |
| 76 | bool fromJSON(const llvm::json::Value& json, TargetAndHeaders& out, |
| 77 | llvm::json::Path path) { |
| 78 | llvm::json::ObjectMapper mapper(json, path); |
| 79 | return mapper && mapper.map("t", out.target) && mapper.map("h", out.headers); |
| 80 | } |
| 81 | |
| 82 | } // namespace |
| 83 | |
Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 84 | absl::StatusOr<Cmdline> Cmdline::Create() { |
| 85 | return CreateFromArgs( |
| 86 | absl::GetFlag(FLAGS_cc_out), absl::GetFlag(FLAGS_rs_out), |
Rosica Dejanovska | abe406f | 2022-09-02 07:06:50 -0700 | [diff] [blame] | 87 | absl::GetFlag(FLAGS_ir_out), absl::GetFlag(FLAGS_namespaces_out), |
| 88 | absl::GetFlag(FLAGS_crubit_support_path), |
Lukasz Anforowicz | d7d68f0 | 2022-05-26 07:41:02 -0700 | [diff] [blame] | 89 | absl::GetFlag(FLAGS_rustfmt_exe_path), |
Lukasz Anforowicz | dd90770 | 2022-05-06 09:24:07 -0700 | [diff] [blame] | 90 | absl::GetFlag(FLAGS_rustfmt_config_path), absl::GetFlag(FLAGS_do_nothing), |
| 91 | absl::GetFlag(FLAGS_public_headers), |
Marcel Hlopko | 18bb5d2 | 2022-05-09 04:23:43 -0700 | [diff] [blame] | 92 | absl::GetFlag(FLAGS_targets_and_headers), |
Devin Jeanpierre | 96bf0bd | 2022-10-04 20:32:15 -0700 | [diff] [blame] | 93 | absl::GetFlag(FLAGS_extra_rs_srcs), |
| 94 | absl::GetFlag(FLAGS_srcs_to_scan_for_instantiations), |
Marcel Hlopko | 18bb5d2 | 2022-05-09 04:23:43 -0700 | [diff] [blame] | 95 | absl::GetFlag(FLAGS_instantiations_out)); |
Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | absl::StatusOr<Cmdline> Cmdline::CreateFromArgs( |
Lukasz Anforowicz | 54ff318 | 2022-05-06 07:17:58 -0700 | [diff] [blame] | 99 | std::string cc_out, std::string rs_out, std::string ir_out, |
Rosica Dejanovska | abe406f | 2022-09-02 07:06:50 -0700 | [diff] [blame] | 100 | std::string namespaces_out, std::string crubit_support_path, |
| 101 | std::string rustfmt_exe_path, std::string rustfmt_config_path, |
| 102 | bool do_nothing, std::vector<std::string> public_headers, |
Devin Jeanpierre | 96bf0bd | 2022-10-04 20:32:15 -0700 | [diff] [blame] | 103 | std::string targets_and_headers_str, std::vector<std::string> extra_rs_srcs, |
| 104 | std::vector<std::string> srcs_to_scan_for_instantiations, |
Marcel Hlopko | 18bb5d2 | 2022-05-09 04:23:43 -0700 | [diff] [blame] | 105 | std::string instantiations_out) { |
Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 106 | Cmdline cmdline; |
| 107 | |
| 108 | if (rs_out.empty()) { |
| 109 | return absl::InvalidArgumentError("please specify --rs_out"); |
| 110 | } |
| 111 | cmdline.rs_out_ = std::move(rs_out); |
| 112 | |
| 113 | if (cc_out.empty()) { |
| 114 | return absl::InvalidArgumentError("please specify --cc_out"); |
| 115 | } |
| 116 | cmdline.cc_out_ = std::move(cc_out); |
| 117 | |
| 118 | cmdline.ir_out_ = std::move(ir_out); |
Lukasz Anforowicz | dd90770 | 2022-05-06 09:24:07 -0700 | [diff] [blame] | 119 | |
Rosica Dejanovska | abe406f | 2022-09-02 07:06:50 -0700 | [diff] [blame] | 120 | cmdline.namespaces_out_ = std::move(namespaces_out); |
| 121 | |
Lukasz Anforowicz | dd90770 | 2022-05-06 09:24:07 -0700 | [diff] [blame] | 122 | if (crubit_support_path.empty()) { |
| 123 | return absl::InvalidArgumentError("please specify --crubit_support_path"); |
| 124 | } |
| 125 | cmdline.crubit_support_path_ = std::move(crubit_support_path); |
| 126 | |
Lukasz Anforowicz | d7d68f0 | 2022-05-26 07:41:02 -0700 | [diff] [blame] | 127 | if (rustfmt_exe_path.empty()) { |
| 128 | return absl::InvalidArgumentError("please specify --rustfmt_exe_path"); |
| 129 | } |
| 130 | cmdline.rustfmt_exe_path_ = std::move(rustfmt_exe_path); |
| 131 | |
Lukasz Anforowicz | 54ff318 | 2022-05-06 07:17:58 -0700 | [diff] [blame] | 132 | cmdline.rustfmt_config_path_ = std::move(rustfmt_config_path); |
Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 133 | cmdline.do_nothing_ = do_nothing; |
| 134 | |
| 135 | if (public_headers.empty()) { |
| 136 | return absl::InvalidArgumentError("please specify --public_headers"); |
| 137 | } |
| 138 | std::transform(public_headers.begin(), public_headers.end(), |
| 139 | std::back_inserter(cmdline.public_headers_), |
| 140 | [](const std::string& s) { return HeaderName(s); }); |
| 141 | |
Devin Jeanpierre | 96bf0bd | 2022-10-04 20:32:15 -0700 | [diff] [blame] | 142 | cmdline.extra_rs_srcs_ = std::move(extra_rs_srcs); |
| 143 | |
| 144 | if (srcs_to_scan_for_instantiations.empty() != instantiations_out.empty()) { |
Marcel Hlopko | 18bb5d2 | 2022-05-09 04:23:43 -0700 | [diff] [blame] | 145 | return absl::InvalidArgumentError( |
| 146 | "please specify both --rust_sources and --instantiations_out when " |
| 147 | "requesting a template instantiation mode"); |
| 148 | } |
| 149 | cmdline.instantiations_out_ = std::move(instantiations_out); |
Devin Jeanpierre | 96bf0bd | 2022-10-04 20:32:15 -0700 | [diff] [blame] | 150 | cmdline.srcs_to_scan_for_instantiations_ = |
| 151 | std::move(srcs_to_scan_for_instantiations); |
Marcel Hlopko | 18bb5d2 | 2022-05-09 04:23:43 -0700 | [diff] [blame] | 152 | |
Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 153 | if (targets_and_headers_str.empty()) { |
| 154 | return absl::InvalidArgumentError("please specify --targets_and_headers"); |
| 155 | } |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 156 | auto targets_and_headers = llvm::json::parse<std::vector<TargetAndHeaders>>( |
| 157 | std::move(targets_and_headers_str)); |
| 158 | if (auto err = targets_and_headers.takeError()) { |
Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 159 | return absl::InvalidArgumentError( |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 160 | absl::StrCat("Malformed `--targets_and_headers` argument: ", |
| 161 | toString(std::move(err)))); |
Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 162 | } |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 163 | for (const TargetAndHeaders& it : *targets_and_headers) { |
| 164 | const std::string& target = it.target; |
| 165 | if (target.empty()) { |
Lukasz Anforowicz | 45f2d26 | 2022-03-02 18:37:27 +0000 | [diff] [blame] | 166 | return absl::InvalidArgumentError( |
| 167 | "Expected `t` fields of `--targets_and_headers` to be a non-empty " |
| 168 | "string"); |
| 169 | } |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 170 | for (const std::string& header : it.headers) { |
| 171 | if (header.empty()) { |
Lukasz Anforowicz | 45f2d26 | 2022-03-02 18:37:27 +0000 | [diff] [blame] | 172 | return absl::InvalidArgumentError( |
| 173 | "Expected `h` fields of `--targets_and_headers` to be an array of " |
| 174 | "non-empty strings"); |
| 175 | } |
| 176 | const auto [it, inserted] = cmdline.headers_to_targets_.insert( |
Googler | 6c3de12 | 2022-03-28 11:40:41 +0000 | [diff] [blame] | 177 | std::make_pair(HeaderName(header), BazelLabel(target))); |
Lukasz Anforowicz | 45f2d26 | 2022-03-02 18:37:27 +0000 | [diff] [blame] | 178 | if (!inserted) { |
| 179 | return absl::InvalidArgumentError(absl::Substitute( |
| 180 | "The `--targets_and_headers` cmdline argument assigns " |
| 181 | "`$0` header to two conflicting targets: `$1` vs `$2`", |
Lukasz Anforowicz | 3b4be12 | 2022-03-16 00:09:35 +0000 | [diff] [blame] | 182 | header, target, it->second.value())); |
Lukasz Anforowicz | 45f2d26 | 2022-03-02 18:37:27 +0000 | [diff] [blame] | 183 | } |
Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Lukasz Anforowicz | d41a4e9 | 2022-03-17 17:08:57 +0000 | [diff] [blame] | 187 | CRUBIT_ASSIGN_OR_RETURN(cmdline.current_target_, |
| 188 | cmdline.FindHeader(cmdline.public_headers_[0])); |
Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 189 | for (const HeaderName& public_header : cmdline.public_headers_) { |
Googler | 6c3de12 | 2022-03-28 11:40:41 +0000 | [diff] [blame] | 190 | CRUBIT_ASSIGN_OR_RETURN(BazelLabel header_target, |
Lukasz Anforowicz | d41a4e9 | 2022-03-17 17:08:57 +0000 | [diff] [blame] | 191 | cmdline.FindHeader(public_header)); |
Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 192 | |
| 193 | if (cmdline.current_target_ != header_target) { |
| 194 | return absl::InvalidArgumentError(absl::Substitute( |
| 195 | "Expected all public headers to belong to the current target '$0', " |
| 196 | "but header '$1' belongs to '$2'", |
| 197 | cmdline.current_target_.value(), public_header.IncludePath(), |
| 198 | header_target.value())); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return cmdline; |
| 203 | } |
| 204 | |
Googler | 6c3de12 | 2022-03-28 11:40:41 +0000 | [diff] [blame] | 205 | absl::StatusOr<BazelLabel> Cmdline::FindHeader(const HeaderName& header) const { |
Lukasz Anforowicz | 016eb36 | 2022-03-02 18:37:03 +0000 | [diff] [blame] | 206 | auto it = headers_to_targets_.find(header); |
| 207 | if (it == headers_to_targets_.end()) { |
| 208 | return absl::InvalidArgumentError(absl::Substitute( |
| 209 | "Couldn't find header '$0' in the `headers_to_target` map " |
| 210 | "derived from the --targets_and_headers cmdline argument", |
| 211 | header.IncludePath())); |
| 212 | } |
| 213 | return it->second; |
| 214 | } |
| 215 | |
| 216 | Cmdline::Cmdline() = default; |
| 217 | |
Marcel Hlopko | f15e8ce | 2022-04-08 08:46:09 -0700 | [diff] [blame] | 218 | } // namespace crubit |