Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide library path to wrapmodule via callback instead of a compile-time constant #366

Merged
merged 4 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CxxWrap"
uuid = "1f15a43c-97ca-5a2a-ae31-89f07a497df4"
authors = ["Bart Janssens <bart@bartjanssens.org>"]
version = "0.13.4"
version = "0.14.0"

[deps]
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Once this code is compiled into a shared library (say `libhello.so`) it can be u
# Load the module and generate the functions
module CppHello
using CxxWrap
@wrapmodule(joinpath("path/to/built/lib","libhello"))
@wrapmodule(() -> joinpath("path/to/built/lib","libhello"))

function __init__()
@initcxx
Expand Down Expand Up @@ -120,20 +120,20 @@ In Julia, the name of the entry point must now be specified explicitly:
```julia
module A
using CxxWrap
@wrapmodule("mylib.so",:define_module_a)
@wrapmodule(() -> "mylib.so",:define_module_a)
end

module B
using CxxWrap
@wrapmodule("mylib.so",:define_module_b)
@wrapmodule(() -> "mylib.so",:define_module_b)
end
```

In specific cases, it may also be necessary to specify `dlopen` flags such as `RTLD_GLOBAL`.
These can be supplied in a third, optional argument to `@wrapmodule`, e.g:

```julia
@wrapmodule(CxxWrapCore.libcxxwrap_julia_stl, :define_cxxwrap_stl_module, Libdl.RTLD_GLOBAL)
@wrapmodule(() -> CxxWrapCore.libcxxwrap_julia_stl, :define_cxxwrap_stl_module, Libdl.RTLD_GLOBAL)
```

## More extensive example and function call performance
Expand Down Expand Up @@ -677,7 +677,7 @@ using CxxWrap
using Base.Test

module Containers
@wrapmodule(libcontainers)
@wrapmodule(() -> libcontainers)
export test_tuple
end
using Containers
Expand Down Expand Up @@ -843,7 +843,7 @@ To do this, call the `wrapmodule` method inside an appropriately named Julia mod
module ExtendedTypes

using CxxWrap
@wrapmodule("libextended")
@wrapmodule(() -> "libextended")
export ExtendedWorld, greet

end
Expand Down
41 changes: 29 additions & 12 deletions src/CxxWrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ function initialize_julia_module(mod::Module)
if has_cxx_module(mod) # Happens when not precompiling
return
end
fptr = Libdl.dlsym(Libdl.dlopen(mod.__cxxwrap_sopath, mod.__cxxwrap_flags), mod.__cxxwrap_wrapfunc)
fptr = Libdl.dlsym(Libdl.dlopen(mod.__cxxwrap_sopath(), mod.__cxxwrap_flags), mod.__cxxwrap_wrapfunc)
register_julia_module(mod, fptr)
funcs = get_module_functions(mod)
precompiling = false
Expand Down Expand Up @@ -726,7 +726,12 @@ function wrapfunctions(jlmod)
wrap_functions(module_functions, jlmod)
end

function readmodule(so_path::AbstractString, funcname, m::Module, flags)
function readmodule(so_path::String, funcname, m::Module, flags)
Base.depwarn("Calling `@readmodule` with the path of the library to load is deprecated. Pass the name of a function returning the path instead, e.g. use `libfoo_jll.get_libfoo_path` instead of `libfoo_jll.libfoo`.", :readmodule; force=true)
readmodule(() -> so_path, funcname, m, flags)
end

function readmodule(so_path_cb::Function, funcname, m::Module, flags)
if isdefined(m, :__cxxwrap_methodkeys)
return
end
Expand All @@ -735,38 +740,50 @@ function readmodule(so_path::AbstractString, funcname, m::Module, flags)
end
Core.eval(m, :(const __cxxwrap_methodkeys = $(MethodKey)[]))
Core.eval(m, :(const __cxxwrap_pointers = $(FunctionPointers)[]))
Core.eval(m, :(const __cxxwrap_sopath = $so_path))
Core.eval(m, :(const __cxxwrap_sopath = $so_path_cb))
Core.eval(m, :(const __cxxwrap_wrapfunc = $(QuoteNode(funcname))))
Core.eval(m, :(const __cxxwrap_flags = $flags))
so_path = so_path_cb()
fptr = Libdl.dlsym(Libdl.dlopen(so_path, flags), funcname)
register_julia_module(m, fptr)
include_dependency(so_path)
end

