Marco Poletti | b5239c9 | 2022-05-11 09:46:05 -0700 | [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 | // Parses C++ code and generates an equivalent Rust source file. |
| 6 | |
Dmitri Gribenko | 37e1942 | 2023-07-17 04:33:44 -0700 | [diff] [blame] | 7 | #include <iostream> |
| 8 | #include <string> |
Marco Poletti | b5239c9 | 2022-05-11 09:46:05 -0700 | [diff] [blame] | 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "absl/flags/flag.h" |
| 13 | #include "absl/flags/parse.h" |
Lukasz Anforowicz | 2c34cae | 2022-08-26 07:19:20 -0700 | [diff] [blame] | 14 | #include "absl/log/check.h" |
Marco Poletti | b5239c9 | 2022-05-11 09:46:05 -0700 | [diff] [blame] | 15 | #include "absl/status/statusor.h" |
| 16 | #include "absl/strings/string_view.h" |
Marco Poletti | b5239c9 | 2022-05-11 09:46:05 -0700 | [diff] [blame] | 17 | #include "common/file_io.h" |
| 18 | #include "migrator/rs_from_cc/rs_from_cc_lib.h" |
Marco Poletti | b5239c9 | 2022-05-11 09:46:05 -0700 | [diff] [blame] | 19 | |
| 20 | ABSL_FLAG(std::string, cc_in, "", |
| 21 | "input path for the C++ source file (it may or may not be a header)"); |
| 22 | ABSL_FLAG(std::string, rs_out, "", |
| 23 | "output path for the Rust source file; will be overwritten if it " |
| 24 | "already exists"); |
| 25 | |
| 26 | int main(int argc, char* argv[]) { |
| 27 | auto args = absl::ParseCommandLine(argc, argv); |
| 28 | |
| 29 | auto cc_in = absl::GetFlag(FLAGS_cc_in); |
| 30 | if (cc_in.empty()) { |
| 31 | std::cerr << "please specify --cc_in" << std::endl; |
| 32 | return 1; |
| 33 | } |
| 34 | auto rs_out = absl::GetFlag(FLAGS_rs_out); |
| 35 | if (rs_out.empty()) { |
| 36 | std::cerr << "please specify --rs_out" << std::endl; |
| 37 | return 1; |
| 38 | } |
| 39 | |
| 40 | auto status_or_cc_file_content = crubit::GetFileContents(cc_in); |
Dmitri Gribenko | 37e1942 | 2023-07-17 04:33:44 -0700 | [diff] [blame] | 41 | CHECK_OK(status_or_cc_file_content); |
Marco Poletti | b5239c9 | 2022-05-11 09:46:05 -0700 | [diff] [blame] | 42 | std::string cc_file_content = std::move(*status_or_cc_file_content); |
| 43 | |
| 44 | // Skip $0. |
| 45 | ++argv; |
| 46 | |
| 47 | absl::StatusOr<std::string> rs_code = crubit_rs_from_cc::RsFromCc( |
| 48 | cc_file_content, cc_in, |
| 49 | std::vector<absl::string_view>(argv, argv + argc)); |
Dmitri Gribenko | 37e1942 | 2023-07-17 04:33:44 -0700 | [diff] [blame] | 50 | CHECK_OK(rs_code); |
Marco Poletti | b5239c9 | 2022-05-11 09:46:05 -0700 | [diff] [blame] | 51 | |
Dmitri Gribenko | 37e1942 | 2023-07-17 04:33:44 -0700 | [diff] [blame] | 52 | CHECK_OK(crubit::SetFileContents(rs_out, *rs_code)); |
Marco Poletti | b5239c9 | 2022-05-11 09:46:05 -0700 | [diff] [blame] | 53 | return 0; |
| 54 | } |