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

Subversion Repositories soc_maker

[/] [soc_maker/] [trunk/] [lib/] [soc_maker/] [lib.rb] - Blame information for rev 5

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

Line No. Rev Author Line
1 3 feddischso
###############################################################
2
#
3
#  File:      spc_lib.rb
4
#
5
#  Author:    Christian Hättich
6
#
7
#  Project:   System-On-Chip Maker
8
#
9
#  Target:    Linux / Windows / Mac
10
#
11
#  Language:  ruby
12
#
13
#
14
###############################################################
15
#
16
#
17
#   Copyright (C) 2014  Christian Hättich  - feddischson [ at ] opencores.org
18
#
19
#   This program is free software: you can redistribute it and/or modify
20
#   it under the terms of the GNU General Public License as published by
21
#   the Free Software Foundation, either version 3 of the License, or
22
#   (at your option) any later version.
23
#
24
#   This program is distributed in the hope that it will be useful,
25
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
26
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
#   GNU General Public License for more details.
28
#
29
#   You should have received a copy of the GNU General Public License
30
#   along with this program.  If not, see .
31
#
32
#
33
###############################################################
34
#
35
#   Description:
36
#     This class represents the library, which holds all
37
#       - cores (core-definitions)
38
#       - interfaces (interface-specifications)
39
#
40
#
41
####
42
#
43
#
44
#
45
###############################################################
46
 
47
module SOCMaker
48
class Lib
49
 
50
  def initialize
51
 
52
    # will store all cores
53
    @cores_lib      = {}
54
 
55
    # will store the versions of all cores { name => { ver1, ver2, ver3 } }
56
    @cores_ver  = {}
57
 
58
    # will store all interfaces
59
    @ifc_lib      = {}
60
 
61
    # will store the versions of all interfaces { name => { ver1, ver2, ver3 } }
62
    @ifc_ver  = {}
63
 
64
 
65
    # we remember paths, which we've already processed
66
    @path_lut = []
67
 
68
  end
69
 
70
 
71
  def clear
72
    @cores_lib.clear
73
    @cores_ver.clear
74
    @ifc_lib.clear
75
    @ifc_ver.clear
76
    @path_lut.clear
77
  end
78
 
79
 
80
  # refreshes the core library:
81
  # it useses the global configuration entry cores_search_path,
82
  # which defines, where to search for inc_fname (defined in soc_maker_conf.rb) files.
83
  # For each directory, we call process_include
84
  def refresh( paths = nil )
85
 
86
    paths = [ paths ] if paths.is_a?( String )
87
 
88
 
89
    SOCMaker::logger.info  "START REFRESHING CORE LIBRARY"
90
 
91
    # clear the libs
92
    clear
93
 
94
    # use argument if given, otherwise config paths
95
    paths ||= SOCMaker::conf[ :cores_search_path ]
96
 
97
 
98
    paths.each do |dir|
99
      process_include dir
100
    end
101
    SOCMaker::logger.info  "DONE REFRESHING CORE LIBRARY"
102
 
103
  end
104
 
105
 
106
 
107
  def process_include( dir )
108
 
109
    #
110
    # this prevents the revursive call
111
    # from an infinite call
112
    #
113
    folder_sym = File.expand_path( dir ).to_sym
114
    if @path_lut.include?( folder_sym )
115
      SOCMaker::logger.warn( "double-include: infinite resursive search?" )
116
      raise SOCMaker::ERR::LibError.new( "", "double-include" )
117
    else
118
      @path_lut << folder_sym
119
    end
120
 
121
    # get all yaml files in the directory
122
    SOCMaker::logger.info  "search for include in: " + dir
123
 
124
 
125
    SOCMaker::from_s( get_all_yaml_in_str( dir ) ) do |o|
126
      o.dir = dir
127
      case o
128
      when SOCMaker::LibInc
129
        add_include( o, dir )
130
      when SOCMaker::CoreDef
131
        add_core( o )
132
      when SOCMaker::SOCDef
133
        add_core( o )
134
      when SOCMaker::IfcSpc
135
        add_ifc( o )
136
      else
137
        #TODO add error
138
      end
139
    end
140
 
141
  end
142
 
143
 
144
 
145
  def get_all_yaml_in_str( dir )
146
    yaml_str = ""
