blob: f5f5067ee3ba687bab17217f3cb0424a25478902 [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
#![feature(rustc_private)]
#![deny(rustc::internal)]
extern crate rustc_driver;
extern crate rustc_session;
use anyhow::{bail, ensure, Result};
use clap::Parser;
use rustc_session::config::ErrorOutputType;
use rustc_session::EarlyDiagCtxt;
use std::path::PathBuf;
#[derive(Debug, Parser)]
#[clap(name = "cc_bindings_from_rs")]
#[clap(about = "Generates C++ bindings for a Rust crate", long_about = None)]
pub struct Cmdline {
/// Output path for C++ header file with bindings.
#[clap(long, value_parser, value_name = "FILE")]
pub h_out: PathBuf,
/// Output path for Rust implementation of the bindings.
#[clap(long, value_parser, value_name = "FILE")]
pub rs_out: PathBuf,
/// This is the format to `#include` Crubit C++ support library headers,
/// using `{header}` as the placeholder. Example:
/// `<crubit/support/{header}>` will produce `#include
/// <crubit/support/hdr.h>`.
#[clap(long, value_parser = validate_crubit_support_path_format, value_name = "STRING")]
pub crubit_support_path_format: String,
/// Path to a clang-format executable that will be used to format the
/// C++ header files generated by the tool.
#[clap(long, value_parser, value_name = "FILE")]
pub clang_format_exe_path: PathBuf,
/// Include paths of bindings for dependencies of the current crate
/// (generated by previous invocations of the tool).
/// Example: "--bindings-from-dependency=foo=some/path/foo_cc_api.h".
#[clap(long = "bindings-from-dependency", value_parser = parse_bindings_from_dependency,
value_name = "CRATE_NAME=INCLUDE_PATH")]
// TODO(b/271857814): A `CRATE_NAME` might not be globally unique - the key needs to also cover
// a "hash" of the crate version and compilation flags.
pub bindings_from_dependencies: Vec<(String, String)>,
/// Path to a rustfmt executable that will be used to format the
/// Rust source files generated by the tool.
#[clap(long, value_parser, value_name = "FILE")]
pub rustfmt_exe_path: PathBuf,
/// Path to a rustfmt.toml file that should replace the
/// default formatting of the .rs files generated by the tool.
#[clap(long, value_parser, value_name = "FILE")]
pub rustfmt_config_path: Option<PathBuf>,
/// Command line arguments of the Rust compiler.
#[clap(last = true, value_parser)]
pub rustc_args: Vec<String>,
}
impl Cmdline {
pub fn new(args: &[String]) -> Result<Self> {
assert_ne!(
0,
args.len(),
"`args` should include the name of the executable (i.e. argsv[0])"
);
let exe_name = args[0].clone();
let early_error_handler = EarlyDiagCtxt::new(ErrorOutputType::default());
// Ensure that `@file` expansion also covers *our* args.
//
// TODO(b/254688847): Decide whether to replace this with a `clap`-declared,
// `--help`-exposed `--flagfile <path>`.
let args = match rustc_driver::args::arg_expand_all(&early_error_handler, args) {
Ok(args) => args,
Err(_) => bail!("Error from rustc_driver::args::arg_expand_all"),
};
// Parse `args` using the parser `derive`d by the `clap` crate.
let mut cmdline = Self::try_parse_from(args)?;
// For compatibility with `rustc_driver` expectations, we prepend `exe_name` to
// `rustc_args. This is needed, because `rustc_driver::RunCompiler::new`
// expects that its `at_args` includes the name of the executable -
// `handle_options` in `rustc_driver/src/lib.rs` throws away the first
// element.
cmdline.rustc_args.insert(0, exe_name);
Ok(cmdline)
}
}
fn validate_crubit_support_path_format(s: &str) -> Result<String> {
ensure!(s.contains("{header}"), "Cannot find placeholder `{{header}}`");
Ok(s.to_string())
}
/// Parse cmdline arguments of the following form:`"crateName=includePath"`.
///
/// Adapted from
/// https://github.com/clap-rs/clap/blob/cc1474f97c78002f3d99261699114e61d70b0634/examples/typed-derive.rs#L47-L59
fn parse_bindings_from_dependency(s: &str) -> Result<(String, String)> {
let Some(pos) = s.find('=') else {
bail!("Expected KEY=VALUE syntax but no `=` found in `{s}`");
};
let crate_name = &s[..pos];
ensure!(!crate_name.is_empty(), "Empty crate names are invalid");
let include = &s[(pos + 1)..];
ensure!(!include.is_empty(), "Empty include paths are invalid");
Ok((crate_name.to_string(), include.to_string()))
}
#[cfg(test)]
mod tests {
use super::*;
use itertools::Itertools;
use std::path::Path;
use tempfile::tempdir;
fn new_cmdline<'a>(args: impl IntoIterator<Item = &'a str>) -> Result<Cmdline> {
// When `Cmdline::new` is invoked from `main.rs`, it includes not only the
// "real" cmdline arguments, but also the name of the executable.
let args = std::iter::once("cc_bindings_from_rs_unittest_executable")
.chain(args)
.map(|s| s.to_string())
.collect_vec();
Cmdline::new(&args)
}
#[test]
fn test_happy_path() {
let cmdline = new_cmdline([
"--h-out=foo.h",
"--rs-out=foo_impl.rs",
"--crubit-support-path-format=<crubit/support/{header}>",
"--clang-format-exe-path=clang-format.exe",
"--rustfmt-exe-path=rustfmt.exe",
])
.unwrap();
assert_eq!(Path::new("foo.h"), cmdline.h_out);
assert_eq!(Path::new("foo_impl.rs"), cmdline.rs_out);
assert_eq!("<crubit/support/{header}>", cmdline.crubit_support_path_format.as_str());
assert_eq!(Path::new("clang-format.exe"), cmdline.clang_format_exe_path);
assert_eq!(Path::new("rustfmt.exe"), cmdline.rustfmt_exe_path);
assert!(cmdline.bindings_from_dependencies.is_empty());
assert!(cmdline.rustfmt_config_path.is_none());
// Ignoring `rustc_args` in this test - they are covered in a separate
// test below: `test_rustc_args_happy_path`.
}
#[test]
fn test_rustc_args_happy_path() {
// Note that this test would fail without the `--` separator.
let cmdline = new_cmdline([
"--h-out=foo.h",
"--rs-out=foo_impl.rs",
"--crubit-support-path-format=<crubit/support/{header}>",
"--clang-format-exe-path=clang-format.exe",
"--rustfmt-exe-path=rustfmt.exe",
"--",
"test.rs",
"--crate-type=lib",
])
.unwrap();
let rustc_args = &cmdline.rustc_args;
assert!(
itertools::equal(
["cc_bindings_from_rs_unittest_executable", "test.rs", "--crate-type=lib"],
rustc_args
),
"rustc_args = {:?}",
rustc_args,
);
}
/// The `test_help` unit test below has multiple purposes:
/// - Direct/obvious purpose: testing that `--help` works
/// - Double-checking the overall shape of our cmdline "API" (i.e.
/// verification that the way we use `clap` attributes results in the
/// desired cmdline "API"). This is a good enough coverage to avoid having
/// flag-specifc tests (e.g. avoiding hypothetical
/// `test_h_out_missing_flag`, `test_h_out_missing_arg`,
/// `test_h_out_duplicated`).
/// - Exhaustively checking runtime asserts (assumming that tests run in a
/// debug build; other tests also trigger these asserts). See also:
/// - https://github.com/clap-rs/clap/issues/2740#issuecomment-907240414
/// - `clap::builder::App::debug_assert`
///
/// To regenerate `expected_msg` do the following steps:
/// - Run `bazel run :cc_bindings_from_rs -- --help`
/// - Copy&paste the output of the command below
/// - Replace the 2nd `cc_bindings_from_rs` with
/// `cc_bindings_from_rs_unittest_executable`
#[test]
fn test_help() {
let anyhow_err = new_cmdline(["--help"]).expect_err("--help should trigger an error");
let clap_err = anyhow_err.downcast::<clap::Error>().unwrap();
let expected_msg = r#"Generates C++ bindings for a Rust crate
Usage: cc_bindings_from_rs_unittest_executable [OPTIONS] --h-out <FILE> --rs-out <FILE> --crubit-support-path-format <STRING> --clang-format-exe-path <FILE> --rustfmt-exe-path <FILE> [-- <RUSTC_ARGS>...]
Arguments:
[RUSTC_ARGS]... Command line arguments of the Rust compiler
Options:
--h-out <FILE>
Output path for C++ header file with bindings
--rs-out <FILE>
Output path for Rust implementation of the bindings
--crubit-support-path-format <STRING>
This is the format to `#include` Crubit C++ support library headers, using `{header}` as the placeholder. Example: `<crubit/support/{header}>` will produce `#include <crubit/support/hdr.h>`
--clang-format-exe-path <FILE>
Path to a clang-format executable that will be used to format the C++ header files generated by the tool
--bindings-from-dependency <CRATE_NAME=INCLUDE_PATH>
Include paths of bindings for dependencies of the current crate (generated by previous invocations of the tool). Example: "--bindings-from-dependency=foo=some/path/foo_cc_api.h"
--rustfmt-exe-path <FILE>
Path to a rustfmt executable that will be used to format the Rust source files generated by the tool
--rustfmt-config-path <FILE>
Path to a rustfmt.toml file that should replace the default formatting of the .rs files generated by the tool
-h, --help
Print help
"#;
let actual_msg = clap_err.to_string();
assert_eq!(
expected_msg, actual_msg,
"Unexpected --help output\n\
EXPECTED OUTPUT:\n\
{expected_msg}\n\
ACTUAL OUTPUT:\n\
{actual_msg}"
);
}
#[test]
fn test_here_file() -> anyhow::Result<()> {
let tmpdir = tempdir()?;
let tmpfile = tmpdir.path().join("herefile");
let file_lines = vec![
"--h-out=foo.h",
"--rs-out=foo_impl.rs",
"--crubit-support-path-format=<crubit/support/{header}>",
"--clang-format-exe-path=clang-format.exe",
"--rustfmt-exe-path=rustfmt.exe",
"--",
"test.rs",
"--crate-type=lib",
];
std::fs::write(&tmpfile, file_lines.as_slice().join("\n"))?;
let flag_file_arg = format!("@{}", tmpfile.display());
let cmdline = new_cmdline([flag_file_arg.as_str()]).unwrap();
assert_eq!(Path::new("foo.h"), cmdline.h_out);
assert_eq!(Path::new("foo_impl.rs"), cmdline.rs_out);
let rustc_args = &cmdline.rustc_args;
assert!(
itertools::equal(
["cc_bindings_from_rs_unittest_executable", "test.rs", "--crate-type=lib"],
rustc_args
),
"rustc_args = {:?}",
rustc_args,
);
Ok(())
}
#[test]
fn test_bindings_from_dependencies_as_multiple_separate_cmdline_args() {
let cmdline = new_cmdline([
"--h-out=foo.h",
"--rs-out=foo_impl.rs",
"--crubit-support-path-format=<crubit/support/{header}>",
"--clang-format-exe-path=clang-format.exe",
"--rustfmt-exe-path=rustfmt.exe",
"--bindings-from-dependency=dep1=path1",
"--bindings-from-dependency=dep2=path2",
])
.unwrap();
assert_eq!(2, cmdline.bindings_from_dependencies.len());
assert_eq!("dep1", cmdline.bindings_from_dependencies[0].0);
assert_eq!("path1", cmdline.bindings_from_dependencies[0].1);
assert_eq!("dep2", cmdline.bindings_from_dependencies[1].0);
assert_eq!("path2", cmdline.bindings_from_dependencies[1].1);
}
#[test]
fn test_parse_bindings_from_dependency() {
assert_eq!(
parse_bindings_from_dependency("foo=bar").unwrap(),
("foo".into(), "bar".into()),
);
assert_eq!(
parse_bindings_from_dependency("").unwrap_err().to_string(),
"Expected KEY=VALUE syntax but no `=` found in ``",
);
assert_eq!(
parse_bindings_from_dependency("no-equal-char").unwrap_err().to_string(),
"Expected KEY=VALUE syntax but no `=` found in `no-equal-char`",
);
assert_eq!(
parse_bindings_from_dependency("=bar").unwrap_err().to_string(),
"Empty crate names are invalid",
);
assert_eq!(
parse_bindings_from_dependency("foo=").unwrap_err().to_string(),
"Empty include paths are invalid",
);
}
#[test]
fn test_crubit_support_path_format_arg_happy_path() {
let cmdline = new_cmdline([
"--h-out=foo.h",
"--rs-out=foo_impl.rs",
"--crubit-support-path-format=<crubit/support/{header}>",
"--clang-format-exe-path=clang-format.exe",
"--rustfmt-exe-path=rustfmt.exe",
"--bindings-from-dependency=dep1=path1",
])
.unwrap();
assert_eq!("<crubit/support/{header}>", cmdline.crubit_support_path_format.as_str());
}
#[test]
fn test_crubit_support_path_format_arg_no_placeholder() {
let anyhow_err = new_cmdline([
"--h-out=foo.h",
"--rs-out=foo_impl.rs",
"--crubit-support-path-format=\"crubit/support\"",
"--clang-format-exe-path=clang-format.exe",
"--rustfmt-exe-path=rustfmt.exe",
"--bindings-from-dependency=dep1=path1",
])
.expect_err("crubit-support-path without `{header}` should trigger an error");
let clap_err = anyhow_err.downcast::<clap::Error>().unwrap();
let expected_msg = "Cannot find placeholder `{header}`";
assert!(clap_err.to_string().contains(expected_msg));
}
}