Skip to content

Commit 55c795d

Browse files
authored
[Rootfs] Improve API of expand_microarchitectures (#232)
Add a positional argument to select the microarchitectures to expand, and a keyword argument to select the platforms to expand. This should provide a flexible interface to limit the expansion and avoid building for many useless platforms.
1 parent 3e0815e commit 55c795d

File tree

3 files changed

+93
-31
lines changed

3 files changed

+93
-31
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "BinaryBuilderBase"
22
uuid = "7f725544-6523-48cd-82d1-3fa08ff4056e"
33
authors = ["Elliot Saba <[email protected]>"]
4-
version = "1.7.0"
4+
version = "1.8.0"
55

66
[deps]
77
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"

src/Rootfs.jl

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ end
675675
choose_shards(::AnyPlatform; kwargs...) = choose_shards(default_host_platform; kwargs...)
676676

677677
"""
678-
supported_platforms(;exclude::Union{Vector{<:Platform},Function}=x->false,
678+
supported_platforms(;exclude::Union{Vector{<:Platform},Function}=Returns(false),
679679
experimental::Bool=false)
680680
681681
Return the list of supported platforms as an array of `Platform`s. These are the platforms we
@@ -690,7 +690,7 @@ or a function that returns true for exclusions i.e.
690690
supported_platforms(exclude=Sys.islinux)
691691
```
692692
"""
693-
function supported_platforms(;exclude::Union{Vector{<:Platform},Function}=x->false,
693+
function supported_platforms(;exclude::Union{Vector{<:Platform},Function}=Returns(false),
694694
experimental::Bool=false)
695695
exclude_platforms!(platforms, exclude::Function) = filter(!exclude, platforms)
696696
exclude_platforms!(platforms, exclude::Vector{<:Platform}) = filter!(!in(exclude), platforms)
@@ -796,11 +796,12 @@ function expand_cxxstring_abis(ps::Vector{<:AbstractPlatform}; kwargs...)
796796
end
797797

798798
"""
799-
expand_microarchitectures(p::AbstractPlatform)
799+
expand_microarchitectures(p::AbstractPlatform, [microarchitectures::Vector{String}])
800800
801801
Given a `Platform`, returns a vector of `Platforms` with differing `march` attributes
802802
as specified by the `ARCHITECTURE_FLAGS` mapping. If the given `Platform` alread has a
803-
`march` tag specified, only that platform is returned.
803+
`march` tag specified, only that platform is returned. If the `microarchitectures`
804+
argument is given, limit the expansion to the given microarchitectures.
804805
805806
```jldoctest
806807
julia> using BinaryBuilderBase
@@ -817,28 +818,30 @@ julia> expand_microarchitectures(Platform("armv7l", "linux"))
817818
Linux armv7l {call_abi=eabihf, libc=glibc, march=armv7l}
818819
Linux armv7l {call_abi=eabihf, libc=glibc, march=neonvfpv4}
819820
820-
julia> expand_microarchitectures(Platform("aarch64", "linux"))
821-
4-element Vector{Platform}:
821+
julia> expand_microarchitectures(Platform("aarch64", "linux"), ["armv8_0", "a64fx"])
822+
2-element Vector{Platform}:
822823
Linux aarch64 {libc=glibc, march=armv8_0}
823-
Linux aarch64 {libc=glibc, march=armv8_4_crypto_sve}
824-
Linux aarch64 {libc=glibc, march=armv8_2_crypto}
825-
Linux aarch64 {libc=glibc, march=armv8_1}
824+
Linux aarch64 {libc=glibc, march=a64fx}
826825
827826
julia> expand_microarchitectures(Platform("i686", "windows"))
828827
2-element Vector{Platform}:
829828
Windows i686 {march=pentium4}
830829
Windows i686 {march=prescott}
831830
```
832831
"""
833-
function expand_microarchitectures(platform::AbstractPlatform)
834-
# If this already has a `march`, or it's an `AnyPlatform`, just return it.
835-
if isa(platform, AnyPlatform) || march(platform) !== nothing
832+
function expand_microarchitectures(platform::AbstractPlatform,
833+
microarchitectures::Vector{String}=get_all_march_names(arch(platform)))
834+
all_marchs = get_all_march_names(arch(platform))
835+
836+
# If this already has a `march`, or it's an `AnyPlatform`, or the microarchitectures we
837+
# want to expand aren't relative to this platform, just return it.
838+
if isa(platform, AnyPlatform) || march(platform) !== nothing ||
839+
!any(in(all_marchs), microarchitectures)
836840
return [platform]
837841
end
838842

839-
# Otherwise, return a bunch of Platform objects with appropriately-set `march` tags, but
840-
# first filter out some meaningless combinations of microarchitectures.
841-
all_marchs = filter(get_all_march_names(arch(platform))) do march
843+
# First, filter out some meaningless combinations of microarchitectures.
844+
marchs = filter(all_marchs) do march
842845
if (!Sys.isapple(platform) && march == "apple_m1") ||
843846
(Sys.isapple(platform) && arch(platform) == "aarch64" && march ("armv8_0", "apple_m1"))
844847
# `apple_m1` makes sense only on macOS, and the only aarch64 microarchitectures
@@ -851,39 +854,89 @@ function expand_microarchitectures(platform::AbstractPlatform)
851854
return true
852855
end
853856

854-
return map(all_marchs) do march
855-
p = deepcopy(platform)
856-
p["march"] = march
857-
return p
857+
# But if none of the remaining microarchitectures are among those we want to expand,
858+
# return the given platform as is.
859+
if !any(in(microarchitectures), marchs)
860+
return [platform]
858861
end
862+
863+
# Otherwise, return a bunch of Platform objects with appropriately-set `march` tags
864+
return [(p = deepcopy(platform); p["march"] = march; p) for march in marchs if march in microarchitectures]
859865
end
860866

861867
"""
862-
expand_microarchitectures(ps::Vector{<:Platform})
868+
expand_microarchitectures(ps::Vector{<:Platform}, [microarchitectures::Vector{String}];
869+
filter=Returns(true))
870+
871+
Expand all platforms in the vector `ps` with the supported microarchitectures.
863872
864-
Expand all platforms in the given vector with the supported microarchitectures.
873+
If the `microarchitectures` argument is given, limit the expansion to the given
874+
platforms. This is useful if you do not want to expand to all available
875+
microarchitectures.
876+
877+
The expansion is applied only to the platforms matching the `filter` predicate, by
878+
default all platforms. This is useful if you want to limit the expansion to some
879+
platforms, without having to explicitly list its microarchitectures in the second
880+
argument.
865881
866882
```jldoctest
867883
julia> using BinaryBuilderBase
868884
869885
julia> expand_microarchitectures(filter!(p -> Sys.islinux(p) && libc(p) == "glibc", supported_platforms()))
870-
13-element Vector{Platform}:
886+
14-element Vector{Platform}:
871887
Linux i686 {libc=glibc, march=pentium4}
872888
Linux i686 {libc=glibc, march=prescott}
873889
Linux x86_64 {libc=glibc, march=x86_64}
874890
Linux x86_64 {libc=glibc, march=avx}
875891
Linux x86_64 {libc=glibc, march=avx2}
876892
Linux x86_64 {libc=glibc, march=avx512}
877893
Linux aarch64 {libc=glibc, march=armv8_0}
878-
Linux aarch64 {libc=glibc, march=armv8_4_crypto_sve}
879-
Linux aarch64 {libc=glibc, march=armv8_2_crypto}
880894
Linux aarch64 {libc=glibc, march=armv8_1}
895+
Linux aarch64 {libc=glibc, march=armv8_2_crypto}
896+
Linux aarch64 {libc=glibc, march=a64fx}
897+
Linux armv6l {call_abi=eabihf, libc=glibc, march=arm1176jzfs}
881898
Linux armv7l {call_abi=eabihf, libc=glibc, march=armv7l}
882899
Linux armv7l {call_abi=eabihf, libc=glibc, march=neonvfpv4}
883900
Linux powerpc64le {libc=glibc, march=power8}
901+
902+
julia> expand_microarchitectures(filter!(p -> Sys.islinux(p) && libc(p) == "glibc", supported_platforms()), ["x86_64", "avx2"])
903+
7-element Vector{Platform}:
904+
Linux i686 {libc=glibc}
905+
Linux x86_64 {libc=glibc, march=x86_64}
906+
Linux x86_64 {libc=glibc, march=avx2}
907+
Linux aarch64 {libc=glibc}
908+
Linux armv6l {call_abi=eabihf, libc=glibc}
909+
Linux armv7l {call_abi=eabihf, libc=glibc}
910+
Linux powerpc64le {libc=glibc}
911+
912+
julia> expand_microarchitectures(filter!(p -> Sys.islinux(p) && libc(p) == "glibc", supported_platforms()); filter=p->arch(p)=="x86_64")
913+
9-element Vector{Platform}:
914+
Linux i686 {libc=glibc}
915+
Linux x86_64 {libc=glibc, march=x86_64}
916+
Linux x86_64 {libc=glibc, march=avx}
917+
Linux x86_64 {libc=glibc, march=avx2}
918+
Linux x86_64 {libc=glibc, march=avx512}
919+
Linux aarch64 {libc=glibc}
920+
Linux armv6l {call_abi=eabihf, libc=glibc}
921+
Linux armv7l {call_abi=eabihf, libc=glibc}
922+
Linux powerpc64le {libc=glibc}
884923
```
885924
"""
886-
expand_microarchitectures(ps::Vector{<:AbstractPlatform}) = collect(Iterators.flatten(expand_microarchitectures.(ps)))
925+
function expand_microarchitectures(ps::Vector{<:AbstractPlatform},
926+
microarchitectures::Vector{String}=collect(Iterators.flatten(get_all_march_names.(unique!(arch.(ps)))));
927+
filter=Returns(true),
928+
)
929+
out = map(ps) do p
930+
return if filter(p)
931+
# If the platform satisfies the predicate, expand its microarchitectures
932+
expand_microarchitectures(p, microarchitectures)
933+
else
934+
# otherwise return it as-is.
935+
[p]
936+
end
937+
end
938+
return collect(Iterators.flatten(out))
939+
end
887940

888941
"""
889942
preferred_libgfortran_version(platform::AbstractPlatform, shard::CompilerShard;

test/rootfs.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ using BinaryBuilderBase
5757
Platform("x86_64", "linux"; libc="glibc", march="avx512", cuda="10.1"),
5858
Platform("x86_64", "linux"; libc="glibc", march="x86_64", cuda="10.1"),
5959
]
60+
@test sort(expand_microarchitectures(Platform("x86_64", "linux"), ["x86_64", "avx512"]), by=triplet) == [
61+
Platform("x86_64", "linux"; libc="glibc", march="avx512"),
62+
Platform("x86_64", "linux"; libc="glibc", march="x86_64"),
63+
]
64+
@test expand_microarchitectures(Platform("i686", "linux"), ["x86_64", "avx512"]) == [Platform("i686", "linux")]
65+
@test expand_microarchitectures(Platform("aarch64", "linux"; libc="musl"), ["a64fx", "apple_m1"]) == [Platform("aarch64", "linux"; libc="musl")]
66+
@test expand_microarchitectures(Platform("aarch64", "macos"), ["a64fx"]) == [Platform("aarch64", "macos")]
67+
6068
@test sort(expand_microarchitectures(filter!(p -> Sys.islinux(p) && libc(p) == "glibc", supported_platforms())), by=triplet) == [
6169
Platform("aarch64", "linux"; libc="glibc", march="a64fx"),
6270
Platform("aarch64", "linux"; libc="glibc", march="armv8_0"),
@@ -73,17 +81,18 @@ using BinaryBuilderBase
7381
Platform("x86_64", "linux"; libc="glibc", march="avx512"),
7482
Platform("x86_64", "linux"; libc="glibc", march="x86_64"),
7583
]
76-
@test sort(expand_microarchitectures(filter!(p -> Sys.islinux(p) && arch(p) == "aarch64" && libc(p) == "musl", supported_platforms())), by=triplet) == [
84+
@test sort(expand_microarchitectures(filter!(p -> Sys.islinux(p) && libc(p) == "musl", supported_platforms()); filter=p->arch(p) == "aarch64"), by=triplet) == [
7785
Platform("aarch64", "linux"; libc="musl", march="armv8_0"),
7886
Platform("aarch64", "linux"; libc="musl", march="armv8_1"),
7987
Platform("aarch64", "linux"; libc="musl", march="armv8_2_crypto"),
88+
Platform("armv6l", "linux"; call_abi="eabihf", libc="musl"),
89+
Platform("armv7l", "linux"; call_abi="eabihf", libc="musl"),
90+
Platform("i686", "linux"; libc="musl"),
91+
Platform("x86_64", "linux"; libc="musl"),
8092
]
81-
@test sort(expand_microarchitectures(filter!(p -> Sys.isapple(p), supported_platforms())), by=triplet) == [
93+
@test sort(expand_microarchitectures(filter!(p -> Sys.isapple(p), supported_platforms()), ["a64fx", "apple_m1", "x86_64", "avx2"]), by=triplet) == [
8294
Platform("aarch64", "macos"; march="apple_m1"),
83-
Platform("aarch64", "macos"; march="armv8_0"),
84-
Platform("x86_64", "macos"; march="avx"),
8595
Platform("x86_64", "macos"; march="avx2"),
86-
Platform("x86_64", "macos"; march="avx512"),
8796
Platform("x86_64", "macos"; march="x86_64"),
8897
]
8998
@test expand_microarchitectures([Platform("x86_64", "windows"; march="avx", cuda="10.1")]) ==

0 commit comments

Comments
 (0)