Recent

Author Topic: [Solved] How can I read specific Ide settings ?  (Read 4727 times)

loaded

  • Hero Member
  • *****
  • Posts: 878
[Solved] How can I read specific Ide settings ?
« on: February 07, 2022, 05:43:10 pm »
Hi All,
Is it possible to access the ide settings from the exe compiled by the IDE?
I need the directory path where FPC is currently installed.
I would be glad if you give your ideas and suggestions. Respects.
« Last Edit: February 08, 2022, 06:09:07 am by loaded »
The more memory computers have, the less memory people seem to use. 😅

ASBzone

  • Hero Member
  • *****
  • Posts: 733
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: How can I read specific Ide settings ?
« Reply #1 on: February 07, 2022, 06:15:59 pm »
What OS are you trying to do this from?


In Windows, if FPC is in your path, you can determine where the executable is running from using the WHERE command or looking at the process list.

Can you elaborate on the use case you are trying to work with?


-ASB
-ASB: https://www.BrainWaveCC.com/

Lazarus v4.3.0.0 (bcf314a670) / FreePascal v3.2.3-46-g77716a79dc (aka fixes)
(Windows 64-bit install w/Win32 and Linux on ARM and x64 cross-compilers via FpcUpDeluxe)

My Systems: Windows 10/11 Pro x64 (Current)

loaded

  • Hero Member
  • *****
  • Posts: 878
Re: How can I read specific Ide settings ?
« Reply #2 on: February 07, 2022, 06:28:59 pm »
What OS are you trying to do this from?
Windows OS

In Windows, if FPC is in your path, you can determine where the executable is running from using the WHERE command or looking at the process list.

Yes, I can do this manually. But the need for something like this arose, both to be a bit technical and to be familiar with the IDE.

Can you elaborate on the use case you are trying to work with?

https://forum.lazarus.freepascal.org/index.php/topic,58173.msg433271.html#msg433271  As I mentioned in the link, I'm thinking of doing a study that will add units automatically. I hope this work will improve me a lot. In fact, I've already made a few clicks of progress.  :)
The more memory computers have, the less memory people seem to use. 😅

ASBzone

  • Hero Member
  • *****
  • Posts: 733
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: How can I read specific Ide settings ?
« Reply #3 on: February 07, 2022, 06:41:21 pm »
Thanks for the link, @loaded.   That was helpful for me to understand what you are trying to accomplish.


Unfortunately, I also don't know how to help you much, either.  ;)
-ASB: https://www.BrainWaveCC.com/

Lazarus v4.3.0.0 (bcf314a670) / FreePascal v3.2.3-46-g77716a79dc (aka fixes)
(Windows 64-bit install w/Win32 and Linux on ARM and x64 cross-compilers via FpcUpDeluxe)

My Systems: Windows 10/11 Pro x64 (Current)

loaded

  • Hero Member
  • *****
  • Posts: 878
Re: How can I read specific Ide settings ?
« Reply #4 on: February 07, 2022, 07:03:08 pm »
Unfortunately, I also don't know how to help you much, either.  ;)

It doesn't matter, just answering is enough. Thank you, your existence is enough.
The more memory computers have, the less memory people seem to use. 😅

balazsszekely

  • Guest
Re: How can I read specific Ide settings ?
« Reply #5 on: February 07, 2022, 07:31:28 pm »
@loaded

Unfortunately that information is not compiled into the exe. However all is not lost, please do the following:

1. Create a package
2. When the package is loaded, wait until the IDE starts. Just start a timer and check if LazarusIDE and PackageEditingInterface is not null. Don't forget to add LazIDEIntf and PackageIntf to uses.
3. If the IDE started, tap into the project openings:
Code: Pascal  [Select][+][-]
  1.    function OnProjectOpened(Sender: TObject; AProject: TLazProject): TModalResult;
  2.    LazarusIDE.AddHandlerOnProjectOpened(@OnProjectOpened);    
Now every time a project is opened in the IDE, you will be notified via OnProjectOpened event. This is also true for your project which has to know the FPC path(ProjectX)
4. Add a new unit to you ProjectX, call it uFPCPath. Also add a variable to the interface section: var FPCPath: String = '';
5. Next time when ProjectX is opened do something like this inside OnProjectOpened:
Code: Pascal  [Select][+][-]
  1. uses ProjectIntf;
  2. //..
  3. for I := 0 to AProject.FileCount - 1 do
  4.   if  ExtractFileName(AProject.Files[I].Filename) = 'uFPCPath' then
  5.   begin
  6.      //we got our file
  7.   end;  
