Permalink
Browse files

implement `@static` macro for replacing osutils macros

implements #5892
closes #6674 and #4233

Sys.KERNEL now replaces OS_NAME and unambiguously returns
the name of the kernel reported by uname for the build system configuration
  • Loading branch information...
1 parent cac984e commit 694413abca84b1745afe000f05a1676e899e6a43 @vtjnash vtjnash committed May 5, 2016
Showing with 1,003 additions and 804 deletions.
  1. +6 −0 NEWS.md
  2. +3 −3 base/LineEdit.jl
  3. +0 −1 base/Makefile
  4. +3 −3 base/REPLCompletions.jl
  5. +12 −9 base/Terminals.jl
  6. +1 −1 base/array.jl
  7. +3 −2 base/atomics.jl
  8. +5 −3 base/c.jl
  9. +1 −1 base/checked.jl
  10. +11 −8 base/client.jl
  11. +0 −10 base/complex.jl
  12. +2 −2 base/dSFMT.jl
  13. +1 −1 base/datafmt.jl
  14. +71 −4 base/deprecated.jl
  15. +2 −60 base/docs/helpdb/Base.jl
  16. +40 −44 base/env.jl
  17. +4 −3 base/event.jl
  18. +6 −11 base/exports.jl
  19. +85 −60 base/file.jl
  20. +3 −1 base/filesystem.jl
  21. +1 −1 base/floatfuncs.jl
  22. +6 −6 base/initdefs.jl
  23. +2 −11 base/int.jl
  24. +74 −44 base/interactiveutil.jl
  25. +1 −1 base/intfuncs.jl
  26. +37 −23 base/libc.jl
  27. +15 −13 base/libdl.jl
  28. +1 −1 base/libgit2/repository.jl
  29. +4 −4 base/loading.jl
  30. +1 −1 base/managers.jl
  31. +30 −23 base/mmap.jl
  32. +1 −1 base/multi.jl
  33. +68 −51 base/osutils.jl
  34. +15 −8 base/path.jl
  35. +8 −6 base/pkg/cache.jl
  36. +10 −8 base/pkg/reqs.jl
  37. +30 −22 base/poll.jl
  38. +1 −1 base/printf.jl
  39. +1 −1 base/process.jl
  40. +6 −4 base/profile.jl
  41. +11 −13 base/random.jl
  42. +19 −18 base/sharedarray.jl
  43. +1 −1 base/show.jl
  44. +1 −1 base/socket.jl
  45. +36 −22 base/stream.jl
  46. +1 −4 base/sysimg.jl
  47. +70 −43 base/sysinfo.jl
  48. +1 −1 base/task.jl
  49. +8 −4 base/util.jl
  50. +16 −18 doc/manual/handling-operating-system-variation.rst
  51. +11 −6 doc/manual/packages.rst
  52. +15 −19 doc/stdlib/base.rst
  53. +30 −18 doc/stdlib/constants.rst
  54. +1 −1 doc/stdlib/parallel.rst
  55. +1 −1 doc/stdlib/test.rst
  56. +7 −0 src/Makefile
  57. +4 −6 src/dump.c
  58. +1 −1 src/julia.h
  59. +3 −17 src/sys.c
  60. +1 −1 test/backtrace.jl
  61. +1 −1 test/ccall.jl
  62. +2 −5 test/checked.jl
  63. +18 −15 test/cmdlineargs.jl
  64. +2 −6 test/core.jl
  65. +4 −2 test/dates/conversions.jl
  66. +1 −1 test/examples.jl
  67. +57 −46 test/file.jl
  68. +1 −1 test/int.jl
  69. +2 −2 test/libdl.jl
  70. +13 −7 test/misc.jl
  71. +6 −5 test/parallel_exec.jl
  72. +17 −4 test/path.jl
  73. +3 −3 test/perf/micro/perf.jl
  74. +1 −0 test/perf/perfutil.jl
  75. +16 −16 test/pollfd.jl
  76. +4 −2 test/printf.jl
  77. +1 −1 test/ranges.jl
  78. +6 −5 test/read.jl
  79. +5 −5 test/repl.jl
  80. +4 −4 test/replcompletions.jl
  81. +1 −1 test/runtests.jl
  82. +3 −3 test/socket.jl
  83. +21 −12 test/spawn.jl
  84. +2 −2 test/strings/basic.jl
  85. +2 −2 test/strings/io.jl
  86. +1 −1 test/threads.jl
