Class: Rscons::Application

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

Overview

Functionality for an instance of the rscons application invocation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#active_variantsArray<Hash> (readonly)

Returns Active variants.

Returns:

  • (Array<Hash>)

    Active variants.



10
11
12
# File 'lib/rscons/application.rb', line 10

def active_variants
  @active_variants
end

#build_dirString

Returns Top-level build directory.

Returns:

  • (String)

    Top-level build directory.



14
15
16
# File 'lib/rscons/application.rb', line 14

def build_dir
  @build_dir
end

#do_ansi_colorBoolean

Returns Whether to output ANSI color escape sequences.

Returns:

  • (Boolean)

    Whether to output ANSI color escape sequences.



18
19
20
# File 'lib/rscons/application.rb', line 18

def do_ansi_color
  @do_ansi_color
end

#n_threadsInteger

Returns The number of threads to use when scheduling subprocesses.

Returns:

  • (Integer)

    The number of threads to use when scheduling subprocesses.



22
23
24
# File 'lib/rscons/application.rb', line 22

def n_threads
  @n_threads
end

#scriptScript (readonly)

Returns Build script.

Returns:



26
27
28
# File 'lib/rscons/application.rb', line 26

def script
  @script
end

#silent_configureBoolean

Returns Whether to configure silently.

Returns:

  • (Boolean)

    Whether to configure silently.



30
31
32
# File 'lib/rscons/application.rb', line 30

def silent_configure
  @silent_configure
end

#verboseBoolean

Returns Whether to run verbosely.

Returns:

  • (Boolean)

    Whether to run verbosely.



34
35
36
# File 'lib/rscons/application.rb', line 34

def verbose
  @verbose
end

Instance Method Details

#_initializeObject

Create Application instance.



37
38
39
40
41
42
43
# File 'lib/rscons/application.rb', line 37

def _initialize
  @silent_configure = true
  @build_dir = ENV["RSCONS_BUILD_DIR"] || "build"
  ENV.delete("RSCONS_BUILD_DIR")
  @n_threads = Util.determine_n_threads
  @variant_groups = []
end

#check_configurevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Check if the project needs to be configured.



184
185
186
187
188
189
190
191
192
# File 'lib/rscons/application.rb', line 184

def check_configure
  apply_task_params(true)
  enable_variants
  unless Cache.instance["configuration_data"]["configured"]
    if @script.autoconf
      configure
    end
  end
end

#check_process_environmentsvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Check if environments need to be processed.



199
200
201
202
203
204
205
206
# File 'lib/rscons/application.rb', line 199

def check_process_environments
  unless @_processed_environments
    Environment[].each do |env|
      env.process
    end
    @_processed_environments = true
  end
end

#cleanvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Remove all generated files.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rscons/application.rb', line 149

def clean
  cache = Cache.instance
  # remove all built files
  cache.targets(false).each do |target|
    cache.remove_target(target)
    FileUtils.rm_f(target)
  end
  # remove all created directories if they are empty
  cache.directories(false).sort {|a, b| b.size <=> a.size}.each do |directory|
    cache.remove_directory(directory)
    next unless File.directory?(directory)
    if (Dir.entries(directory) - ['.', '..']).empty?
      Dir.rmdir(directory) rescue nil
    end
  end
  cache.write
end

#configurevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Configure the project.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/rscons/application.rb', line 213

def configure
  unless @_configured
    @_configured = true
    co = ConfigureOp.new(@script)
    begin
      @script.configure(co)
    rescue RsconsError => e
      co.close(false)
      raise e
    end
    Cache.instance["configuration_data"]["enabled_variants"] = @enabled_variants
    co.close(true)
  end
end

#distcleanvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Remove the build directory and clear the cache.



172
173
174
175
176
177
# File 'lib/rscons/application.rb', line 172

def distclean
  cache = Cache.instance
  clean
  cache.clear
  FileUtils.rm_rf(@build_dir)
end

#enable_variantsObject

Apply user-specified variant enables and complain if they don’t make sense given the build script variant configuration.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rscons/application.rb', line 109

def enable_variants
  unless @_variants_enabled
    if @enabled_variants != ""
      exact = !(@enabled_variants =~ /^(\+|-)/)
      enabled_variants = @enabled_variants.split(",")
      specified_variants = {}
      enabled_variants.each do |enable_variant|
        enable_variant =~ /^(\+|-)?(.*)$/
        enable_disable, variant_name = $1, $2
        specified_variants[variant_name] = enable_disable != "-"
      end
      each_variant do |variant|
        if specified_variants.include?(variant[:name])
          variant[:enabled] = specified_variants[variant[:name]]
        elsif exact
          variant[:enabled] = false
        end
      end
    end
    @_variants_enabled = true
  end
  check_enabled_variants
end

#run(rsconscript, tasks_and_params, show_tasks, all_tasks, enabled_variants) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Run the application.

Execute user-specified tasks.

Parameters:

  • rsconscript (String)

    Build script file name.

  • tasks_and_params (Hash<String => Hash<String => String>>)

    List of task(s) to execute.

  • show_tasks (Boolean)

    Flag to show tasks and exit.

  • all_tasks (Boolean)

    Flag to show all tasks (not just those with a description).

  • enabled_variants (String)

    User-specified variants list.

Returns:

  • (Integer)

    Process exit code (0 on success).



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rscons/application.rb', line 74

