Author Topic: GetAppPath vs. DetermineExecutablePath  (Read 25236 times)

Offline afb

  • Developer
  • Lives here!
  • *****
  • Posts: 884
GetAppPath vs. DetermineExecutablePath
« on: November 20, 2006, 09:23:51 pm »
app#GetAppPath vs. configmanager#DetermineExecutablePath
Why do both these square wheels exist ? Should I just copy/paste ?

And unfortunately wxStandardPathsBase::Get().GetDataDir() seems
to return the wrong result in wx 2.6.3, might bug-report it for 2.8.0.

Offline afb

  • Developer
  • Lives here!
  • *****
  • Posts: 884
Re: GetAppPath vs. DetermineExecutablePath
« Reply #1 on: November 21, 2006, 12:02:23 am »
If we want to go the copy-and-paste route, here is a patch for wxMac:
http://www.algonet.se/~afb/wx/codeblocks-configmanagermac.patch

We might want to look into using/upgrading BinReloc for Linux, or not ?
http://autopackage.org/docs/binreloc/ (prefix.cpp/h is an old version)

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: GetAppPath vs. DetermineExecutablePath
« Reply #2 on: November 21, 2006, 09:04:27 am »
Quote
app#GetAppPath vs. configmanager#DetermineExecutablePath
Why do both these square wheels exist ? Should I just copy/paste ?
They both exist because I did not remove the one in CodeBlocksApp hastily (not knowing where it might be used). If no other code depends on it (plugins?), we could remove/replace it, but let's not be hasty.

Formerly, the application used to set app_path at some time during startup. Before this, the value returned by ConfigManager was undefined. That was ugly, but not a big issue because we made sure we didn't use it earlier.
Now, ConfigManager makes user paths dependent on where the config file was found. That means that it does all that path initialising a lot earlier (the first time any ConfigManager function is called) and consequently needs to know app_path a lot earlier too (and perferrably not depending on the state of startup).

The Linux code in CodeBlocksApp was not very good anyway, it never really determined a path, but simply assumed ".", which may accidentially work, but often does not.
The new version tries to read /proc/self/exe (and falls back to ".") which is not perfect, but a bit better. If you know of a yet better method, please step forward. :)

Quote
And unfortunately wxStandardPathsBase::Get().GetDataDir() seems
to return the wrong result in wx 2.6.3, might bug-report it for 2.8.0.
It's exactly what was used before too, though. Only now we have if(...) return and before we had return wxPathList.FindValid(...).
Where does it return a wrong value, MacOS? Works under Windows. Have not tested under Linux myself, but nobody complained during the last 6 weeks...?  :)
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline afb

  • Developer
  • Lives here!
  • *****
  • Posts: 884
Re: GetAppPath vs. DetermineExecutablePath
« Reply #3 on: November 21, 2006, 09:25:39 am »
The Linux code in CodeBlocksApp was not very good anyway, it never really determined a path, but simply assumed ".", which may accidentially work, but often does not.

The code that I saw used APP_PREFIX, and if that wasn't available it used BinReloc instead. Unfortunately it used the application path instead of the application prefix, so it was still broken but I fixed that in the latest revision (appended a "/.." to it)

Quote
The new version tries to read /proc/self/exe (and falls back to ".") which is not perfect, but a bit better. If you know of a yet better method, please step forward. :)

BinReloc.

Quote
Quote
And unfortunately wxStandardPathsBase::Get().GetDataDir() seems
to return the wrong result in wx 2.6.3, might bug-report it for 2.8.0.
It's exactly what was used before too, though. Only now we have if(...) return and before we had return wxPathList.FindValid(...).
Where does it return a wrong value, MacOS? Works under Windows. Have not tested under Linux myself, but nobody complained during the last 6 weeks...?  :)

(yes - wrong in wxMac, sorry for the lack of context)

