Skip to content

Commit

Permalink
Add Sys.isfreebsd
Browse files Browse the repository at this point in the history
We have `Sys.is*` for all officially supported platforms except for
FreeBSD... until now! Previously, the only way to detect FreeBSD was
to check `Sys.KERNEL === :FreeBSD`, as `Sys.isbsd() && !Sys.isapple()`
will be insufficiently specific should we ever support other BSDs.
  • Loading branch information
ararslan committed Dec 3, 2018
1 parent 6b04291 commit d814772
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
15 changes: 14 additions & 1 deletion base/sysinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export BINDIR,
total_memory,
isapple,
isbsd,
isfreebsd,
islinux,
isunix,
iswindows,
Expand Down Expand Up @@ -301,6 +302,18 @@ See documentation in [Handling Operating System Variation](@ref).
"""
isbsd(os::Symbol) = (os == :FreeBSD || os == :OpenBSD || os == :NetBSD || os == :DragonFly || os == :Darwin || os == :Apple)

"""
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.
"""
isfreebsd(os::Symbol) = (os == :FreeBSD)

"""
Sys.iswindows([os])
Expand All @@ -317,7 +330,7 @@ See documentation in [Handling Operating System Variation](@ref).
"""
isapple(os::Symbol) = (os == :Apple || os == :Darwin)

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

Expand Down
1 change: 1 addition & 0 deletions doc/src/base/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ Base.Sys.isunix
Base.Sys.isapple
Base.Sys.islinux
Base.Sys.isbsd
Base.Sys.isfreebsd
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
Original file line number Diff line number Diff line change
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
7 changes: 6 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,11 @@
@test !Sys.isapple(:Windows)
@test Sys.isunix(:Darwin)
@test Sys.isunix(:FreeBSD)
@test Sys.isbsd(:FreeBSD)
@test Sys.isfreebsd(:FreeBSD)
@test Sys.isbsd(:OpenBSD)
@test !Sys.isfreeBSD(:OpenBSD)
@test Sys.isbsd(:Darwin)
@test_throws ArgumentError Sys.isunix(:BeOS)
if !Sys.iswindows()
@test Sys.windows_version() == v"0.0.0"
Expand Down

0 comments on commit d814772

Please sign in to comment.