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.



181
182
183
184
185
186
187
188
# File 'lib/rscons/application.rb', line 181

def check_configure
  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.



195
196
197
198
199
200
201
202
# File 'lib/rscons/application.rb', line 195

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.



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

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.



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rscons/application.rb', line 209

def configure
  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

#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.



169
170
171
172
173
174
# File 'lib/rscons/application.rb', line 169

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

#enable_variantsObject

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



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

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
# File 'lib/rscons/application.rb', line 74

def run(rsconscript, tasks_and_params, show_tasks, all_tasks, enabled_variants)
  Cache.instance["failed_commands"] = []
  @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
  @script.load(rsconscript)
  enable_variants
  if show_tasks
    show_script_tasks(all_tasks)
    return 0
  end
  apply_task_params(tasks_and_params)
  @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.



133
134
135
136
137
138
139
# File 'lib/rscons/application.rb', line 133

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)


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/rscons/application.rb', line 229

def uninstall
  cache = Cache.instance
  cache.targets(true).each do |target|
    cache.remove_target(target)
    next unless File.exists?(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.



254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/rscons/application.rb', line 254

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.



280
281
282
283
284
285
286
287
# File 'lib/rscons/application.rb', line 280

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

#variant_group(*args, &block) ⇒ Object

Create a variant group.



290
291
292
293
294
295
296
297
298
299
# File 'lib/rscons/application.rb', line 290

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.



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/rscons/application.rb', line 305

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