Package Specific Environment Variables in JLL

I would like to package OpenFOAM from 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 using this 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.

    Could not find mandatory etc entry (mode=ugo)

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:

import Pkg

using OpenFOAM_com_jll

## 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
# 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

However, If I set the WM_PROJECT_DIR environment variable correctly pointing to the openfoam folder within the package, and add it to the Cmds 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

Use the init_block keyword argument of build_tarballs(), see for example

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?

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.

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