diff --git a/build_tests/configure/check_c_header_failure.rb b/build_tests/configure/check_c_header_failure.rb new file mode 100644 index 0000000..b08dbb0 --- /dev/null +++ b/build_tests/configure/check_c_header_failure.rb @@ -0,0 +1,3 @@ +configure do + check_c_header "not___found.h" +end diff --git a/build_tests/configure/check_c_header_no_fail.rb b/build_tests/configure/check_c_header_no_fail.rb new file mode 100644 index 0000000..6c96718 --- /dev/null +++ b/build_tests/configure/check_c_header_no_fail.rb @@ -0,0 +1,3 @@ +configure do + check_c_header "not___found.h", fail: false +end diff --git a/build_tests/configure/check_c_header_success.rb b/build_tests/configure/check_c_header_success.rb new file mode 100644 index 0000000..55df020 --- /dev/null +++ b/build_tests/configure/check_c_header_success.rb @@ -0,0 +1,3 @@ +configure do + check_c_header "string.h" +end diff --git a/lib/rscons/application.rb b/lib/rscons/application.rb index a6534a9..ce8ee19 100644 --- a/lib/rscons/application.rb +++ b/lib/rscons/application.rb @@ -94,6 +94,11 @@ module Rscons if cdc = @script.check_d_compiler co.check_d_compiler(cdc) end + if cchs = @script.check_c_headers + cchs.each do |cch| + co.check_c_header(*cch) + end + end rescue ConfigureOp::ConfigureFailure rv = 1 end diff --git a/lib/rscons/configure_op.rb b/lib/rscons/configure_op.rb index 798dcfc..3977ab7 100644 --- a/lib/rscons/configure_op.rb +++ b/lib/rscons/configure_op.rb @@ -98,6 +98,27 @@ module Rscons end end + # Check for a C header. + def check_c_header(header_name, options = {}) + $stdout.write("Checking for C header '#{header_name}'... ") + File.open("#{@work_dir}/cfgtest.c", "wb") do |fh| + fh.puts <<-EOF + #include "#{header_name}" + int main(int argc, char * argv[]) { + return 0; + } + EOF + end + vars = { + "LD" => "${CC}", + "_SOURCES" => "#{@work_dir}/cfgtest.c", + "_TARGET" => "#{@work_dir}/cfgtest.exe", + } + command = @env.build_command("${LDCMD}", vars) + _, _, status = log_and_test_command(command) + common_config_checks(status, options) + end + private # Test a C compiler. @@ -234,5 +255,31 @@ module Rscons end end + # Perform processing common to several configure checks. + # + # @param status [Process::Status, Integer] + # Process exit code. + # @param options [Hash] + # Common check options. + # @option options [Boolean] :fail + # Whether to fail configuration if the requested item is not found. + # @option options [String] :set_define + # A define to set (in CPPDEFINES) if the requested item is found. + def common_config_checks(status, options) + if status == 0 + Ansi.write($stdout, :green, "found\n") + else + if options.has_key?(:fail) and not options[:fail] + Ansi.write($stdout, :yellow, "not found\n") + else + Ansi.write($stdout, :red, "not found\n") + raise ConfigureFailure.new + end + end + if options[:set_define] + @env["CPPDEFINES"] << options[:set_define] + end + end + end end diff --git a/spec/build_tests_spec.rb b/spec/build_tests_spec.rb index 2aadb65..e75b4f9 100644 --- a/spec/build_tests_spec.rb +++ b/spec/build_tests_spec.rb @@ -1620,6 +1620,32 @@ EOF end end end + + context "check_c_header" do + it "succeeds when the requested header is found" do + test_dir "configure" + result = run_rscons(rsconsfile: "check_c_header_success.rb", op: "configure") + expect(result.stderr).to eq "" + expect(result.status).to eq 0 + expect(result.stdout).to match /Checking for C header 'string\.h'... found/ + end + + it "fails when the requested header is not found" do + test_dir "configure" + result = run_rscons(rsconsfile: "check_c_header_failure.rb", op: "configure") + expect(result.stderr).to eq "" + expect(result.status).to_not eq 0 + expect(result.stdout).to match /Checking for C header 'not___found\.h'... not found/ + end + + it "succeeds when the requested header is not found but :fail is set to false" do + test_dir "configure" + result = run_rscons(rsconsfile: "check_c_header_no_fail.rb", op: "configure") + expect(result.stderr).to eq "" + expect(result.status).to eq 0 + expect(result.stdout).to match /Checking for C header 'not___found\.h'... not found/ + end + end end end