Skip to content

Commit

Permalink
Add Sys.is* for all recognized kernels
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 committed Dec 3, 2018
1 parent 6b04291 commit 543eec6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
29 changes: 25 additions & 4 deletions base/sysinfo.jl
Expand Up @@ -286,7 +286,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,24 +299,45 @@ 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))

for bsd in (:FreeBSD, :OpenBSD, :NetBSD, :DragonFly)
s = String(bsd)
f = Symbol("is", lowercase(s))
@eval begin
"""
Sys.$($f)([os])
Predicate for testing if the OS is a derivative of $($s).
See documentation in [Handling Operating System Variation](@ref).
!!! note
Not to be confused with `Sys.isbsd()`, which is `true` on $($s) but also on
other BSD-based systems. `Sys.$($f)()` refers only to $($s).
"""
$f(os::Symbol) = (os === $(QuoteNode(bsd)))
$f() = $(getfield(@__MODULE__, f)(KERNEL))
export $f
end
end

"""
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)

# NOTE: BSDs are handled in their own loop above
for f in (:isunix, :islinux, :isbsd, :isapple, :iswindows)
@eval $f() = $(getfield(@__MODULE__, f)(KERNEL))
end
Expand Down
4 changes: 4 additions & 0 deletions doc/src/base/base.md
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
9 changes: 5 additions & 4 deletions doc/src/manual/handling-operating-system-variation.md
Expand Up @@ -3,17 +3,18 @@
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:
`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
@@ -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 543eec6

Please sign in to comment.