6. Load the file in a StringList, loop through the items until you find:
Code: Pascal  [Select][+][-]
  1. var FPCPath: String = '';
and replace it with:
Code: Pascal  [Select][+][-]
  1. uses IDEOptionsIntf;
  2.  
  3. var FPCPath: String = QuotedStr(IDEEnvironmentOptions.GetParsedCompilerFilename) + ';'
  4.  
Save back the stringlist to file.
7. If everything went well, each time you open ProjectX, you should have the FPC path as a variable inside unit uFPCPath.pas and it will be compiled into your executable.

Please note I did not test the above code, you may have to modify it slightly.

loaded

  • Hero Member
  • *****
  • Posts: 878
Re: How can I read specific Ide settings ?
« Reply #6 on: February 07, 2022, 07:45:14 pm »
My esteemed master Getmem, You are a Living Wise.
Thank you for taking your precious time to share your experience.
What you wrote will probably lead me to the solution (Looks like I'll have to sweat a little).
Thanks again.
The more memory computers have, the less memory people seem to use. 😅

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12121
  • Debugger - SynEdit - and more
    • wiki
Re: How can I read specific Ide settings ?
« Reply #7 on: February 07, 2022, 07:46:17 pm »
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


MarkMLl

  • Hero Member
  • *****
  • Posts: 8533
Re: How can I read specific Ide settings ?
« Reply #8 on: February 07, 2022, 07:49:06 pm »
Try this: in your main file (usually .lpr) use something like this to set a global variable:

Code: Pascal  [Select][+][-]
  1.   B5k5_Project := {$I %FILE% } ;        (* Possibly used for .ini files etc.    *)
  2.  

You can then inspect and parse the result.

There might be other useful strings, but I can't remember where they're documented and you'll probably need to be careful with backward compatibility.

[Slightly later] Thinking back to an earlier discussion, on unix this should work

Code: Pascal  [Select][+][-]
  1. pwd := {$I %PWD%};
  2.  

although that's obviously looking at the state of the shell rather than the working/project directory assumed by the IDE.

MarkMLl
« Last Edit: February 07, 2022, 07:55:18 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

balazsszekely

  • Guest
Re: How can I read specific Ide settings ?
« Reply #9 on: February 07, 2022, 07:53:25 pm »
Try this: in your main file (usually .lpr) use something like this to set a global variable:

Code: [Select]
  B5k5_Project := {$I %FILE% } ;        (* Possibly used for .ini files etc.    *)

You can then inspect and parse the result.

There might be other useful strings, but I can't remember where they're documented and you'll probably need to be careful with backward compatibility.

MarkMLl
That will print out the project file. He needs the FPC install path.

loaded

  • Hero Member
  • *****
  • Posts: 878
Re: How can I read specific Ide settings ?
« Reply #10 on: February 07, 2022, 08:13:35 pm »
Many thanks to Martin_fr and MarkMLI for your answers.
As the masters come, this place turns into a gold mine.
Although I cannot do exactly the same operation I want from the information here, I think I can do a similar one. Of course, as always, I have to do a little trial and error. By the way, the hint given by MarkMLI seems to be very useful for me to detect errors.
Much Regards.
The more memory computers have, the less memory people seem to use. 😅

MarkMLl

  • Hero Member
  • *****
  • Posts: 8533
Re: How can I read specific Ide settings ?
« Reply #11 on: February 07, 2022, 08:22:30 pm »
That will print out the project file. He needs the FPC install path.

[Checks] True, my apologies.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

loaded

  • Hero Member
  • *****
  • Posts: 878
Re: How can I read specific Ide settings ?
« Reply #12 on: February 07, 2022, 08:44:32 pm »
MarkMLl, I realized that the information you provided will have a very important use in another of my projects. I'll probably catch bugs more easily now.
For me, even a piece of bread and a crumb of knowledge is very valuable. I would never waste either of them.
Thank you very much.

Code: Pascal  [Select][+][-]
  1. procedure TForm_Main.P_action_errors(const msg: String; E: Exception);
  2. begin
  3. ....
  4.   DataModule1.TrayIcon1.BalloonHint:=msg + '-> ' + E.ClassName + '  '+ E.Message;
  5. .....
  6. end;
  7. .....
  8.  
  9. try
  10. ...
  11. except
  12.   on E: Exception do P_action_errors({$i %CURRENTROUTINE%},E );
  13. end;
The more memory computers have, the less memory people seem to use. 😅

MarkMLl

  • Hero Member
  • *****
  • Posts: 8533
Re: How can I read specific Ide settings ?
« Reply #13 on: February 07, 2022, 09:02:35 pm »

Code: Pascal  [Select][+][-]
  1. procedure TForm_Main.P_action_errors(const msg: String; E: Exception);
  2.   on E: Exception do P_action_errors({$i %CURRENTROUTINE%},E );
  3. [/quote]
  4.  
  5. You might find some variant of this useful:
  6.  
  7. [code=pascal]
  8. {$undef HAS_CURRENTROUTINE     }
  9. {$if FPC_FULLVERSION >= 030200 }        (* Requires FPC 2.2.4 minimum           *)
  10. {$define HAS_CURRENTROUTINE    }        (* Requires FPC 3.2.0 minimum           *)
  11. {$assertions on                }        (* Make sure name checks are operative  *)
  12. {$endif FPC_FULLVERSION        }
  13.  

Rationale: FPC_FULLVERSION was introduced by FPC 2.2.4, this is older than anybody is likely to be using but I think is worth noting. Provided that this is available, you can check that the current version of FPC is not < 3.2.0, since if it is then CURRENTROUTINE will not be available.

I've also been very pleasantly surprised to find that multi-line macros work, hence e.g.

Code: Pascal  [Select][+][-]
  1. {$macro on  }
  2. {$ifdef HAS_CURRENTROUTINE }
  3. {$define TEST_NAME:= Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: ' +
  4.                         'compiler-generated ' + {$I %CURRENTROUTINE%} + '()' +
  5.                         'does not match hardcoded ' + thisName) }
  6. {$else                     }
  7. {$define TEST_NAME:= }
  8. {$endif HAS_CURRENTROUTINE }
  9.  
  10. {$ifdef ERROR2 }
  11. {$define EXIT_ERROR:= begin
  12.                         if fatal then begin
  13.                           ErrorLine;
  14.                           WriteLn('*** In ', PeekRule(), ': unable to parse ', PoppedRule())
  15.                         end;
  16.                         exit
  17.                       end }
  18. {$else         }
  19. {$define EXIT_ERROR:= exit }
  20. {$endif ERROR2 }
  21.  

