matsadler / magnus

Ruby bindings for Rust. Write Ruby extension gems in Rust, or call Ruby from Rust.
https://docs.rs/magnus/latest/magnus/
MIT License
682 stars 35 forks source link

Clang bindgen on linux, windows #64

Closed SlashScreen closed 1 year ago

SlashScreen commented 1 year ago

Attempting to use this crate with the embed feature results in the following stack trace:

error: failed to run custom build command for `rb-sys v0.9.65`

Caused by:
  process didn't exit successfully: `/home/skyrbunny/Documents/Programs/Rust/rbquest/target/debug/build/rb-sys-e899aefe966f4115/build-script-main` (exit status: 101)
  --- stdout
  cargo:rerun-if-env-changed=RUBY
  cargo:rerun-if-env-changed=RBCONFIG_ruby_version
  cargo:rerun-if-env-changed=RBCONFIG_platform
  cargo:rerun-if-env-changed=RBCONFIG_arch
  cargo:rerun-if-env-changed=RUBY_ROOT
  cargo:rerun-if-env-changed=RUBY_VERSION
  cargo:rerun-if-env-changed=RUBY
  cargo:rerun-if-changed=build/features.rs
  cargo:rerun-if-changed=build/version.rs
  cargo:rerun-if-changed=build/main.rs
  cargo:rerun-if-changed=build/ruby_macros.rs
  cargo:rerun-if-env-changed=RUBY_STATIC
  cargo:rerun-if-env-changed=RBCONFIG_ENABLE_SHARED
  cargo:rerun-if-env-changed=RBCONFIG_rubyhdrdir
  cargo:rerun-if-env-changed=RBCONFIG_rubyarchhdrdir
  cargo:rerun-if-env-changed=RBCONFIG_CPPFLAGS

  --- stderr
  Using bindgen with clang args: ["-I/usr/include/ruby-3.0.0", "-I/usr/include/x86_64-linux-gnu/ruby-3.0.0", "-fms-extensions", "-O3", "-ggdb3", "-Wall", "-Wextra", "-Wdeprecated-declarations", "-Wduplicated-cond", "-Wimplicit-function-declaration", "-Wimplicit-int", "-Wmisleading-indentation", "-Wpointer-arith", "-Wwrite-strings", "-Wimplicit-fallthrough=0", "-Wmissing-noreturn", "-Wno-cast-function-type", "-Wno-constant-logical-operand", "-Wno-long-long", "-Wno-missing-field-initializers", "-Wno-overlength-strings", "-Wno-packed-bitfield-compat", "-Wno-parentheses-equality", "-Wno-self-assign", "-Wno-tautological-compare", "-Wno-unused-parameter", "-Wno-unused-value", "-Wsuggest-attribute=format", "-Wsuggest-attribute=noreturn", "-Wunused-variable", "-Wdate-time", "-D_FORTIFY_SOURCE=2"]
  warning: unknown warning option '-Wduplicated-cond' [-Wunknown-warning-option]
  warning: unknown warning option '-Wimplicit-fallthrough=0'; did you mean '-Wimplicit-fallthrough'? [-Wunknown-warning-option]
  warning: unknown warning option '-Wno-packed-bitfield-compat' [-Wunknown-warning-option]
  warning: unknown warning option '-Wsuggest-attribute=format'; did you mean '-Wproperty-attribute-mismatch'? [-Wunknown-warning-option]
  warning: unknown warning option '-Wsuggest-attribute=noreturn' [-Wunknown-warning-option]
  /home/skyrbunny/.cargo/registry/src/github.com-1ecc6299db9ec823/rb-sys-0.9.65/wrapper.h:1:10: fatal error: 'ruby.h' file not found
  clang diag: warning: unknown warning option '-Wduplicated-cond' [-Wunknown-warning-option]
  clang diag: warning: unknown warning option '-Wimplicit-fallthrough=0'; did you mean '-Wimplicit-fallthrough'? [-Wunknown-warning-option]
  clang diag: warning: unknown warning option '-Wno-packed-bitfield-compat' [-Wunknown-warning-option]
  clang diag: warning: unknown warning option '-Wsuggest-attribute=format'; did you mean '-Wproperty-attribute-mismatch'? [-Wunknown-warning-option]
  clang diag: warning: unknown warning option '-Wsuggest-attribute=noreturn' [-Wunknown-warning-option]
  thread 'main' panicked at 'generate bindings: ClangDiagnostic("/home/skyrbunny/.cargo/registry/src/github.com-1ecc6299db9ec823/rb-sys-0.9.65/wrapper.h:1:10: fatal error: 'ruby.h' file not found\n")', /home/skyrbunny/.cargo/registry/src/github.com-1ecc6299db9ec823/rb-sys-0.9.65/build/main.rs:53:6
  stack backtrace:
     0: rust_begin_unwind
               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:584:5
     1: core::panicking::panic_fmt
               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/panicking.rs:142:14
     2: core::result::unwrap_failed
               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/result.rs:1814:5
     3: core::result::Result<T,E>::expect
               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/result.rs:1064:23
     4: build_script_main::main
               at ./build/main.rs:48:25
     5: core::ops::function::FnOnce::call_once
               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/ops/function.rs:248:5
  note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

At first, I got errors about bindgen not finding clang, but after installing as per bindgen's instructions, it started saying these things. I did a sanity heck by running the same setup process on my linux machine, with the same result. I'm not sure how far down the chain of command this is, so I'm opening an issue here to get some pointers in the right direction.

matsadler commented 1 year ago

Hey, thanks for reporting this issue.

This looks like it is a problem building rb-sys. rb-sys is the low-level bindings direct to Ruby's C API and handles linking to Ruby (Magnus is a nice easy-to-use layer on top). You might get better help reporting this kind of issue there, though that project's maintainer, @ianks, is super helpful, so if I @ mention him here he might be happy to help you out here.

I can take a first stab at helping. The error seems to be it can't find the Ruby header file, ruby.h. Guessing from the file paths it looks like the error message is from a linux system with a Ruby installed via the distributions package manager? Many distributions have separate packages for the ruby binary and the Ruby headers, for example on Ubuntu there's the ruby package and then the headers are part of the ruby-dev package. Do you have ruby-dev (or your distribution's equivalent) installed?

SlashScreen commented 1 year ago

Oh, yes, I do believe I have ruby-dev installed, and both are installed via apt, if remember correctly (ubuntu).

matsadler commented 1 year ago

Humm, I'm not really sure why else it wouldn't find the Ruby headers.

rb-sys is running bindgen (which calls clang), and giving it the paths /usr/include/ruby-3.0.0 and /usr/include/x86_64-linux-gnu/ruby-3.0.0 to look for Ruby's headers. It gets these by asking Ruby, you can query the same thing with:

ruby -e 'p RbConfig::CONFIG.values_at("rubyhdrdir", "rubyarchhdrdir")'

Does the result of that match up with the directories in the error message? Is there actually a ruby.h in one of those directories?

Also, have you tried installing any Ruby C extension gems on this system, does that work? fast_blank is a good one to try, it's simple enough that there's not too many ways it could break, and it don't have pre-compiled versions so you could be sure it's building on your machine.

SlashScreen commented 1 year ago

I figured it out! I thought I had ruby-dev installed on this system, but I didn't. the rust build seems to pass now. Note to self: ruby-dev is not a gem. I figured it out while trying and failing to install fast_blank. thank you for your help!

matsadler commented 1 year ago

Great! I'm happy to have been of help.

SlashScreen commented 1 year ago

Note to anybody else having issues with this: If you're getting "error linking with cc" issues, just delete the target directory and have rust rebuild.