View
@@ -165,6 +165,12 @@ Library improvements
* The `libjulia` library is now properly versioned and installed to the public `<prefix>/lib`
directory, instead of the private `<prefix>/lib/julia` directory.
+ * System reflection is now more consistently exposed from Sys and not Base.
+ `OS_NAME` has been replaced by `Sys.KERNEL` and always reports the name of the kernel (as reported by `uname`).
+ The `@windows_only` and `@osx` family of macros have been replaced with functions such as `is_windows()` and
+ or `is_apple()`. There's now also an `@static` macro that will evaluate the condition of an if-statement at
+ compile time, for when a static branch is required.
+
Deprecated or removed
---------------------
View
@@ -204,7 +204,7 @@ function refresh_multi_line(termbuf::TerminalBuffer, terminal::UnixTerminal, buf
write_prompt(termbuf, prompt)
prompt = prompt_string(prompt)
# Count the '\n' at the end of the line if the terminal emulator does (specific to DOS cmd prompt)
- miscountnl = @windows ? (isa(Terminals.pipe_reader(terminal), Base.TTY) && !Base.ispty(Terminals.pipe_reader(terminal))) : false
+ miscountnl = @static is_windows() ? (isa(Terminals.pipe_reader(terminal), Base.TTY) && !Base.ispty(Terminals.pipe_reader(terminal))) : false
lindent = strwidth(prompt)
# Now go through the buffer line by line
@@ -1564,7 +1564,7 @@ function run_interface(terminal, m::ModalInterface)
p = s.current_mode
buf, ok, suspend = prompt!(terminal, m, s)
while suspend
- @unix_only ccall(:jl_repl_raise_sigtstp, Cint, ())
+ @static if is_unix(); ccall(:jl_repl_raise_sigtstp, Cint, ()); end
buf, ok, suspend = prompt!(terminal, m, s)
end
mode(state(s, s.current_mode)).on_done(s, buf, ok)
@@ -1604,7 +1604,7 @@ function prompt!(term, prompt, s = init_state(term, prompt))
elseif state == :done
return buffer(s), true, false
elseif state == :suspend
- @unix_only begin
+ if is_unix()
return buffer(s), true, true
end
else
View
@@ -38,7 +38,6 @@ $(BUILDDIR)/uv_constants.jl: $(SRCDIR)/../src/uv_constants.h $(build_includedir)
$(BUILDDIR)/build_h.jl.phony:
@echo "# This file is automatically generated in base/Makefile" > $@
- @echo "const ARCH = :$(ARCH)" >> $@
ifeq ($(XC_HOST),)
@echo "const MACHINE = \"$(BUILD_MACHINE)\"" >> $@
else
@@ -100,7 +100,7 @@ function complete_keyword(s::String)
end
function complete_path(path::AbstractString, pos; use_envpath=false)
- if Base.is_unix(OS_NAME) && ismatch(r"^~(?:/|$)", path)
+ if Base.is_unix() && ismatch(r"^~(?:/|$)", path)
# if the path is just "~", don't consider the expanded username as a prefix
if path == "~"
dir, prefix = homedir(), ""
@@ -128,13 +128,13 @@ function complete_path(path::AbstractString, pos; use_envpath=false)
if startswith(file, prefix)
id = try isdir(joinpath(dir, file)) catch; false end
# joinpath is not used because windows needs to complete with double-backslash
- push!(matches, id ? file * (@windows? "\\\\" : "/") : file)
+ push!(matches, id ? file * (@static is_windows() ? "\\\\" : "/") : file)
end
end
if use_envpath && length(dir) == 0
# Look for files in PATH as well
- local pathdirs = split(ENV["PATH"], @unix? ":" : ";")
+ local pathdirs = split(ENV["PATH"], @static is_windows() ? ";" : ":")
for pathdir in pathdirs
local actualpath
View
@@ -116,7 +116,7 @@ cmove_line_up(t::UnixTerminal, n) = (cmove_up(t, n); cmove_col(t, 0))
cmove_line_down(t::UnixTerminal, n) = (cmove_down(t, n); cmove_col(t, 0))
cmove_col(t::UnixTerminal, n) = write(t.out_stream, "$(CSI)$(n)G")
-@windows ? begin
+if is_windows()
function raw!(t::TTYTerminal,raw::Bool)
check_open(t.in_stream)
if Base.ispty(t.in_stream)
@@ -132,7 +132,7 @@ cmove_col(t::UnixTerminal, n) = write(t.out_stream, "$(CSI)$(n)G")
t.in_stream.handle, raw) != -1
end
end
-end : begin
+else
function raw!(t::TTYTerminal, raw::Bool)
check_open(t.in_stream)
ccall(:jl_tty_set_mode, Int32, (Ptr{Void},Int32), t.in_stream.handle, raw) != -1
@@ -151,14 +151,17 @@ clear(t::UnixTerminal) = write(t.out_stream, "\x1b[H\x1b[2J")
clear_line(t::UnixTerminal) = write(t.out_stream, "\x1b[0G\x1b[0K")
#beep(t::UnixTerminal) = write(t.err_stream,"\x7")
-@unix_only function hascolor(t::TTYTerminal)
- startswith(t.term_type, "xterm") && return true
- try
- return success(`tput setaf 0`)
- catch
- return false
+if is_windows()
+ hascolor(t::TTYTerminal) = true
+else
+ function hascolor(t::TTYTerminal)
+ startswith(t.term_type, "xterm") && return true
+ try
+ return success(`tput setaf 0`)
+ catch
+ return false
+ end
end
end
-@windows_only hascolor(t::TTYTerminal) = true
end # module
View
@@ -696,7 +696,7 @@ function vcat{T}(arrays::Vector{T}...)
if isbits(T)
elsz = sizeof(T)
else
- elsz = div(WORD_SIZE,8)
+ elsz = Core.sizeof(Ptr{Void})
end
for a in arrays
nba = length(a)*elsz
View
@@ -3,6 +3,7 @@
using Core.Intrinsics: llvmcall
import Base: setindex!, getindex, unsafe_convert
+import Base.Sys: ARCH, WORD_SIZE
export
Atomic,
@@ -16,8 +17,8 @@ export
# Disable 128-bit types on 32-bit Intel sytems due to LLVM problems;
# see <https://github.com/JuliaLang/julia/issues/14818> (fixed on LLVM 3.9)
# 128-bit atomics do not exist on AArch32.
-if (VersionNumber(Base.libllvm_version) < v"3.9-" && Base.ARCH === :i686) ||
- startswith(string(Base.ARCH), "arm")
+if (VersionNumber(Base.libllvm_version) < v"3.9-" && ARCH === :i686) ||
+ startswith(string(ARCH), "arm")
const inttypes = (Int8, Int16, Int32, Int64,
UInt8, UInt16, UInt32, UInt64)
else
View
@@ -16,7 +16,7 @@ typealias Cshort Int16
typealias Cushort UInt16
typealias Cint Int32
typealias Cuint UInt32
-if OS_NAME === :Windows
+if is_windows()
typealias Clong Int32
typealias Culong UInt32
typealias Cwchar_t UInt16
@@ -35,7 +35,7 @@ typealias Culonglong UInt64
typealias Cfloat Float32
typealias Cdouble Float64
-if OS_NAME !== :Windows
+if !is_windows()
const sizeof_mode_t = ccall(:jl_sizeof_mode_t, Cint, ())
if sizeof_mode_t == 2
typealias Cmode_t Int16
@@ -95,11 +95,13 @@ convert(::Type{Cstring}, s::Symbol) = Cstring(unsafe_convert(Ptr{Cchar}, s))
# in string.jl: unsafe_convert(::Type{Cwstring}, s::WString)
# FIXME: this should be handled by implicit conversion to Cwstring, but good luck with that
-@windows_only function cwstring(s::AbstractString)
+if is_windows()
+function cwstring(s::AbstractString)
bytes = String(s).data
0 in bytes && throw(ArgumentError("embedded NULs are not allowed in C strings: $(repr(s))"))
return push!(utf8to16(bytes), 0)
end
+end
# conversions between UTF-8 and UTF-16 for Windows APIs
View
@@ -68,7 +68,7 @@ brokenSignedInt = Union{}
brokenUnsignedInt = Union{}
brokenSignedIntMul = Int128
brokenUnsignedIntMul = UInt128
-if WORD_SIZE == 32
+if Core.sizeof(Ptr{Void}) == 4
brokenSignedIntMul = Union{brokenSignedIntMul, Int64}
brokenUnsignedIntMul = Union{brokenUnsignedIntMul, UInt64}
end
View
@@ -37,10 +37,13 @@ text_colors
have_color = false
default_color_warn = :red
default_color_info = :blue
-@unix_only default_color_input = :bold
-@unix_only default_color_answer = :bold
-@windows_only default_color_input = :normal
-@windows_only default_color_answer = :normal
+if is_windows()
+ default_color_input = :normal
+ default_color_answer = :normal
+else
+ default_color_input = :bold
+ default_color_answer = :bold
+end
color_normal = text_colors[:normal]
function repl_color(key, default)
@@ -77,15 +80,15 @@ function repl_cmd(cmd, out)
end
cd(ENV["OLDPWD"])
else
- cd(@windows? dir : readchomp(`$shell -c "echo $(shell_escape(dir))"`))
+ cd(@static is_windows() ? dir : readchomp(`$shell -c "echo $(shell_escape(dir))"`))
end
else
cd()
end
ENV["OLDPWD"] = new_oldpwd
println(out, pwd())
else
- run(ignorestatus(@windows? cmd : (isa(STDIN, TTY) ? `$shell -i -c "($(shell_escape(cmd))) && true"` : `$shell -c "($(shell_escape(cmd))) && true"`)))
+ run(ignorestatus(@static is_windows() ? cmd : (isa(STDIN, TTY) ? `$shell -i -c "($(shell_escape(cmd))) && true"` : `$shell -c "($(shell_escape(cmd))) && true"`)))
end
nothing
end
@@ -323,10 +326,10 @@ function _start()
global active_repl_backend
if repl
if !isa(STDIN,TTY)
- global is_interactive |= !isa(STDIN,Union{File,IOStream})
+ global is_interactive |= !isa(STDIN, Union{File, IOStream})
color_set || (global have_color = false)
else
- term = Terminals.TTYTerminal(get(ENV,"TERM",@windows? "" : "dumb"),STDIN,STDOUT,STDERR)
+ term = Terminals.TTYTerminal(get(ENV, "TERM", @static is_windows() ? "" : "dumb"), STDIN, STDOUT, STDERR)
global is_interactive = true
color_set || (global have_color = Terminals.hascolor(term))
quiet || REPL.banner(term,term)
View
@@ -760,22 +760,12 @@ function lexcmp(a::Complex, b::Complex)
end
#Rounding complex numbers
-# Superfluous tuple splatting in return arguments is a work around for 32-bit systems (#10027)
#Requires two different RoundingModes for the real and imaginary components
-
-if WORD_SIZE==32
-function round{T<:AbstractFloat, MR, MI}(z::Complex{T}, ::RoundingMode{MR}, ::RoundingMode{MI})
- Complex((round(real(z), RoundingMode{MR}()),
- round(imag(z), RoundingMode{MI}()))...)
-end
-round(z::Complex) = Complex((round(real(z)), round(imag(z)))...)
-else
function round{T<:AbstractFloat, MR, MI}(z::Complex{T}, ::RoundingMode{MR}, ::RoundingMode{MI})
Complex(round(real(z), RoundingMode{MR}()),
round(imag(z), RoundingMode{MI}()))
end
round(z::Complex) = Complex(round(real(z)), round(imag(z)))
-end
@vectorize_1arg Complex round
View
@@ -151,9 +151,9 @@ end
## Windows entropy
-@windows_only begin
+if is_windows()
function win32_SystemFunction036!{T}(a::Array{T})
- ccall((:SystemFunction036,:Advapi32),stdcall,UInt8,(Ptr{Void},UInt32),a,sizeof(a))
+ ccall((:SystemFunction036, :Advapi32), stdcall, UInt8, (Ptr{Void}, UInt32), a, sizeof(a))
end
end
View
@@ -42,7 +42,7 @@ readdlm(input, dlm::Char, T::Type, eol::Char; opts...) =
function readdlm_auto(input, dlm::Char, T::Type, eol::Char, auto::Bool; opts...)
optsd = val_opts(opts)
- use_mmap = get(optsd, :use_mmap, @windows ? false : true)
+ use_mmap = get(optsd, :use_mmap, is_windows() ? false : true)
if isa(input, AbstractString)
fsz = filesize(input)
if use_mmap && fsz > 0 && fsz < typemax(Int)
View
@@ -585,7 +585,7 @@ function msync end
msync{T}(A::Array{T}) = msync(pointer(A), length(A)*sizeof(T))
msync(B::BitArray) = msync(pointer(B.chunks), length(B.chunks)*sizeof(UInt64))
-@unix_only begin
+if is_unix()
export mmap
@noinline function mmap(len::Integer, prot::Integer, flags::Integer, fd, offset::Integer)
depwarn("`mmap` is deprecated, use `Mmap.mmap(io, Array{T,N}, dims, offset)` instead to return an mmapped-array", :mmap)
@@ -634,7 +634,7 @@ end
end
-@windows_only begin
+if is_windows()
@noinline function munmap(viewhandle::Ptr, mmaphandle::Ptr)
depwarn("`munmap` is deprecated, `mmap` Arrays are automatically munmapped when finalized", :munmap)
status = ccall(:UnmapViewOfFile, stdcall, Cint, (Ptr{Void},), viewhandle)!=0
@@ -654,9 +654,11 @@ end
end
-@unix_only @deprecate mmap_array{T,N}(::Type{T}, dims::NTuple{N,Integer}, s::IO, offset=position(s)) Mmap.mmap(s, Array{T,N}, dims, offset)
+if is_unix()
+ @deprecate mmap_array{T,N}(::Type{T}, dims::NTuple{N,Integer}, s::IO, offset=position(s)) Mmap.mmap(s, Array{T,N}, dims, offset)
+end
-@windows_only begin
+if is_windows()
type SharedMemSpec
name :: AbstractString
readonly :: Bool
@@ -1186,6 +1188,71 @@ end
isequal(x::Char, y::Integer) = false
isequal(x::Integer, y::Char) = false
+#6674 and #4233
+macro windows(qm,ex)
+ depwarn("`@windows` is deprecated, use `@static is_windows()` instead", Symbol("@windows"))
+ return @static is_windows() ? esc(ex.args[1]) : esc(ex.args[2])
+end
+macro unix(qm,ex)
+ depwarn("`@unix` is deprecated, use `@static is_unix()` instead", Symbol("@unix"))
+ return @static is_unix() ? esc(ex.args[1]) : esc(ex.args[2])
+end
+macro osx(qm,ex)
+ depwarn("`@osx` is deprecated, use `@static is_apple()` instead", Symbol("@osx"))
+ return @static is_apple() ? esc(ex.args[1]) : esc(ex.args[2])
+end
+macro linux(qm,ex)
+ depwarn("`@linux` is deprecated, use `@static is_linux()` instead", Symbol("@linux"))
+ return @static is_linux() ? esc(ex.args[1]) : esc(ex.args[2])
+end
+macro windows_only(ex)
+ depwarn("`@windows_only` is deprecated, use `@static if is_windows()` instead", Symbol("@windows_only"))
+ return @static if is_windows() esc(ex) end
+end
+macro unix_only(ex)
+ depwarn("`@unix_only` is deprecated, use `@static if is_unix()` instead", Symbol("@unix_only"))
+ return @static if is_unix() esc(ex) end
+end
+macro osx_only(ex)
+ depwarn("`@osx_only` is deprecated, use `@static if is_apple()` instead", Symbol("@osx_only"))
+ return @static if is_apple() esc(ex) end
+end
+macro linux_only(ex)
+ depwarn("`@linux_only` is deprecated, use `@static if is_linux()` instead", Symbol("@linux_only"))
+ return @static if is_linux() esc(ex) end
+end
+export
+ @windows,
+ @unix,
+ @osx,
+ @linux,
+ @windows_only,
+ @unix_only,
+ @osx_only,
+ @linux_only
+
+const OS_NAME =
+ if Sys.KERNEL === :Darwin
+ :OSX
+ elseif Sys.KERNEL === :NT
+ :Windows
+ else
+ Sys.KERNEL
+ end
+deprecate(:OS_NAME) # use Sys.KERNEL now
+
+export CPU_CORES
+function _set_CPU_CORES()
+ global const CPU_CORES = Sys.CPU_CORES
+ deprecate(Base, :CPU_CORES)
+end
+module Init_CPU_CORES
+ const __init__ = Base._set_CPU_CORES
+end
+
+@deprecate_binding WORD_SIZE Sys.WORD_SIZE
+
+
# During the 0.5 development cycle, do not add any deprecations below this line
# To be deprecated in 0.6
Oops, something went wrong.

0 comments on commit 694413a

Please sign in to comment.