Skip to content

Commit a7da72f

Browse files
Merge pull request #21 from brenhinkeller/dlopenext
Match extension-appending behaviour of `Libdl.dlopen`
2 parents 3b08470 + 0d9efaf commit a7da72f

File tree

11 files changed

+102
-15
lines changed

11 files changed

+102
-15
lines changed

src/abstractstaticstring.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,13 @@
112112
c[end] = 0x00 # Null-terminate
113113
return c
114114
end
115+
116+
# Other handy functions
117+
@inline function Base.contains(haystack::AbstractStaticString, needle::AbstractStaticString)
118+
lₕ, lₙ = length(haystack), length(needle)
119+
lₕ < lₙ && return false
120+
for i 0:(lₕ-lₙ)
121+
(haystack[1+i:lₙ+i] == needle) && return true
122+
end
123+
return false
124+
end

src/llvmio.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@
232232
10
233233
```
234234
"""
235-
@static if Sys.isapple()
235+
@static if Sys.isbsd()
236236
@inline function stdoutp()
237237
Base.llvmcall(("""
238238
@__stdoutp = external global i8*
@@ -283,7 +283,7 @@ end
283283
10
284284
```
285285
"""
286-
@static if Sys.isapple()
286+
@static if Sys.isbsd()
287287
@inline function stderrp()
288288
Base.llvmcall(("""
289289
@__stderrp = external global i8*
@@ -330,7 +330,7 @@ end
330330
Ptr{StaticTools.FILE} @0x00007fffc92b9110
331331
```
332332
"""
333-
@static if Sys.isapple()
333+
@static if Sys.isbsd()
334334
@inline function stdinp()
335335
Base.llvmcall(("""
336336
@__stdinp = external global i8*

src/llvmlibc.jl

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,22 @@ julia> StaticTools.dlclose(lib)
640640
0
641641
```
642642
"""
643+
@inline function dlopen(name, flag=RTLD_LOCAL|RTLD_LAZY)
644+
dlext = @static if Sys.isapple()
645+
c".dylib"
646+
elseif Sys.iswindows()
647+
c".dll"
648+
else
649+
c".so"
650+
end
651+
namext = name*dlext
652+
if contains(name, dlext)
653+
GC.@preserve name dlopen(pointer(name), flag)
654+
else
655+
GC.@preserve namext dlopen(pointer(namext), flag)
656+
end
657+
end
643658
@inline dlopen(name::AbstractMallocdMemory, flag=RTLD_LOCAL|RTLD_LAZY) = dlopen(pointer(name), flag)
644-
@inline dlopen(name, flag=RTLD_LOCAL|RTLD_LAZY) = GC.@preserve name dlopen(pointer(name), flag)
645659
@inline function dlopen(name::Ptr{UInt8}, flag::Int32)
646660
Base.llvmcall(("""
647661
; External declaration of the dlopen function
@@ -659,12 +673,13 @@ julia> StaticTools.dlclose(lib)
659673
""", "main"), Ptr{DYLIB}, Tuple{Ptr{UInt8}, Int32}, name, flag)
660674
end
661675

676+
# Prevent this from getting converted into a bare pointer by making it a function
662677
@static if Sys.isapple()
663-
const DLEXT = c".dylib"
678+
@inline DLEXT() = c".dylib"
664679
elseif Sys.iswindows()
665-
const DLEXT = c".dll"
680+
@inline DLEXT() = c".dll"
666681
else
667-
const DLEXT = c".so"
682+
@inline DLEXT() = c".so"
668683
end
669684

670685
## --- dylsym

test/scripts/interop.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using StaticCompiler
2+
using StaticTools
3+
4+
function interop(argc, argv)
5+
lib = StaticTools.dlopen(c"libm")
6+
printf(lib)
7+
sin = StaticTools.dlsym(lib, c"sin")
8+
printf(sin)
9+
x = @ptrcall sin(5.0::Float64)::Float64
10+
printf(x)
11+
newline()
12+
StaticTools.dlclose(lib)
13+
end
14+
15+
# Attempt to compile
16+
path = compile_executable(interop, (Int64, Ptr{Ptr{UInt8}}), "./", cflags=`-ldl -lm`)

test/scripts/rand_matrix.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ function rand_matrix(argc::Int, argv::Ptr{Ptr{UInt8}})
1919
end
2020

2121
# Attempt to compile
22-
path = compile_executable(rand_matrix, (Int64, Ptr{Ptr{UInt8}}), "./")
22+
# cflags=`-lm`: need to explicitly include libm math library on linux
23+
path = compile_executable(rand_matrix, (Int64, Ptr{Ptr{UInt8}}), "./", cflags=`-lm`)

test/scripts/randn_matrix.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ function randn_matrix(argc::Int, argv::Ptr{Ptr{UInt8}})
1818
end
1919

2020
# Attempt to compile
21-
path = compile_executable(randn_matrix, (Int64, Ptr{Ptr{UInt8}}), "./")
21+
# cflags=`-lm`: need to explicitly include libm math library on linux
22+
path = compile_executable(randn_matrix, (Int64, Ptr{Ptr{UInt8}}), "./", cflags=`-lm`)

test/scripts/withmallocarray.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ function withmallocarray(argc::Int, argv::Ptr{Ptr{UInt8}})
2727
end
2828

2929
# Attempt to compile
30+
# cflags=`-lm`: need to explicitly include libm math library on linux
3031
path = compile_executable(withmallocarray, (Int64, Ptr{Ptr{UInt8}}), "./", cflags=`-lm`)

test/testllvminterop.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
## --- dlopen / dlsym / dlclose / @ptrcall / @symbolcall
1111

12-
dlpath = c"libc" * StaticTools.DLEXT
12+
dlpath = c"libc" * StaticTools.DLEXT()
1313
if Sys.islinux()
1414
dlpath *= c".6"
1515
end
@@ -55,6 +55,13 @@
5555

5656
@test StaticTools.dlclose(lib) == 0
5757

58+
# Try opening without specifying extension
59+
if Sys.isbsd()
60+
lib = StaticTools.dlopen(c"libm")
61+
@test lib != C_NULL
62+
@test (lib != C_NULL) && StaticTools.dlclose(lib) == 0
63+
end
64+
5865
## --- more ``@symbolcall`s
5966

6067
cmalloc(nbytes) = @symbolcall malloc(nbytes::Int)::Ptr{Float64}
@@ -71,14 +78,14 @@
7178

7279
## --- @externptr / @externload
7380

74-
if Sys.isapple()
81+
if Sys.isbsd()
7582
foo() = @externload __stderrp::Ptr{UInt8}
7683
else
7784
foo() = @externload stderr::Ptr{UInt8}
7885
end
7986
@test foo() == stderrp()
8087

81-
if Sys.isapple()
88+
if Sys.isbsd()
8289
fooptr() = @externptr __stderrp::Ptr{UInt8}
8390
else
8491
fooptr() = @externptr stderr::Ptr{UInt8}

test/testllvmio.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
@test free(str) == 0
181181

182182
@test fclose(fp) == 0
183-
# @static if fp != 0 && Sys.isapple()
183+
# @static if fp != 0 && Sys.isbsd()
184184
# @test getchar(fp) === UInt8('1')
185185
# @test getchar(fp) === UInt8('\n')
186186
# @test fclose(fp) == 0

test/teststaticcompiler.jl

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ let
101101
@warn "Could not compile $testpath/scripts/randn_matrix.jl"
102102
println(e)
103103
end
104-
@static if Sys.isapple()
104+
@static if Sys.isbsd()
105105
@test isa(status, Base.Process)
106106
@test isa(status, Base.Process) && status.exitcode == 0
107107
end
@@ -115,7 +115,7 @@ let
115115
@warn "Could not run $(scratch)/randn_matrix"
116116
println(e)
117117
end
118-
@static if Sys.isapple()
118+
@static if Sys.isbsd()
119119
@test isa(status, Base.Process)
120120
@test isa(status, Base.Process) && status.exitcode == 0
121121
end
@@ -239,6 +239,36 @@ let
239239
@test isa(status, Base.Process) && status.exitcode == 0
240240
end
241241

242+
## --- Test interop
243+
244+
@static if Sys.isbsd()
245+
let
246+
# Compile...
247+
status = -1
248+
try
249+
isfile("interop") && rm("interop")
250+
status = run(`julia --compile=min $testpath/scripts/interop.jl`)
251+
catch e
252+
@warn "Could not compile $testpath/scripts/interop.jl"
253+
println(e)
254+
end
255+
@test isa(status, Base.Process)
256+
@test isa(status, Base.Process) && status.exitcode == 0
257+
258+
# Run...
259+
println("Interop:")
260+
status = -1
261+
try
262+
status = run(`./interop`)
263+
catch e
264+
@warn "Could not run $(scratch)/interop"
265+
println(e)
266+
end
267+
@test isa(status, Base.Process)
268+
@test isa(status, Base.Process) && status.exitcode == 0
269+
end
270+
end
271+
242272
## --- Clean up
243273

244274
cd(testpath)

0 commit comments

Comments
 (0)