Recent

Author Topic: Absolute file path in {$include %file%}  (Read 1531 times)

simone

  • Sr. Member
  • ****
  • Posts: 482
Absolute file path in {$include %file%}
« on: October 03, 2022, 09:40:49 am »
As know, {$include %file%} is expanded by compiler with the name of current source file. As far as I understand, the filename is without the path. Is there any way to get the full path name? Thank you.
Microsoft Windows 10 64 bit - Lazarus 2.2.4

MarkMLl

  • Hero Member
  • *****
  • Posts: 5567
Re: Absolute file path in {$include %file%}
« Reply #1 on: October 03, 2022, 10:41:51 am »
Since your question is OS- and version-specific, I'd suggest putting that info in the body of your message, not in your sig (and in fact, editing your message to include those before anything else). The problem is that at some point your sig will change, and that will appear immediately in all historical messages thus destroying the context of the question for anybody who arrives here via a forum or Google search.

I don't believe there is a way of expanding %FILE% directly to a full path. There's various things you can do in the Lazarus IDE at the build settings level to get more info, but at the compiler level you're going to be restricted to whatever shell/environment variables your OS makes available (and please note my para above).

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

simone

  • Sr. Member
  • ****
  • Posts: 482
Re: Absolute file path in {$include %file%}
« Reply #2 on: October 03, 2022, 10:50:37 am »
My question was OS independent. However, thanks for the reply.
Microsoft Windows 10 64 bit - Lazarus 2.2.4

MarkMLl

  • Hero Member
  • *****
  • Posts: 5567
Re: Absolute file path in {$include %file%}
« Reply #3 on: October 03, 2022, 11:04:05 am »
My question was OS independent. However, thanks for the reply.

Your question is NOT OS-independent since it depends on the shell/environment variables made available, and is NOT version-independent since the $I expansions might change.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

simone

  • Sr. Member
  • ****
  • Posts: 482
Re: Absolute file path in {$include %file%}
« Reply #4 on: October 03, 2022, 11:14:10 am »
My question is os independent. The solution is os dependent.
Microsoft Windows 10 64 bit - Lazarus 2.2.4

KodeZwerg

  • Hero Member
  • *****
  • Posts: 525
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Absolute file path in {$include %file%}
« Reply #5 on: October 03, 2022, 12:03:50 pm »
Is there any way to get the full path name?
How is that meant?
You want to find out at runtime where the source of {$I} was?
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

simone

  • Sr. Member
  • ****
  • Posts: 482
Re: Absolute file path in {$include %file%}
« Reply #6 on: October 03, 2022, 12:19:38 pm »
The following code in the filename "C:\Users\Desktop\test\project1.lpr"

Code: Pascal  [Select][+][-]
  1. program project1;
  2. begin
  3.   writeln({$include %file%});
  4.   readln;
  5. end.

shows project.lpr, while while I would need to have: C:\Users\Desktop\test\project1.lpr
Microsoft Windows 10 64 bit - Lazarus 2.2.4

wp

  • Hero Member
  • *****
  • Posts: 10284
Re: Absolute file path in {$include %file%}
« Reply #7 on: October 03, 2022, 12:31:59 pm »
I think that the IDE changes path to the directory with the project file. So, you could try this:
Code: Pascal  [Select][+][-]
  1. program project2;
  2. uses
  3.   SysUtils;
  4. begin
  5.   writeln(IncludeTrailingPathDelimiter(GetCurrentDir) + {$include %file%});
  6.   readln;
  7. end.
Don't beat me if this is wrong... For example, I could imagine that when a project is structured in subfolders you might not get the subfolder of the file...

MarkMLl

  • Hero Member
  • *****
  • Posts: 5567
Re: Absolute file path in {$include %file%}
« Reply #8 on: October 03, 2022, 12:36:27 pm »
Don't beat me if this is wrong... For example, I could imagine that when a project is structured in subfolders you might not get the subfolder of the file...

Except that that's an unholy mix of compile- and runtime behaviour :-)

On unix you could possibly use %PWD%

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

simone

  • Sr. Member
  • ****
  • Posts: 482
Re: Absolute file path in {$include %file%}
« Reply #9 on: October 03, 2022, 12:39:38 pm »
I'm trying a similar approach:

