Repository: rust-lang/rust-bindgen
Pull request #2751 (2024-02-07 10:25:04)

Add target mappings for `riscv64imac` and `riscv32imafc`
Similar change to #2551. Just filling in more mappings.

`riscv64imac` and `riscv32imafc` both appear in built-in targets (`riscv64imac-unknown-none-elf` and `riscv32imafc-unknown-none-elf`, respectively).


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 94b19f56..95b7ac29 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -204,6 +204,7 @@
 # Unreleased
 ## Added
 ## Changed
+- Add target mappings for riscv64imac and riscv32imafc.
 ## Removed
 ## Fixed
 ## Security
diff --git a/bindgen/lib.rs b/bindgen/lib.rs
index c4ab1148..f672f3ff 100644
--- a/bindgen/lib.rs
+++ b/bindgen/lib.rs
@@ -695,6 +695,11 @@ fn rust_to_clang_target(rust_target: &str) -> Box<str> {
         let mut clang_target = "riscv64-".to_owned();
         clang_target.push_str(rust_target.strip_prefix("riscv64gc-").unwrap());
         return clang_target.into();
+    } else if rust_target.starts_with("riscv64imac-") {
+        let mut clang_target = "riscv64-".to_owned();
+        clang_target
+            .push_str(rust_target.strip_prefix("riscv64imac-").unwrap());
+        return clang_target.into();
     } else if rust_target.ends_with("-espidf") {
         let mut clang_target =
             rust_target.strip_suffix("-espidf").unwrap().to_owned();
@@ -713,6 +718,11 @@ fn rust_to_clang_target(rust_target: &str) -> Box<str> {
         clang_target
             .push_str(rust_target.strip_prefix("riscv32imac-").unwrap());
         return clang_target.into();
+    } else if rust_target.starts_with("riscv32imafc-") {
+        let mut clang_target = "riscv32-".to_owned();
+        clang_target
+            .push_str(rust_target.strip_prefix("riscv32imafc-").unwrap());
+        return clang_target.into();
     }
     rust_target.into()
 }
@@ -1357,6 +1367,10 @@ fn test_rust_to_clang_target_riscv() {
         rust_to_clang_target("riscv64gc-unknown-linux-gnu").as_ref(),
         "riscv64-unknown-linux-gnu"
     );
+    assert_eq!(
+        rust_to_clang_target("riscv64imac-unknown-none-elf").as_ref(),
+        "riscv64-unknown-none-elf"
+    );
     assert_eq!(
         rust_to_clang_target("riscv32imc-unknown-none-elf").as_ref(),
         "riscv32-unknown-none-elf"
@@ -1365,6 +1379,10 @@ fn test_rust_to_clang_target_riscv() {
         rust_to_clang_target("riscv32imac-unknown-none-elf").as_ref(),
         "riscv32-unknown-none-elf"
     );
+    assert_eq!(
+        rust_to_clang_target("riscv32imafc-unknown-none-elf").as_ref(),
+        "riscv32-unknown-none-elf"
+    );
 }
 
 #[test]
diff --git a/bindgen/options/mod.rs b/bindgen/options/mod.rs
index 1fc22416..d6e7ad51 100644
--- a/bindgen/options/mod.rs
+++ b/bindgen/options/mod.rs
@@ -1179,6 +1179,35 @@ options! {
                 self.options.input_headers.push(header.into().into_boxed_str());
                 self
             }
+
+            /// Add input C/C++ header(s) to generate bindings for.
+            ///
+            /// This can be used to generate bindings for a single header:
+            ///
+            /// ```ignore
+            /// let bindings = bindgen::Builder::default()
+            ///     .headers(["input.h"])
+            ///     .generate()
+            ///     .unwrap();
+            /// ```
+            ///
+            /// Or for multiple headers:
+            ///
+            /// ```ignore
+            /// let bindings = bindgen::Builder::default()
+            ///     .headers(["first.h", "second.h", "third.h"])
+            ///     .generate()
+            ///     .unwrap();
+            /// ```
+            pub fn headers<I: IntoIterator>(mut self, headers: I) -> Builder
+            where
+                I::Item: Into<String>,
+            {
+                self.options
+                    .input_headers
+                    .extend(headers.into_iter().map(Into::into).map(Into::into));
+                self
+            }
         },
         // This field is handled specially inside the macro.
         as_args: ignore,


Test patch:
diff --git a/bindgen-tests/tests/tests.rs b/bindgen-tests/tests/tests.rs
index 9df45c3b..0e15155d 100644
--- a/bindgen-tests/tests/tests.rs
+++ b/bindgen-tests/tests/tests.rs
@@ -472,6 +472,42 @@ fn test_multiple_header_calls_in_builder() {
     }
 }
 
+#[test]
+fn test_headers_call_in_builder() {
+    let actual = builder()
+        .headers([
+            concat!(env!("CARGO_MANIFEST_DIR"), "/tests/headers/func_ptr.h"),
+            concat!(env!("CARGO_MANIFEST_DIR"), "/tests/headers/char.h"),
+        ])
+        .clang_arg("--target=x86_64-unknown-linux")
+        .generate()
+        .unwrap()
+        .to_string();
+
+    let actual = format_code(actual).unwrap();
+
+    let expected_filename = concat!(
+        env!("CARGO_MANIFEST_DIR"),
+        "/tests/expectations/tests/test_multiple_header_calls_in_builder.rs"
+    );
+    let expected = include_str!(concat!(
+        env!("CARGO_MANIFEST_DIR"),
+        "/tests/expectations/tests/test_multiple_header_calls_in_builder.rs"
+    ));
+    let expected = format_code(expected).unwrap();
+
+    if actual != expected {
+        println!("Generated bindings differ from expected!");
+        error_diff_mismatch(
+            &actual,
+            &expected,
+            None,
+            Path::new(expected_filename),
+        )
+        .unwrap();
+    }
+}
+
 #[test]
 fn test_multiple_header_contents() {
     let actual = builder()