It returns SharedSupport, which is not absolutely wrong (although intended for templates and such non-mandatory) material but we have decided on using Resources instead partly since Mandrav wanted to use share/codeblocks on all platforms...

i.e. if you ask wxWidgets it would use CodeBlocks.app/Contents/SharedSupport for resources and CodeBlocks.app/Contents/PlugIns for plugins, but we want it to use CodeBlocks.app/Contents/Resources/share/codeblocks[/plugins]

Offline afb

  • Developer
  • Lives here!
  • *****
  • Posts: 884
Re: GetAppPath vs. DetermineExecutablePath
« Reply #4 on: November 21, 2006, 09:28:20 am »
Quote
Why do both these square wheels exist ? Should I just copy/paste ?
They both exist because I did not remove the one in CodeBlocksApp hastily (not knowing where it might be used). If no other code depends on it (plugins?), we could remove/replace it, but let's not be hasty.

I just wanted to refactor them into one single nice solution instead...

The canonical application path retriever would use:
  • GetModuleFileName for Windows
  • br_find_exe for Linux (BinReloc)
  • _NSGetExecutablePath for Mac OS X
« Last Edit: November 21, 2006, 09:38:15 am by afb »

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: GetAppPath vs. DetermineExecutablePath
« Reply #5 on: November 21, 2006, 09:49:46 am »
The code that I saw...
Ah right... I must have confused the Mac (which really just sets ".") and Linux #ifdef paths there. Probably just saw the "." and turned away, saying "ugh...". :)

BinReloc.
(looks into code) Uses readlink instead of realpath (which is really the same, in the end), but for some reason, it's 13 kB of code? The documentation says that it also hardcodes paths on other platforms. If that's what we want...

Feel free to make one single, better solution out of it. No objections :)
Regarding SharedSupport versus Resources, does it matter so much for the moment? If it is an actual (and known) bug, chances are that it will fixed without us bothering about it at all, no? Or would you want to add a temporary hack which will be removed for 2.8.0?
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline afb

  • Developer
  • Lives here!
  • *****
  • Posts: 884
Re: GetAppPath vs. DetermineExecutablePath
« Reply #6 on: November 21, 2006, 10:19:44 am »
Regarding SharedSupport versus Resources, does it matter so much for the moment? If it is an actual (and known) bug, chances are that it will fixed without us bothering about it at all, no? Or would you want to add a temporary hack which will be removed for 2.8.0?

It's not a real bug I guess, just a matter of opinion. (so I don't think they'll change)
I reimplemented it myself instead, so that it wouldn't be dependent on wx version ?

It was just that I was misleading the Mac users on where to place the resources.
i.e. just as I had finally decided on one location it transmogrified into another ;-)


For me it is perfectly fine to move to GetDataDir on all the C::B platforms.
It was just that the Don said that he wanted the extra "share/", so I complied...

Offline afb

  • Developer
  • Lives here!
  • *****
  • Posts: 884
Re: GetAppPath vs. DetermineExecutablePath
« Reply #7 on: November 21, 2006, 10:27:31 am »
BinReloc.
(looks into code) Uses readlink instead of realpath (which is really the same, in the end), but for some reason, it's 13 kB of code? The documentation says that it also hardcodes paths on other platforms. If that's what we want...

It's probably the same in the end, I just thought that someone else could maintain it :-)

Offline afb

  • Developer
  • Lives here!
  • *****
  • Posts: 884
Re: GetAppPath vs. DetermineExecutablePath
« Reply #8 on: November 21, 2006, 10:41:14 am »
Apparently the /proc hack might work (with variations) in BSD too, if one is lucky enough to have that filesystem mounted (seems to not be by default). Solaris has a getexecname so that should be easy. What other platforms do we have ? (I'm not sure that anything beyond Windows / Linux / Mac OS X will be supported, but...)

Why Can't We All Just Get Along ? :|
« Last Edit: November 21, 2006, 10:50:43 am by afb »

Offline afb

  • Developer
  • Lives here!
  • *****
  • Posts: 884
Re: GetAppPath vs. DetermineExecutablePath
« Reply #9 on: November 21, 2006, 10:55:29 am »
Feel free to make one single, better solution out of it. No objections :)

