Class: Rscons::Script::GlobalDsl

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

Overview

Global DSL methods.

Direct Known Subclasses

ConfigureDsl, TopLevelDsl

Instance Method Summary collapse

Constructor Details

#initialize(script) ⇒ GlobalDsl

Create a GlobalDsl.



9
10
11
# File 'lib/rscons/script.rb', line 9

def initialize(script)
  @script = script
end

Instance Method Details

#build_dirString

Get the Rscons build directory path.

Returns:

  • (String)

    Rscons build directory path.



17
18
19
# File 'lib/rscons/script.rb', line 17

def build_dir
  Rscons.application.build_dir
end

#download(url, dest, options = {}) ⇒ void

This method returns an undefined value.

Download a file.

Parameters:

  • url (String)

    URL.

  • dest (String)

    Path to where to save the file.

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

    Options.

Options Hash (options):

  • :sha256sum (String)

    Expected file checksum.

  • :redirect_limit (Integer)

    Maximum number of times to allow HTTP redirection (default 5).



62
63
64
65
66
67
68
69
70
71
72
73
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/script.rb', line 62

def download(url, dest, options = {})
  options[:redirect_limit] ||= 5
  unless options[:redirected]
    if File.exist?(dest) && options[:sha256sum]
      if Digest::SHA2.hexdigest(File.binread(dest)) == options[:sha256sum]
        # Destination file already exists and has the expected checksum.
        return
      end
    end
  end
  uri = URI(url)
  use_ssl = url.start_with?("https://")
  response = nil
  socketerror_message = ""
  digest = Digest::SHA2.new
  begin
    Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl) do |http|
      File.open(dest, "wb") do |fh|
        response = http.get(uri.request_uri) do |data|
          fh.write(data)
          digest << data
        end
      end
    end
  rescue SocketError => e
    raise RsconsError.new("Error downloading #{dest}: #{e.message}")
  end
  if response.is_a?(Net::HTTPRedirection)
    if options[:redirect_limit] == 0
      raise RsconsError.new("Redirect limit reached when downloading #{dest}")
    else
      return download(response["location"], dest, options.merge(redirect_limit: options[:redirect_limit] - 1, redirected: true))
    end
  end
  unless response.is_a?(Net::HTTPSuccess)
    raise RsconsError.new("Error downloading #{dest}")
  end
  if options[:sha256sum] && options[:sha256sum] != digest.hexdigest
    raise RsconsError.new("Unexpected checksum on #{dest}")
  end
end

#env(name, options) ⇒ Object #env(options) ⇒ Object

Create an environment.

If a block is given it is immediately executed and passed the newly created Environment object as an argument. If the Environment is created in task context, the process method is immediately called. Otherwise, the Environment is not processed until the task execution phase.

Overloads:

  • #env(name, options) ⇒ Object

    Parameters:

    • name (String)

      Environment name. This determines the folder name used to store all environment build files under the top-level build directory.

    • options (Hash)

    Options Hash (options):

    • :echo (Symbol)

      :command, :short, or :off (default :short)

    • :exclude_builders (Boolean)

      Whether to omit adding default builders (default false)

    • :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.

  • #env(options) ⇒ Object

    Parameters:

    • options (Hash)

    Options Hash (options):

    • :echo (Symbol)

      :command, :short, or :off (default :short)

    • :exclude_builders (Boolean)

      Whether to omit adding default builders (default false)

    • :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.

Yields:

Yield Parameters:



141
142
143
144
145
146
147
148
# File 'lib/rscons/script.rb', line 141

def env(*args, &block)
  Rscons.application.check_configure
  e = Environment.new(*args, &block)
  if Rscons.application.task_execution_phase?
    e.process
  end
  e
end

#glob(*patterns) ⇒ Array<String>

Return a list of paths matching the specified pattern(s).

A pattern can contain a “/**” component to recurse through directories. If the pattern ends with “/**” then only the recursive list of directories will be returned.

Examples:

  • “src/**”: return all directories under “src”, recursively (including “src” itself).

  • “src/*/”: return all files and directories recursively under the src directory.

  • “src/*/.c”: return all .c files recursively under the src directory.

  • “dir/*/”: return all directories in dir, but no files.

Returns:

  • (Array<String>)

    Paths matching the specified pattern(s).



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rscons/script.rb', line 36

def glob(*patterns)
  require "pathname"
  patterns.reduce([]) do |result, pattern|
    if pattern.end_with?("/**")
      pattern += "/"
    end
    result += Dir.glob(pattern).map do |path|
      Pathname.new(path.gsub("\\", "/")).cleanpath.to_s
    end
  end.sort
end

#param(name, value, takes_arg, description) ⇒ Object

Construct a task parameter.

Parameters:

  • name (String)

    Param name.

  • value (String, nil)

    Param value.

  • takes_arg (String)

    Whether the parameter takes an argument.

  • description (String)

    Param description.



160
161
162
# File 'lib/rscons/script.rb', line 160

def param(name, value, takes_arg, description)
  Task::Param.new(name, value, takes_arg, description)
end

#path_append(path) ⇒ void

This method returns an undefined value.

Append a path component to the PATH variable.

Parameters:

  • path (String)

    Path to append.



