Skip to content

Commit

Permalink
Extract rbconfig patch to rbconfig-patch.rb
Browse files Browse the repository at this point in the history
This way, the new values are available without a full `require 'mkmf'`.
Note that `rbconfig.rb` would hide the original `rbconfig` from the `$LOAD_PATH` which the current solution depends on to “require super”.
  • Loading branch information
ParadoxV5 committed May 27, 2024
1 parent 3c4e91a commit 0ae4bba
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ However, [__Minimum__ GNU for Windows (MinGW)](https://github.com/niXman/mingw-b
a key component of MSYS2, is sufficient for half of the jobs all on its own.

`mingw-make` is a set of tiny non-intrusive mods for [RubyInstaller2 for Windows](https://rubyinstaller.org)
that solves the hurdles on the other half minimally.
that solves the hurdles on the other half minimally, namely by replacing `rbconfig` entries of compilation commands.
With the aid of the secret [`ruby -run`](https://github.com/ruby/un) capability it has unleashed,
all it took was a couple of quick hacks and we have ourselves a lightweight Devkit.

Setups with MinGW and RubyInstaller’s prebuilds with these patches are ideal to keep footprints small,
for experts strongly recommend WSL for a fully-fledged Ruby environment.
though experts strongly recommend WSL for a fully-fledged Ruby environment.

### Compatibility

This is a newly published experiment; I have only tested with the following projects.
In theory, it’s compatible with anything that doesn’t leave [`mkmf`](https://rubyapi.org/o/MakeMakefile)’s comfort zone,
In theory, it’s compatible with anything that doesn’t leave
[`rbconfig`](https://rubyapi.org/o/RbConfig)+[`mkmf`](https://rubyapi.org/o/MakeMakefile)’s comfort zone,
which I expect to be the majority of C-based gems. (It *will indeed* “take a while” when “Building native extensions”.)
Please do [let me know](https://github.com/ParadoxV5/ruby-mingw-make/issues)
if it doesn’t meet the expectations on something not unusual.
Expand Down Expand Up @@ -74,10 +75,15 @@ end
```

### Use manually
Specify `mingw32-make` as the `MAKE` tool and prepend the `lib/mingw-make/mkmf.rb` script to the Ruby `$LOAD_PATH`.
*Prepend* (so it takes precedence) the [`lib/mingw-make/mkmf.rb`](lib/mingw-make/mkmf.rb)
script to the Ruby `$LOAD_PATH`. Because RubyGems `require 'rbconfig'`,
we must explicitly apply [the `rbconfig` replacements](lib/mingw-make/rbconfig-patch.rb).
```bat
set RUBYOPT=-Ipath/to/gems/mingw-make/lib/mingw-make/ -rrbconfig-patch
```
You may also need to specify a fallback for the `MAKE` tool, e.g.:
```bat
set MAKE=mingw32-make
set RUBYOPT=-Ipath/to/gems/mingw-make/lib/mingw-make/
```


Expand Down
4 changes: 2 additions & 2 deletions lib/mingw-make.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

# Set up `RUBYOPT=-I/absolute/path/to/lib/mingw-make/` for subprocesses
ENV['RUBYOPT'] = "#{ENV['RUBYOPT']} -I#{File.join __dir__, 'mingw-make'}"
# Set up `RUBYOPT=-I/absolute/path/to/lib/mingw-make/ -rrbconfig-patch` for subprocesses
ENV['RUBYOPT'] = "#{ENV['RUBYOPT']} -I#{File.join __dir__, 'mingw-make'} -rrbconfig-patch"
ENV['MAKE'] ||= 'make'
21 changes: 1 addition & 20 deletions lib/mingw-make/mkmf.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
# frozen_string_literal: true

require 'rbconfig'

# Use `ruby -run` fall-backs
install = 'ruby -run -e install -- -p'
fixed = {
'RM' => 'ruby -run -e rm -- -f',
'RMALL' => 'ruby -run -e rm -- -rf',
'RMDIRS' => 'ruby -run -e rmdir -- -p',
'MAKEDIRS' => 'ruby -run -e mkdir -- -p',
'CP' => 'ruby -run -e cp --',
'INSTALL' => install
}
install_args = {
'INSTALL_PROG' => '-m 0755',
'INSTALL_DATA' => '-m 0644'
}
RbConfig::MAKEFILE_CONFIG.merge! fixed, install_args.transform_values { "$(INSTALL) #{_1}" }
RbConfig::CONFIG.merge! fixed, install_args.transform_values { "#{install} #{_1}" }

require_relative 'rbconfig-patch'
require File.join RbConfig::CONFIG['rubylibdir'], 'mkmf'

module MakeMakefile
Expand Down
24 changes: 24 additions & 0 deletions lib/mingw-make/rbconfig-patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

# Normally, RubyGems `require`s `rbconfig` itself, so overloading `require 'rbconfig'` may be ineffective.
# Howëver, if RubyGems are disabled (`--disable-gems`), nameing this file `rbconfig.rb` would conflict with the design,
# which depends on the original {RbConfig} to bypass the modified `$LOAD_PATH` to fetch the original {MakeMakefile}.

require 'rbconfig'

# Use `ruby -run` fall-backs
install = 'ruby -run -e install -- -p'
fixed = {
'RM' => 'ruby -run -e rm -- -f',
'RMALL' => 'ruby -run -e rm -- -rf',
'RMDIRS' => 'ruby -run -e rmdir -- -p',
'MAKEDIRS' => 'ruby -run -e mkdir -- -p',
'CP' => 'ruby -run -e cp --',
'INSTALL' => install
}
install_args = {
'INSTALL_PROG' => '-m 0755',
'INSTALL_DATA' => '-m 0644'
}
RbConfig::MAKEFILE_CONFIG.merge! fixed, install_args.transform_values { "$(INSTALL) #{_1}" }
RbConfig::CONFIG.merge! fixed, install_args.transform_values { "#{install} #{_1}" }

0 comments on commit 0ae4bba

Please sign in to comment.