Well, here goes.
When "lazarus" or "startlazarus" is started, it takes the following steps.
1)
Find the directory in which "lazarus.exe" /"startlazarus" is.
There is no 100% way for your app to know this.
On Windows, you could try the "inno setup" registry entries (see tools/install/win/lazarus.iss), but that only works for your "primary install" (if you have more than one..., if you did not use inno ...)
On other OS => depends if a package manager was used, and if that stores info that you can find.
2)
In this directory it looks for "lazarus.cfg"
This is an optional file (usually only for 2ndary installs). This file may contain a line
--ppc=
or
--primary-config-path=
If the file exists, and has such a line then the given path is the "pcp"
Otherwise it is
On Win: %AppData%\Local\lazarus %AppData% is a system path, usually C:\Users\USERNAME\
On Linux: ~/.lazarus
3)
In the PCP is the file environmentoptions.xml
It's xml, it can be parsed. If you look at it, you will find where the compiler path is.
The compiler path may however contain ide-macros, such as $(LazarusDir) => which need to be replaced.
I don't have a full list of them, or how they are computed.
The code dealing with that is in the IDE. (not sure which file, start searching in ide/environmentoptions.pas)
This code is part of the "ide" project. It is not part of a package. There is no good way to use it in your own code. As long as you keep to GPL, you can just copy the relevant units... You can modify "search path" options... However, I have no idea how much code you need to pull in, if you want to try and use that code (once found).
Consider passing the path to FPC as a command line option to your app. Menu RUN > RUN PARAMS