I think that I will do just that ... BinReloc is Public Domain, so I'll rip out any interesting bits.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: GetAppPath vs. DetermineExecutablePath
« Reply #10 on: November 21, 2006, 01:32:22 pm »
Well, the most interesting bit (I think) is the 5 lines of code around readlink("/proc/self/exe") or realname, respectively. :)

If other Unix-derivates support /proc/self too, that's even better. I used the strict __linux__ instead of something more relaxed because I thougth it was a proprietary Linux feature. But actually there is no harm if we always use it, as realname will return 0 if the given location does not exist, so it is perfectly safe to just try and see if we get back a valid pointer.

Another possibility to get the install dir would be popen("which codeblocks"), but that's a lot slower than reading /proc/self, and unreliable, too (so we should probably abstain from it).

EDIT:
Btw, you said "proc hack". The proc hack is really not a hack at all, it is the one perfectly legal way of doing it. That's what proc is for, after all :)
« Last Edit: November 21, 2006, 01:37:09 pm by thomas »
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline afb

  • Developer
  • Lives here!
  • *****
  • Posts: 884
Re: GetAppPath vs. DetermineExecutablePath
« Reply #11 on: November 21, 2006, 02:27:35 pm »
Well, the most interesting bit (I think) is the 5 lines of code around readlink("/proc/self/exe") or realname, respectively. :)
Yup. BinReloc says that doesn't work for libraries, but we don't care I guess.

Quote
If other Unix-derivates support /proc/self too, that's even better. I used the strict __linux__ instead of something more relaxed because I thougth it was a proprietary Linux feature.
Apparently /proc is not mounted on FreeBSD, so it's not really useful there.

Quote
EDIT:
Btw, you said "proc hack". The proc hack is really not a hack at all, it is the one perfectly legal way of doing it. That's what proc is for, after all :)

Fair enough. I just think that fiddling around with /proc/self/exe is ugly :-)
(not the solution itself, but the need to use it in application level code...)

It's not like we really need the application path for anything as it is,
except messing around with finding the datadir and plugins relative to it...

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4315
    • Code::Blocks IDE
Re: GetAppPath vs. DetermineExecutablePath
« Reply #12 on: November 21, 2006, 03:17:07 pm »
Quote from: afb
It's not like we really need the application path for anything as it is,
except messing around with finding the datadir and plugins relative to it...

Does this seem to be a minor/non-important task?
Be patient!
This bug will be fixed soon...

Offline afb

  • Developer
  • Lives here!
  • *****
  • Posts: 884
Re: GetAppPath vs. DetermineExecutablePath
« Reply #13 on: November 21, 2006, 03:21:10 pm »
Quote from: afb
It's not like we really need the application path for anything as it is,
except messing around with finding the datadir and plugins relative to it...

Does this seem to be a minor/non-important task?

What I meant was that if we don't need it for that task, we don't need it at all ?
So it doesn't matter much if it fails on FreeBSD, since it finds the files by prefix ?

Do you think we should lose using the BinReloc library, as Thomas suggests above ?
(his implementation is a fair bit shorter than what prefix.cpp and prefix.h are...)
« Last Edit: November 21, 2006, 03:22:46 pm by afb »

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4315
    • Code::Blocks IDE
Re: GetAppPath vs. DetermineExecutablePath
« Reply #14 on: November 21, 2006, 03:37:48 pm »
Quote
Do you think we should lose using the BinReloc library, as Thomas suggests above ?
(his implementation is a fair bit shorter than what prefix.cpp and prefix.h are...)

binreloc is working under many unix implementations (including freebsd) while Thomas' solution only works for linux if I understood correctly?
Be patient!
This bug will be fixed soon...