# Package Specific Environment Variables in JLL

**URL:** https://discourse.julialang.org/t/package-specific-environment-variables-in-jll/111116
**Category:** Package Management
**Tags:** binarybuilder
**Created:** [March 4, 2024, 8:02am UTC](https://discourse.julialang.org/t/package-specific-environment-variables-in-jll/111116 "2024-03-04T08:02:11Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![thealanjason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thealanjason/32/213969_2.png) [@thealanjason](https://discourse.julialang.org/u/thealanjason)
#### Post date: [March 4, 2024, 8:02am UTC](https://discourse.julialang.org/t/package-specific-environment-variables-in-jll/111116/1 "2024-03-04T08:02:11Z")

</div>

I would like to package OpenFOAM from [www.openfoam.com](http://www.openfoam.com) with BinaryBuilder and use it for my research.  
Towards this goal, I have been able to build a JLL using BinaryBuilder for the Linux x86\_64 platform [see here](https://github.com/JuliaBinaryWrappers/OpenFOAM_com_jll.jl) using [this](https://github.com/JuliaPackaging/Yggdrasil/blob/5ad904ce19353e6f066630ec57049812e26a3d5e/O/OpenFOAM_com/build_tarballs.jl) build script.  
Before I spend some time in building the package for other platforms, I wanted to check if it works as expected.

So I went ahead and tried to solve a small test case. In this, I have no problems importing the package. However, when I try to use executables from within the package I get an error.

```julia
run(gridToSTL())
--> FOAM FATAL ERROR :
    Could not find mandatory etc entry (mode=ugo)
    'controlDict'

```

I understand that this is specific to OpenFOAM. On reading a bit further, I found out that the executables and their linked libraries search for the `ControlDict` file in the `openfoam/etc/` folder, which is packaged along with the JLL as a file product. However, the binary products specifically look for the `WM_PROJECT_DIR` environment variable that points to the directory `openfoam` directory in the package, which is not set while bulding the package. See below:

```julia
import Pkg
Pkg.activate(".")
Pkg.instantiate()

using OpenFOAM_com_jll

cd("synthetic-slope-elliptic-hemisphere/")
## This following calls fail because the libraries used by the executables
## cannot find the WM_PROJECT_DIR path in the environment of the command
# run(gridToSTL())
# run(pMesh())
# run(makeFaMesh())
# # restore0dir
# rm("0", recursive=true)
# cp("0.orig", "0")
# run(releaseAreaMapping())
# run(faSavageHutterFoam())

## When we add the WM_PROJECT_DIR with the correct path to the Cmd env
## the calls work as expected

# This is the path the etc folder is searched for by OpenFOAM libraries
dirname(OpenFOAM_com_jll.openfoam_etc)
# So we set this path to the WM_PROJECT_DIR variable in the Julia script 
# and add it to the environment for each call
WM_PROJECT_DIR = dirname(OpenFOAM_com_jll.openfoam_etc) 
run(addenv(gridToSTL(), "WM_PROJECT_DIR"=>WM_PROJECT_DIR));
run(addenv(pMesh(), "WM_PROJECT_DIR"=>WM_PROJECT_DIR));
run(addenv(makeFaMesh(), "WM_PROJECT_DIR"=>WM_PROJECT_DIR));
# restore0dir
rm("0", recursive=true)
cp("0.orig", "0")
run(addenv(releaseAreaMapping(), "WM_PROJECT_DIR"=>WM_PROJECT_DIR));
run(addenv(faSavageHutterFoam(), "WM_PROJECT_DIR"=>WM_PROJECT_DIR));

```

Github repo for above code with necessary files is here: [GitHub - thealanjason/ofjulia: A test repository to use OpenFOAM\_com\_jll package](https://github.com/thealanjason/ofjulia)

However, If I set the `WM_PROJECT_DIR` environment variable correctly pointing to the `openfoam` folder within the package, and add it to the `Cmd`s environment using `addenv()` then the code works as expected.

Concrete Question: Is there a way to set such environment variables in the `build_tarballs.jl` so that it does not need to be explicitly set when using the JLL package.

P.S. This was also the reason that the build libraries were failing to open in the BinaryBuilder audit phase. Hence, I went with using `dont_dlopen=true` for the `LibraryProducts`

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [March 4, 2024, 9:12am UTC](https://discourse.julialang.org/t/package-specific-environment-variables-in-jll/111116/2 "2024-03-04T09:12:40Z")

</div>

Use the `init_block` keyword argument of [`build_tarballs()`](https://docs.binarybuilder.org/stable/reference/#BinaryBuilder.build_tarballs), see for example

> <https://github.com/JuliaPackaging/Yggdrasil/blob/f39d0a9a6463b407b762d6573b84e4652c80abd9/F/Fontconfig/build_tarballs.jl#L83-L86>

> <https://github.com/JuliaPackaging/Yggdrasil/blob/f39d0a9a6463b407b762d6573b84e4652c80abd9/E/elmerfem/build_tarballs.jl#L136>

---

<div class="post-metadata">

### Author: ![thealanjason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thealanjason/32/213969_2.png) [@thealanjason](https://discourse.julialang.org/u/thealanjason)
#### Post date: [March 4, 2024, 11:58am UTC](https://discourse.julialang.org/t/package-specific-environment-variables-in-jll/111116/3 "2024-03-04T11:58:18Z")

</div>

Thanks! I will try this out.

A follow up question: Do these environment variables added as `ENV["VARIABLE_NAME"] = "VARIABLE_VALUE"` to the `init_block` then get added to the global ENV dictionary in the Julia session or only to the `setenv` calls when running the executables?

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [March 4, 2024, 12:07pm UTC](https://discourse.julialang.org/t/package-specific-environment-variables-in-jll/111116/4 "2024-03-04T12:07:43Z")

</div>

That code is literally doing what it says: adding an environment variable to the global `ENV` object. If you don’t want to do that (sensible idea, affecting `ENV` isn’t great) then your consumer package has to deal with it manually.

---

<div class="post-metadata">

### Author: ![thealanjason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thealanjason/32/213969_2.png) [@thealanjason](https://discourse.julialang.org/u/thealanjason)
#### Post date: [March 4, 2024, 12:41pm UTC](https://discourse.julialang.org/t/package-specific-environment-variables-in-jll/111116/5 "2024-03-04T12:41:00Z")

</div>

Thanks, I thought the same, but just wasn’t sure.
