Recent

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

440bx

  • Hero Member
  • *****
  • Posts: 3152
Re: Absolute file path in {$include %file%}
« Reply #15 on: October 03, 2022, 09:57:27 pm »
No, there is no way.
I could be wrong but, if the program is compiled with debugging symbols, doesn't the full filename end up somewhere in there ?
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 SP1 64bit.

GetMem

  • Hero Member
  • *****
  • Posts: 3815
Re: Absolute file path in {$include %file%}
« Reply #16 on: October 03, 2022, 10:31:53 pm »
@simone

It is possible if you extend the IDE, but the solution is somewhat convoluted and I'm not sure is worth the effort. The idea is to create a package which will tap into the project openings:
Code: Pascal  [Select][+][-]
  1. function OnProjectOpened(Sender: TObject; AProject: TLazProject): TModalResult;
  2. //...
  3. LazarusIDE.AddHandlerOnProjectOpened(@OnProjectOpened);
Each time a project is opened in the IDE, the package will by notified via the OnProjectOpened event. Now all you have to do, is to loop through the project files, find a specific unit, then update a variable, which represents the path to the source file(see below AProject.Files.Filename):
Code: Pascal  [Select][+][-]
  1.   for I := 0 to AProject.FileCount - 1 do
  2.     if ExtractFileName(LowerCase(AProject.Files[I].Filename)) = 'myUnit.pas' then
  3.       //update variable here

The variable will be compiled into your executable. 

BobDog

  • Sr. Member
  • ****
  • Posts: 364
Re: Absolute file path in {$include %file%}
« Reply #17 on: October 03, 2022, 11:25:43 pm »

You could do something like
Code: Pascal  [Select][+][-]
  1.  
  2. program project1;
  3.  
  4. {$macro ON}
  5. {$define mypath:= extractfilepath(paramstr(0))+{$include %file%}}
  6. uses
  7. sysutils;
  8.  
  9. begin
  10.   writeln(mypath);
  11.   readln;
  12. end.
maybe.

GetMem

  • Hero Member
  • *****
  • Posts: 3815
Re: Absolute file path in {$include %file%}
« Reply #18 on: October 04, 2022, 04:28:21 am »

You could do something like
Code: Pascal  [Select][+][-]
  1.  
  2. program project1;
  3.  
  4. {$macro ON}
  5. {$define mypath:= extractfilepath(paramstr(0))+{$include %file%}}
  6. uses
  7. sysutils;
  8.  
  9. begin
  10.   writeln(mypath);
  11.   readln;
  12. end.
maybe.

Let's say your project is located in c:\MyProjects\Project1\. Build the application,  copy the executable to d:\test\, the run it. ExtractFilePath(Paramstr(0)) will point to d:\test\, OP needs c:\MyProjects\Project1\.

PascalDragon

  • Hero Member
  • *****
  • Posts: 4744
  • Compiler Developer
Re: Absolute file path in {$include %file%}
« Reply #19 on: October 04, 2022, 01:34:27 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).

There is no built-in way (except maybe using debug information as 440bx hinted at, but that means parsing that whole stuff and including it in the first place which might not be desired).

MarkMLl

  • Hero Member
  • *****
  • Posts: 5563
Re: Absolute file path in {$include %file%}
« Reply #20 on: October 04, 2022, 02:48:52 pm »
There is no built-in way (except maybe using debug information as 440bx hinted at, but that means parsing that whole stuff and including it in the first place which might not be desired).

Yes, agreed. That was why I was very careful to qualify what I said in terms of development environment and OS.

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

BobDog

  • Sr. Member
  • ****
  • Posts: 364
Re: Absolute file path in {$include %file%}
« Reply #21 on: October 06, 2022, 12:17:56 am »

What about a resource file with the file's (birthplace) written in, for member GetMem, who reckons this is what is required.
bob.rc
Code: Pascal  [Select][+][-]
  1. 1 VERSIONINFO
  2. FILEVERSION     1,0,0,0
  3. PRODUCTVERSION  1,0,0,0
  4. BEGIN
  5.   BLOCK "StringFileInfo"
  6.   BEGIN
  7.     BLOCK "080904E4"
  8.     BEGIN
  9.       VALUE "CompanyName", "My own company"
  10.       VALUE "FileDescription", "Born in C:\\Users\\computer\\Desktop\\bob"
  11.       VALUE "FileVersion", "1.0"
  12.       VALUE "InternalName", "Some file name"
  13.       VALUE "LegalCopyright", "Some copyright"
  14.       VALUE "OriginalFilename", "tester.exe"
  15.       VALUE "ProductName", "Some product name"
  16.       VALUE "ProductVersion", "1.0"
  17.     END
  18.   END
  19.  
  20.   BLOCK "VarFileInfo"
  21.   BEGIN
  22.     VALUE "Translation", 0x809, 1252
  23.   END
  24. END
  25.  
