2

Similar to How do I find the location of the executable in C? , but in Ada.

Is there a nice package that I can use to determine the executable location in a cross-platform manner? (Windows & Linux)

Nothing has been turned up on Google, or a look through GNAT/Ada packages

Failing an existing package, how would I go about doing this?

LordAro
  • 1,269
  • 3
  • 18
  • 35

2 Answers2

2

Ada.Command_Line.Command_Name gives you the name of the executable. On Linux, this gives you the name as you called the program. Since you often just use ls and let the shell search the path for the actual executable, you will have to reimplement that functionality yourself on top of Ada.Command_Line and Ada.Directories.

Jacob Sparre Andersen
  • 6,733
  • 17
  • 22
  • Needed a `Ada.Directories.Containing_Directory` wrapping around it, but otherwise exactly what i wanted :) – LordAro Sep 21 '17 at 13:57
  • ...Except it appears to be blank on Linux. Any idea about that? – LordAro Sep 21 '17 at 14:50
  • At a guess this is because this code is running inside a (dynamic) library, rather than the executable itself. Who knows how it managed to work on Windows – LordAro Sep 21 '17 at 15:15
1

As @egilhh comments, the language defined approach is to use the Current_Directory function provided by Ada.Directories.

with Ada.Text_IO, Ada.Directories;

procedure PWD is
begin
   Ada.Text_IO.Put_Line(Ada.Directories.Current_Directory);
end PWD;

As cross-platform behavior is critical to your use case, you'll need to test on representative targets. You may also want to study the corresponding Ada.Directories implementations. Use gnatkr to determine the relevant file names. Given an environment variable named ADA_INC that points to your adainclude directory,

export ADA_INC=${ADA_HOME}/lib/gcc/…/adainclude

the following commands should let you view the relevant sources:

view $ADA_INC/$(gnatkr Ada.Directories.ads)
view $ADA_INC/$(gnatkr Ada.Directories.adb)

Note that Current_Directory imports a C function that is compiled for each supported platform and installed with the distribution. In contrast, Containing_Directory handles Windows as a special case explicitly.

Finally, you can implement any of the cited approaches by importing a suitable function or by invoking Spawn and Expect, as shown in Gem #54: Scripting Capabilities in GNAT (Part 2)

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • that doesn't work if i'm running from a different directory though, e.g. `../../bin/executable` – LordAro Sep 21 '17 at 11:59
  • (Assume I'm deep inside a library and probably can't access argv[0] (or whatever Ada's equivalent is) – LordAro Sep 21 '17 at 12:07
  • 2
    (@trashgod, no need to use GNAT-specifics here, `Ada.Directories.Current_Directory` is more portable) – egilhh Sep 21 '17 at 12:26
  • LordAro: @egilhh suggests the better approach. Sorry for the [wrong turn](https://stackoverflow.com/revisions/46343133/1). – trashgod Sep 21 '17 at 12:47
  • 1
    well, @Jacob Sparre Andersen has an even better answer... `Ada.Command_Line.Command_Name` corresponds to argv[0]... – egilhh Sep 21 '17 at 12:49
  • @egilhh: I thought that LordAro wanted the _location_ rather than the name; sorry off I'm misinterpreting. – trashgod Sep 21 '17 at 12:55
  • You are correct, but that was a simple Containing_Directory away so I've accepted the other solution. However, I did want to use the current directory as well, so this is also useful! – LordAro Sep 21 '17 at 13:35