diff --git a/lib/rscons/application.rb b/lib/rscons/application.rb index 216f5e6..2e6db12 100644 --- a/lib/rscons/application.rb +++ b/lib/rscons/application.rb @@ -21,7 +21,7 @@ module Rscons # Create Application instance. def initialize - @n_threads = determine_n_threads + @n_threads = Util.determine_n_threads @vars = VarSet.new end @@ -157,39 +157,6 @@ module Rscons rv end - # Determine the number of threads to use by default. - # - # @return [Integer] - # The number of threads to use by default. - def determine_n_threads - # If the user specifies the number of threads in the environment, then - # respect that. - if ENV["RSCONS_NTHREADS"] =~ /^(\d+)$/ - return $1.to_i - end - - # Otherwise try to figure out how many threads are available on the - # host hardware. - begin - case RbConfig::CONFIG["host_os"] - when /linux/ - return File.read("/proc/cpuinfo").scan(/^processor\s*:/).size - when /mswin|mingw/ - if `wmic cpu get NumberOfLogicalProcessors /value` =~ /NumberOfLogicalProcessors=(\d+)/ - return $1.to_i - end - when /darwin/ - if `sysctl -n hw.ncpu` =~ /(\d+)/ - return $1.to_i - end - end - rescue - end - - # If we can't figure it out, default to 1. - 1 - end - end end diff --git a/lib/rscons/util.rb b/lib/rscons/util.rb index 710e2dc..0c6ba25 100644 --- a/lib/rscons/util.rb +++ b/lib/rscons/util.rb @@ -26,6 +26,39 @@ module Rscons command.map { |c| c =~ /\s/ ? "'#{c}'" : c }.join(' ') end + # Determine the number of threads to use by default. + # + # @return [Integer] + # The number of threads to use by default. + def determine_n_threads + # If the user specifies the number of threads in the environment, then + # respect that. + if ENV["RSCONS_NTHREADS"] =~ /^(\d+)$/ + return $1.to_i + end + + # Otherwise try to figure out how many threads are available on the + # host hardware. + begin + case RbConfig::CONFIG["host_os"] + when /linux/ + return File.read("/proc/cpuinfo").scan(/^processor\s*:/).size + when /mswin|mingw/ + if `wmic cpu get NumberOfLogicalProcessors /value` =~ /NumberOfLogicalProcessors=(\d+)/ + return $1.to_i + end + when /darwin/ + if `sysctl -n hw.ncpu` =~ /(\d+)/ + return $1.to_i + end + end + rescue + end + + # If we can't figure it out, default to 1. + 1 + end + # Return a string showing the path specified, or if more than one, then # the first path with a "(+D)" afterward, where D is the number of # remaining paths. diff --git a/spec/rscons/application_spec.rb b/spec/rscons/application_spec.rb index 6a754ea..05a0f68 100644 --- a/spec/rscons/application_spec.rb +++ b/spec/rscons/application_spec.rb @@ -26,71 +26,5 @@ module Rscons end end - describe ".determine_n_threads" do - context "when specified by environment variable" do - before(:each) do - expect(ENV).to receive(:[]).with("RSCONS_NTHREADS").and_return("3") - end - it "returns the user-specified number of threads to use" do - expect(Rscons.application.__send__(:determine_n_threads)).to eq(3) - end - end - - context "when not specified by environment variable" do - before(:each) do - expect(ENV).to receive(:[]).with("RSCONS_NTHREADS").and_return(nil) - end - - context "on Linux" do - before(:each) do - expect(RbConfig::CONFIG).to receive(:[]).with("host_os").and_return("linux") - end - it "returns the number of processors from /proc/cpuinfo" do - expect(File).to receive(:read).with("/proc/cpuinfo").and_return(<