# How do libraries compiled by PackageCompiler.jl interact with Preferences.jl?

**URL:** <https://discourse.julialang.org/t/how-do-libraries-compiled-by-packagecompiler-jl-interact-with-preferences-jl/103479>\
**Category:** General Usage\
**Tags:** package-compiler\
**Created:** [September 3, 2023, 11:48am UTC](https://discourse.julialang.org/t/how-do-libraries-compiled-by-packagecompiler-jl-interact-with-preferences-jl/103479 "2023-09-03T11:48:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sloede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sloede/32/44787_2.png) [@sloede](https://discourse.julialang.org/u/sloede)\
**Post date:** [September 3, 2023, 11:48am UTC](https://discourse.julialang.org/t/how-do-libraries-compiled-by-packagecompiler-jl-interact-with-preferences-jl/103479/1 "2023-09-03T11:48:12Z")

</div>

How do [Preferences.jl](https://github.com/JuliaPackaging/Preferences.jl) work together libraries created by [PackageCompiler.jl](https://github.com/JuliaLang/PackageCompiler.jl)?

For example, where do I have to put the Preferences.jl file such that the choice of system MPI library with [MPI.jl](https://github.com/JuliaParallel/MPI.jl) is respected while compiling the library?

Furthermore, is it possible to also change preferences after the library has been compiled - especially when it affects compile-time preferences such as (in the example above) the path to the system library and ABI.

Our use case is [libtrixi](https://github.com/trixi-framework/libtrixi), and more specifically [LibTrixi.jl](https://github.com/trixi-framework/libtrixi/tree/main/LibTrixi.jl), where we want to compile a standalone library from a Julia package and then use it in an HPC context that includes external libraries such as MPI, HDF5, p4est, oder t8code.

cc @Benedict @simonbyrne

---

<div class="post-metadata">

**Author:** ![sloede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sloede/32/44787_2.png) [@sloede](https://discourse.julialang.org/u/sloede)\
**Post date:** [September 5, 2023, 2:05pm UTC](https://discourse.julialang.org/t/how-do-libraries-compiled-by-packagecompiler-jl-interact-with-preferences-jl/103479/2 "2023-09-05T14:05:46Z")

</div>

Quoting @staticfloat on Slack:

> Theoretically, if preferences are saved within the package compiler depot, they should apply, but compile-time preferences will not trigger recompilation and will have no effect, whereas runtime preference lookups will have an effect. This may not be what happens in practice, but that’s what _should_ happen (theoretically).

Thanks again for the feedback! Based on this, I tried a couple of things, but was - mostly - unsuccessful 😕

The following do **not** seem to work:

- Setting _compile-time_ preferences by storing them in the `LocalPreferences.toml` next to the `Project.toml` that was loaded when invoking PackageCompiler.jl’s `create_library`.
- Setting _compile-time_ preferences by storing them directly in the loaded `Project.toml`.
- Setting _load-time_ preferences by creating an appropriate `LocalPreferences.toml` in the library’s `LOAD_PATH`/`DEPOT_PATH` (i.e., in `PREFIX/share/julia`).
- Setting _load-time_ preferences by creating an appropriate `Project.toml` in the library’s `LOAD_PATH`/`DEPOT_PATH` (i.e., in `PREFIX/share/julia`).

This **does** seem to work:

- Setting _compile-time_ preferences by storing them in the `Project.toml` of the corresponding package that is being compiled into the library (the package has been `dev`’d into the PC.jl environment).

It seems to me that I’m either missing something about how preferences should be set to be considered by the created library, or there is something not working properly in PackageCompiler.jl. Any further suggestions?

---

<div class="post-metadata">

**Author:** ![staticfloat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/staticfloat/32/34_2.png) [@staticfloat](https://discourse.julialang.org/u/staticfloat)\
**Post date:** [September 6, 2023, 6:41pm UTC](https://discourse.julialang.org/t/how-do-libraries-compiled-by-packagecompiler-jl-interact-with-preferences-jl/103479/3 "2023-09-06T18:41:24Z")

</div>

I made a [testing package](https://github.com/staticfloat/package-compiler-preferences-test) and [opened an issue against `PackageCompiler.jl`](https://github.com/JuliaLang/PackageCompiler.jl/issues/840), as I believe there is, in fact, a missing piece of functionality in PackageCompiler.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 10, 2023, 4:10am UTC](https://discourse.julialang.org/t/how-do-libraries-compiled-by-packagecompiler-jl-interact-with-preferences-jl/103479/4 "2023-09-10T04:10:01Z")

</div>

Could you share with us example code for how you use PackageCompiler.jl and then deploy the resulting system image?

Is it similar to Elliot’s example?

---

<div class="post-metadata">

**Author:** ![sloede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sloede/32/44787_2.png) [@sloede](https://discourse.julialang.org/u/sloede)\
**Post date:** [September 12, 2023, 7:26am UTC](https://discourse.julialang.org/t/how-do-libraries-compiled-by-packagecompiler-jl-interact-with-preferences-jl/103479/5 "2023-09-12T07:26:58Z")

</div>

Well, originally I didn’t think it would be worth to create another example, especially after @staticfloat having already provided a great test repo. However, I then decided to create a proper MWE myself, and lo!, I was unable to reproduce my earlier findings. Since this basically means that most of my original complaints are gone, I am not sure if I should 🎉 or 😢.

Anyhow, here’s the MWE repo I created for testing using Preferences.jl with PackageCompiler.jl-generated libraries:

> **[GitHub - sloede/package-compiler-library-with-preferences](https://github.com/sloede/package-compiler-library-with-preferences)**
>
> Contribute to sloede/package-compiler-library-with-preferences development by creating an account on GitHub.

With this, I was able to make the following work:

- Setting _compile-time_ preferences by storing them in the `LocalPreferences.toml` located in the `package_dir` argument to `PackageCompiler.create_library`
- Setting _compile-time_ preferences by storing them in the `Project.toml` located in the `package_dir` argument to `PackageCompiler.create_library`
- Setting _run-time_ preferences by creating an appropriate `LocalPreferences.toml` in the library’s `LOAD_PATH`/`DEPOT_PATH` (i.e., in `PREFIX/share/julia`).
- Setting _run-time_ preferences by creating an appropriate `Project.toml` in the library’s `LOAD_PATH`/`DEPOT_PATH` (i.e., in `PREFIX/share/julia`).

What still does not work:

1. Setting _compile-time_ preferences in the library by setting them in the environment that _invokes_ `PackageCompiler.create_library` (such that an existing package does not have to be modified in-source but can be loaded, e.g., from the default depot)
2. Setting _run-time_ preferences in the library without having to add files to the install prefix of the library (such that it can really be done at runtime)

Re 1: I feel like this should be doable, to respect the preferences set in the PackageCompiler.jl environment. Alternatively, maybe it would be possible to add a `local_preferences` argument that accepts a user-prepared `LocalPreferences.jl` file with settings to be respected in the compiled library? I am thinking about use cases such as MPI.jl, HDF5.jl etc. where you need to switch out the JLL-provided binaries for system-local binaries.

Re 2: Right now, because of [these two lines](https://github.com/JuliaLang/PackageCompiler.jl/blob/7f0607c990003099969f449c28ad2aeffd0671f4/src/julia_init.c#L58-L59), there is no way to modify the `DEPOT_PATH` or `LOAD_PATH` from outside. Maybe it would be an option to _not_ force setting these environment variables if they have already been set by the user? Or, alternatively, to have an additional set of environment variables that, if set, are used for the depot and load path?

However, with respect to the ability to set compile-time preferences, most of my original troubles have been solved by providing a `LocalPreferences.toml` in the package dir. It is now less a matter of necessity but more of convenience.