147
    Dir[ File.join( dir, "*.yaml" ) ].sort.each do |yaml_file|
148
      SOCMaker::logger.info "reading:" + yaml_file
149
      yaml_str << File.read( yaml_file )
150
    end
151
    return yaml_str
152
  end
153
 
154
 
155
 
156
  # gets an SOCMaker::LibInc object and iterates
157
  # over all folders.
158
  # Note: this is moved from process_include to this extra function
159
  # to support test capability
160
  def add_include( soc_inc_object, dir )
161
    soc_inc_object.dirs.each { |d| process_include( File.expand_path( File.join( dir, d ) ) ) }
162
  end
163
 
164
  def add_core( core )
165
    # generate key-string from name and vesion
166
    core_key = core.name + core.version
167
 
168
    # save core
169
    @cores_lib[ core_key ] = core
170
    @cores_ver[ core.name.to_sym ] = [] unless @cores_ver.has_key?(core.name.to_sym)
171
    @cores_ver[ core.name.to_sym ] << core.version
172
 
173
    SOCMaker::logger.info  "loaded "     +
174
                            core.name     +
175
                            " version "   +
176
                            core.version
177
  end
178
  def get_core( name, version = "" )
179
    core_key = name + version
180
    tmp = @cores_lib[ core_key ]
181
    check_nil( tmp, "Core '#{name}' version '#{version}' does not exist" )
182
    return tmp
183
  end
184
  def rm_core( core )
185
    core_key = core.name + core.version
186
    @cores_lib.delete( core_key )
187
  end
188
 
189
 
190
  def add_ifc( ifc )
191
    ifc_key = ifc.name + ifc.version
192
    @ifc_lib[ ifc_key         ] = ifc
193
    @ifc_ver[ ifc.name.to_sym ] = [] unless @ifc_ver.has_key?(ifc.name.to_sym)
194
    @ifc_ver[ ifc.name.to_sym ] << ifc.version
195
  end
196
  def get_ifc( name, version = "" )
197
    ifc_key = name + version
198
    tmp = @ifc_lib[ ifc_key ]
199
    check_nil( tmp, "Interface '#{name}' version '#{version}' does not exist" )
200
    return tmp
201
  end
202
  def rm_ifc( ifc )
203
    ifc_key = ifc.name + ifc.version
204
    @ifc_lib.delete( ifc_key )
205
  end
206
 
207
  def to_s
208
      "IP-Core - lib: \n"             +
209
      @cores_lib.keys.to_s            +
210
      "\n\nIP-Core - versions: \n"    +
211
      @cores_ver.to_s                 +
212
      "\n\nInterface - lib: \n"       +
213
      @ifc_lib.keys.to_s              +
214
      "\n\nInterface - versions: \n"  +
215
      @ifc_ver.to_s  + "\n"
216
  end
217
 
218
 
219
 
220
  def check_nil( var, error_msg = "")
221
    if var == nil
222
      SOCMaker::logger.error error_msg
223
      raise SOCMaker::ERR::LibError.new( "", error_msg )
224
    end
225
  end
226
 
227
 
228
 
229
  #
230
  # get all interfaces in a list
231
  #
232
  # TODO untested: do we need this?
233
  def get_ifcs( core )
234
    ifc_list = [];
235
    core.interfaces.values.each do |ifc; ifc_tmp|
236
      ifc_tmp = get_ifc( ifc[ :name ], ifc[ :version ] )
237
 
238
      # error handling
239
      if ifc_tmp == nil
240
        SOCMaker::logger.error  "Can't find #{ifc[ :name ]} version #{ifc[ :version ]} in SOC library"
241
        raise NameError, "Can't find #{ifc[ :name ]} version #{ifc[ :version ]} in SOC library"
242
      end
243
 
244
      # add interface to list
245
      ifc_list << ifc_tmp
246
    end
247
    return ifc_list
248
  end
249
 
250 5 feddischso
 
251
  #
252
  # TODO add test code
253
  #
254
  def cores
255
    @cores_lib.each do |nameversion,core|
256
      yield( nameversion.to_s, core )
257
    end
258
  end
259 3 feddischso
 
260
 
261
end #class Lib
262
end #Module SOCMaker
263
 
264
#
265
# vim: noai:ts=2:sw=2

powered by: WebSVN 2.1.0

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