class Origen::Application::Target
Class to handle the target.
The target is a Ruby file that is run prior to generating each pattern, and it should be used to instantiate the top-level models used by the application. It can also be used to override and settings within these classes after they have been instantiated and before they are run.
All target files must live in Origen.root
/target.
An instance of this class is automatically instantiated and available globally as Origen.app
.target
Public Instance Methods
Returns Array
of all targets available
# File lib/origen/application/target.rb, line 127 def all_targets targets = [] find('').sort.each do |file| targets << File.basename(file) end targets # return end
As temporary=
except that the given target will be set to the workspace default
# File lib/origen/application/target.rb, line 227 def default=(name) if name self.temporary = name else @file = nil end save end
Load the default file from the workspace default if it exists and return it, otherwise returns nil
# File lib/origen/application/target.rb, line 280 def default_file return @default_file if @default_file if File.exist?(SAVE_FILE) File.open(SAVE_FILE) do |f| @default_file = Marshal.load(f) end elsif File.exist?(DEFAULT_FILE) @default_file = Pathname.new(DEFAULT_FILE) end @default_file end
Prints out the current target details to the command line
# File lib/origen/application/target.rb, line 237 def describe f = file! puts "Current target: #{f.basename}" puts '*' * 70 File.open(f).each do |line| puts " #{line}" end puts '*' * 70 end
Use this to implement a loop for each production target, it will automatically load each target before yielding to the block.
The production targets are defined by the production_targets
configuration option.
Example¶ ↑
Origen.app.target.each_production do Run something within the context of each target end
# File lib/origen/application/target.rb, line 58 def each_production(options = {}) options = { force_debug: false # Set true to force debug mode for all targets }.merge(options) prod_targets.each do |moo, targets| [targets].flatten.each do |target| self.temporary = target Origen.app.load_target!(options) yield moo end end end
As each_production
except it only yields unique targets. i.e. if you have two MOOs that use the same target file defined in the production_targets
then this method will only yield once.
An array of MOOs that use each target is returned each time.
Example¶ ↑
Origen.app.target.each_unique_production do |moos| Run something within the context of each unique target end
# File lib/origen/application/target.rb, line 80 def each_unique_production(options = {}) options = { force_debug: false # Set true to force debug mode for all targets }.merge(options) targets = {} prod_targets.each do |moo, moos_targets| [moos_targets].flatten.each do |target| if targets[target] targets[target] << moo else targets[target] = [moo] end end end targets.each do |target, moos| self.temporary = target Origen.app.load_target!(options) yield moos end end
Returns true if the target exists, this can be used to test for the presence of a target before calling one of the other methods to actually apply it.
It will return true if one or more targets are found matching the given name, use the unique? method to test if the given name uniquely identifies a valid target.
# File lib/origen/application/target.rb, line 146 def exists?(name) tgts = resolve_mapping(name) targets = tgts.is_a?(Array) ? tgts : find(tgts) targets.size > 0 end
Returns an array of matching target file paths
# File lib/origen/application/target.rb, line 248 def find(name) if name name = name.gsub('*', '') if File.exist?(name) [name] elsif File.exist?("#{Origen.root}/target/#{name}") && name != '' ["#{Origen.root}/target/#{name}"] else # The below weirdness is to make it recurse into symlinked directories Dir.glob("#{DIR}/**{,/*/**}/*").sort.uniq.select do |file| File.basename(file) =~ /#{name}/ && file !~ /.*\.rb.+$/ end end else [nil] end end
Remove the workspace default target
# File lib/origen/application/target.rb, line 322 def forget File.delete(SAVE_FILE) if File.exist?(SAVE_FILE) @default_file = nil end
Load the target, calling this will re-instantiate all top-level objects defined there.
# File lib/origen/application/target.rb, line 119 def load!(options = {}) options = { force_debug: false # Set true to force debug mode for all targets }.merge(options) Origen.app.load_target!(options) end
Implement a target loop based on the supplied options. The options can contain the keys :target or :targets or neither.
In the neither case the loop will run once for the current workspace default target.
In the case where one of the keys is present the loop will run for each target and the options will be passed into the block with the key :target set to the current target.
# File lib/origen/application/target.rb, line 34 def loop(options = {}) options = { set_target: true, force_debug: false # Set true to force debug mode for all targets }.merge(options) targets = [options.delete(:target), options.delete(:targets)].flatten.compact.uniq targets = [file!.basename.to_s] if targets.empty? set = options.delete(:set_target) targets.each do |target| Origen.load_target(target, options) if set options[:target] = target yield options end end
If the production_targets
moo number mapping inclues the current target then the MOO number will be returned, otherwise nil
# File lib/origen/application/target.rb, line 103 def moo prod_targets.each do |moo, targets| [targets].flatten.each do |target| return moo if File.basename(target, '.rb').to_s == file.basename('.rb').to_s end end nil end
Returns the name (the filename) of the current target
# File lib/origen/application/target.rb, line 113 def name file.basename('.rb').to_s if file end
# File lib/origen/application/target.rb, line 209 def proc @proc end
Returns an array containing all current production targets
# File lib/origen/application/target.rb, line 136 def production_targets prod_targets.map { |_moo, targets| targets }.uniq end
@api private
# File lib/origen/application/target.rb, line 221 def set_signature(options) options ||= {} @signature = options.merge(_tname: name).to_a.hash end
Returns a signature for the current target, can be used to track target changes in cases where the name is not unique - i.e. when using a configurable target
# File lib/origen/application/target.rb, line 216 def signature @signature ||= set_signature(nil) end
Switch to the supplied target, name can be a fragment as long as it allows a unique target to be identified.
The name can also be a MOO number mapping from the config.production_targets attribute of the application.
Calling this method does not affect the default target setting in the workspace.
The target can also be set to a proc to be called instead, this is really intended to be used for testing purposes:
Origen.target.temporary = -> { $dut = SomeLocalClass.new }
# File lib/origen/application/target.rb, line 172 def temporary=(name) if name.is_a?(Proc) self.file = nil @proc = name return else @proc = nil end tgts = resolve_mapping(name) targets = tgts.is_a?(Array) ? tgts : find(tgts) if targets.size == 0 puts "Sorry no targets were found matching '#{name}'!" puts 'Here are the available options:' find('').sort.each do |file| puts File.basename(file) end exit 1 elsif targets.size > 1 if is_a_moo_number?(name) && prod_targets puts "Multiple production targets exist for #{name.upcase}, use one of the following instead of the MOO number:" targets.sort.each do |file| puts File.basename(file) end else puts 'Please try again with one of the following targets:' targets.sort.each do |file| puts File.basename(file) end end exit 1 else self.file = targets[0] end end
Returns true if running with a temporary target, i.e. if the current target is not the same as the default target
# File lib/origen/application/target.rb, line 352 def temporary? @file == @default_file end
Similar to the exists? method, this will return true only if the given name resolves to a single valid target.
# File lib/origen/application/target.rb, line 155 def unique?(name) tgts = resolve_mapping(name) targets = tgts.is_a?(Array) ? tgts : find(tgts) targets.size == 1 end
Not a clean unload, but allows objects to be re-instantiated for testing @api private
# File lib/origen/application/target.rb, line 21 def unload! Origen.app.unload_target! end