From d656df54073cb773ceaa7b03bffa30daf6a1c3d6 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 7 Nov 2018 19:05:08 -0500 Subject: [PATCH] implement ConfigureOp#check_d_compiler --- build_tests/configure/check_d_compiler.rb | 3 ++ .../configure/check_d_compiler_find_first.rb | 3 ++ lib/rscons/application.rb | 3 ++ lib/rscons/configure_op.rb | 53 +++++++++++++++++++ spec/build_tests_spec.rb | 34 ++++++++++++ 5 files changed, 96 insertions(+) create mode 100644 build_tests/configure/check_d_compiler.rb create mode 100644 build_tests/configure/check_d_compiler_find_first.rb diff --git a/build_tests/configure/check_d_compiler.rb b/build_tests/configure/check_d_compiler.rb new file mode 100644 index 0000000..312355b --- /dev/null +++ b/build_tests/configure/check_d_compiler.rb @@ -0,0 +1,3 @@ +configure do + check_d_compiler +end diff --git a/build_tests/configure/check_d_compiler_find_first.rb b/build_tests/configure/check_d_compiler_find_first.rb new file mode 100644 index 0000000..4467e4a --- /dev/null +++ b/build_tests/configure/check_d_compiler_find_first.rb @@ -0,0 +1,3 @@ +configure do + check_d_compiler "gdc", "ldc2" +end diff --git a/lib/rscons/application.rb b/lib/rscons/application.rb index 12e4a21..e7c005b 100644 --- a/lib/rscons/application.rb +++ b/lib/rscons/application.rb @@ -91,6 +91,9 @@ module Rscons if ccc = @script.check_cxx_compiler co.check_cxx_compiler(ccc) end + if cdc = @script.check_d_compiler + co.check_d_compiler(cdc) + end rescue ConfigureOp::ConfigureFailure rv = 1 end diff --git a/lib/rscons/configure_op.rb b/lib/rscons/configure_op.rb index 0225e42..31da29c 100644 --- a/lib/rscons/configure_op.rb +++ b/lib/rscons/configure_op.rb @@ -72,6 +72,29 @@ module Rscons end end + # Check for a working D compiler. + # + # @param cdc [Array] + # D compiler(s) to check for. + # + # @return [void] + def check_d_compiler(cdc) + $stdout.write("Checking for D compiler... ") + if cdc.empty? + # Default D compiler search array. + cdc = %w[gdc ldc2] + end + dc = cdc.find do |dc| + test_d_compiler(dc) + end + if dc + Ansi.write($stdout, :green, dc, "\n") + else + Ansi.write($stdout, :red, "not found\n") + raise ConfigureFailure.new + end + end + private # Test a C compiler. @@ -135,6 +158,36 @@ module Rscons status == 0 end + # Test a D compiler. + # + # @param dc [String] + # D compiler to test. + # + # @return [Boolean] + # Whether the D compiler tested successfully. + def test_d_compiler(dc) + File.open("#{@work_dir}/cfgtest.d", "wb") do |fh| + fh.puts <<-EOF + import std.stdio; + int main() { + writeln("Hello."); + return 0; + } + EOF + end + case dc + when "gdc" + command = %W[gdc -o #{@work_dir}/cfgtest.exe #{@work_dir}/cfgtest.d] + when "ldc2" + command = %W[ldc2 -of=#{@work_dir}/cfgtest.exe #{@work_dir}/cfgtest.d] + else + $stderr.puts "Unknown D compiler (#{dc})" + raise ConfigureFailure.new + end + _, _, status = log_and_test_command(command) + status == 0 + end + # Execute a test command and log the result. def log_and_test_command(command) begin diff --git a/spec/build_tests_spec.rb b/spec/build_tests_spec.rb index ddf9bb1..bcdbfd5 100644 --- a/spec/build_tests_spec.rb +++ b/spec/build_tests_spec.rb @@ -1568,6 +1568,40 @@ EOF end end end + + context "check_d_compiler" do + {"check_d_compiler.rb" => "when no arguments are given", + "check_d_compiler_find_first.rb" => "when arguments are given"}.each_pair do |rsconsfile, desc| + context desc do + it "finds the first listed D compiler" do + test_dir "configure" + result = run_rscons(rsconsfile: rsconsfile, op: "configure") + expect(result.stderr).to eq "" + expect(result.status).to eq 0 + expect(result.stdout).to match /Checking for D compiler\.\.\. gdc/ + end + + it "finds the second listed D compiler" do + test_dir "configure" + create_exe "gdc", "exit 1" + result = run_rscons(rsconsfile: rsconsfile, op: "configure") + expect(result.stderr).to eq "" + expect(result.status).to eq 0 + expect(result.stdout).to match /Checking for D compiler\.\.\. ldc2/ + end + + it "fails to configure when it cannot find a D compiler" do + test_dir "configure" + create_exe "gdc", "exit 1" + create_exe "ldc2", "exit 1" + result = run_rscons(rsconsfile: rsconsfile, op: "configure") + expect(result.stderr).to eq "" + expect(result.status).to_not eq 0 + expect(result.stdout).to match /Checking for D compiler\.\.\. not found/ + end + end + end + end end end