Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit f2b2844

Browse files
committed
platform macros with (ternary operator) else clauses, of the form @windows? a : b
1 parent fa9721e commit f2b2844

File tree

5 files changed

+121
-13
lines changed

5 files changed

+121
-13
lines changed

base/exports.jl

+4
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,10 @@ export
12471247
@allocated,
12481248
@profile,
12491249
@which,
1250+
@windows,
1251+
@unix,
1252+
@osx,
1253+
@linux,
12501254
@windows_only,
12511255
@unix_only,
12521256
@osx_only,

base/osutils.jl

+32-9
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,43 @@ function is_unix(os::Symbol)
77
end
88
end
99

10-
macro windows_only(ex)
11-
OS_NAME == :Windows ? esc(ex) : nothing
10+
os_name(os::Symbol) = string(os)
11+
12+
macro _os_test(qm,ex,test)
13+
esc(quote
14+
@assert $qm == :?
15+
@assert isa($ex,Expr)
16+
@assert $ex.head == :(:)
17+
@assert length($ex.args) == 2
18+
if $test
19+
return esc($ex.args[1])
20+
else
21+
return esc($ex.args[2])
22+
end
23+
end)
24+
end
25+
macro windows(qm,ex)
26+
@_os_test qm ex OS_NAME===:Windows
27+
end
28+
macro unix(qm,ex)
29+
@_os_test qm ex is_unix(OS_NAME)
30+
end
31+
macro osx(qm,ex)
32+
@_os_test qm ex OS_NAME===:Darwin
33+
end
34+
macro linux(qm,ex)
35+
@_os_test qm ex is_unix(OS_NAME) && OS_NAME!==:Darwin
1236
end
1337

38+
macro windows_only(ex)
39+
@windows? esc(ex) : nothing
40+
end
1441
macro unix_only(ex)
15-
is_unix(OS_NAME) ? esc(ex) : nothing
42+
@unix? esc(ex) : nothing
1643
end
17-
1844
macro osx_only(ex)
19-
OS_NAME == :Darwin ? esc(ex) : nothing
45+
@osx? esc(ex) : nothing
2046
end
21-
2247
macro linux_only(ex)
23-
is_unix(OS_NAME) && OS_NAME != :Darwin ? esc(ex) : nothing
48+
@linux? esc(ex) : nothing
2449
end
25-
26-
os_name(os::Symbol) = string(os)

doc/helpdb.jl

+34-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@
407407
408408
The byte offset of each field of a type relative to the data start.
409409
For example, we could use it in the following manner to summarize
410-
information about the struct type:
410+
information about a struct type:
411411
412412
structinfo(T) = [zip(fieldoffsets(T),names(T),T.types)...]
413413
structinfo(Stat)
@@ -5133,6 +5133,39 @@
51335133
51345134
"),
51355135

