From: "Eregon (Benoit Daloze) via ruby-core" Date: 2024-01-08T13:30:29+00:00 Subject: [ruby-core:116064] [Ruby master Feature#20152] mkmf / extconf: Add a proper way to not compile the extension Issue #20152 has been updated by Eregon (Benoit Daloze). jeremyevans0 (Jeremy Evans) wrote in #note-10: > I agree that this isn't a `mkmf`/`extconf.rb` issue. This is a rubygems issue. I think the easiest way to fix this would be for rubygems to check for a `skip-compilation-RUBY_ENGINE` file in the same directory as `extconf.rb`. If it exists, do not run `extconf.rb` or `make`. If it exists after calling `extconf.rb`, do not call `make`. This fixes cases where an extension is not needed for a certain engine, as well as cases where an extension is not needed in certain configurations, such as when a pure-ruby implementation is also shipped in the same gem, and a native library necessary for the native extension is not available. I think `skip-compilation-RUBY_ENGINE` is going too far and counter-productive. For instance I have seen some C extensions in gems which realistically will only work on CRuby (e.g. depends on CRuby deep internals, on CRuby bytecode, optimization only profitable on CRuby, etc), in that case we'd want to avoid the extension on any `RUBY_ENGINE != "ruby"`, so a `skip-compilation-RUBY_ENGINE` would not help. There could be a `skip-make`/`skip-compilation` or so file which does not include `RUBY_ENGINE` in the filename and is created dynamically by the `extconf.rb`, but this will take a lot longer to get adopted, as it means each gem skipping compilation would need to use that. But it is error-prone, because e.g. then if one builds (e.g. `rake compile`) a gem locally on JRuby and then on CRuby then compilation will be unexpectedly skipped on CRuby, unless the code also takes care of removing that file (I guess mkmf create_makefile could remove it, it seems too annoying if `extconf.rb` or `Rakefile` needs to remove it manually). OTOH if we detect the `dummy_makefile` pattern, it's immediately picked up as soon as that RubyGems change is in, and JRuby could even cherry-pick it sooner if they care enough about it. That is why I think this is the best solution as it solves it with very few changes and faster. Another possibility would be if there is no Makefile created by extconf.rb, then RubyGems would not call `make` (`make` in a dir with no Makefile is an error so we cannot just use that). It's very simple and I think rather intuitive. And extconf.rb could just e.g. `return if RUBY_ENGINE == "jruby"/RUBY_ENGINE != "ruby"/...` at the top. A concern is if one builds (e.g. `rake compile`) a gem locally on CRuby and then on JRuby. But `rake clean` should already remove the Makefile and so that seems no problem. It is already expected and necessary that one needs to `rake clean` when switching Rubies before `rake compile`. kou (Kouhei Sutou) wrote in #note-9: > Really? https://rubyinstaller.org/ provides MSYS2 integration by "Devkit". It provides `make`. It's mostly relevant for JRuby, it's basically not relevant for CRuby (where it's very common to have non-precompiled native extensions, like `erb`). ---------------------------------------- Feature #20152: mkmf / extconf: Add a proper way to not compile the extension https://bugs.ruby-lang.org/issues/20152#change-106062 * Author: byroot (Jean Boussier) * Status: Open * Priority: Normal ---------------------------------------- ### Context There are various gems that ship with a native extension as a way to speedup part of the gem, but also ship with a pure Ruby version of these methods as a fallback. So they only want to compile the extension if the platform supports it, and if not, just fallback to the slightly slower Ruby version. Right now users rely on one of two hacks to do this. Either they create an empty Makefile, but then still depend on `make` being available, or publish platform specific packages without any extension in them. Examples: - [`bootsnap` skip compilation if not on MRI or TruffleRuby](https://github.com/Shopify/bootsnap/blob/070151f1305f23102365d6b4476a91c02dead35a/ext/bootsnap/extconf.rb) - [`erb` has an extension for MRI but then need to publish a `java` version of the gem that doesn't actually contain Java code, just to skip compilation on JRuby](https://github.com/ruby/erb/issues/52) - [`hiredis-client` skips the compilation for Windows and non-MRI rubies](https://github.com/redis-rb/redis-client/blob/1ab081c1d0e47df5d55e011c9390c70b2eef6731/hiredis-client/ext/redis_client/hiredis/extconf.rb#L10-L17) ### Feature It would be very useful to have some proper first class API to skip compiling the extension. Something like: ```ruby require "mkmf" if RUBY_ENGINE != "ruby" || RUBY_PLATFORM.match?(/mswin/) skip_compilation else # ... end ``` cc @k0kubun @headius -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/