Skip to content

Commit

Permalink
Add Sys.is* for all recognized kernels (#30249)
Browse files Browse the repository at this point in the history
We have `Sys.is*` for a subset of supported platforms, but not for all
recognized kernels, e.g. FreeBSD, OpenBSD, etc. `Sys.isbsd` isn't
specific enough in some cases, and `Sys.KERNEL === x` is inconsistent
with other systems.
  • Loading branch information
ararslan authored and staticfloat committed Dec 5, 2018
1 parent 0e155bc commit ff0acfe
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 11 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ New library functions
* `eachrow`, `eachcol` and `eachslice` functions provide efficient iterators over slices of arrays ([#29749]).
* `fieldtypes(T::Type)` which return the declared types of the field in type T ([#29600]).
* `uuid5` has been added to the `UUIDs` standard library ([#28761]).
* Predicate functions `Sys.isfreebsd`, `Sys.isopenbsd`, `Sys.isnetbsd`, and `Sys.isdragonfly` for
detecting BSD systems have been added ([#30249]).

Standard library changes
------------------------
Expand Down Expand Up @@ -175,3 +177,4 @@ Deprecated or removed
[#30035]: https://github.com/JuliaLang/julia/issues/30035
[#30083]: https://github.com/JuliaLang/julia/issues/30083
[#30159]: https://github.com/JuliaLang/julia/issues/30159
[#30249]: https://github.com/JuliaLang/julia/issues/30249
70 changes: 65 additions & 5 deletions base/sysinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export BINDIR,
total_memory,
isapple,
isbsd,
isdragonfly,
isfreebsd,
islinux,
isnetbsd,
isopenbsd,
isunix,
iswindows,
isexecutable,
Expand Down Expand Up @@ -286,7 +290,7 @@ end
Predicate for testing if the OS is a derivative of Linux.
See documentation in [Handling Operating System Variation](@ref).
"""
islinux(os::Symbol) = (os == :Linux)
islinux(os::Symbol) = (os === :Linux)

"""
Sys.isbsd([os])
Expand All @@ -299,25 +303,81 @@ See documentation in [Handling Operating System Variation](@ref).
`true` on macOS systems. To exclude macOS from a predicate, use
`Sys.isbsd() && !Sys.isapple()`.
"""
isbsd(os::Symbol) = (os == :FreeBSD || os == :OpenBSD || os == :NetBSD || os == :DragonFly || os == :Darwin || os == :Apple)
isbsd(os::Symbol) = (isfreebsd(os) || isopenbsd(os) || isnetbsd(os) || isdragonfly(os) || isapple(os))

"""
Sys.isfreebsd([os])
Predicate for testing if the OS is a derivative of FreeBSD.
See documentation in [Handling Operating System Variation](@ref).
!!! note
Not to be confused with `Sys.isbsd()`, which is `true` on FreeBSD but also on
other BSD-based systems. `Sys.isfreebsd()` refers only to FreeBSD.
!!! compat "Julia 1.1"
This function requires at least Julia 1.1.
"""
isfreebsd(os::Symbol) = (os === :FreeBSD)

"""
Sys.isopenbsd([os])
Predicate for testing if the OS is a derivative of OpenBSD.
See documentation in [Handling Operating System Variation](@ref).
!!! note
Not to be confused with `Sys.isbsd()`, which is `true` on OpenBSD but also on
other BSD-based systems. `Sys.isopenbsd()` refers only to OpenBSD.
!!! compat "Julia 1.1"
This function requires at least Julia 1.1.
"""
isopenbsd(os::Symbol) = (os === :OpenBSD)

"""
Sys.isnetbsd([os])
Predicate for testing if the OS is a derivative of NetBSD.
See documentation in [Handling Operating System Variation](@ref).
!!! note
Not to be confused with `Sys.isbsd()`, which is `true` on NetBSD but also on
other BSD-based systems. `Sys.isnetbsd()` refers only to NetBSD.
!!! compat "Julia 1.1"
This function requires at least Julia 1.1.
"""
isnetbsd(os::Symbol) = (os === :NetBSD)

"""
Sys.isdragonfly([os])
Predicate for testing if the OS is a derivative of DragonFly BSD.
See documentation in [Handling Operating System Variation](@ref).
!!! note
Not to be confused with `Sys.isbsd()`, which is `true` on DragonFly but also on
other BSD-based systems. `Sys.isdragonfly()` refers only to DragonFly.
!!! compat "Julia 1.1"
This function requires at least Julia 1.1.
"""
isdragonfly(os::Symbol) = (os === :DragonFly)

"""
Sys.iswindows([os])
Predicate for testing if the OS is a derivative of Microsoft Windows NT.
See documentation in [Handling Operating System Variation](@ref).
"""
iswindows(os::Symbol) = (os == :Windows || os == :NT)
iswindows(os::Symbol) = (os === :Windows || os === :NT)

"""
Sys.isapple([os])
Predicate for testing if the OS is a derivative of Apple Macintosh OS X or Darwin.
See documentation in [Handling Operating System Variation](@ref).
"""
isapple(os::Symbol) = (os == :Apple || os == :Darwin)
isapple(os::Symbol) = (os === :Apple || os === :Darwin)

for f in (:isunix, :islinux, :isbsd, :isapple, :iswindows)
for f in (:isunix, :islinux, :isbsd, :isapple, :iswindows, :isfreebsd, :isopenbsd, :isnetbsd, :isdragonfly)
@eval $f() = $(getfield(@__MODULE__, f)(KERNEL))
end

Expand Down
4 changes: 4 additions & 0 deletions doc/src/base/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ Base.Sys.isunix
Base.Sys.isapple
Base.Sys.islinux
Base.Sys.isbsd
Base.Sys.isfreebsd
Base.Sys.isopenbsd
Base.Sys.isnetbsd
Base.Sys.isdragonfly
Base.Sys.iswindows
Base.Sys.windows_version
Base.@static
Expand Down
11 changes: 6 additions & 5 deletions doc/src/manual/handling-operating-system-variation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

When writing cross-platform applications or libraries, it is often necessary to allow for
differences between operating systems. The variable `Sys.KERNEL` can be used to handle such
cases. There are several functions in the `Sys` module intended to make this easier:
`isunix`, `islinux`, `isapple`, `isbsd`, and `iswindows`. These may be used as follows:
cases. There are several functions in the `Sys` module intended to make this easier, such as
`isunix`, `islinux`, `isapple`, `isbsd`, `isfreebsd`, and `iswindows`. These may be used
as follows:

```julia
if Sys.iswindows()
windows_specific_thing(a)
end
```

Note that `islinux` and `isapple` are mutually exclusive subsets of `isunix`. Additionally,
there is a macro `@static` which makes it possible to use these functions to conditionally hide
invalid code, as demonstrated in the following examples.
Note that `islinux`, `isapple`, and `isfreebsd` are mutually exclusive subsets of `isunix`.
Additionally, there is a macro `@static` which makes it possible to use these functions to
conditionally hide invalid code, as demonstrated in the following examples.

Simple blocks:

Expand Down
12 changes: 11 additions & 1 deletion test/osutils.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

@testset "isunix/islinux/iswindows" begin
@testset "Operating system predicates" begin
@test !Sys.isunix(:Windows)
@test !Sys.islinux(:Windows)
@test Sys.islinux(:Linux)
Expand All @@ -12,6 +12,16 @@
@test !Sys.isapple(:Windows)
@test Sys.isunix(:Darwin)
@test Sys.isunix(:FreeBSD)
for bsd in (:FreeBSD, :OpenBSD, :NetBSD, :DragonFly)
f = Symbol("is", lowercase(String(bsd)))
q = QuoteNode(bsd)
@eval begin
@test Sys.$f($q)
@test Sys.isbsd($q)
@test Sys.isunix($q)
@test !Sys.isapple($q)
end
end
@test_throws ArgumentError Sys.isunix(:BeOS)
if !Sys.iswindows()
@test Sys.windows_version() == v"0.0.0"
Expand Down

0 comments on commit ff0acfe

Please sign in to comment.