forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osutils.jl
83 lines (69 loc) · 2.76 KB
/
osutils.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# This file is a part of Julia. License is MIT: http://julialang.org/license
"""
is_unix([os])
Predicate for testing if the OS provides a Unix-like interface.
See documentation in [Handling Operating System Variation](:ref:`Handling Operating System Variation <man-handling-operating-system-variation>`).
"""
function is_unix(os::Symbol)
if is_windows(os)
return false
elseif is_linux(os) || is_bsd(os)
return true
else
throw(ArgumentError("unknown operating system \"$os\""))
end
end
"""
is_linux([os])
Predicate for testing if the OS is a derivative of Linux.
See documentation in [Handling Operating System Variation](:ref:`Handling Operating System Variation <man-handling-operating-system-variation>`).
"""
is_linux(os::Symbol) = (os == :Linux)
"""
is_bsd([os])
Predicate for testing if the OS is a derivative of BSD.
See documentation in [Handling Operating System Variation](:ref:`Handling Operating System Variation <man-handling-operating-system-variation>`).
"""
is_bsd(os::Symbol) = (os == :FreeBSD || os == :OpenBSD || os == :NetBSD || os == :Darwin || os == :Apple)
"""
is_windows([os])
Predicate for testing if the OS is a derivative of Microsoft Windows NT.
See documentation in [Handling Operating System Variation](:ref:`Handling Operating System Variation <man-handling-operating-system-variation>`).
"""
is_windows(os::Symbol) = (os == :Windows || os == :NT)
"""
is_apple([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:`Handling Operating System Variation <man-handling-operating-system-variation>`).
"""
is_apple(os::Symbol) = (os == :Apple || os == :Darwin)
"""
@static
Partially evaluates an expression at parse time.
For example, `@static is_windows() ? foo : bar` will evaluate `is_windows()` and insert either `foo` or `bar` into the expression.
This is useful in cases where a construct would be invalid on other platforms,
such as a `ccall` to a non-existent function.
"""
macro static(ex)
if isa(ex, Expr)
if ex.head === :if
cond = eval(current_module(), ex.args[1])
if cond
return esc(ex.args[2])
elseif length(ex.args) == 3
return esc(ex.args[3])
else
return nothing
end
end
end
throw(ArgumentError("invalid @static macro"))
end
let KERNEL = ccall(:jl_get_UNAME, Any, ())
# evaluate the zero-argument form of each of these functions
# as a function returning a static constant based on the build-time
# operating-system kernel
for f in (:is_unix, :is_linux, :is_bsd, :is_apple, :is_windows)
@eval $f() = $(getfield(current_module(),f)(KERNEL))
end
end