Module: Rscons::Util

Defined in:
lib/rscons/util.rb

Overview

A collection of stand-alone utility methods.

Class Method Summary collapse

Class Method Details

.absolute_path?(path) ⇒ Boolean

Return whether the given path is an absolute filesystem path.

Parameters:

  • path (String)

    the path to examine.

Returns:

  • (Boolean)

    Whether the given path is an absolute filesystem path.



11
12
13
14
15
16
17
# File 'lib/rscons/util.rb', line 11

def absolute_path?(path)
  if RUBY_PLATFORM =~ /mingw/
    path =~ %r{^(?:\w:)?[\\/]}
  else
    path.start_with?("/")
  end
end

.colorize_markup(message) ⇒ Array

Colorize a builder run message.

Parameters:

  • message (String)

    Builder run message.

Returns:

  • (Array)

    Colorized message with color codes for Ansi module.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rscons/util.rb', line 26

def colorize_markup(message)
  if message =~ /^(.*?)(<[^>]+>)(.*)$/
    prefix, code, suffix = $1, $2, $3
    case code
    when "<target>"
      code = :magenta
    when "<source>"
      code = :cyan
    when "<reset>"
      code = :reset
    end
    [prefix, code, *colorize_markup(suffix)].delete_if {|v| v == ""}
  else
    [message]
  end
end

.command_to_s(command) ⇒ String

Return a string representation of a command.

Parameters:

  • command (Array<String>)

    The command.

Returns:

  • (String)

    The string representation of the command.



50
51
52
# File 'lib/rscons/util.rb', line 50

def command_to_s(command)
  command.map { |c| c =~ /\s/ ? "'#{c}'" : c }.join(' ')
end

.determine_n_threadsInteger

Determine the number of threads to use by default.

Returns:

  • (Integer)

    The number of threads to use by default.



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

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

.find_executable(name) ⇒ String?

Look for an executable.

Returns:

  • (String, nil)

    Executable path, if found.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rscons/util.rb', line 108

def find_executable(name)
  if name["/"] or name["\\"]
    if File.file?(name) and File.executable?(name)
      return name
    end
  else
    path_entries = ENV["PATH"].split(File::PATH_SEPARATOR)
    path_entries.find do |path_entry|
      if path = test_path_for_executable(path_entry, name)
        return path
      end
    end
  end
end

.format_elapsed_time(elapsed) ⇒ String

Format an elapsed time in human-readable format.

Returns:

  • (String)

    Elapsed time in human-readable format.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/rscons/util.rb', line 127

def format_elapsed_time(elapsed)
  hours = (elapsed / (60 * 60)).to_i
  elapsed -= hours * 60 * 60
  minutes = (elapsed / 60).to_i
  elapsed -= minutes * 60
  seconds = elapsed.ceil
  result = ""
  if hours > 0
    result += "#{hours}h "
  end
  if hours > 0 || minutes > 0
    result += "#{minutes}m "
  end
  result += "#{seconds}s"
  result
end

.make_relative_path(path) ⇒ String

Make a relative path corresponding to a possibly absolute one.

Parameters:

  • path (String)

    Input path that is possibly absolute.

Returns:

  • (String)

    Relative path.



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rscons/util.rb', line 151

def make_relative_path(path)
  if absolute_path?(path)
    if path =~ %r{^(\w):(.*)$}
      "_#{$1}#{$2}"
    else
      "_#{path}"
    end
  else
    path
  end
end

.parse_dependency_file(mf_fname) ⇒ Array<String>

Parse dependencies from a Makefile or ldc2 dependency file.

This method is used internally by Rscons builders.

Parameters:

  • mf_fname (String)

    File name of the Makefile to read.

Returns:

  • (Array<String>)

    Paths of dependency files.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rscons/util.rb', line 172

def parse_dependency_file(mf_fname)
  deps = []
  buildup = ''
  File.read(mf_fname).each_line do |line|
    if line =~ /^(.*)\\\s*$/
      buildup += ' ' + $1
    else
      buildup += ' ' + line
      if buildup =~ /^[^:]+\(\S+\)\s*:.*?:[^:]+\((\S+)\)/
        # ldc2-style dependency line
        deps << $1
      elsif buildup =~ /^.*: (.*)$/
        # Makefile-style dependency line
        mf_deps = $1
        deps += mf_deps.split(' ').map(&:strip)
      end
      buildup = ''
    end
  end
  deps
end

.short_format_paths(paths) ⇒ String

Return a string showing the path specified, or if more than one, then the first path with a “(+D)” afterward, where D is the number of remaining paths.

Parameters:

  • paths (Array<String>)

    Paths.

Returns:

  • (String)

    Condensed path readout.



96
97
98
99
100
101
102
# File 'lib/rscons/util.rb', line 96

def short_format_paths(paths)
  if paths.size == 1
    paths.first
  else
    "#{paths.first} (+#{paths.size - 1})"
  end
end