function wrapmodule(so_path::AbstractString, funcname, m::Module, flags)
readmodule(so_path, funcname, m, flags)
function wrapmodule(so_path::String, funcname, m::Module, flags)
Base.depwarn("Calling `@wrapmodule` with the path of the library to load is deprecated. Pass the name of a function returning the path instead, e.g. use `libfoo_jll.get_libfoo_path` instead of `libfoo_jll.libfoo`.", :wrapmodule; force=true)
wrapmodule(() -> so_path, funcname, m, flags)
end

function wrapmodule(so_path_cb::Function, funcname, m::Module, flags)
readmodule(so_path_cb, funcname, m, flags)
wraptypes(m)
wrapfunctions(m)
end

"""
@wrapmodule libraryfile [functionname]
@wrapmodule libraryfile_cb [functionname]

Place the functions and types from the C++ lib into the module enclosing this macro call
Calls an entry point named `define_julia_module`, unless another name is specified as
the second argument.

`libraryfile_cb` is a
function that returns the shared library file to load as a string. In case of a JLL exporting `libfoo.so`
it is possible to use `foo_jll.get_libfoo_path()`
"""
macro wrapmodule(libraryfile, register_func=:(:define_julia_module), flags=:(nothing))
return :(wrapmodule($(esc(libraryfile)), $(esc(register_func)), $__module__, $(esc(flags))))
macro wrapmodule(libraryfile_cb, register_func=:(:define_julia_module), flags=:(nothing))
return :(wrapmodule($(esc(libraryfile_cb)), $(esc(register_func)), $__module__, $(esc(flags))))
end

"""
@readmodule libraryfile [functionname]
@readmodule libraryfile_cb [functionname]

Read a C++ module and associate it with the Julia module enclosing the macro call.
Read a C++ module and associate it with the Julia module enclosing the macro call. `libraryfile_cb` is a
function that returns the shared library file to load as a string. In case of a JLL exporting `libfoo.so`
it is possible to use `foo_jll.get_libfoo_path()`
"""
macro readmodule(libraryfile, register_func=:(:define_julia_module), flags=:(nothing))
return :(readmodule($(esc(libraryfile)), $(esc(register_func)), $__module__, $(esc(flags))))
macro readmodule(libraryfile_cb, register_func=:(:define_julia_module), flags=:(nothing))
return :(readmodule($(esc(libraryfile_cb)), $(esc(register_func)), $__module__, $(esc(flags))))
end

