rust-lang / rust-bindgen

Automatically generates Rust FFI bindings to C (and some C++) libraries.
https://rust-lang.github.io/rust-bindgen/
BSD 3-Clause "New" or "Revised" License
4.23k stars 679 forks source link

Unable to generate bindings for Arduino library IRremote.hpp on MacOS #2830

Open snowwolf007cn opened 1 month ago

snowwolf007cn commented 1 month ago

Input C/C++ Header

Since the header file generated has 15000+ lines, I won't copy it here, you can find the repository Arduino-IRremote

Bindgen Invocation

fn configure_bindgen_for_arduino(config: &Config) -> Builder {
    let mut builder = Builder::default();
    for (k, v) in &config.definitions {
        builder = builder.clang_arg(&format!("-D{}={}", k, v));
    }
    for flag in &config.flags {
        builder = builder.clang_arg(flag);
    }
    builder = builder
        .clang_args(&["-x", "c++"])
        .use_core()
        .header("wrapper.h")
        .layout_tests(false)
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()));

    for include_dir in config.include_dirs() {
        builder = builder.clang_arg(&format!("-I{}", include_dir.to_string_lossy()));
    }

    for item in &config.bindgen_lists.allowlist_function {
        builder = builder.allowlist_function(item);
    }

    for item in &config.bindgen_lists.allowlist_type {
        builder = builder.allowlist_type(item);
    }

    for item in &config.bindgen_lists.blocklist_function {
        builder = builder.blocklist_function(item);
    }

    for item in &config.bindgen_lists.blocklist_type {
        builder = builder.blocklist_type(item);
    }

    builder
}

fn generate_bindings(config: &Config) {
    let bindings: Bindings = configure_bindgen_for_arduino(&config)
        .generate()
        .expect("Unable to generate bindings");
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");
}

definitions and flags

definitions:
  ARDUINO: "10807"
  F_CPU: 16000000L
  ARDUINO_AVR_UNO: "1"
  ARDUINO_ARCH_AVR: "1"
flags:
  - "-mmcu=atmega328p"
# binding filter
bindgen_lists:
  allowlist_function:
    # - Arduino.*
    - Servo.*
    - IRsend.*
  allowlist_type:
    - Servo.*
    - IRsend.*
  blocklist_function:
    - Print.*
    - String.*
    - std::.*
  blocklist_type:
    - Print.*
    - String.*
    - std::.*

wrapper.h

#include <Arduino.h>
#include <IRremote.hpp>

Actual Results