5136+
("System","Base","@unix","@unix()
5137+
5138+
Given \"@unix? a : b\", do \"a\" on Unix systems (including Linux
5139+
and OS X) and \"b\" elsewhere. See documentation for Handling
5140+
Platform Variations in the Calling C and Fortran Code section of
5141+
the manual.
5142+
5143+
"),
5144+
5145+
("System","Base","@osx","@osx()
5146+
5147+
Given \"@osx? a : b\", do \"a\" on OS X and \"b\" elsewhere. See
5148+
documentation for Handling Platform Variations in the Calling C and
5149+
Fortran Code section of the manual.
5150+
5151+
"),
5152+
5153+
("System","Base","@linux","@linux()
5154+
5155+
Given \"@linux? a : b\", do \"a\" on Linux and \"b\" elsewhere. See
5156+
documentation for Handling Platform Variations in the Calling C and
5157+
Fortran Code section of the manual.
5158+
5159+
"),
5160+
5161+
("System","Base","@windows","@windows()
5162+
5163+
Given \"@windows? a : b\", do \"a\" on Windows and \"b\" elsewhere.
5164+
See documentation for Handling Platform Variations in the Calling C
5165+
and Fortran Code section of the manual.
5166+
5167+
"),
5168+
51365169
("C Interface","Base","ccall","ccall((symbol, library) or fptr, RetType, (ArgType1, ...), ArgVar1, ...)
51375170
51385171
Call function in C-exported shared library, specified by (function

doc/manual/calling-c-and-fortran-code.rst

+29
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,32 @@ C++
417417

418418
Limited support for C++ is provided by the `Cpp <http://github.com/timholy/Cpp.jl>`_
419419
and `Clang <https://github.com/ihnorton/Clang.jl>`_ packages.
420+
421+
Handling Platform Variations
422+
----------------------------
423+
424+
When dealing with platform libraries, it is often necessary to provide special cases
425+
for various platforms. The variable ``OS_NAME`` can be used to write these special
426+
cases. Additionally, there are several macros intended to make this easier:
427+
``@windows``, ``@unix``, ``@linux``, and ``@osx``. Note that linux and osx are mutually
428+
exclusive subsets of unix. Their usage takes the form of a ternary conditional
429+
operator, as demonstrated in the following examples.
430+
431+
Simple blocks::
432+
433+
ccall( (@windows? :_fopen : :fopen), ...)
434+
435+
Complex blocks::
436+
437+
@linux? (
438+
begin
439+
some_complicated_thing(a)
440+
end
441+
: begin
442+
some_different_thing(a)
443+
end
444+
)
445+
446+
Chaining (parentheses optional, but recommended for readability)::
447+
448+
@windows? :a : (@osx? :b : :c)

doc/stdlib/base.rst

+22-3
Original file line numberDiff line numberDiff line change
@@ -1145,8 +1145,6 @@ Text I/O
11451145
Memory-mapped I/O
11461146
-----------------
11471147

1148-
Note: Currently not available on Windows.
1149-
11501148
.. function:: mmap_array(type, dims, stream, [offset])
11511149

11521150
Create an ``Array`` whose values are linked to a file, using memory-mapping. This provides a convenient way of working with data too large to fit in the computer's memory.
@@ -3413,12 +3411,33 @@ System
34133411

34143412
Reference to the singleton ``EnvHash``, providing a dictionary interface to system environment variables.
34153413

3414+
.. function:: @unix
3415+
3416+
Given ``@unix? a : b``, do ``a`` on Unix systems (including Linux and OS X) and ``b`` elsewhere. See documentation
3417+
for Handling Platform Variations in the Calling C and Fortran Code section of the manual.
3418+
3419+
.. function:: @osx
3420+
3421+
Given ``@osx? a : b``, do ``a`` on OS X and ``b`` elsewhere. See documentation for Handling Platform Variations
3422+
in the Calling C and Fortran Code section of the manual.
3423+
3424+
.. function:: @linux
3425+
3426+
Given ``@linux? a : b``, do ``a`` on Linux and ``b`` elsewhere. See documentation for Handling Platform Variations
3427+
in the Calling C and Fortran Code section of the manual.
3428+
3429+
.. function:: @windows
3430+
3431+
Given ``@windows? a : b``, do ``a`` on Windows and ``b`` elsewhere. See documentation for Handling Platform Variations
3432+
in the Calling C and Fortran Code section of the manual.
3433+
34163434
C Interface
34173435
-----------
34183436

34193437
.. function:: ccall((symbol, library) or fptr, RetType, (ArgType1, ...), ArgVar1, ...)
34203438

3421-
Call function in C-exported shared library, specified by (function name, library) tuple (String or :Symbol). Alternatively, ccall may be used to call a function pointer returned by dlsym, but note that this usage is generally discouraged to facilitate future static compilation.
3439+
Call function in C-exported shared library, specified by (function name, library) tuple (String or :Symbol). Alternatively,
3440+
ccall may be used to call a function pointer returned by dlsym, but note that this usage is generally discouraged to facilitate future static compilation.
34223441

34233442
.. function:: cglobal((symbol, library) or ptr [, Type=Void])
34243443

0 commit comments

Comments
 (0)