Skip to content

Commit 080bf30

Browse files
Edouard-chinmatzbot
authored andcommitted
[ruby/rubygems] Allow to specify the number of make jobs when installing gems:
- Added a new `-j` option to `gem install` and `gem update`. This option allows to specify the number of jobs we pass to `make` when compiling gem with native extensions. By default its the number of processors, but users may want a way to control this. You can use it like so: `gem install json -j8` ruby/rubygems@67aad88ca6
1 parent 9f59315 commit 080bf30

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

lib/rubygems/dependency_installer.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def initialize(options = {})
8383
@user_install = options[:user_install]
8484
@wrappers = options[:wrappers]
8585
@build_args = options[:build_args]
86+
@build_jobs = options[:build_jobs]
8687
@build_docs_in_background = options[:build_docs_in_background]
8788
@dir_mode = options[:dir_mode]
8889
@data_mode = options[:data_mode]
@@ -154,6 +155,7 @@ def install(dep_or_name, version = Gem::Requirement.default)
154155
options = {
155156
bin_dir: @bin_dir,
156157
build_args: @build_args,
158+
build_jobs: @build_jobs,
157159
document: @document,
158160
env_shebang: @env_shebang,
159161
force: @force,

lib/rubygems/install_update_options.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ def add_install_update_options
3131
options[:bin_dir] = File.expand_path(value)
3232
end
3333

34+
add_option(:"Install/Update", "-j", "--build-jobs VALUE", Integer,
35+
"Specify the number of jobs to pass to `make` when installing",
36+
"gems with native extensions.",
37+
"Defaults to the number of processors.",
38+
"This option is ignored on the mswin platform or",
39+
"if the MAKEFLAGS environment variable is set.") do |value, options|
40+
options[:build_jobs] = value
41+
end
42+
3443
add_option(:"Install/Update", "--document [TYPES]", Array,
3544
"Generate documentation for installed gems",
3645
"List the documentation types you wish to",

test/rubygems/test_gem_commands_install_command.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,34 @@ def test_suggest_update_if_enabled
15841584
end
15851585
end
15861586

1587+
def test_pass_down_the_job_option_to_make
1588+
gemspec = nil
1589+
1590+
spec_fetcher do |fetcher|
1591+
fetcher.gem "a", 2 do |spec|
1592+
gemspec = spec
1593+
1594+
extconf_path = "#{spec.gem_dir}/extconf.rb"
1595+
1596+
write_file(extconf_path) do |io|
1597+
io.puts "require 'mkmf'"
1598+
io.puts "create_makefile '#{spec.name}'"
1599+
end
1600+
1601+
spec.extensions = "extconf.rb"
1602+
end
1603+
end
1604+
1605+
use_ui @ui do
1606+
assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
1607+
@cmd.invoke "a", "-j4"
1608+
end
1609+
end
1610+
1611+
gem_make_out = File.read(File.join(gemspec.extension_dir, "gem_make.out"))
1612+
assert_includes(gem_make_out, "make -j4")
1613+
end
1614+
15871615
def test_execute_bindir_with_nonexistent_parent_dirs
15881616
spec_fetcher do |fetcher|
15891617
fetcher.gem "a", 2 do |s|

test/rubygems/test_gem_commands_update_command.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,34 @@ def test_fetch_remote_gems_prerelease
696696
assert_equal expected, @cmd.fetch_remote_gems(specs["a-1"])
697697
end
698698

699+
def test_pass_down_the_job_option_to_make
700+
gemspec = nil
701+
702+
spec_fetcher do |fetcher|
703+
fetcher.download "a", 3 do |spec|
704+
gemspec = spec
705+
706+
extconf_path = "#{spec.gem_dir}/extconf.rb"
707+
708+
write_file(extconf_path) do |io|
709+
io.puts "require 'mkmf'"
710+
io.puts "create_makefile '#{spec.name}'"
711+
end
712+
713+
spec.extensions = "extconf.rb"
714+
end
715+
716+
fetcher.gem "a", 2
717+
end
718+
719+
use_ui @ui do
720+
@cmd.invoke("a", "-j2")
721+
end
722+
723+
gem_make_out = File.read(File.join(gemspec.extension_dir, "gem_make.out"))
724+
assert_includes(gem_make_out, "make -j2")
725+
end
726+
699727
def test_handle_options_system
700728
@cmd.handle_options %w[--system]
701729

test/rubygems/test_gem_install_update_options.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,16 @@ def test_minimal_deps
202202

203203
assert_equal true, @cmd.options[:minimal_deps]
204204
end
205+
206+
def test_build_jobs_short_version
207+
@cmd.handle_options %w[-j 4]
208+
209+
assert_equal 4, @cmd.options[:build_jobs]
210+
end
211+
212+
def test_build_jobs_long_version
213+
@cmd.handle_options %w[--build-jobs 4]
214+
215+
assert_equal 4, @cmd.options[:build_jobs]
216+
end
205217
end

0 commit comments

Comments
 (0)