diff --git a/base/binaryplatforms.jl b/base/binaryplatforms.jl index 84f77b2888a0a..9cbb9011aea7d 100644 --- a/base/binaryplatforms.jl +++ b/base/binaryplatforms.jl @@ -142,6 +142,20 @@ function Base.setindex!(p::AbstractPlatform, v::String, k::String) return p end +# Hash definitino to ensure that it's stable +function Base.hash(p::Platform, h::UInt) + h += 0x506c6174666f726d % UInt + h = hash(p.tags, h) + h = hash(p.compare_strategies, h) + return h +end + +# Simple equality definition; for compatibility testing, use `platforms_match()` +function Base.:(==)(a::Platform, b::Platform) + return a.tags == b.tags && a.compare_strategies == b.compare_strategies +end + + # Allow us to easily serialize Platform objects function Base.repr(p::Platform; context=nothing) str = string( @@ -166,9 +180,6 @@ function Base.show(io::IO, p::Platform) print(io, str) end -# Simple equality definition; for compatibility testing, use `platforms_match()` -Base.:(==)(a::AbstractPlatform, b::AbstractPlatform) = tags(a) == tags(b) - function validate_tags(tags::Dict) throw_invalid_key(k) = throw(ArgumentError("Key \"$(k)\" cannot have value \"$(tags[k])\"")) # Validate `arch` diff --git a/test/binaryplatforms.jl b/test/binaryplatforms.jl index 102ac668d5e44..b4438a06aef15 100644 --- a/test/binaryplatforms.jl +++ b/test/binaryplatforms.jl @@ -136,6 +136,16 @@ end # Test that trying to set illegal tags fails @test_throws ArgumentError p["os"] = "a+b" + + # Test that our `hash()` is stable + @test hash(HostPlatform()) == hash(HostPlatform()) + + # Test that round-tripping through `triplet` for a does not + # maintain equality, as we end up losing the `compare_strategies`: + p = Platform("x86_64", "linux"; cuda = v"11") + Base.BinaryPlatforms.set_compare_strategy!(p, "cuda", Base.BinaryPlatforms.compare_version_cap) + q = parse(Platform, triplet(p)) + @test q != p end @testset "Triplet parsing" begin @@ -186,13 +196,12 @@ end # Round-trip our little homie through `triplet()`, with some bending # of the rules for MacOS and FreeBSD, who have incomplete `os_version` # numbers embedded within their triplets. - p = HostPlatform() - if !Sys.isbsd(p) - @test parse(Platform, triplet(p)) == p - end + p = Platform("x86_64", "linux") + @test parse(Platform, triplet(p)) == p # Also test round-tripping through `repr()`: - @test eval(Meta.parse(repr(HostPlatform()))) == HostPlatform() + p = Platform("aarch64", "macos"; os_version=v"20", march="armv8_4_crypto_sve") + @test eval(Meta.parse(repr(p))) == p end @testset "platforms_match()" begin