Class: Rscons::BasicEnvironment
- Inherits:
-
Object
- Object
- Rscons::BasicEnvironment
- Defined in:
- lib/rscons/basic_environment.rb
Overview
The BasicEnvironment class contains a collection of construction variables.
Direct Known Subclasses
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access the value of a construction variable.
-
#[]=(key, val) ⇒ Object
Assign a value to a construction variable.
-
#append(values) ⇒ void
Add a set of construction variables to the BasicEnvironment.
-
#apply_configuration_data!(vars) ⇒ Object
Load a specific set of construction variables saved from the configure operation.
-
#dump ⇒ Object
Print the Environment's construction variables for debugging.
-
#expand_varref(varref, extra_vars = nil) ⇒ nil, ...
(also: #build_command)
Expand a construction variable reference.
-
#get_var(key) ⇒ Object
Access the value of a construction variable.
-
#initialize(options = {}) ⇒ BasicEnvironment
constructor
Create a BasicEnvironment object.
-
#load_configuration_data!(options) ⇒ Object
Load all construction variables saved from the configure operation.
-
#merge_flags(flags) ⇒ void
Merge construction variable flags into this Environment's construction variables.
-
#parse_flags(flags) ⇒ Hash
Parse command-line flags for compilation/linking options into separate construction variables.
-
#parse_flags!(flags) ⇒ Hash
Parse command-line flags for compilation/linking options into separate construction variables.
-
#shell(command) ⇒ String
Execute a command using the system shell.
Constructor Details
#initialize(options = {}) ⇒ BasicEnvironment
Create a BasicEnvironment object.
12 13 14 15 |
# File 'lib/rscons/basic_environment.rb', line 12 def initialize( = {}) @varset = VarSet.new(Rscons::DEFAULT_CONSTRUCTION_VARIABLES) load_configuration_data!() end |
Instance Method Details
#[](key) ⇒ Object
Access the value of a construction variable.
24 25 26 |
# File 'lib/rscons/basic_environment.rb', line 24 def [](key) @varset[key] end |
#[]=(key, val) ⇒ Object
Assign a value to a construction variable.
51 52 53 |
# File 'lib/rscons/basic_environment.rb', line 51 def []=(key, val) @varset[key] = val end |
#append(values) ⇒ void
This method returns an undefined value.
Add a set of construction variables to the BasicEnvironment.
60 61 62 |
# File 'lib/rscons/basic_environment.rb', line 60 def append(values) @varset.append(values) end |
#apply_configuration_data!(vars) ⇒ Object
Load a specific set of construction variables saved from the configure operation.
237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/rscons/basic_environment.rb', line 237 def apply_configuration_data!(vars) if merge_vars = vars["merge"] append(merge_vars) end if append_vars = vars["append"] merge_flags(append_vars) end if parse_vars = vars["parse"] parse_vars.each do |parse_string| parse_flags!(parse_string) end end end |
#dump ⇒ Object
Print the Environment's construction variables for debugging.
211 212 213 214 215 216 217 |
# File 'lib/rscons/basic_environment.rb', line 211 def dump varset_hash = @varset.to_h varset_hash.keys.sort_by(&:to_s).each do |var| var_str = var.is_a?(Symbol) ? var.inspect : var Ansi.write($stdout, :cyan, var_str, :reset, " => #{varset_hash[var].inspect}\n") end end |
#expand_varref(varref, extra_vars = nil) ⇒ nil, ... Also known as: build_command
Expand a construction variable reference.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rscons/basic_environment.rb', line 72 def (varref, extra_vars = nil) vars = if extra_vars.nil? @varset else @varset.merge(extra_vars) end lambda_args = [env: self, vars: vars] vars.(varref, lambda_args) end |
#get_var(key) ⇒ Object
Access the value of a construction 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.
40 41 42 |
# File 'lib/rscons/basic_environment.rb', line 40 def get_var(key) @varset.get_var(key) end |
#load_configuration_data!(options) ⇒ Object
Load all construction variables saved from the configure operation.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/rscons/basic_environment.rb', line 220 def load_configuration_data!() if vars = Cache.instance["configuration_data"]["vars"] if default_vars = vars["_default_"] apply_configuration_data!(default_vars) end if [:use] Array([:use]).each do |use| if use_vars = vars[use] apply_configuration_data!(use_vars) end end end end end |
#merge_flags(flags) ⇒ void
This method returns an undefined value.
Merge construction variable flags into this Environment's construction variables.
This method does the same thing as #append, except that Array values in flags
are appended to the end of Array construction variables instead of replacing their contents.
200 201 202 203 204 205 206 207 208 |
# File 'lib/rscons/basic_environment.rb', line 200 def merge_flags(flags) flags.each_pair do |key, val| if self.get_var(key).is_a?(Array) and val.is_a?(Array) self[key] += val else self[key] = val end end end |
#parse_flags(flags) ⇒ Hash
Parse command-line flags for compilation/linking options into separate construction variables.
For #parse_flags, the parsed construction variables are returned in a Hash instead of merging them directly to the Environment. They can be merged with #merge_flags. The #parse_flags! version immediately merges the parsed flags as well.
Example:
# Import FreeType build options
env.parse_flags!("!freetype-config --cflags --libs")
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/rscons/basic_environment.rb', line 105 def parse_flags(flags) if flags =~ /^!(.*)$/ flags = shell($1) end rv = {} words = Shellwords.split(flags) skip = false words.each_with_index do |word, i| if skip skip = false next end append = lambda do |var, val| rv[var] ||= [] rv[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 =~ /^#{get_var("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 =~ /^#{get_var("INCPREFIX")}(.*)$/ handle["CPPPATH", $1] elsif word =~ /^#{get_var("LIBLINKPREFIX")}(.*)$/ handle["LIBS", $1] elsif word =~ /^#{get_var("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 rv end |
#parse_flags!(flags) ⇒ Hash
Parse command-line flags for compilation/linking options into separate construction variables.
For #parse_flags, the parsed construction variables are returned in a Hash instead of merging them directly to the Environment. They can be merged with #merge_flags. The #parse_flags! version immediately merges the parsed flags as well.
Example:
# Import FreeType build options
env.parse_flags!("!freetype-config --cflags --libs")
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/rscons/basic_environment.rb', line 105 def parse_flags(flags) if flags =~ /^!(.*)$/ flags = shell($1) end rv = {} words = Shellwords.split(flags) skip = false words.each_with_index do |word, i| if skip skip = false next end append = lambda do |var, val| rv[var] ||= [] rv[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 =~ /^#{get_var("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 =~ /^#{get_var("INCPREFIX")}(.*)$/ handle["CPPPATH", $1] elsif word =~ /^#{get_var("LIBLINKPREFIX")}(.*)$/ handle["LIBS", $1] elsif word =~ /^#{get_var("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 rv end |
#shell(command) ⇒ String
Execute a command using the system shell.
The shell is automatically determined but can be overridden by the SHELL construction variable. If the SHELL construction variable is specified, the flag to pass to the shell is automatically dtermined but can be overridden by the SHELLFLAG construction variable.
261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/rscons/basic_environment.rb', line 261 def shell(command) shell_cmd = if shell = get_var("SHELL") flag = get_var("SHELLFLAG") || (shell == "cmd" ? "/c" : "-c") [shell, flag] else Rscons.get_system_shell end IO.popen([*shell_cmd, command]) do |io| io.read end end |