Skip to content

Commit

Permalink
Add convenience functions to set preferences (#88)
Browse files Browse the repository at this point in the history
* add convenience functions to set preferences

* Fix typos

Co-authored-by: Hendrik Ranocha <[email protected]>

---------

Co-authored-by: Hendrik Ranocha <[email protected]>
  • Loading branch information
JoshuaLampert and ranocha committed Oct 12, 2023
1 parent 1811e9b commit a6371a3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
28 changes: 21 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ julia> set_preferences!(
UUID("7d669430-f675-4ae7-b43e-fab78ec5a902"), # UUID of P4est.jl
"libp4est" => "/path/to/your/libp4est.so", force = true)
```
Alternatively, you can use the convenience function `set_library_p4est!` to set
the path:

```julia
julia> using P4est

julia> P4est.set_library_p4est!("/path/to/your/libp4est.so")
[ Info: Please restart Julia and reload P4est.jl for the library changes to take effect
```
On Windows you also need to set the path to the local build of the shared library
of [`libsc`](https://github.com/cburstedde/libsc/tree/master), which is a subpackage
of `p4est`. On other systems, this is not necessary as the library is already linked
Expand All @@ -108,6 +117,13 @@ julia> set_preferences!(
UUID("7d669430-f675-4ae7-b43e-fab78ec5a902"), # UUID of P4est.jl
"libsc" => "/path/to/your/libsc.so", force = true)
```
Again, for convenience you can also use
```julia
julia> P4est.set_library_sc!("/path/to/your/libsc.so")
[ Info: Please restart Julia and reload P4est.jl for the library changes to take effect
```
To delete the preferences again, you can call `P4est.set_library_p4est!()` and
`P4est.set_library_sc!()`, respectively.
Note that you should restart your Julia session after changing the preferences.
To sum up, follow these steps to use
Expand All @@ -131,15 +147,13 @@ installation of the underlying C libraries.
```
- Set [P4est.jl](https://github.com/trixi-framework/P4est.jl) preferences.
```julia
julia> using Preferences, UUIDs
julia> using P4est

julia> set_preferences!(
UUID("7d669430-f675-4ae7-b43e-fab78ec5a902"), # UUID of P4est.jl
"libp4est" => "/path/to/your/libp4est.so", force = true)
julia> P4est.set_library_p4est!("/path/to/your/libp4est.so")
[ Info: Please restart Julia and reload P4est.jl for the library changes to take effect

julia> set_preferences!(
UUID("7d669430-f675-4ae7-b43e-fab78ec5a902"), # UUID of P4est.jl
"libsc" => "/path/to/your/libsc.so", force = true)
julia> P4est.set_library_sc!("/path/to/your/libsc.so")
[ Info: Please restart Julia and reload P4est.jl for the library changes to take effect
```
- Restart the Julia REPL and load the packages.
```julia
Expand Down
42 changes: 41 additions & 1 deletion src/P4est.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ using Reexport: @reexport
# We need to load the preference setting from here and not from `LibP4est.jl`
# since `@load_preference` looks into the module it is running from. Thus, we
# load all preferences here and access them from the `module LibP4est`.
using Preferences: @load_preference
using Preferences: @load_preference, set_preferences!, delete_preferences!
using UUIDs: UUID
const _PREFERENCE_LIBP4EST = @load_preference("libp4est", "P4est_jll")
const _PREFERENCE_LIBSC = @load_preference("libsc", _PREFERENCE_LIBP4EST)

Expand Down Expand Up @@ -45,6 +46,45 @@ to check whether `p4est` has been initialized.
"""
package_id() = unsafe_load(cglobal((:p4est_package_id, LibP4est.libp4est), Cint))

const P4EST_UUID = UUID("7d669430-f675-4ae7-b43e-fab78ec5a902")

"""
P4est.set_library_p4est!(path; force = true)
Set the `path` to a system-provided `p4est` installation. Restart the Julia session
after executing this function so that the changes take effect. Calling this
function is necessary when you want to use a system-provided `p4est`
installation.
"""
function set_library_p4est!(path = nothing; force = true)
if isnothing(path)
delete_preferences!(P4EST_UUID, "libp4est"; force = force)
else
isfile(path) || throw(ArgumentError("$path is not a file that exists."))
set_preferences!(P4EST_UUID, "libp4est" => path, force = force)
end
@info "Please restart Julia and reload P4est.jl for the library changes to take effect"
end

"""
P4est.set_library_sc!(path; force = true)
Set the `path` to a system-provided `sc` installation. Restart the Julia session
after executing this function so that the changes take effect. Calling this
function is necessary, when you want to use a system-provided `p4est`
installation on Windows or when you want to use another `sc`
installation than the one that `libp4est.so` already links to.
"""
function set_library_sc!(path = nothing; force = true)
if isnothing(path)
delete_preferences!(P4EST_UUID, "libsc"; force = force)
else
isfile(path) || throw(ArgumentError("$path is not a file that exists."))
set_preferences!(P4EST_UUID, "libsc" => path, force = force)
end
@info "Please restart Julia and reload P4est.jl for the library changes to take effect"
end

"""
P4est.init(log_handler, log_threshold)
Expand Down

0 comments on commit a6371a3

Please sign in to comment.