From 21a29844a34589506e88a2473c62f84e417a61bc Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Fri, 26 Nov 2021 20:53:52 +0900 Subject: mkmf: deal with environment variables in MakeMakefile#xpopen --- lib/mkmf.rb | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) (limited to 'lib/mkmf.rb') diff --git a/lib/mkmf.rb b/lib/mkmf.rb index f4ad2070a0..6cc455424e 100644 --- a/lib/mkmf.rb +++ b/lib/mkmf.rb @@ -386,30 +386,56 @@ MESSAGE end end - def xsystem command, opts = nil + def expand_command(commands, envs = libpath_env) varpat = /\$\((\w+)\)|\$\{(\w+)\}/ - if varpat =~ command - vars = Hash.new {|h, k| h[k] = ENV[k]} - command = command.dup - nil while command.gsub!(varpat) {vars[$1||$2]} + vars = nil + expand = proc do |command| + case command + when Array + command.map(&expand) + when String + if varpat =~ command + vars ||= Hash.new {|h, k| h[k] = ENV[k]} + command = command.dup + nil while command.gsub!(varpat) {vars[$1||$2]} + end + command + else + command + end + end + if Array === commands + env, *commands = commands if Hash === commands.first + envs.merge!(env) if env end + return envs, expand[commands] + end + + def env_quote(envs) + envs.map {|e, v| "#{e}=#{v.quote}"} + end + + def xsystem command, opts = nil + env, command = expand_command(command) Logging::open do - puts command.quote + puts [env_quote(env), command.quote].join(' ') if opts and opts[:werror] result = nil Logging.postpone do |log| - output = IO.popen(libpath_env, command, &:read) + output = IO.popen(env, command, &:read) result = ($?.success? and File.zero?(log.path)) output end result else - system(libpath_env, command) + system(env, *command) end end end def xpopen command, *mode, &block + env, commands = expand_command(command) + command = [env_quote(env), command].join(' ') Logging::open do case mode[0] when nil, Hash, /^r/ @@ -417,7 +443,7 @@ MESSAGE else puts "| #{command}" end - IO.popen(libpath_env, command, *mode, &block) + IO.popen(env, commands, *mode, &block) end end -- cgit v1.2.3