implement ConfigureOp#check_executable

This commit is contained in:
Josh Holtrop 2018-11-07 22:22:07 -05:00
parent e8990199ef
commit 7869d38dd8
6 changed files with 94 additions and 1 deletions

View File

@ -0,0 +1,3 @@
configure do
check_executable "executable-that-is-not-found"
end

View File

@ -0,0 +1,3 @@
configure do
check_executable "executable-that-is-not-found", fail: false
end

View File

@ -0,0 +1,3 @@
configure do
check_executable "find"
end

View File

@ -115,6 +115,11 @@ module Rscons
co.check_d_import(*cdi) co.check_d_import(*cdi)
end end
end end
if ces = @script.check_executables
ces.each do |ce|
co.check_executable(*ce)
end
end
rescue ConfigureOp::ConfigureFailure rescue ConfigureOp::ConfigureFailure
rv = 1 rv = 1
end end

View File

@ -161,6 +161,27 @@ module Rscons
common_config_checks(status, options) common_config_checks(status, options)
end end
# Check for an executable.
def check_executable(executable, options = {})
$stdout.write("Checking for executable '#{executable}'... ")
found = false
if executable["/"] or executable["\\"]
if File.file?(executable) and File.executable?(executable)
found = true
success_message = executable
end
else
path_entries = ENV["PATH"].split(File::PATH_SEPARATOR)
path_entries.find do |path_entry|
if path = test_path_for_executable(path_entry, executable)
found = true
success_message = path
end
end
end
common_config_checks(found ? 0 : 1, options.merge(success_message: success_message))
end
private private
# Test a C compiler. # Test a C compiler.
@ -307,9 +328,12 @@ module Rscons
# Whether to fail configuration if the requested item is not found. # Whether to fail configuration if the requested item is not found.
# @option options [String] :set_define # @option options [String] :set_define
# A define to set (in CPPDEFINES) if the requested item is found. # A define to set (in CPPDEFINES) if the requested item is found.
# @option options [String] :success_message
# Message to print on success (default "found").
def common_config_checks(status, options) def common_config_checks(status, options)
success_message = options[:success_message] || "found"
if status == 0 if status == 0
Ansi.write($stdout, :green, "found\n") Ansi.write($stdout, :green, "#{success_message}\n")
else else
if options.has_key?(:fail) and not options[:fail] if options.has_key?(:fail) and not options[:fail]
Ansi.write($stdout, :yellow, "not found\n") Ansi.write($stdout, :yellow, "not found\n")
@ -323,5 +347,34 @@ module Rscons
end end
end end
# Check if a directory contains a certain executable.
#
# @param path_entry [String]
# Directory to look in.
# @param executable [String]
# Executable to look for.
def test_path_for_executable(path_entry, executable)
is_executable = lambda do |path|
File.file?(path) and File.executable?(path)
end
if RbConfig::CONFIG["host_os"] =~ /mswin|windows|mingw/i
executable = executable.downcase
dir_entries = Dir.entries(path_entry)
dir_entries.find do |entry|
path = "#{path_entry}/#{entry}"
entry = entry.downcase
if ((entry == executable) or
(entry == "#{executable}.exe") or
(entry == "#{executable}.com") or
(entry == "#{executable}.bat")) and is_executable[path]
return path
end
end
else
path = "#{path_entry}/#{executable}"
return path if is_executable[path]
end
end
end end
end end

View File

@ -1698,6 +1698,32 @@ EOF
expect(result.stdout).to match /Checking for D import 'not\.found'... not found/ expect(result.stdout).to match /Checking for D import 'not\.found'... not found/
end end
end end
context "check_executable" do
it "succeeds when the requested executable is found" do
test_dir "configure"
result = run_rscons(rsconsfile: "check_executable_success.rb", op: "configure")
expect(result.stderr).to eq ""
expect(result.status).to eq 0
expect(result.stdout).to match /Checking for executable 'find'... .*find/
end
it "fails when the requested executable is not found" do
test_dir "configure"
result = run_rscons(rsconsfile: "check_executable_failure.rb", op: "configure")
expect(result.stderr).to eq ""
expect(result.status).to_not eq 0
expect(result.stdout).to match /Checking for executable 'executable-that-is-not-found'... not found/
end
it "succeeds when the requested executable is not found but :fail is set to false" do
test_dir "configure"
result = run_rscons(rsconsfile: "check_executable_no_fail.rb", op: "configure")
expect(result.stderr).to eq ""
expect(result.status).to eq 0
expect(result.stdout).to match /Checking for executable 'executable-that-is-not-found'... not found/
end
end
end end
end end