And the file using it
Code: Pascal  [Select][+][-]
  1. program mydata;
  2.  
  3. {$R bob.rc}
  4. begin
  5. writeln ('Please check file properties to view birthplace');
  6. readln;
  7. end.
  8.  
Works OK here.
Right click the exe,properties/details.

MarkMLl

  • Hero Member
  • *****
  • Posts: 5563
Re: Absolute file path in {$include %file%}
« Reply #22 on: October 06, 2022, 09:18:38 am »
What about a resource file with the file's (birthplace) written in, for member GetMem, who reckons this is what is required.

Better if the location could be generated automatically by whatever VCS OP's using (svn, git or whatever).

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 #23 on: October 06, 2022, 04:19:19 pm »
Win x86_64 {$I %sourcefile%}, compiler patch to add %sourcefile%.
When run with patch applied, the zipped project with 3 units, will output  :
Quote
ExeName=C:\FPC_SourceName\SourceName.exe
$main SourceFile=C:\FPC_SourceName\SourceName.pas
DoProjUnit C:\FPC_SourceName\SubDir\projunit.pas
DoProjUnit C:\FPC_SourceName\SubDir\SubSubDir\projuniti.INC
The code to generate the above result is :
Code: Pascal  [Select][+][-]
  1.   WriteLn({$I %CURRENTROUTINE%}, ' SourceFile=', {$I %sourcefile%});

To work, you need to apply the SourceFIle.patch to FPC 3.2.2 and rebuild the  FPC compiler.

MarkMLl

  • Hero Member
  • *****
  • Posts: 5563
Re: Absolute file path in {$include %file%}
« Reply #24 on: October 06, 2022, 04:33:52 pm »
Win x86_64 {$I %sourcefile%}, compiler patch to add %sourcefile%.
When run with patch applied, the zipped project with 3 units, will output  :
Quote
ExeName=C:\FPC_SourceName\SourceName.exe
$main SourceFile=C:\FPC_SourceName\SourceName.pas
DoProjUnit C:\FPC_SourceName\SubDir\projunit.pas
DoProjUnit C:\FPC_SourceName\SubDir\SubSubDir\projuniti.INC
The code to generate the above result is :
Code: Pascal  [Select][+][-]
  1.   WriteLn({$I %CURRENTROUTINE%}, ' SourceFile=', {$I %sourcefile%});

To work, you need to apply the SourceFIle.patch to FPC 3.2.2 and rebuild the  FPC compiler.

Thanks for that, I've been thinking through a different issue ** during the day and have concluded that it's well worth having.

** security-related, which I might raise separately

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 #25 on: October 06, 2022, 04:45:55 pm »
... worth having.
MarkMLl
I think that recompiling application mentionned in
Quote
Re: ??? How is this possible ???
with the {$I %sourcefile%} in the units in correct places might show exactly what units are included and whether it is what it is expected.

I have been doing a bit of patching to JCF these days and know how it becomes difficult to not mix-up various iterations of same named sources simultaneously opened.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1131
Re: Absolute file path in {$include %file%}
« Reply #26 on: October 14, 2022, 10:41:14 am »
It might be important to explain what the path is used for. If it is to find resources during runtime, for example, ParamStr(0) would work. And if it is to include files from different locations, the paths property from the project options would work. Or one of the path macros, like $(ProjOutDir). Etc.

PascalDragon

  • Hero Member
  • *****
  • Posts: 4744
  • Compiler Developer
Re: Absolute file path in {$include %file%}
« Reply #27 on: October 14, 2022, 03:31:27 pm »
Or one of the path macros, like $(ProjOutDir). Etc.

Those don't work inside the code (unless you somehow forward them to the code using either defines or environment variables)

 

TinyPortal © 2005-2018