blob: dd03fac3d44d37a808366b2b580ebb6b8db42dec [file] [log] [blame]
// Part of the Crubit project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// Parses C++ code and generates an equivalent Rust source file.
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "absl/log/check.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "common/file_io.h"
#include "migrator/rs_from_cc/rs_from_cc_lib.h"
ABSL_FLAG(std::string, cc_in, "",
"input path for the C++ source file (it may or may not be a header)");
ABSL_FLAG(std::string, rs_out, "",
"output path for the Rust source file; will be overwritten if it "
"already exists");
int main(int argc, char* argv[]) {
auto args = absl::ParseCommandLine(argc, argv);
auto cc_in = absl::GetFlag(FLAGS_cc_in);
if (cc_in.empty()) {
std::cerr << "please specify --cc_in" << std::endl;
return 1;
}
auto rs_out = absl::GetFlag(FLAGS_rs_out);
if (rs_out.empty()) {
std::cerr << "please specify --rs_out" << std::endl;
return 1;
}
auto status_or_cc_file_content = crubit::GetFileContents(cc_in);
CHECK_OK(status_or_cc_file_content);
std::string cc_file_content = std::move(*status_or_cc_file_content);
// Skip $0.
++argv;
absl::StatusOr<std::string> rs_code = crubit_rs_from_cc::RsFromCc(
cc_file_content, cc_in,
std::vector<absl::string_view>(argv, argv + argc));
CHECK_OK(rs_code);
CHECK_OK(crubit::SetFileContents(rs_out, *rs_code));
return 0;
}