which allows me to tidy up earlier code enormously by doing things like

Code: Pascal  [Select][+][-]
  1. (* The sigil defines the special character indicating expansion of an identifier
  2.   in an output string.
  3. *)
  4. function sigil(var defs: text; fatal: boolean= true): boolean;
  5.  
  6. const
  7.   thisName= 'sigil()';
  8.  
  9. var
  10.   ch: ansistring;
  11.  
  12. begin
  13.   TEST_NAME;
  14.   result := true;
  15.   PushRule(thisName);
  16.   try
  17.     if not Expect_Literal(defs, 'sigil', false) then
  18.       exit(false);
  19.     if not Expect_Literal(defs, ' ') then
  20.       EXIT_ERROR;
  21.     if not Expect_Ch(defs, ch) then
  22.       EXIT_ERROR;
  23.     if not (ch[1] in ['!'..'~']) then
  24.       EXIT_ERROR;
  25.     if not Expect_Literal(defs, ';') then
  26.       EXIT_ERROR;
  27.     emit('Sigil: ' + ch);
  28.     result := true
  29.   finally
  30.     PopRule()
  31.   end
  32. end { sigil } ;
  33.  

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

loaded

  • Hero Member
  • *****
  • Posts: 878
Re: How can I read specific Ide settings ?
« Reply #14 on: February 07, 2022, 09:19:41 pm »
To tell the truth, I am far from grasping the above lines at the moment. I felt like a primary school kid.  :o
Now I'm going to do a little trial and error with what I have, maybe one day in the future I can write something like this.  ;D
The more memory computers have, the less memory people seem to use. 😅

 

TinyPortal © 2005-2018