OpenCores
URL https://opencores.org/ocsvn/soc_maker/soc_maker/trunk

Subversion Repositories soc_maker

[/] [soc_maker/] [trunk/] [lib/] [soc_maker/] [lib.rb] - Rev 5

Go to most recent revision | Compare with Previous | Blame | View Log

###############################################################
#   
#  File:      spc_lib.rb
#
#  Author:    Christian Hättich
#
#  Project:   System-On-Chip Maker
#
#  Target:    Linux / Windows / Mac
#
#  Language:  ruby
#
#
###############################################################
#
#
#   Copyright (C) 2014  Christian Hättich  - feddischson [ at ] opencores.org
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
###############################################################
#
#   Description:
#     This class represents the library, which holds all
#       - cores (core-definitions)
#       - interfaces (interface-specifications)
#
#
####
#
#
#
###############################################################

module SOCMaker
class Lib

  def initialize

    # will store all cores
    @cores_lib      = {}

    # will store the versions of all cores { name => { ver1, ver2, ver3 } }
    @cores_ver  = {}

    # will store all interfaces
    @ifc_lib      = {}

    # will store the versions of all interfaces { name => { ver1, ver2, ver3 } }
    @ifc_ver  = {}


    # we remember paths, which we've already processed
    @path_lut = []

  end


  def clear
    @cores_lib.clear
    @cores_ver.clear
    @ifc_lib.clear
    @ifc_ver.clear
    @path_lut.clear
  end


  # refreshes the core library:
  # it useses the global configuration entry cores_search_path,
  # which defines, where to search for inc_fname (defined in soc_maker_conf.rb) files.
  # For each directory, we call process_include
  def refresh( paths = nil )

    paths = [ paths ] if paths.is_a?( String )
    

    SOCMaker::logger.info  "START REFRESHING CORE LIBRARY"
    
    # clear the libs
    clear

    # use argument if given, otherwise config paths
    paths ||= SOCMaker::conf[ :cores_search_path ]


    paths.each do |dir|
      process_include dir
    end
    SOCMaker::logger.info  "DONE REFRESHING CORE LIBRARY"

  end



  def process_include( dir )

    #
    # this prevents the revursive call
    # from an infinite call
    #
    folder_sym = File.expand_path( dir ).to_sym
    if @path_lut.include?( folder_sym )
      SOCMaker::logger.warn( "double-include: infinite resursive search?" )
      raise SOCMaker::ERR::LibError.new( "", "double-include" )
    else
      @path_lut << folder_sym
    end
  
    # get all yaml files in the directory
    SOCMaker::logger.info  "search for include in: " + dir
    

    SOCMaker::from_s( get_all_yaml_in_str( dir ) ) do |o|
      o.dir = dir
      case o
      when SOCMaker::LibInc
        add_include( o, dir )
      when SOCMaker::CoreDef
        add_core( o )
      when SOCMaker::SOCDef
        add_core( o )
      when SOCMaker::IfcSpc
        add_ifc( o )
      else
        #TODO add error
      end
    end

  end



  def get_all_yaml_in_str( dir )
    yaml_str = ""
    Dir[ File.join( dir, "*.yaml" ) ].sort.each do |yaml_file|
      SOCMaker::logger.info "reading:" + yaml_file
      yaml_str << File.read( yaml_file )
    end
    return yaml_str
  end



  # gets an SOCMaker::LibInc object and iterates
  # over all folders.
  # Note: this is moved from process_include to this extra function
  # to support test capability
  def add_include( soc_inc_object, dir )
    soc_inc_object.dirs.each { |d| process_include( File.expand_path( File.join( dir, d ) ) ) }
  end

  def add_core( core )
    # generate key-string from name and vesion
    core_key = core.name + core.version

    # save core
    @cores_lib[ core_key ] = core
    @cores_ver[ core.name.to_sym ] = [] unless @cores_ver.has_key?(core.name.to_sym)
    @cores_ver[ core.name.to_sym ] << core.version
    
    SOCMaker::logger.info  "loaded "     + 
                            core.name     + 
                            " version "   + 
                            core.version
  end
  def get_core( name, version = "" )
    core_key = name + version
    tmp = @cores_lib[ core_key ]
    check_nil( tmp, "Core '#{name}' version '#{version}' does not exist" )
    return tmp
  end
  def rm_core( core )
    core_key = core.name + core.version
    @cores_lib.delete( core_key )
  end


  def add_ifc( ifc )
    ifc_key = ifc.name + ifc.version
    @ifc_lib[ ifc_key         ] = ifc
    @ifc_ver[ ifc.name.to_sym ] = [] unless @ifc_ver.has_key?(ifc.name.to_sym)
    @ifc_ver[ ifc.name.to_sym ] << ifc.version
  end
  def get_ifc( name, version = "" )
    ifc_key = name + version
    tmp = @ifc_lib[ ifc_key ]
    check_nil( tmp, "Interface '#{name}' version '#{version}' does not exist" )
    return tmp
  end
  def rm_ifc( ifc )
    ifc_key = ifc.name + ifc.version
    @ifc_lib.delete( ifc_key )
  end

  def to_s
      "IP-Core - lib: \n"             +
      @cores_lib.keys.to_s            +
      "\n\nIP-Core - versions: \n"    +
      @cores_ver.to_s                 +
      "\n\nInterface - lib: \n"       +
      @ifc_lib.keys.to_s              +
      "\n\nInterface - versions: \n"  +
      @ifc_ver.to_s  + "\n" 
  end



  def check_nil( var, error_msg = "")
    if var == nil
      SOCMaker::logger.error error_msg
      raise SOCMaker::ERR::LibError.new( "", error_msg )
    end
  end



  #
  # get all interfaces in a list
  #
  # TODO untested: do we need this?
  def get_ifcs( core )
    ifc_list = [];
    core.interfaces.values.each do |ifc; ifc_tmp|
      ifc_tmp = get_ifc( ifc[ :name ], ifc[ :version ] )
      
      # error handling
      if ifc_tmp == nil
        SOCMaker::logger.error  "Can't find #{ifc[ :name ]} version #{ifc[ :version ]} in SOC library"
        raise NameError, "Can't find #{ifc[ :name ]} version #{ifc[ :version ]} in SOC library"
      end

      # add interface to list
      ifc_list << ifc_tmp
    end
    return ifc_list
  end


  #
  # TODO add test code
  #
  def cores
    @cores_lib.each do |nameversion,core|
      yield( nameversion.to_s, core )
    end
  end
  

end #class Lib
end #Module SOCMaker

#
# vim: noai:ts=2:sw=2

Go to most recent revision | Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.