From ab4ca71ac7e5c1945f130df85b16db4ecd8aa7ed Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 14 Nov 2018 23:28:00 -0500 Subject: [PATCH] implement ConfigureOp#check_cfg - not tested yet --- lib/rscons/application.rb | 5 ++ lib/rscons/configure_op.rb | 110 +++++++++++++++++++++++++++++++++++++ lib/rscons/script.rb | 10 ++++ 3 files changed, 125 insertions(+) diff --git a/lib/rscons/application.rb b/lib/rscons/application.rb index 951e425..c36acd8 100644 --- a/lib/rscons/application.rb +++ b/lib/rscons/application.rb @@ -100,6 +100,11 @@ module Rscons if cdc = @script.check_d_compiler co.check_d_compiler(cdc) end + if ccs = @script.check_cfgs + ccs.each do |cc| + co.check_cfg(*cc) + end + end if cchs = @script.check_c_headers cchs.each do |cch| co.check_c_header(*cch) diff --git a/lib/rscons/configure_op.rb b/lib/rscons/configure_op.rb index 3eb5df5..414f6a8 100644 --- a/lib/rscons/configure_op.rb +++ b/lib/rscons/configure_op.rb @@ -98,6 +98,21 @@ module Rscons end end + # Check for a package or configure program output. + def check_cfg(options = {}) + program = options[:program] || "pkg-config" + args = options[:args] || %w[--cflags --libs] + command = [program] + args + if options[:package] + command += [options[:package]] + end + stdout, _, status = log_and_test_command(command) + if status == 0 + parse_flags(stdout) + end + common_config_checks(status, options) + end + # Check for a C header. def check_c_header(header_name, options = {}) $stdout.write("Checking for C header '#{header_name}'... ") @@ -339,6 +354,24 @@ module Rscons end end + # Merge construction variables into the configured Environment. + # + # This method does the same thing as {#merge_vars}, except that Array + # values in +vars+ are appended to the end of Array construction variables + # instead of replacing their contents. + # + # @param vars [Hash] + # Hash containing the variables to merge. + def append_vars(vars) + vars.each_pair do |key, val| + if @env[key].is_a?(Array) and val.is_a?(Array) + @env[key] += val + else + @env[key] = val + end + end + end + # Perform processing common to several configure checks. # # @param status [Process::Status, Integer] @@ -397,5 +430,82 @@ module Rscons end end + # Parse compilation flags (from a program like pkg-config for example). + # + # @param flags [String] + # Compilation flags. + def parse_flags(flags) + vars = {} + words = Shellwords.split(flags) + skip = false + words.each_with_index do |word, i| + if skip + skip = false + next + end + append = lambda do |var, val| + vars[var] ||= [] + vars[var] += val + end + handle = lambda do |var, val| + if val.nil? or val.empty? + val = words[i + 1] + skip = true + end + if val and not val.empty? + append[var, [val]] + end + end + if word == "-arch" + if val = words[i + 1] + append["CCFLAGS", ["-arch", val]] + append["LDFLAGS", ["-arch", val]] + end + skip = true + elsif word =~ /^#{@env["CPPDEFPREFIX"]}(.*)$/ + handle["CPPDEFINES", $1] + elsif word == "-include" + if val = words[i + 1] + append["CCFLAGS", ["-include", val]] + end + skip = true + elsif word == "-isysroot" + if val = words[i + 1] + append["CCFLAGS", ["-isysroot", val]] + append["LDFLAGS", ["-isysroot", val]] + end + skip = true + elsif word =~ /^#{@env["INCPREFIX"]}(.*)$/ + handle["CPPPATH", $1] + elsif word =~ /^#{@env["LIBLINKPREFIX"]}(.*)$/ + handle["LIBS", $1] + elsif word =~ /^#{@env["LIBDIRPREFIX"]}(.*)$/ + handle["LIBPATH", $1] + elsif word == "-mno-cygwin" + append["CCFLAGS", [word]] + append["LDFLAGS", [word]] + elsif word == "-mwindows" + append["LDFLAGS", [word]] + elsif word == "-pthread" + append["CCFLAGS", [word]] + append["LDFLAGS", [word]] + elsif word =~ /^-Wa,(.*)$/ + append["ASFLAGS", $1.split(",")] + elsif word =~ /^-Wl,(.*)$/ + append["LDFLAGS", $1.split(",")] + elsif word =~ /^-Wp,(.*)$/ + append["CPPFLAGS", $1.split(",")] + elsif word.start_with?("-") + append["CCFLAGS", [word]] + elsif word.start_with?("+") + append["CCFLAGS", [word]] + append["LDFLAGS", [word]] + else + append["LIBS", [word]] + end + end + append_vars(vars) + end + end end diff --git a/lib/rscons/script.rb b/lib/rscons/script.rb index 12ed1de..f30d1e7 100644 --- a/lib/rscons/script.rb +++ b/lib/rscons/script.rb @@ -42,6 +42,12 @@ module Rscons @script.check_d_compiler = args end + # Check for a package or configure program output. + def check_cfg(*args) + @script.check_cfgs ||= [] + @script.check_cfgs << args + end + # Check for a C header. def check_c_header(*args) @script.check_c_headers ||= [] @@ -89,6 +95,10 @@ module Rscons # D compilers to check for. attr_accessor :check_d_compiler + # @return [Array] + # Configs to check for. + attr_accessor :check_cfgs + # @return [Array] # C headers to check for. attr_accessor :check_c_headers