Add VarSet#get_var and Environment#get_var - #92

This commit is contained in:
Josh Holtrop 2019-05-09 21:52:07 -04:00
parent dd3d468a28
commit 458eeaf93d
4 changed files with 77 additions and 24 deletions

View File

@ -13,18 +13,42 @@ module Rscons
load_configuration_data!(options) load_configuration_data!(options)
end end
# Get a construction variable's value. # Access the value of a construction variable.
# #
# @see VarSet#[] # @param key [String, Symbol]
def [](*args) # The construction variable name.
@varset.__send__(:[], *args) #
# @return [Object]
# The construction variable's value.
def [](key)
@varset[key]
end end
# Set a construction variable's value. # Access the value of a construction variable.
# #
# @see VarSet#[]= # This method is similar to #[] but does not make a copy-on-access copy of
def []=(*args) # the variable accessed. This means that the returned value is NOT safe to
@varset.__send__(:[]=, *args) # be modified by the caller. Thus the caller must guarantee that it does
# not modify the returned value.
#
# @param key [String, Symbol]
# The construction variable name.
#
# @return [Object]
# The construction variable's value.
def get_var(key)
@varset.get_var(key)
end
# Assign a value to a construction variable.
#
# @param key [String, Symbol]
# The construction variable name.
#
# @param val [Object]
# The value to set.
def []=(key, val)
@varset[key] = val
end end
# Add a set of construction variables to the BasicEnvironment. # Add a set of construction variables to the BasicEnvironment.
@ -108,7 +132,7 @@ module Rscons
append["LDFLAGS", ["-arch", val]] append["LDFLAGS", ["-arch", val]]
end end
skip = true skip = true
elsif word =~ /^#{self["CPPDEFPREFIX"]}(.*)$/ elsif word =~ /^#{get_var("CPPDEFPREFIX")}(.*)$/
handle["CPPDEFINES", $1] handle["CPPDEFINES", $1]
elsif word == "-include" elsif word == "-include"
if val = words[i + 1] if val = words[i + 1]
@ -121,11 +145,11 @@ module Rscons
append["LDFLAGS", ["-isysroot", val]] append["LDFLAGS", ["-isysroot", val]]
end end
skip = true skip = true
elsif word =~ /^#{self["INCPREFIX"]}(.*)$/ elsif word =~ /^#{get_var("INCPREFIX")}(.*)$/
handle["CPPPATH", $1] handle["CPPPATH", $1]
elsif word =~ /^#{self["LIBLINKPREFIX"]}(.*)$/ elsif word =~ /^#{get_var("LIBLINKPREFIX")}(.*)$/
handle["LIBS", $1] handle["LIBS", $1]
elsif word =~ /^#{self["LIBDIRPREFIX"]}(.*)$/ elsif word =~ /^#{get_var("LIBDIRPREFIX")}(.*)$/
handle["LIBPATH", $1] handle["LIBPATH", $1]
elsif word == "-mno-cygwin" elsif word == "-mno-cygwin"
append["CCFLAGS", [word]] append["CCFLAGS", [word]]
@ -174,7 +198,7 @@ module Rscons
# @return [void] # @return [void]
def merge_flags(flags) def merge_flags(flags)
flags.each_pair do |key, val| flags.each_pair do |key, val|
if self[key].is_a?(Array) and val.is_a?(Array) if self.get_var(key).is_a?(Array) and val.is_a?(Array)
self[key] += val self[key] += val
else else
self[key] = val self[key] = val

View File

@ -507,9 +507,9 @@ module Rscons
# @return [String] The command's standard output. # @return [String] The command's standard output.
def shell(command) def shell(command)
shell_cmd = shell_cmd =
if self["SHELL"] if shell = get_var("SHELL")
flag = self["SHELLFLAG"] || (self["SHELL"] == "cmd" ? "/c" : "-c") flag = get_var("SHELLFLAG") || (shell == "cmd" ? "/c" : "-c")
[self["SHELL"], flag] [shell, flag]
else else
Rscons.get_system_shell Rscons.get_system_shell
end end

