Calling `ghc --print-lib-dir` is the only way because the `$libdir` is actually provided on the cmdline using the -B flag:
https://gitlab.haskell.org/ghc/ghc/-/blob/master/ghc/Main.hs#L110-112

That's why the `ghc` you normally execute is a shell-wrapper around the `ghc` executable applied to a `-B` argument set at installation time.
But a user is free to move that `$libdir` dir to any other place after installation and update the `-B` flag accordingly.

On Mon, 8 Feb 2021 at 20:09, Fendor <power.walross@gmail.com> wrote:
Hi,

Well the purpose is a bit convoluted, but here it goes:

Basically, we want to know which `ghc` version `cabal-install` is going
to pick for compiling the project in a platform independent way without
resorting to linking to `Cabal`.

You can execute `cabal exec -- ghc` which launches the correct `ghc`
version, but as far as we found out, there is no programmatic way of
finding the path to the `ghc` executable itself (`cabal exec -- where
ghc` or `cabal exec -- which ghc` fails on windows). What we really want
is `cabal-install` to tell us, e.g. with `cabal show-build-info` which
is a command not implemented yet[0].

As long as this command is not implemented, we have to monkey patch it
in order to satisfy our goals. You can execute `cabal exec -- ghc
--print-libdir`, which gives you the `$libdir`, which brings us back to
the original question: can we find the `ghc` executable given `$libdir`
in a platform independent way?

[0] https://github.com/haskell/cabal/pull/6241

Best regards,
Fendor

On 2/8/21 7:48 PM, Daniel Gröber wrote:
> Hi Fendor,
>
> On Mon, Feb 08, 2021 at 07:35:12PM +0100, Fendor wrote:
>> I want to find out the location of the `ghc` (and `ghc-pkg`) executable
>> given the `$libdir` (obtained by `ghc --print-libdir`).
>> Is there a platform independent way of doing that, which we can rely on?
> Are you aware of the `ghc --print-libdir` option? I think you'd be much
> better off going from ghc binary to libdir instead of the other way around.
>
> Could you explain why you're trying to do it your way?
>
>> (P. S. first time using a mailing list, hope I am doing it correctly)
> Seems fine to me :)
>
> --Daniel
_______________________________________________
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs