blob: 40e7cfdb8707f1392850271d5b14eff7237f163c [file] [log] [blame]
Marco Polettib5239c92022-05-11 09:46:05 -07001// 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
7#include <utility>
8#include <vector>
9
10#include "absl/flags/flag.h"
11#include "absl/flags/parse.h"
12#include "absl/status/status.h"
13#include "absl/status/statusor.h"
14#include "absl/strings/string_view.h"
15#include "common/check.h"
16#include "common/file_io.h"
17#include "migrator/rs_from_cc/rs_from_cc_lib.h"
18#include "llvm/Support/FileSystem.h"
19
20ABSL_FLAG(std::string, cc_in, "",
21 "input path for the C++ source file (it may or may not be a header)");
22ABSL_FLAG(std::string, rs_out, "",
23 "output path for the Rust source file; will be overwritten if it "
24 "already exists");
25
26int 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);
41 CRUBIT_CHECK(status_or_cc_file_content.ok());
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));
50 if (!rs_code.ok()) {
51 CRUBIT_CHECK(rs_code.ok());
52 }
53
54 CRUBIT_CHECK(crubit::SetFileContents(rs_out, *rs_code).ok());
55 return 0;
56}