Formula needs to setup path to a keg-only dependency

I’m creating a custom tap and formula, but I’ve got a situation I don’t know how to handle.

At runtime, the software installed by this formula needs to know the complete path to an executable file that belongs to a dependency which was also installed by Homebrew.

So, for example, the path to the executable it needs might be $(brew --prefix)/opt/my_dependency/bin/some_exe.

There are two ways provided to supply this path to the software:

  • The path can be build into the executable during compilation. I certainly know how to do this with a patch… but only if the path is a fixed string I know before installation. It’s not fixed, though, because it involves $(brew --prefix), which might differ from user to user.
  • It can be written to a special configuration file in the users’ home directory that the software will look for. However, I don’t know if it’s possible (due to sandboxing) to write a file to the users’ home directory.

(If interested in specifics, I’m building GV, which needs to know the path to a keg-only version of ghostscript, also a custom tap, that was built with X11 support). Yes, it actually compiles this path into the gv executable by default.

Thanks for any advice on how to do this!

There is a plethora of Homebrew directory references you can use in your formula. It’s almost certain one or more of these are appropriate to your use case: https://docs.brew.sh/Formula-Cookbook#variables-for-directory-locations

When the formula uses the directory references I mentioned above, any successful build on a system would contain the correct paths for that system, so I’m not sure how “differ from user to user” would be a concern.

Thanks for the response, @gromgit. I am aware of those directory references. I guess my question is more specific. Let’s say I need to modify file src/location.c at formula build time, and place a path in there that includes the user’s Homebrew path. I know how to use a patch (with patch :DATA) to replace string old_location with new_location in src/location.c, as long as new_location is fixed. Since new_location is a function of the user’s Homebrew installation path, how do I create a patch that dynamically assigns new_location at build time?

Thanks for any help, all… much appreciated.

You want the inreplace command then: https://docs.brew.sh/Formula-Cookbook#inreplace

1 Like

Thank you @gromgit! Problem solved.