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 ⇒ Pathname
readonly
path to find configuration files.
-
#custom_path ⇒ Pathname
readonly
path to custom overlay specifications.
-
#gen_path ⇒ Pathname
readonly
path to put generated files into.
-
#schemas_path ⇒ Pathname
readonly
path to find database schema files.
-
#std_path ⇒ Pathname
readonly
path to the standard specification.
Instance Method Summary collapse
-
#any_newer?(target, deps) ⇒ Boolean
returns true if either
targetdoes not exist, or if any ofdepsare newer thantarget. -
#cfg_arch_for(config_path_or_name) ⇒ Udb::ConfiguredArchitecture
resolve the specification for a config, and return a ConfiguredArchitecture.
-
#cfg_arch_for_data(config_data) ⇒ Udb::ConfiguredArchitecture
Create a ConfiguredArchitecture directly from an in-memory config data hash, bypassing resolve_config and resolve_arch entirely.
-
#cfg_arch_for_pointer(pointer, relative_dir:) ⇒ Udb::ConfiguredArchitecture
Resolve a config pointer (name or file path) to a ConfiguredArchitecture.
- #cfg_info(config_path_or_name) ⇒ ConfigInfo
-
#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)
constructor
create a new resolver.
- #merge_arch(config_yaml)
-
#merged_spec_path(cfg_path_or_name) ⇒ Pathname
path to merged spec (merged with custom overley, but prior to resolution).
- #resolve_arch(config_yaml)
-
#resolve_config(config_path) ⇒ Hash{String => T.untyped}
resolve config file and write it to gen_path returns the config data.
-
#resolve_schemas
Resolve schema files by rewriting their $id to the full published URL and writing the result to gen/schemas/SCHEMA_NAME/VERSION/SCHEMA_FILENAME.
-
#resolved_spec_path(cfg_path_or_name) ⇒ Pathname
path to merged and resolved spec.
-
#run(cmd)
run command in the shell.
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)
create a new resolver.
With no arguments, resolver will assume it exists in the riscv-unified-db repository and use standard paths
If repo_root is given, use it as the path to a riscv-unified-db repository
Any specific path can be overridden. If all paths are overridden, it doesn't matter what repo_root is.
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 ⇒ Pathname (readonly)
path to find configuration files
44 45 46 |
# File 'lib/udb/resolver.rb', line 44 def cfgs_path @cfgs_path end |
#custom_path ⇒ Pathname (readonly)
path to custom overlay specifications
56 57 58 |
# File 'lib/udb/resolver.rb', line 56 def custom_path @custom_path end |
#gen_path ⇒ Pathname (readonly)
path to put generated files into
48 49 50 |
# File 'lib/udb/resolver.rb', line 48 def gen_path @gen_path end |
#schemas_path ⇒ Pathname (readonly)
path to find database schema files
40 41 42 |
# File 'lib/udb/resolver.rb', line 40 def schemas_path @schemas_path end |
#std_path ⇒ Pathname (readonly)
path to the standard specification
52 53 54 |
# File 'lib/udb/resolver.rb', line 52 def std_path @std_path end |
Instance Method Details
#any_newer?(target, deps) ⇒ Boolean
returns true if either target does not exist, or if any of deps are newer than target
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) ⇒ Udb::ConfiguredArchitecture
resolve the specification for a config, and return a ConfiguredArchitecture
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) ⇒ Udb::ConfiguredArchitecture
Create a ConfiguredArchitecture directly from an in-memory config data hash, bypassing resolve_config and resolve_arch entirely. Only valid when the config has no arch_overlay (i.e., it uses the standard spec at gen/resolved_spec/_). Callers must ensure this precondition holds before calling this method.
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:) ⇒ Udb::ConfiguredArchitecture
Resolve a config pointer (name or file path) to a ConfiguredArchitecture. Resolution order:
1. If <cfgs_path>/<pointer>.yaml exists on disk, treat as a repo config name
(handles names that contain '/', e.g. "profile/RVA23U64").
2. If the pointer ends in .yaml/.yml, is absolute, or starts with ./ or ../,
treat as a file path resolved relative to +relative_dir+.
3. Otherwise raise ArgumentError — the pointer is neither a known config name
nor a recognisable file path.
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) ⇒ ConfigInfo
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)
This method returns an undefined value.
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) ⇒ Pathname
path to merged spec (merged with custom overley, but prior to resolution)
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)
This method returns an undefined value.
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) ⇒ Hash{String => T.untyped}
resolve config file and write it to gen_path returns the config data
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
This method returns an undefined value.
Resolve schema files by rewriting their $id to the full published URL and writing the result to gen/schemas/SCHEMA_NAME/VERSION/SCHEMA_FILENAME.
Each schema file has its own independent version (the $id field, e.g. "v0.1").
The resolved file is written to gen/schemas/<schema_name>/
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) ⇒ Pathname
path to merged and resolved spec
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)
This method returns an undefined value.
run command in the shell. raise if exit is not zero
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 |