--- stderr
  clang: warning: argument unused during compilation: '-mmcu=atmega328p' [-Wunused-command-line-argument]
  In file included from __bindgen.cpp:1:
  In file included from ./wrapper.h:1:
  In file included from /Users/zhangzhi/Library/Arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/Arduino.h:28:
  In file included from /Users/zhangzhi/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/avr/include/avr/pgmspace.h:90:
  /Users/zhangzhi/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/avr/include/avr/io.h:711:6: warning: "device type not defined" [-W#warnings]
  #    warning "device type not defined"
       ^
  In file included from __bindgen.cpp:1:
  In file included from ./wrapper.h:1:
  In file included from /Users/zhangzhi/Library/Arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/Arduino.h:234:
  In file included from /Users/zhangzhi/Library/Arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/USBAPI.h:25:
  /Users/zhangzhi/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/avr/include/avr/eeprom.h:41:3: warning: "Device does not have EEPROM available." [-W#warnings]
  # warning "Device does not have EEPROM available."
    ^
  In file included from __bindgen.cpp:1:
  In file included from ./wrapper.h:1:
  In file included from /Users/zhangzhi/Library/Arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/Arduino.h:234:
  In file included from /Users/zhangzhi/Library/Arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/USBAPI.h:27:
  /Users/zhangzhi/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/avr/include/util/delay.h:112:3: warning: "Compiler optimizations disabled; functions from <util/delay.h> won't work as designed" [-W#warnings]
  # warning "Compiler optimizations disabled; functions from <util/delay.h> won't work as designed"
    ^
  In file included from __bindgen.cpp:1:
  In file included from ./wrapper.h:3:
  In file included from /Users/zhangzhi/Documents/Arduino/libraries/IRremote/src/IRremote.hpp:264:
  /Users/zhangzhi/Documents/Arduino/libraries/IRremote/src/private/IRTimer.hpp:1987:2: error: Internal code configuration error, no timer functions implemented for this CPU / board
  #error Internal code configuration error, no timer functions implemented for this CPU / board
   ^
  3 warnings and 1 error generated.
  thread 'main' panicked at build.rs:235:39:
  called `Result::unwrap()` on an `Err` value: Custom { kind: Other, error: "clang exited with non-zero status" }
  stack backtrace:
     0:        0x108ec4b63 - std::backtrace_rs::backtrace::libunwind::trace::h03136b7b4ce63c8d
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
     1:        0x108ec4b63 - std::backtrace_rs::backtrace::trace_unsynchronized::he50d19b877e32d81
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
     2:        0x108ec4b63 - std::sys_common::backtrace::_print_fmt::ha128008280351119
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/sys_common/backtrace.rs:68:5
     3:        0x108ec4b63 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h84ee1fd57c46bba1
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/sys_common/backtrace.rs:44:22
     4:        0x108eeaaeb - core::fmt::rt::Argument::fmt::hf4a4a456fa7ca3e0
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/core/src/fmt/rt.rs:142:9
     5:        0x108eeaaeb - core::fmt::write::h123b335250020b84
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/core/src/fmt/mod.rs:1153:17
     6:        0x108ec171e - std::io::Write::write_fmt::hda015eca4cf47a4e
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/io/mod.rs:1843:15
     7:        0x108ec4951 - std::sys_common::backtrace::_print::h7323b605ff848043
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/sys_common/backtrace.rs:47:5
     8:        0x108ec4951 - std::sys_common::backtrace::print::ha8973aaaf4ba8d60
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/sys_common/backtrace.rs:34:9
     9:        0x108ec6649 - std::panicking::default_hook::{{closure}}::h75f2ccfa168dcb7c
    10:        0x108ec63b0 - std::panicking::default_hook::h00c64a52932be1d3
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/panicking.rs:292:9
    11:        0x108ec708e - std::panicking::rust_panic_with_hook::hfde80e8a46eed7c0
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/panicking.rs:779:13
    12:        0x108ec69e4 - std::panicking::begin_panic_handler::{{closure}}::hff01ef6aad810b31
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/panicking.rs:657:13
    13:        0x108ec5039 - std::sys_common::backtrace::__rust_end_short_backtrace::h330cba627087babe
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/sys_common/backtrace.rs:171:18
    14:        0x108ec6716 - rust_begin_unwind
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/panicking.rs:645:5
    15:        0x108f0c835 - core::panicking::panic_fmt::h2a011b097ccd0049
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/core/src/panicking.rs:72:14
    16:        0x108f0ce15 - core::result::unwrap_failed::hea2c2659c9676c1d
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/core/src/result.rs:1654:5
    17:        0x10881a0c2 - core::result::Result<T,E>::unwrap::h52e532e81b022259
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/core/src/result.rs:1077:23
    18:        0x10881a0c2 - build_script_build::configure_bindgen_for_arduino::h64bc02a44425595f
                                 at /Users/zhangzhi/workspace/rust/arduino/tutorial/arduino_uno_example/build.rs:235:5
    19:        0x10881a5ea - build_script_build::generate_bindings::hbc9f012bdfac1aab
                                 at /Users/zhangzhi/workspace/rust/arduino/tutorial/arduino_uno_example/build.rs:241:30
    20:        0x108817255 - build_script_build::main::h43213882a8b71a63
                                 at /Users/zhangzhi/workspace/rust/arduino/tutorial/arduino_uno_example/build.rs:27:5
    21:        0x10881532e - core::ops::function::FnOnce::call_once::h8f814b1c2ecb4147
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/core/src/ops/function.rs:250:5
    22:        0x10880c9b1 - std::sys_common::backtrace::__rust_begin_short_backtrace::h75f3245420fd938c
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/sys_common/backtrace.rs:155:18
    23:        0x10880a5c4 - std::rt::lang_start::{{closure}}::h1c65cce0f3365ab2
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/rt.rs:166:18
    24:        0x108ebcc40 - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h31936847bb2ea9da
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/core/src/ops/function.rs:284:13
    25:        0x108ebcc40 - std::panicking::try::do_call::hd0b7a38aaf21d85e
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/panicking.rs:552:40
    26:        0x108ebcc40 - std::panicking::try::he975670dbe333245
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/panicking.rs:516:19
    27:        0x108ebcc40 - std::panic::catch_unwind::hfeafa5faebbec479
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/panic.rs:146:14
    28:        0x108ebcc40 - std::rt::lang_start_internal::{{closure}}::h143264f88c4ac43b
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/rt.rs:148:48
    29:        0x108ebcc40 - std::panicking::try::do_call::h8214801e1afde609
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/panicking.rs:552:40
    30:        0x108ebcc40 - std::panicking::try::hc61b00624572adb5
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/panicking.rs:516:19
    31:        0x108ebcc40 - std::panic::catch_unwind::h02bc6883fb1430ae
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/panic.rs:146:14
    32:        0x108ebcc40 - std::rt::lang_start_internal::h8309c4bfb14094e1
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/rt.rs:148:20
    33:        0x10880a597 - std::rt::lang_start::h51b4cbc837c762d5
                                 at /rustc/0ad927c0c07b65fc0dae37105e09c877c87c296a/library/std/src/rt.rs:165:17
    34:        0x10881ab98 - _main

Expected Results

generate bindings

pvdrz commented 1 month ago

I suspect this is a misconfiguration of your development environment as this error comes from clang itself, not from bindgen.

From the stderr dump, it is clear that clang doesn't understand the -mmcu=atmega328p flag and the subsequent errors seem to be a consequence of this.