Code: Pascal  [Select][+][-]
  1. program project2;
  2. uses
  3.   SysUtils;
  4. begin
  5.   writeln(ExpandFileName({$include %file%}));
  6.   readln;
  7. end.


but i'm not sure if it is correct in all possible cases, for the reasons explained by MarkMLI in the last post.
Microsoft Windows 10 64 bit - Lazarus 2.2.4

MarkMLl

  • Hero Member
  • *****
  • Posts: 5567
Re: Absolute file path in {$include %file%}
« Reply #10 on: October 03, 2022, 12:58:48 pm »
It's a good start /if/ a runtime solution is acceptable. One thing I've found useful- in the context of a larger project- is to save the project name like that as a global in the main unit (i.e. .lpr or main .pas/.pp file) so that a different unit can read it back and decide on the appropriate .ini name... which can actually be a fairly complex decision if the installer has changed it.

If you need it at build time I'm pretty confident you could get it from (Lazarus) predefined values at the build level. However I'm not putting time into this.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BrunoK

  • Sr. Member
  • ****
  • Posts: 364
  • Retired programmer
Re: Absolute file path in {$include %file%}
« Reply #11 on: October 03, 2022, 01:52:38 pm »
It could be useful to store the source filepath and filename to the executable for debugging.
It happens very frequently to me to have multiple copies of files/units with the same name but in different directories and in the end not knowing which one was selected  for the compilation.

PascalDragon

  • Hero Member
  • *****
  • Posts: 4749
  • Compiler Developer
Re: Absolute file path in {$include %file%}
« Reply #12 on: October 03, 2022, 02:18:02 pm »
As know, {$include %file%} is expanded by compiler with the name of current source file. As far as I understand, the filename is without the path. Is there any way to get the full path name? Thank you.

No, there is no way.

My question was OS independent. However, thanks for the reply.

Your question is NOT OS-independent since it depends on the shell/environment variables made available, and is NOT version-independent since the $I expansions might change.

The question is OS-independant, because %FILE% is handled by the compiler itself and returns the filename of the currently file being processed and is not fetched from the environment of the OS.

I think that the IDE changes path to the directory with the project file. So, you could try this:
Code: Pascal  [Select][+][-]
  1. program project2;
  2. uses
  3.   SysUtils;
  4. begin
  5.   writeln(IncludeTrailingPathDelimiter(GetCurrentDir) + {$include %file%});
  6.   readln;
  7. end.
Don't beat me if this is wrong... For example, I could imagine that when a project is structured in subfolders you might not get the subfolder of the file...

That suggestion is, to put it simple, absolute nonsense (sorry). What simone tries to retrieve is the path of the currently compiled file, but your code would prepend to that the path at runtime where the source might not even remotely be available.

Kays

  • Sr. Member
  • ****
  • Posts: 465
  • Whasup!?
    • KaiBurghardt.de
Re: Absolute file path in {$include %file%}
« Reply #13 on: October 03, 2022, 02:20:25 pm »
[…] Is there any way to get the full path name? […]
No, not without external help. With bash(1) or zsh(1) you could do something like
Code: Pascal  [Select][+][-]
  1. #!/usr/bin/ifpc
  2. program helloWorld(output);
  3. begin
  4.         writeLn('Hello from ', {$include %_%}, '!');
  5. end.
Code: Bash  [Select][+][-]
  1. chmod a+x helloWorld.pas
  2. ./helloWorld.pas
Code: Text  [Select][+][-]
  1. Hello from /tmp/./helloWorld.pas!
The said shells pass an environment variable _ (a single underscore) containing the full path name of the executable file.
« Last Edit: October 03, 2022, 02:34:25 pm by Kays »
Yours Sincerely
Kai Burghardt

MarkMLl

  • Hero Member
  • *****
  • Posts: 5567
Re: Absolute file path in {$include %file%}
« Reply #14 on: October 03, 2022, 09:06:54 pm »
No, there is no way.

With respect, I express qualified disagreement: I strongly suspect I could do it on unix provided that the build was handled by Lazarus (a carefully-tailored echo|tr in "Execute before" creating a string in a .inc file).

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018