"""
Expand Down
7 changes: 6 additions & 1 deletion src/StdLib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ function resize end

import Libdl
import libcxxwrap_julia_jll
@wrapmodule(libcxxwrap_julia_jll.libcxxwrap_julia_stl, :define_cxxwrap_stl_module, Libdl.RTLD_GLOBAL)

function get_libcxxwrap_julia_stl_path()::AbstractString
libcxxwrap_julia_jll.libcxxwrap_julia_stl
end

@wrapmodule(get_libcxxwrap_julia_stl_path, :define_cxxwrap_stl_module, Libdl.RTLD_GLOBAL)

function __init__()
@initcxx
Expand Down
2 changes: 1 addition & 1 deletion test/basic_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module BasicTypes
y :: Float32
end

@wrapmodule CxxWrap.CxxWrapCore.libbasic_types()
@wrapmodule CxxWrap.CxxWrapCore.libbasic_types

function __init__()
@initcxx
Expand Down
2 changes: 1 addition & 1 deletion test/containers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ end

module Containers
include(joinpath(@__DIR__, "testcommon.jl"))
@wrapmodule CxxWrap.CxxWrapCore.libjlcxx_containers()
@wrapmodule CxxWrap.CxxWrapCore.libjlcxx_containers

function __init__()
@initcxx
Expand Down
2 changes: 1 addition & 1 deletion test/extended_module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module ExtendedTypes

include(joinpath(@__DIR__, "testcommon.jl"))

@readmodule CxxWrap.CxxWrapCore.libextended()
@readmodule CxxWrap.CxxWrapCore.libextended
@wraptypes
@wrapfunctions

Expand Down
8 changes: 3 additions & 5 deletions test/functions.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
include(joinpath(@__DIR__, "testcommon.jl"))
cxx_available = false

const functions_lib_path = CxxWrap.CxxWrapCore.libfunctions()

# Wrap the functions defined in C++
module CppHalfFunctions
using CxxWrap

@wrapmodule(Main.functions_lib_path, :init_half_module)
@wrapmodule(CxxWrap.CxxWrapCore.libfunctions, :init_half_module)

function __init__()
@initcxx
Expand All @@ -17,7 +15,7 @@ end
module CppTestFunctions

using CxxWrap
@wrapmodule(Main.functions_lib_path, :init_test_module)
@wrapmodule(CxxWrap.CxxWrapCore.libfunctions, :init_test_module)

function __init__()
@initcxx
Expand Down Expand Up @@ -235,7 +233,7 @@ end
half_julia(d::Float64) = d*0.5

# C version
half_c(d::Float64) = ccall((:half_c, functions_lib_path), Cdouble, (Cdouble,), d)
half_c(d::Float64) = ccall((:half_c, CxxWrap.CxxWrapCore.libfunctions()), Cdouble, (Cdouble,), d)

# Bring C++ versions into scope
using .CppHalfFunctions: half_d, half_lambda, half_loop_cpp!, half_loop_jlcall!, half_loop_cfunc!
Expand Down
2 changes: 1 addition & 1 deletion test/hello.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Wrap the functions defined in C++
module CppHello
include(joinpath(@__DIR__, "testcommon.jl"))
@wrapmodule CxxWrap.CxxWrapCore.libhello()
@wrapmodule CxxWrap.CxxWrapCore.libhello

function __init__()
@initcxx
Expand Down
4 changes: 2 additions & 2 deletions test/inheritance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include(joinpath(@__DIR__, "testcommon.jl"))
module CppInheritance

using CxxWrap
@wrapmodule(CxxWrap.CxxWrapCore.libinheritance(), :define_types_module)
@wrapmodule(CxxWrap.CxxWrapCore.libinheritance, :define_types_module)

function __init__()
@initcxx
Expand All @@ -16,7 +16,7 @@ end

module VirtualSolver
using CxxWrap
@wrapmodule(CxxWrap.CxxWrapCore.libinheritance(), :define_vsolver_module)
@wrapmodule(CxxWrap.CxxWrapCore.libinheritance, :define_vsolver_module)

function __init__()
@initcxx
Expand Down
2 changes: 1 addition & 1 deletion test/parametric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include(joinpath(@__DIR__, "testcommon.jl"))
module ParametricTypes

using CxxWrap
@wrapmodule(CxxWrap.CxxWrapCore.libparametric())
@wrapmodule(CxxWrap.CxxWrapCore.libparametric)

function __init__()
@initcxx
Expand Down
2 changes: 1 addition & 1 deletion test/pointer_modification.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module PtrModif
end

include(joinpath(@__DIR__, "testcommon.jl"))
@wrapmodule CxxWrap.CxxWrapCore.libpointer_modification()
@wrapmodule CxxWrap.CxxWrapCore.libpointer_modification

function __init__()
@initcxx
Expand Down
6 changes: 3 additions & 3 deletions test/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module CppTypes
using CxxWrap

GC.gc()
@readmodule(CxxWrap.CxxWrapCore.libtypes())
@readmodule(CxxWrap.CxxWrapCore.libtypes)
@wraptypes
@wrapfunctions
GC.gc()
Expand All @@ -29,7 +29,7 @@ module CppTypes2

using CxxWrap

@wrapmodule CxxWrap.CxxWrapCore.libtypes() :define_types2_module
@wrapmodule CxxWrap.CxxWrapCore.libtypes :define_types2_module

function __init__()
@initcxx
Expand All @@ -41,7 +41,7 @@ module CppTypes3

using CxxWrap

@wrapmodule CxxWrap.CxxWrapCore.libtypes() :define_types3_module
@wrapmodule CxxWrap.CxxWrapCore.libtypes :define_types3_module

function __init__()
@initcxx
Expand Down