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



54
55
56
57
58
59
60
61
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
# File 'lib/rscons/script.rb', line 54

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:



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

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

def glob(*patterns)
  Util.glob(*patterns)
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.



152
153
154
# File 'lib/rscons/script.rb', line 152

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.



180
181
182
# File 'lib/rscons/script.rb', line 180

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.



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

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.



170
171
172
# File 'lib/rscons/script.rb', line 170

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.



190
191
192
193
194
195
# File 'lib/rscons/script.rb', line 190

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.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/rscons/script.rb', line 204

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.



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/rscons/script.rb', line 255

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(name, options = {}, &block) ⇒ Object

Create or modify a task.

The action block given will be invoked by Rscons when the task executes. It will be passed two arguments:

1) The Task object.
2) A Hash of task parameter names and values.

Parameters:

  • name (String)

    Task name.

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

    Optional task attributes.

Options Hash (options):

  • :autoconf (Boolean)

    Whether to automatically configure before running this task (default: true).

  • :desc (String)

    Description for the task.

  • :depends (Array<String>)

    Dependencies of the task.

  • :params (Array<Task::Param>)

    Task parameter definitions.



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

def task(*args, &block)
  Util.task(*args, &block)
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.



321
322
323
# File 'lib/rscons/script.rb', line 321

def variant(name, options = {})
  Rscons.application.variant(name, options)
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.



336
337
338
# File 'lib/rscons/script.rb', line 336

def variant_enabled?(variant_name)
  Rscons.application.variant_enabled?(variant_name)
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.



350
351
352
# File 'lib/rscons/script.rb', line 350

def variant_group(*args, &block)
  Rscons.application.variant_group(*args, &block)
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.



358
359
360
361
# File 'lib/rscons/script.rb', line 358

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