188
189
190
# File 'lib/rscons/script.rb', line 188

def path_append(path)
  path_set(path_components + [File.expand_path(path)])
end

#path_componentsArray<String>

Return path components from the PATH variable.

Returns:

  • (Array<String>)

    Path components from the PATH variable.



168
169
170
# File 'lib/rscons/script.rb', line 168

def path_components
  ENV["PATH"].split(File::PATH_SEPARATOR)
end

#path_prepend(path) ⇒ void

This method returns an undefined value.

Prepend a path component to the PATH variable.

Parameters:

  • path (String)

    Path to prepend.



178
179
180
# File 'lib/rscons/script.rb', line 178

def path_prepend(path)
  path_set([File.expand_path(path)] + path_components)
end

#path_set(new_path) ⇒ void

This method returns an undefined value.

Set the PATH variable.

Parameters:

  • new_path (String, Array<String>)

    New PATH variable value as an array or string.



198
199
200
201
202
203
# File 'lib/rscons/script.rb', line 198

def path_set(new_path)
  if new_path.is_a?(Array)
    new_path = new_path.join(File::PATH_SEPARATOR)
  end
  ENV["PATH"] = new_path
end

#rscons(path, *args) ⇒ Object

Invoke rscons in a subprocess for a subsidiary Rsconscript file.

Parameters:

  • path (String)

    Path to subsidiary Rsconscript to execute, or path to subsidiary directory to run rscons in.

  • args (Array<String>)

    Arguments to pass to rscons subprocess.



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/rscons/script.rb', line 212

def rscons(path, *args)
  rscons_path = File.expand_path($0)
  path = File.expand_path(path)
  if File.directory?(path)
    command = [*args]
    dir = path
  else
    command = ["-f", path, *args]
    dir = File.dirname(path)
  end
  if File.exist?("#{dir}/rscons")
    rscons_path = "#{dir}/rscons"
  end
  command = [rscons_path] + command
  print_dir = dir != "." && dir != File.expand_path(Dir.pwd)
  if ENV["specs"] and not ENV["dist_specs"] # specs
    command = ["ruby", $LOAD_PATH.map {|p| ["-I", p]}, command].flatten # specs
  end # specs
  puts "rscons: Entering directory '#{dir}'" if print_dir
  result = system(*command, chdir: dir)
  puts "rscons: Leaving directory '#{dir}'" if print_dir
  unless result
    raise RsconsError.new("Failed command: " + command.join(" "))
  end
end

#sh(command, options = {}) ⇒ Object #sh(*command, options = {}) ⇒ Object

Execute a shell command, exiting on failure. The behavior to exit on failure is suppressed if the :continue option is given.

Overloads:

  • #sh(command, options = {}) ⇒ Object

    Parameters:

    • command (String, Array<String>)

      Command to execute. The command is executed and interpreted by the system shell when given as a single string. It is not passed to the system shell if the array size is greater than 1.

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

      Options.

    Options Hash (options):

    • :continue (Boolean)

      If set to true, rscons will continue executing afterward, even if the command fails.

  • #sh(*command, options = {}) ⇒ Object

    Parameters:

    • command (String, Array<String>)

      Command to execute. The command is executed and interpreted by the system shell when given as a single string. It is not passed to the system shell if the array size is greater than 1.

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

      Options.

    Options Hash (options):

    • :continue (Boolean)

      If set to true, rscons will continue executing afterward, even if the command fails.



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/rscons/script.rb', line 263

def sh(*command)
  options = {}
  if command.last.is_a?(Hash)
    options = command.slice!(-1)
  end
  continue = options.delete(:continue)
  if command.size == 1 && command[0].is_a?(Array)
    command = command[0]
  end
  if Rscons.application.verbose
    if command.size > 1
      puts Util.command_to_s(command)
    else
      puts command[0]
    end
  end
  begin
    system(*command, options.merge(exception: true))
  rescue StandardError => e
    message = "#{e.backtrace[2]}: #{e.message}"
    if continue
      Ansi.write($stderr, :red, message, :reset, "\n")
    else
      raise RsconsError.new(message)
    end
  end
end

#task(*args, &block) ⇒ Object

Create or modify a task.



292
293
294
# File 'lib/rscons/script.rb', line 292

def task(*args, &block)
  Util.task(*args, &block)
end

#variant(*args) ⇒ Object

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



298
299
300
# File 'lib/rscons/script.rb', line 298

def variant(*args)
  Rscons.application.variant(*args)
end

#variant_enabled?(*args) ⇒ Boolean

Check if a variant is enabled.

Returns:

  • (Boolean)


303
304
305
# File 'lib/rscons/script.rb', line 303

def variant_enabled?(*args)
  Rscons.application.variant_enabled?(*args)
end

#variant_group(*args, &block) ⇒ Object

Create a variant group.



308
309
310
# File 'lib/rscons/script.rb', line 308

def variant_group(*args, &block)
  Rscons.application.variant_group(*args, &block)
end

#with_variants(&block) ⇒ Object

Iterate through variants.



313
314
315
316
# File 'lib/rscons/script.rb', line 313

def with_variants(&block)
  Rscons.application.enable_variants
  Rscons.application.with_variants(&block)
end