Class: Udb::Resolver
- Inherits:
-
Object
- Object
- Udb::Resolver
- Extended by:
- T::Sig
- Defined in:
- lib/udb/resolver.rb,
lib/udb/schema.rb
Overview
The primary interface for users will be #cfg_arch_for
Defined Under Namespace
Classes: ConfigInfo
Constant Summary collapse
- SCHEMAS_BASE_URL =
"https://riscv.github.io/riscv-unified-db/schemas"
Instance Attribute Summary collapse
-
#cfgs_path ⇒ Object
readonly
Returns the value of attribute cfgs_path.
-
#custom_path ⇒ Object
readonly
Returns the value of attribute custom_path.
-
#gen_path ⇒ Object
readonly
Returns the value of attribute gen_path.
-
#schemas_path ⇒ Object
readonly
Returns the value of attribute schemas_path.
-
#std_path ⇒ Object
readonly
Returns the value of attribute std_path.
Instance Method Summary collapse
- #any_newer?(target, deps) ⇒ Boolean
- #cfg_arch_for(config_path_or_name) ⇒ Object
- #cfg_arch_for_data(config_data) ⇒ Object
- #cfg_arch_for_pointer(pointer, relative_dir:) ⇒ Object
- #cfg_info(config_path_or_name) ⇒ Object
-
#initialize(repo_root = Udb.repo_root, schemas_path_override: nil, cfgs_path_override: nil, gen_path_override: nil, std_path_override: nil, custom_path_override: nil, quiet: false, compile_idl: false) ⇒ Resolver
constructor
A new instance of Resolver.
- #merge_arch(config_yaml) ⇒ Object
- #merged_spec_path(cfg_path_or_name) ⇒ Object
- #resolve_arch(config_yaml) ⇒ Object
- #resolve_config(config_path) ⇒ Object
- #resolve_schemas ⇒ Object
- #resolved_spec_path(cfg_path_or_name) ⇒ Object
- #run(cmd) ⇒ Object
Constructor Details
#initialize(repo_root = Udb.repo_root, schemas_path_override: nil, cfgs_path_override: nil, gen_path_override: nil, std_path_override: nil, custom_path_override: nil, quiet: false, compile_idl: false) ⇒ Resolver
Returns a new instance of Resolver.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/udb/resolver.rb', line 98 def initialize( repo_root = Udb.repo_root, schemas_path_override: nil, cfgs_path_override: nil, gen_path_override: nil, std_path_override: nil, custom_path_override: nil, quiet: false, compile_idl: false ) @repo_root = repo_root @schemas_path = schemas_path_override || Udb.default_schemas_path @cfgs_path = cfgs_path_override || Udb.default_cfgs_path @gen_path = gen_path_override || Udb.default_gen_path @std_path = std_path_override || Udb.default_std_isa_path @custom_path = custom_path_override || Udb.default_custom_isa_path @quiet = quiet @compile_idl = compile_idl @mutex = Thread::Mutex.new # cache of config names @cfg_info = T.let(Concurrent::Hash.new, T::Hash[T.any(String, Pathname), ConfigInfo]) FileUtils.mkdir_p @gen_path end |
Instance Attribute Details
#cfgs_path ⇒ Object (readonly)
Returns the value of attribute cfgs_path.
44 45 46 |
# File 'lib/udb/resolver.rb', line 44 def cfgs_path @cfgs_path end |
#custom_path ⇒ Object (readonly)
Returns the value of attribute custom_path.
56 57 58 |
# File 'lib/udb/resolver.rb', line 56 def custom_path @custom_path end |
#gen_path ⇒ Object (readonly)
Returns the value of attribute gen_path.
48 49 50 |
# File 'lib/udb/resolver.rb', line 48 def gen_path @gen_path end |
#schemas_path ⇒ Object (readonly)
Returns the value of attribute schemas_path.
40 41 42 |
# File 'lib/udb/resolver.rb', line 40 def schemas_path @schemas_path end |
#std_path ⇒ Object (readonly)
Returns the value of attribute std_path.
52 53 54 |
# File 'lib/udb/resolver.rb', line 52 def std_path @std_path end |
Instance Method Details
#any_newer?(target, deps) ⇒ Boolean
126 127 128 129 130 131 132 |
# File 'lib/udb/resolver.rb', line 126 def any_newer?(target, deps) if target.exist? deps.any? { |d| target.mtime < d.mtime } else true end end |
#cfg_arch_for(config_path_or_name) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/udb/resolver.rb', line 335 def cfg_arch_for(config_path_or_name) config_info = cfg_info(config_path_or_name) @cfg_archs ||= Concurrent::Hash.new return @cfg_archs[config_info.path] if @cfg_archs.key?(config_info.path) resolve_config(config_info.path) resolve_arch(config_info.unresolved_yaml) @mutex.synchronize do return @cfg_archs[config_info.path] if @cfg_archs.key?(config_info.path) @cfg_archs[config_info.path] = Udb::ConfiguredArchitecture.new( config_info.name, Udb::AbstractConfig.create(gen_path / "cfgs" / "#{config_info.name}.yaml", config_info) ) end end |
#cfg_arch_for_data(config_data) ⇒ Object
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/udb/resolver.rb', line 359 def cfg_arch_for_data(config_data) info = ConfigInfo.new( name: config_data["name"], path: Pathname.new("portfolio/#{config_data["name"]}"), overlay_path: nil, unresolved_yaml: config_data, spec_path: std_path, merged_spec_path: @gen_path / "spec" / "_", resolved_spec_path: @gen_path / "resolved_spec" / "_", resolver: self ) Udb::ConfiguredArchitecture.new( config_data["name"], Udb::AbstractConfig.create_from_data(config_data, info) ) end |
#cfg_arch_for_pointer(pointer, relative_dir:) ⇒ Object
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/udb/resolver.rb', line 315 def cfg_arch_for_pointer(pointer, relative_dir:) if (@cfgs_path / "#{pointer}.yaml").file? # Repo config name — may contain '/' for nested configs (e.g. "profile/RVA23U64"). cfg_arch_for(pointer) elsif pointer.end_with?(".yaml", ".yml") || Pathname.new(pointer).absolute? || pointer.start_with?("./", "../") # Explicit file path — resolve relative to the caller's directory. path = Pathname.new(pointer) cfg_arch_for(path.absolute? ? path : (relative_dir / path).cleanpath) else raise ArgumentError, "Cannot resolve config pointer '#{pointer}': not a known config name " \ "under '#{@cfgs_path}' and not a recognisable file path " \ "(.yaml/.yml extension, absolute, or starting with ./ or ../)" end end |
#cfg_info(config_path_or_name) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/udb/resolver.rb', line 238 def cfg_info(config_path_or_name) return @cfg_info.fetch(config_path_or_name) if config_path_or_name.is_a?(String) && @cfg_info.key?(config_path_or_name) return @cfg_info.fetch(config_path_or_name.realpath) if config_path_or_name.is_a?(Pathname) && @cfg_info.key?(config_path_or_name.realpath) @mutex.synchronize do return @cfg_info.fetch(config_path_or_name) if config_path_or_name.is_a?(String) && @cfg_info.key?(config_path_or_name) return @cfg_info.fetch(config_path_or_name.realpath) if config_path_or_name.is_a?(Pathname) && @cfg_info.key?(config_path_or_name.realpath) config_path = case config_path_or_name when Pathname raise "Path does not exist: #{config_path_or_name}" unless config_path_or_name.file? config_path_or_name.realpath when String if (@cfgs_path / "#{config_path_or_name}.yaml").file? (@cfgs_path / "#{config_path_or_name}.yaml").realpath else raise ConfigNotFoundError, "Could not find config: #{config_path_or_name}" end else T.absurd(config_path_or_name) end config_yaml = YAML.safe_load_file(config_path) if config_yaml.nil? puts File.read(config_path) raise "Could not load config at #{config_path}" end = if config_yaml["arch_overlay"].nil? nil elsif Pathname.new(config_yaml["arch_overlay"]).exist? Pathname.new(config_yaml["arch_overlay"]) elsif (@custom_path / config_yaml["arch_overlay"]).exist? @custom_path / config_yaml["arch_overlay"] else raise "Cannot resolve path to overlay (#{config_yaml["arch_overlay"]})" end merged_spec_path = if .nil? @gen_path / "spec" / "_" else @gen_path / "spec" / config_yaml["name"] end resolved_spec_path = if .nil? @gen_path / "resolved_spec" / "_" else @gen_path / "resolved_spec" / config_yaml["name"] end info = ConfigInfo.new( name: config_yaml["name"], path: config_path, overlay_path:, unresolved_yaml: config_yaml, spec_path: std_path, merged_spec_path: @gen_path / "spec" / (.nil? ? "_" : config_yaml["name"]), resolved_spec_path: @gen_path / "resolved_spec" / (.nil? ? "_" : config_yaml["name"]), resolver: self ) @cfg_info[config_path] = info @cfg_info[info.name] = info end end |
#merge_arch(config_yaml) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/udb/resolver.rb', line 173 def merge_arch(config_yaml) @mutex.synchronize do config_name = config_yaml["name"] deps = Dir[std_path / "**" / "*.yaml"].map { |p| Pathname.new(p) } deps += Dir[custom_path / config_yaml["arch_overlay"] / "**" / "*.yaml"].map { |p| Pathname.new(p) } unless config_yaml["arch_overlay"].nil? = if config_yaml["arch_overlay"].nil? nil else if config_yaml.fetch("arch_overlay")[0] == "/" Pathname.new(config_yaml.fetch("arch_overlay")) else custom_path / config_yaml.fetch("arch_overlay") end end raise "custom directory '#{}' does not exist" if !.nil? && !.directory? FileUtils.mkdir_p(@gen_path / "spec") merge_lock_name = merged_spec_path(config_name).basename File.open(@gen_path / "spec" / ".#{merge_lock_name}.lock", File::CREAT | File::RDWR) do |f| f.flock(File::LOCK_EX) if any_newer?(merged_spec_path(config_name) / ".stamp", deps) # Use Ruby YAML resolver instead of Python yaml_resolver = Udb::Yaml::Resolver.new(quiet: @quiet, compile_idl: @compile_idl) yaml_resolver.merge_files( std_path.to_s, &.to_s, merged_spec_path(config_name).to_s ) FileUtils.touch(merged_spec_path(config_name) / ".stamp") end end end end |
#merged_spec_path(cfg_path_or_name) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/udb/resolver.rb', line 60 def merged_spec_path(cfg_path_or_name) if cfg_info(cfg_path_or_name)..nil? @gen_path / "spec" / "_" else @gen_path / "spec" / cfg_info(cfg_path_or_name).name end end |
#resolve_arch(config_yaml) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/udb/resolver.rb', line 211 def resolve_arch(config_yaml) merge_arch(config_yaml) @mutex.synchronize do config_name = config_yaml["name"] FileUtils.mkdir_p(@gen_path / "resolved_spec") resolve_lock_name = resolved_spec_path(config_name).basename File.open(@gen_path / "resolved_spec" / ".#{resolve_lock_name}.lock", File::CREAT | File::RDWR) do |f| f.flock(File::LOCK_EX) deps = Dir[merged_spec_path(config_name) / "**" / "*.yaml"].map { |p| Pathname.new(p) } if any_newer?(resolved_spec_path(config_name) / ".stamp", deps) # Use Ruby YAML resolver instead of Python yaml_resolver = Udb::Yaml::Resolver.new(quiet: @quiet, compile_idl: @compile_idl) yaml_resolver.resolve_files( merged_spec_path(config_name).to_s, resolved_spec_path(config_name).to_s, no_checks: false ) FileUtils.touch(resolved_spec_path(config_name) / ".stamp") end FileUtils.cp_r(std_path / "isa", resolved_spec_path(config_name)) end end end |
#resolve_config(config_path) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/udb/resolver.rb', line 149 def resolve_config(config_path) @mutex.synchronize do config_info = cfg_info(config_path) return T.must(config_info.resolved_yaml) unless config_info.resolved_yaml.nil? resolved_config_yaml = T.let({}, T.nilable(T::Hash[String, T.untyped])) # write the config with arch_overlay expanded if any_newer?(gen_path / "cfgs" / "#{config_info.name}.yaml", [config_path]) # is there anything to do here? validate? resolved_config_yaml = config_info.unresolved_yaml.dup resolved_config_yaml["$source"] = config_path.realpath.to_s FileUtils.mkdir_p gen_path / "cfgs" File.write(gen_path / "cfgs" / "#{config_info.name}.yaml", YAML.dump(resolved_config_yaml)) else resolved_config_yaml = YAML.load_file(gen_path / "cfgs" / "#{config_info.name}.yaml") end config_info.resolved_yaml = resolved_config_yaml end end |
#resolve_schemas ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/udb/resolver.rb', line 386 def resolve_schemas require "json" schemas_path.glob("*.json").each do |schema_file| next if schema_file.basename.to_s == "json-schema-draft-07.json" schema_data = JSON.parse(schema_file.read) version = schema_data["$id"] next if version.nil? schema_name = schema_file.basename.to_s resolved_id = "#{SCHEMAS_BASE_URL}/#{schema_name}/#{version}/#{schema_name}" resolved_schema = schema_data.merge("$id" => resolved_id) out_dir = gen_path / "schemas" / schema_name / version out_dir.mkpath out_path = out_dir / schema_name out_path.write(JSON.pretty_generate(resolved_schema) + "\n") end end |
#resolved_spec_path(cfg_path_or_name) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/udb/resolver.rb', line 70 def resolved_spec_path(cfg_path_or_name) if cfg_info(cfg_path_or_name)..nil? @gen_path / "resolved_spec" / "_" else @gen_path / "resolved_spec" / cfg_info(cfg_path_or_name).name end end |
#run(cmd) ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/udb/resolver.rb', line 136 def run(cmd) puts cmd.join(" ") unless @quiet if @quiet T.unsafe(self).send(:system, *cmd, out: "/dev/null", err: "/dev/null") else T.unsafe(self).send(:system, *cmd) end raise "data resolution error while executing '#{cmd.join(' ')}'" unless $?.success? end |