Class: Rscons::BasicEnvironment

Inherits:
Object
  • Object
show all
Defined in:
lib/rscons/basic_environment.rb

Overview

The BasicEnvironment class contains a collection of construction variables.

Direct Known Subclasses

Environment

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BasicEnvironment

Create a BasicEnvironment object.

Parameters:

  • options (Hash) (defaults to: {})

    Construction options.

Options Hash (options):

  • :use (String, Array<String>)

    Use flag(s). If specified, any configuration flags which were saved with a corresponding ‘:use` value will be applied to this Environment.



12
13
14
15
16
# File 'lib/rscons/basic_environment.rb', line 12

def initialize(options = {})
  @varset = VarSet.new(Rscons::DEFAULT_CONSTRUCTION_VARIABLES)
  load_configuration_data!(options)
  load_task_param_variables!
end

Instance Method Details

#[](key) ⇒ Object

Access the value of a construction variable.

Parameters:

  • key (String, Symbol)

    The construction variable name.

Returns:

  • (Object)

    The construction variable’s value.



25
26
27
# File 'lib/rscons/basic_environment.rb', line 25

def [](key)
  @varset[key]
end

#[]=(key, val) ⇒ Object

Assign a value to a construction variable.

Parameters:

  • key (String, Symbol)

    The construction variable name.

  • val (Object)

    The value to set.



52
53
54
# File 'lib/rscons/basic_environment.rb', line 52

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.

Parameters:

  • values (VarSet, Hash)

    New set of variables.



61
62
63
# File 'lib/rscons/basic_environment.rb', line 61

def append(values)
  @varset.append(values)
end

#apply_configuration_data!(vars) ⇒ Object

Load a specific set of construction variables saved from the configure operation.



238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/rscons/basic_environment.rb', line 238

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

#dumpObject

Print the Environment’s construction variables for debugging.



212
213
214
215
216
217
218
# File 'lib/rscons/basic_environment.rb', line 212

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.

Parameters:

  • varref (nil, String, Array, Proc, Symbol, TrueClass, FalseClass)

    Variable reference to expand.

  • extra_vars (Hash, VarSet) (defaults to: nil)

    Extra variables to use in addition to (or replace) the Environment’s construction variables when expanding the variable reference.

Returns:

  • (nil, String, Array, Symbol, TrueClass, FalseClass)

    Expansion of the variable reference.



73
74
75
76
77
78
79
80
81
82
# File 'lib/rscons/basic_environment.rb', line 73

def expand_varref(varref, extra_vars = nil)
  vars =
    if extra_vars.nil?
      @varset
    else
      @varset.merge(extra_vars)
    end
  lambda_args = [env: self, vars: vars]
  vars.expand_varref(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.

Parameters:

  • key (String, Symbol)

    The construction variable name.

Returns:

  • (Object)

    The construction variable’s value.



41
42
43
# File 'lib/rscons/basic_environment.rb', line 41

def get_var(key)
  @varset.get_var(key)
end

#load_configuration_data!(options) ⇒ Object

Load all construction variables saved from the configure operation.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/rscons/basic_environment.rb', line 221

def load_configuration_data!(options)
  if vars = Cache.instance["configuration_data"]["vars"]
    if default_vars = vars["_default_"]
      apply_configuration_data!(default_vars)
    end
    if options[:use]
      Array(options[:use]).each do |use|
        if use_vars = vars[use]
          apply_configuration_data!(use_vars)
        end
      end
    end
  end
end

#load_task_param_variables!Object

Load task parameters as construction variables.



276
277
278
279
280
281
282
# File 'lib/rscons/basic_environment.rb', line 276

def load_task_param_variables!
  Task[].each do |task_name, task|
    task.param_values.each do |param_name, param_value|
      self["#{task.name}:#{param_name}"] = param_value
    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.

Parameters:

  • flags (Hash)

    Set of construction variables to merge into the current Environment. This can be the value (or a modified version) returned by #parse_flags.



201
202
203
204
205
206
207
208
209
# File 'lib/rscons/basic_environment.rb', line 201

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")

Parameters:

  • flags (String)

    String containing the flags to parse, or if the flags string begins with “!”, a shell command to execute using #shell to obtain the flags to parse.

Returns:

  • (Hash)

    Set of construction variables to append.



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
180
# File 'lib/rscons/basic_environment.rb', line 106

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")

Parameters:

  • flags (String)

    String containing the flags to parse, or if the flags string begins with “!”, a shell command to execute using #shell to obtain the flags to parse.

Returns:

  • (Hash)

    Set of construction variables to append.



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
180
# File 'lib/rscons/basic_environment.rb', line 106

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.

Parameters:

  • command (String)

    Command to execute.

Returns:

  • (String)

    The command’s standard output.



262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/rscons/basic_environment.rb', line 262

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