Skip to content

Commit

Permalink
implement @static macro for replacing osutils macros
Browse files Browse the repository at this point in the history
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
vtjnash committed May 21, 2016
1 parent b45adc5 commit 16088d1
Show file tree
Hide file tree
Showing 86 changed files with 994 additions and 801 deletions.
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ Library improvements

* Concatenating dense and sparse matrices now returns a sparse matrix ([#15172]).

* 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
---------------------

Expand Down
6 changes: 3 additions & 3 deletions base/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion base/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions base/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(), ""
Expand Down Expand Up @@ -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
Expand Down
21 changes: 12 additions & 9 deletions base/Terminals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions base/atomics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Core.Intrinsics: llvmcall

import Base: setindex!, getindex, unsafe_convert
import Base.Sys: Sys, WORD_SIZE

export
Atomic,
Expand All @@ -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-" && Sys.ARCH === :i686) ||
startswith(string(Sys.ARCH), "arm")
const inttypes = (Int8, Int16, Int32, Int64,
UInt8, UInt16, UInt32, UInt64)
else
Expand Down
8 changes: 5 additions & 3 deletions base/c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion base/checked.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 11 additions & 8 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 0 additions & 10 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions base/dSFMT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion base/datafmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
71 changes: 67 additions & 4 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -1186,6 +1188,67 @@ 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

module Init_CPU_CORES
__init__() = eval(Base, :(@deprecate_binding CPU_CORES Sys.CPU_CORES))
end
export CPU_CORES

@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

Expand Down
Loading

0 comments on commit 16088d1

Please sign in to comment.