Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 52 additions & 26 deletions src/BinaryPlatforms_compat.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
module BinaryPlatforms

using Base.BinaryPlatforms
using Base: BinaryPlatforms as BBP
using Base.BinaryPlatforms: platform_dlext, arch, libc,
libgfortran_version, libstdcxx_version, cxxstring_abi, parse_dl_name_version,
detect_libgfortran_version, detect_libstdcxx_version, detect_cxxstring_abi,
call_abi, wordsize, select_platform, platforms_match,
Platform, platform_name

export platform_key_abi, platform_dlext, valid_dl_path, arch, libc,
libgfortran_version, libstdcxx_version, cxxstring_abi, parse_dl_name_version,
detect_libgfortran_version, detect_libstdcxx_version, detect_cxxstring_abi,
call_abi, wordsize, triplet, select_platform, platforms_match,
CompilerABI, Platform, UnknownPlatform, Linux, MacOS, Windows, FreeBSD

import Base.BinaryPlatforms: libgfortran_version, libstdcxx_version, platform_name,
wordsize, platform_dlext, tags, arch, libc, call_abi,
cxxstring_abi
# Distinct from Base.BinaryPlatforms.AbstractPlatform
abstract type AbstractPlatform end
Base.convert(::Type{T}, p::AbstractPlatform) where T <: BBP.AbstractPlatform = p.p::T

struct UnknownPlatform <: AbstractPlatform
UnknownPlatform(args...; kwargs...) = new()
p::Platform
function UnknownPlatform(args...; kwargs...)
p = Platform("unknown", "unknown")
delete!(p.tags, "arch")
new(p)
end
end
tags(::UnknownPlatform) = Dict{String,String}("os"=>"unknown")


struct CompilerABI
libgfortran_version::Union{Nothing,VersionNumber}
Expand All @@ -40,9 +49,9 @@ function CompilerABI(cabi::CompilerABI; libgfortran_version=nothing,
)
end

libgfortran_version(cabi::CompilerABI) = cabi.libgfortran_version
libstdcxx_version(cabi::CompilerABI) = cabi.libstdcxx_version
cxxstring_abi(cabi::CompilerABI) = cabi.cxxstring_abi
BBP.libgfortran_version(cabi::CompilerABI) = cabi.libgfortran_version
BBP.libstdcxx_version(cabi::CompilerABI) = cabi.libstdcxx_version
BBP.cxxstring_abi(cabi::CompilerABI) = cabi.cxxstring_abi

for T in (:Linux, :Windows, :MacOS, :FreeBSD)
@eval begin
Expand All @@ -64,10 +73,11 @@ end

const PlatformUnion = Union{Linux,MacOS,Windows,FreeBSD}


# First, methods we need to coerce to Symbol for backwards-compatibility
for f in (:arch, :libc, :call_abi, :cxxstring_abi)
@eval begin
function $(f)(p::PlatformUnion)
function BBP.$(f)(p::AbstractPlatform)
str = $(f)(p.p)
if str === nothing
return nothing
Expand All @@ -77,23 +87,16 @@ for f in (:arch, :libc, :call_abi, :cxxstring_abi)
end
end

# Next, things we don't need to coerce
for f in (:libgfortran_version, :libstdcxx_version, :platform_name, :wordsize, :platform_dlext, :tags, :triplet)
@eval begin
$(f)(p::PlatformUnion) = $(f)(p.p)
end
end

# Finally, add equality testing between these wrapper types and other AbstractPlatforms
@eval begin
Base.:(==)(a::PlatformUnion, b::AbstractPlatform) = b == a.p
end
# Finally, add equality testing between these wrapper types and BBP.AbstractPlatforms
Base.:(==)(a::AbstractPlatform, b::AbstractPlatform) = a.p == b.p
Base.:(==)(a::AbstractPlatform, b::BBP.AbstractPlatform) = b == a.p
Base.:(==)(a::BBP.AbstractPlatform, b::AbstractPlatform) = a == b.p

# Add one-off functions
MacOS(; kwargs...) = MacOS(:x86_64; kwargs...)
FreeBSD(; kwargs...) = FreeBSD(:x86_64; kwargs...)

