1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

mkmf: deal with environment variables in MakeMakefile#xpopen

This commit is contained in:
Nobuyoshi Nakada 2021-11-26 20:53:52 +09:00
parent 1c3d6d6cc9
commit 21a29844a3
Notes: git 2021-11-26 22:32:46 +09:00

View file

@ -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