kwilczynski / ruby-magic

Simple interface to libmagic for Ruby Programming Language
Apache License 2.0
27 stars 8 forks source link

Native support for Ruby 3.1 #29

Closed flavorjones closed 2 years ago

flavorjones commented 2 years ago

I started working on adding native (precompiled) support for Ruby 3.1. Unfortunately it appears that Ruby 3.1 introduced a change that breaks how we use mkmf's pkg_config method, so I'm creating this issue to make that issue visible.

In summary, ruby-magic's extconf.rb was relying on the ability to pass multiple flags:

https://github.com/kwilczynski/ruby-magic/blob/f1a875bfdf0f6f78d1a337879fa4436bf3512031/ext/magic/extconf.rb#L299-L301

Unfortunately, this commit in Ruby breaks the behavior we were relying on:

https://github.com/ruby/ruby/commit/dff8d12

and so as a result, the build breaks with this error output:

...
Building Ruby Magic with a packaged version of libmagic-5.39.
Activating libmagic 5.39 (from /home/flavorjones/code/oss/ruby-magic/ports/x86_64-redhat-linux/libmagic/5.39)...
*** ../../../../ext/magic/extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=../../../../ext/magic
    --curdir
    --ruby=/usr/local/rake-compiler/ruby/x86_64-redhat-linux/ruby-3.1.0/bin/$(RUBY_BASE_NAME)
    --help
    --clean
    --enable-system-libraries
    --disable-system-libraries
    --use-system-libraries
    --enable-static
    --enable-cross-build
    --with-libmagic-dir
    --without-libmagic-dir
    --with-libmagic-include
    --without-libmagic-include=${libmagic-dir}/include
    --with-libmagic-lib
    --without-libmagic-lib=${libmagic-dir}/lib
    --with-libmagic-config
    --without-libmagic-config
    --with-pkg-config
    --without-pkg-config
../../../../ext/magic/extconf.rb:304:in `<main>': Please install the `pkg-config` utility (RuntimeError)

and the contents of mkmf.log are:

LD_LIBRARY_PATH=.:/usr/local/rake-compiler/ruby/x86_64-redhat-linux/ruby-3.1.0/lib:/opt/rh/devtoolset-10/root/usr/lib64:/opt/rh/devtoolset-10/root/usr/lib:/opt/rh/devtoolset-10/root/usr/lib64/dyninst:/opt/rh/devtoolset-10/root/usr/lib/dyninst:/usr/local/lib64 pkg-config --exists libmagic
LD_LIBRARY_PATH=.:/usr/local/rake-compiler/ruby/x86_64-redhat-linux/ruby-3.1.0/lib:/opt/rh/devtoolset-10/root/usr/lib64:/opt/rh/devtoolset-10/root/usr/lib:/opt/rh/devtoolset-10/root/usr/lib64/dyninst:/opt/rh/devtoolset-10/root/usr/lib/dyninst:/usr/local/lib64 pkg-config --libs --static libmagic |
=> "Unknown option --libs --static\n"

I think the right thing to do is to submit a PR upstream to Ruby to allow passing multiple options to pkg_config, but that will take some time. We could also monkeypatch pkg_config but it's a rather long and complicated method and so that may be a bit risky.

I'm curious if anybody has any other clever ideas for working around this? I do believe we need to pass both --libs and --static to pkg_config to get the right flags for the ruby-magic build, though I guess we could work around it for now by passing just --libs and then hardcoding -lz.

flavorjones commented 2 years ago

Note that #30 takes the approached mentioned last above, passing just --libs to pkg-config and hardcoding -lz.

flavorjones commented 2 years ago

I'm working on a PR to send upstream to Ruby for mkmf pkg-config support.

stanhu commented 2 years ago

Thanks @flavorjones! That seems like the right approach. I don't see a way to inject multiple arguments now.

flavorjones commented 2 years ago

See upstream PR at https://github.com/ruby/ruby/pull/5436. If that goes green I'll open a new issue at https://bugs.ruby-lang.org/ for review.

kwilczynski commented 2 years ago

Hi @flavorjones,

Thank you for the heads up! And for working towards a fix (both here and in the upstream Ruby). I really appreciate that!

@stanhu thank you for looking into this!

I had a brief look a the pkg-config Ruby Gem, however, it does not offer what we need here.

flavorjones commented 2 years ago

PR submitted upstream at https://bugs.ruby-lang.org/issues/18490

flavorjones commented 2 years ago

The upstream PR has been merged into 3.2.0.dev / ruby head. I've updated #32 to only monkeypatch Ruby 3.1.x.