function triplet(p::AbstractPlatform)
function triplet(p::Union{AbstractPlatform, BBP.AbstractPlatform})
# We are going to sub off to `Base.BinaryPlatforms.triplet()` here,
# with the important exception that we override `os_version` to better
# mimic the old behavior of `triplet()`
Expand All @@ -117,7 +120,7 @@ This method is deprecated, import `Base.BinaryPlatforms` and use either `HostPla
to get the current host platform, or `parse(Base.BinaryPlatforms.Platform, triplet)`
to parse the triplet for some other platform instead.
"""
platform_key_abi() = HostPlatform()
platform_key_abi() = BBP.HostPlatform()
platform_key_abi(triplet::AbstractString) = parse(Platform, triplet)

"""
Expand All @@ -129,9 +132,9 @@ E.g. returns `true` for a path like `"usr/lib/libfoo.so.3.5"`, but returns

This method is deprecated and will be removed in Julia 2.0.
"""
function valid_dl_path(path::AbstractString, platform::AbstractPlatform)
function valid_dl_path(path::AbstractString, platform::Union{AbstractPlatform, BBP.AbstractPlatform})
try
parse_dl_name_version(path, string(os(platform))::String)
parse_dl_name_version(path, string(BBP.os(platform))::String)
return true
catch e
if isa(e, ArgumentError)
Expand All @@ -141,4 +144,27 @@ function valid_dl_path(path::AbstractString, platform::AbstractPlatform)
end
end

for f in (
:(Base.getindex), :(Base.haskey), :(Base.setindex!),
:(Sys.isapple), :(Sys.islinux), :(Sys.iswindows), :(Sys.isfreebsd), :(Sys.isbsd), :(Sys.isunix),
:(BBP.HostPlatform),
:(BBP.libgfortran_version), :(BBP.libstdcxx_version), :(BBP.os), :(BBP.os_version),
:(BBP.platform_dlext), :(BBP.platform_name), :(BBP.platforms_match),
:(BBP.triplet), :(BBP.wordsize)
)
@eval begin
$f(p::AbstractPlatform, args...) = $f(p.p, args...)
end
end
BBP.platforms_match(a::String, b::AbstractPlatform) =
BBP.platforms_match(a, b.p)
BBP.platforms_match(a::AbstractString, b::AbstractPlatform) =
BBP.platforms_match(a, b.p)
BBP.platforms_match(a::AbstractPlatform, b::AbstractPlatform) =
BBP.platforms_match(a.p, b.p)
BBP.platforms_match(a::BBP.AbstractPlatform, b::AbstractPlatform) =
BBP.platforms_match(a, b.p)
BBP.select_platform(download_info::Dict, platform::AbstractPlatform) =
BBP.select_platform(download_info, platform.p)

end # module BinaryPlatforms
8 changes: 8 additions & 0 deletions test/binaryplatforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const platform = @inferred Platform platform_key_abi()
# Explicitly test that we can pass arguments to UnknownPlatform,
# and it doesn't do anything.
@test UnknownPlatform(:riscv; libc=:fuschia_libc) == UnknownPlatform()
@test arch(FreeBSD()) == :x86_64
@test arch(MacOS()) == :x86_64
end

@testset "Platform properties" begin
Expand Down Expand Up @@ -82,6 +84,7 @@ const platform = @inferred Platform platform_key_abi()
@test triplet(MacOS()) == "x86_64-apple-darwin14"
@test triplet(FreeBSD(:x86_64)) == "x86_64-unknown-freebsd11.1"
@test triplet(FreeBSD(:i686)) == "i686-unknown-freebsd11.1"
@test platform_key_abi("i686-w64-mingw32") == Windows(:i686)
end

@testset "Valid DL paths" begin
Expand Down Expand Up @@ -109,6 +112,8 @@ const platform = @inferred Platform platform_key_abi()
cxxstring_abi=cxxstring_abi,
)
@test platforms_match(Linux(:x86_64), Linux(:x86_64, compiler_abi=cabi))
@test platforms_match(convert(Platform,Linux(:x86_64)), Linux(:x86_64, compiler_abi=cabi))
@test platforms_match(Linux(:x86_64), convert(Platform, Linux(:x86_64, compiler_abi=cabi)))
@test platforms_match(Linux(:x86_64, compiler_abi=cabi), Linux(:x86_64))

# Also test auto-string-parsing
Expand Down Expand Up @@ -137,6 +142,9 @@ const platform = @inferred Platform platform_key_abi()

@test !platforms_match(Linux(arch, compiler_abi=base_cabi), Linux(arch, compiler_abi=cabi))
end
@test platforms_match(triplet(Linux(:x86_64)), Linux(:x86_64))
@test platforms_match(@view(triplet(Linux(:x86_64))[1:end]), Linux(:x86_64))
@test Base.BinaryPlatforms.select_platform(Dict(Linux(:x86_64) => 5), Linux(:x86_64)) == 5
end

@testset "Sys.is* overloading" begin
Expand Down