module Rscons

Namespace module for rscons classes

Constants

DEFAULT_BUILDERS

Names of the default builders which will be added to all newly created {Environment} objects.

VERSION

gem version

Attributes

do_ansi_color[RW]

@return [Boolean]

Whether to output ANSI color escape sequences.
n_threads[RW]

@return [Integer]

The number of threads to use when scheduling subprocesses.
vars[R]

@since 1.16.0 @return [VarSet]

Access any variables set on the rscons command-line.

Public Class Methods

absolute_path?(path) click to toggle source

Return whether the given path is an absolute filesystem path.

@param path [String] the path to examine.

@return [Boolean] Whether the given path is an absolute filesystem path.

# File lib/rscons.rb, line 88
def absolute_path?(path)
  if RUBY_PLATFORM =~ /mingw/
    path =~ %r{^(?:\w:)?[\\/]}
  else
    path.start_with?("/")
  end
end
clean() click to toggle source

Remove all generated files.

@return [void]

# File lib/rscons.rb, line 67
def clean
  cache = Cache.instance
  # remove all built files
  cache.targets.each do |target|
    FileUtils.rm_f(target)
  end
  # remove all created directories if they are empty
  cache.directories.sort {|a, b| b.size <=> a.size}.each do |directory|
    next unless File.directory?(directory)
    if (Dir.entries(directory) - ['.', '..']).empty?
      Dir.rmdir(directory) rescue nil
    end
  end
  cache.clear
end
command_executer() click to toggle source

Return an Array containing a command used to execute commands.

This will normally be an empty Array, but on Windows if Rscons detects that it is running in MSYS then [“env”] will be returned.

@return [Array<String>] Command used to execute commands.

# File lib/rscons.rb, line 151
def command_executer
  @command_executer ||=
    if Object.const_get("RUBY_PLATFORM") =~ /mingw/
      if ENV.keys.find {|key| key =~ /MSYS/}
        begin
          if IO.popen(["env", "echo", "success"]) {|io| io.read.strip} == "success"
            ["env"]
          end
        rescue
        end
      end
    end || []
end
command_executer=(val) click to toggle source

Set the command executer array.

@param val [Array<String>] Command used to execute commands.

@return [Array<String>] Command used to execute commands.

# File lib/rscons.rb, line 170
def command_executer=(val)
  @command_executer = val
end
get_system_shell() click to toggle source

Return the system shell and arguments for executing a shell command.

@return [Array<String>] The shell and flag.

# File lib/rscons.rb, line 118
def get_system_shell
  @shell ||=
    begin
      test_shell = lambda do |*args|
        begin
          "success" == IO.popen([*args, "echo success"]) do |io|
            io.read.strip
          end
        rescue
          false
        end
      end
      if ENV["SHELL"] and ENV["SHELL"] != "" and test_shell[ENV["SHELL"], "-c"]
        [ENV["SHELL"], "-c"]
      elsif Object.const_get("RUBY_PLATFORM") =~ /mingw/
        if test_shell["sh", "-c"]
          # Using Rscons from MSYS should use MSYS's shell.
          ["sh", "-c"]
        else
          ["cmd", "/c"]
        end
      else
        ["sh", "-c"]
      end
    end
end
glob(*patterns) click to toggle source

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

@since 1.16.0

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.

@return [Array<String>] Paths matching the specified pattern(s).

# File lib/rscons.rb, line 191
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
phony_target?(target) click to toggle source

Return whether the given target is a phony target.

@param target [Symbol, String] Target name.

@return [Boolean] Whether the given target is a phony target.

# File lib/rscons.rb, line 101
def phony_target?(target)
  target.is_a?(Symbol)
end
set_suffix(path, suffix) click to toggle source

Return a new path by changing the suffix in path to suffix.

@param path [String] The path to alter. @param suffix [String] The new filename suffix, e.g. “.exe”.

@return [String] New path.

# File lib/rscons.rb, line 111
def set_suffix(path, suffix)
  path.sub(/\.[^.]*$/, "") + suffix
end

Private Class Methods

determine_n_threads() click to toggle source

Determine the number of threads to use by default.

@return [Integer]

The number of threads to use by default.
# File lib/rscons.rb, line 209
def determine_n_threads
  # If the user specifies the number of threads in the environment, then
  # respect that.
  if ENV["RSCONS_NTHREADS"] =~ /^(\d+)$/
    return $1.to_i
  end

  # Otherwise try to figure out how many threads are available on the
  # host hardware.
  begin
    case RbConfig::CONFIG["host_os"]
    when /linux/
      return File.read("/proc/cpuinfo").scan(/^processor\s*:/).size
    when /mswin|mingw/
      if `wmic cpu get NumberOfLogicalProcessors /value` =~ /NumberOfLogicalProcessors=(\d+)/
        return $1.to_i
      end
    when /darwin/
      if `sysctl -n hw.ncpu` =~ /(\d+)/
        return $1.to_i
      end
    end
  rescue
  end

  # If we can't figure it out, default to 1.
  1
end