def run(rsconscript, tasks_and_params, show_tasks, all_tasks, enabled_variants)
  Cache.instance["failed_commands"] = []
  @tasks_and_params = tasks_and_params
  @enabled_variants = enabled_variants
  if enabled_variants == "" && !tasks_and_params.include?("configure")
    if cache_enabled_variants = Cache.instance["configuration_data"]["enabled_variants"]
      @enabled_variants = cache_enabled_variants
    end
  end
  @script = Script.new
  if should_load_script
    @script.load(rsconscript)
    enable_variants
  end
  if show_tasks
    show_script_tasks(all_tasks)
    return 0
  end
  apply_task_params(false)
  @task_execution_phase = true
  if tasks_and_params.empty?
    check_process_environments
    if Task.tasks["default"]
      Task["default"].check_execute
    end
  else
    tasks_and_params.each do |task_name, params|
      Task[task_name].check_execute
    end
  end
  0
end

#show_failurevoid

This method returns an undefined value.

Show the last failures.



136
137
138
139
140
141
142
# File 'lib/rscons/application.rb', line 136

def show_failure
  failed_commands = Cache.instance["failed_commands"]
  failed_commands.each_with_index do |command, i|
    Ansi.write($stdout, :red, "Failed command (#{i + 1}/#{failed_commands.size}):", :reset, "\n")
    $stdout.puts Util.command_to_s(command)
  end
end

#task_execution_phase?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return if Rscons is in the task execution phase.

Returns:

  • (Boolean)

    If Rscons is in the task execution phase.



51
52
53
# File 'lib/rscons/application.rb', line 51

def task_execution_phase?
  @task_execution_phase
end

#uninstallInteger, void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Remove installed files.

Returns:

  • (Integer)

    Exit code.

  • (void)


236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/rscons/application.rb', line 236

def uninstall
  cache = Cache.instance
  cache.targets(true).each do |target|
    cache.remove_target(target)
    next unless File.exist?(target)
    puts "Removing #{target}" if verbose
    FileUtils.rm_f(target)
  end
  # remove all created directories if they are empty
  cache.directories(true).sort {|a, b| b.size <=> a.size}.each do |directory|
    cache.remove_directory(directory)
    next unless File.directory?(directory)
    if (Dir.entries(directory) - ['.', '..']).empty?
      puts "Removing #{directory}" if verbose
      Dir.rmdir(directory) rescue nil
    end
  end
  cache.write
end

#variant(name, options = {}) ⇒ Object

Define a variant, or within a with_variants block, query if it is active.

Parameters:

  • name (String)

    Variant name.

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

    Optional parameters.

Options Hash (options):

  • :default (String)

    Whether the variant is enabled by default (default: true).

  • :key (String)

    Variant key, used to name an Environment’s build directory. If nil, this variant will not contribute to the Environment’s build directory name.



269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/rscons/application.rb', line 269

def variant(name, options = {})
  if @active_variants
    !!@active_variants.find {|variant| variant[:name] == name}
  else
    if @variant_groups.empty?
      variant_group
    end
    options = options.dup
    options[:name] = name
    options[:enabled] = options.fetch(:default, true)
    options[:key] = options.fetch(:key, name)
    @variant_groups.last[:variants] << options
  end
end

#variant_enabled?(variant_name) ⇒ Boolean

Check if a variant is enabled.

This can be used, for example, in a configuration block to omit or include configuration checks based on which variants have been configured.

Parameters:

  • variant_name (String)

    Variant name.

Returns:

  • (Boolean)

    Whether the requested variant is enabled.



295
296
297
298
299
300
301
302
# File 'lib/rscons/application.rb', line 295

def variant_enabled?(variant_name)
  each_variant do |variant|
    if variant[:name] == variant_name
      return variant[:enabled]
    end
  end
  false
end

#variant_group(name, options = {}) ⇒ Object #variant_group(options = {}) ⇒ Object

Create a variant group.

Overloads:

  • #variant_group(name, options = {}) ⇒ Object

    Parameters:

    • name (String)

      Variant group name (optional).

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

      Optional variant group parameters.

  • #variant_group(options = {}) ⇒ Object

    Parameters:

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

      Optional variant group parameters.



314
315
316
317
318
319
320
321
322
323
# File 'lib/rscons/application.rb', line 314

def variant_group(*args, &block)
  if args.first.is_a?(String)
    name = args.slice!(0)
  end
  options = args.first || {}
  @variant_groups << options.merge(name: name, variants: [])
  if block
    block[]
  end
end

#with_variants(&block) ⇒ Object

Iterate through enabled variants.

The given block is called for each combination of enabled variants across the defined variant groups.



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/rscons/application.rb', line 329

def with_variants(&block)
  if @active_variants
    raise "with_variants cannot be called within another with_variants block"
  end
  if @variant_groups.empty?
    raise "with_variants cannot be called with no variants defined"
  end
  iter_vgs = lambda do |iter_variants|
    if iter_variants.size == @variant_groups.size
      @active_variants = iter_variants.compact
      block[]
      @active_variants = nil
    else
      @variant_groups[iter_variants.size][:variants].each do |variant|
        if variant[:enabled]
          iter_vgs[iter_variants + [variant]]
        end
      end
    end
  end
  iter_vgs[[]]
end