Bindgen seems using wrong infrastructure

I am trying to port a C++ numerical library to Rust, using bindgen library. Yet I am seeing some weird errors from llvm, e.g.

  /Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/emmintrin.h:2378:19: error: use of undeclared identifier '__builtin_ia32_pmaxsw128'
  /Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include/emmintrin.h:2398:19: error: use of undeclared identifier '__builtin_ia32_pmaxub128'

which implies I am using some SSE/AVX functions. I tried to forward -march=native to clang but the same errors are reported. My questions: 1) is there a way to figure out how llvm is configured in bindgen? 2) I am not sure if ia32 is meaningful here, as I am using a 2.3G 8core intel core i9 in a Mac.

From this it appears __builtin_ia32_pmaxsw128 was removed / replaced. My guess is that your clang needs to be updated.

Not sure, but I just installed llvm by

brew install llvm@14

Does it work of you use xcode's version of clang rather than the one installed using homebrew. It seems like it is mixing one version of clang with an incompatible version of the header files from xcode. Note that even though xcode's clang reports version 14 too, this is not accurate. Apple doesn't align with official releases of LLVM, but uses it's own fork of LLVM which branches before an official release and then applies bugfixes and other patches on top.

I searched for llvm-config in xcode, and did not find it. From xcode - Where is the system llvm-config on macOS? - Stack Overflow it seems xcode does not include LLVM tools. That is the reason I myself installed llvm@14 via brew. But it seems a clue -- I am using llvm@14 yet the include files are from xcode?

I see. I can't try it myself, but maybe using the llvm installed using homebrew in combination with setting the C_INCLUDE_PATH/ CPLUS_INCLUDE_PATH env var to the directory of the include files for the homebrew installed clang would work?

I think updating C_INCLUDE_PATH, CPLUS_INCLUDE_PATH, LIBRARY_PATH and PATH to corresponding llvm directory would solve this problem. Thanks all!

With the C_INCLUDE_PATH, CPLUS_INCLUDE_PATH set, the bindgen is generating Rust code that would not compile.

31357 | pub type rep = ::std::os::raw::c_longlong;
      | ------------------------------------------ previous definition of the type `rep` here
...
31401 | pub type rep = ::std::os::raw::c_longlong;
      | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `rep` redefined here
      |
      = note: `rep` must be defined only once in the type namespace of this module

error[E0428]: the name `type_` is defined multiple times
     --> src/c_bindings.rs:31462:1
      |
31422 | pub type type_ = ::std::os::raw::c_uint;
      | ---------------------------------------- previous definition of the type `type_` here
...
31462 | pub type type_ = ::std::os::raw::c_longlong;
      | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `type_` redefined here
      |
      = note: `type_` must be defined only once in the type namespace of this module

error[E0428]: the name `type_` is defined multiple times
     --> src/c_bindings.rs:31463:1
      |
31422 | pub type type_ = ::std::os::raw::c_uint;
      | ---------------------------------------- previous definition of the type `type_` here
...
31463 | pub type type_ = ::std::os::raw::c_ulonglong;
      | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `type_` redefined here
      |
      = note: `type_` must be defined only once in the type namespace of this module

error[E0428]: the name `char_type` is defined multiple times
     --> src/c_bindings.rs:31610:1
      |
31609 | pub type char_type = ::std::os::raw::c_char;
      | -------------------------------------------- previous definition of the type `char_type` here
31610 | pub type char_type = u32;
      | ^^^^^^^^^^^^^^^^^^^^^^^^^ `char_type` redefined here
      |
      = note: `char_type` must be defined only once in the type namespace of this module

error[E0428]: the name `char_type` is defined multiple times
     --> src/c_bindings.rs:31611:1
      |
31609 | pub type char_type = ::std::os::raw::c_char;
      | -------------------------------------------- previous definition of the type `char_type` here
31610 | pub type char_type = u32;
31611 | pub type char_type = u16;
      | ^^^^^^^^^^^^^^^^^^^^^^^^^ `char_type` redefined here
      |
      = note: `char_type` must be defined only once in the type namespace of this module

error[E0428]: the name `char_type` is defined multiple times
     --> src/c_bindings.rs:31612:1
      |
31609 | pub type char_type = ::std::os::raw::c_char;
      | -------------------------------------------- previous definition of the type `char_type` here
...
31612 | pub type char_type = u32;
      | ^^^^^^^^^^^^^^^^^^^^^^^^^ `char_type` redefined here
      |
      = note: `char_type` must be defined only once in the type namespace of this module

error[E0412]: cannot find type `_Tp` in this scope
    --> src/c_bindings.rs:1854:27
     |
1854 |     pub static std_value: _Tp;
     |                           ^^^ not found in this scope

error[E0412]: cannot find type `_Pred` in this scope
    --> src/c_bindings.rs:3166:41
     |
3166 | pub type std__IsCharLikeType = std__And<_Pred>;
     |                             -           ^^^^^ not found in this scope
     |                             |
     |                             help: you might be missing a type parameter: `<_Pred>`

error[E0412]: cannot find type `_Pred` in this scope
    --> src/c_bindings.rs:4400:25
     |
4399 | pub struct std_tuple__EnableUTypesCtor {
     |                                       - help: you might be missing a type parameter: `<_Pred>`
4400 |     pub _base: std__And<_Pred>,
     |                         ^^^^^ not found in this scope

error[E0412]: cannot find type `_Pred` in this scope
    --> src/c_bindings.rs:4405:25
     |
4404 | pub struct std_tuple__EnableCopyFromOtherTuple {
     |                                               - help: you might be missing a type parameter: `<_Pred>`
4405 |     pub _base: std__And<_Pred>,
     |                         ^^^^^ not found in this scope

error[E0412]: cannot find type `_Pred` in this scope
    --> src/c_bindings.rs:4410:25
     |
4409 | pub struct std_tuple__EnableMoveFromOtherTuple {
     |                                               - help: you might be missing a type parameter: `<_Pred>`
4410 |     pub _base: std__And<_Pred>,
     |                         ^^^^^ not found in this scope

error[E0412]: cannot find type `_Pred` in this scope
    --> src/c_bindings.rs:4415:25
     |
4414 | pub struct std_tuple__EnableImplicitCopyFromPair {
     |                                                 - help: you might be missing a type parameter: `<_Pred>`
4415 |     pub _base: std__And<_Pred>,
     |                         ^^^^^ not found in this scope

error[E0412]: cannot find type `_Pred` in this scope
    --> src/c_bindings.rs:4420:25
     |
4419 | pub struct std_tuple__EnableExplicitCopyFromPair {
     |                                                 - help: you might be missing a type parameter: `<_Pred>`
4420 |     pub _base: std__And<_Pred>,
     |                         ^^^^^ not found in this scope

error[E0412]: cannot find type `_Pred` in this scope
    --> src/c_bindings.rs:4425:25
     |
4424 | pub struct std_tuple__EnableImplicitMoveFromPair {
     |                                                 - help: you might be missing a type parameter: `<_Pred>`
4425 |     pub _base: std__And<_Pred>,
     |                         ^^^^^ not found in this scope

error[E0412]: cannot find type `_Pred` in this scope
    --> src/c_bindings.rs:4430:25
     |
4429 | pub struct std_tuple__EnableExplicitMoveFromPair {
     |                                                 - help: you might be missing a type parameter: `<_Pred>`
4430 |     pub _base: std__And<_Pred>,
     |                         ^^^^^ not found in this scope
...