View File

@ -11,11 +11,13 @@ module Rscons
append(vars) append(vars)
end end
# Access the value of variable. # Access the value of a variable.
# #
# @param key [String, Symbol] The variable name. # @param key [String, Symbol]
# The variable name.
# #
# @return [Object] The variable's value. # @return [Object]
# The variable's value.
def [](key) def [](key)
if @my_vars.include?(key) if @my_vars.include?(key)
@my_vars[key] @my_vars[key]
@ -30,11 +32,38 @@ module Rscons
end end
end end
# Access the value of a variable.
#
# This method is similar to #[] but does not make a copy-on-access copy of
# the variable accessed. This means that the returned value is NOT safe to
# be modified by the caller. Thus the caller must guarantee that it does
# not modify the returned value.
#
# @param key [String, Symbol]
# The variable name.
#
# @return [Object]
# The variable's value.
def get_var(key)
if @my_vars.include?(key)
@my_vars[key]
else
@coa_vars.each do |coa_vars|
if coa_vars.include?(key)
return coa_vars[key]
end
end
nil
end
end
# Assign a value to a variable. # Assign a value to a variable.
# #
# @param key [String, Symbol] The variable name. # @param key [String, Symbol]
# The variable name.
# #
# @param val [Object] The value to set. # @param val [Object]
# The value to set.
def []=(key, val) def []=(key, val)
@my_vars[key] = val @my_vars[key] = val
end end
@ -99,7 +128,7 @@ module Rscons
if varref =~ /^(.*)\$\{([^}]+)\}(.*)$/ if varref =~ /^(.*)\$\{([^}]+)\}(.*)$/
prefix, varname, suffix = $1, $2, $3 prefix, varname, suffix = $1, $2, $3
prefix = expand_varref(prefix, lambda_args) unless prefix.empty? prefix = expand_varref(prefix, lambda_args) unless prefix.empty?
varval = expand_varref(self[varname], lambda_args) varval = expand_varref(get_var(varname), lambda_args)
# suffix needs no expansion since the regex matches the last occurence # suffix needs no expansion since the regex matches the last occurence
case varval case varval
when Array when Array
@ -115,7 +144,7 @@ module Rscons
"#{prefix}#{varval}#{suffix}" "#{prefix}#{varval}#{suffix}"
end end
else else
raise "Unknown construction variable type: #{varval.class} (from #{varname.inspect} => #{self[varname].inspect})" raise "Unknown construction variable type: #{varval.class} (from #{varname.inspect} => #{get_var(varname).inspect})"
end end
else else
varref varref

View File

@ -169,7 +169,7 @@ module Rscons
expect { v.expand_varref({a: :b}, :lambda_args) }.to raise_error /Unknown construction variable type: Hash/ expect { v.expand_varref({a: :b}, :lambda_args) }.to raise_error /Unknown construction variable type: Hash/
end end
it "raises an error when an expanded variable is an unexpected type" do it "raises an error when an expanded variable is an unexpected type" do
expect(v).to receive(:[]).at_least(1).times.with("bad").and_return("bad_val") expect(v).to receive(:get_var).at_least(1).times.with("bad").and_return("bad_val")
expect(v).to receive(:expand_varref).with("bad_val", :lambda_args).and_return({a: :b}) expect(v).to receive(:expand_varref).with("bad_val", :lambda_args).and_return({a: :b})
expect(v).to receive(:expand_varref).and_call_original expect(v).to receive(:expand_varref).and_call_original
expect { v.expand_varref("${bad}", :lambda_args) }.to raise_error /Unknown construction variable type: Hash/ expect { v.expand_varref("${bad}", :lambda_args) }.to raise_error /Unknown construction variable type: Hash/