Recent

Author Topic: Reusing unit paths across build modes  (Read 1622 times)

Чебурашка

  • Hero Member
  • *****
  • Posts: 593
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Reusing unit paths across build modes
« on: November 26, 2023, 02:00:47 pm »
Hi,
it happened to me several times that I have 2/3 Lazarus build modes, and I need to modify the unit paths (-Fu) to include additional stuff.

The problem is that I have to make it manually in each build mode every time, and of course I do mistakes almost always.

Of course it is senseful to have different unit paths depending on the build mode, for example to include extra units related to debugging, but I would like to know if is possible to "import" or "refer to" the unit path of another build mode, so that in case I need modify I can make it in the "principal" build bode, an the modification is reflected automatically in other ones.
« Last Edit: November 26, 2023, 02:03:24 pm by Чебурашка »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

n7800

  • Hero Member
  • *****
  • Posts: 689
  • Lazarus IDE contributor
    • GitLab profile
Re: Reusing unit paths across build modes
« Reply #1 on: December 24, 2023, 04:08:20 am »
Unfortunately, Lazarus does not yet support "inheritance" of build modes. But I can offer you alternatives:

1) Use conditional compilation:
https://wiki.freepascal.org/Conditional_compilation

You can define "symbols" for each build mode by clicking on the "Defines..." button in this options section:
https://wiki.freepascal.org/IDE_Window:_Compiler_Options#Custom_Options

This will allow certain parts of the code to only compile in specified build profiles:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   {$IFDEF DebugBuild}
  4.   ShowMessage('This message will only be shown in debug build mode');
  5.   {$ENDIF}
  6. end;

2) If you want to specify different units in each build mode, then use conditional compilation directly in the "uses" section:
Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, SysUtils {$IFDEF DebugBuild}, MyDebugUnit{$ENDIF};

3) Or you can use the following compiler directive:
https://www.freepascal.org/docs-html/prog/progsu119.html

Code: Pascal  [Select][+][-]
  1. {$IFDEF ReleaseBuild}
  2.   {$UNITPATH ​units\release}
  3. {$ELSE}
  4.   {$UNITPATH ​units\debug}
  5. {$ENDIF}
  6.  
  7. uses
  8.   Classes, SysUtils, MyUnit;

4) The same link (at the end of the page) provides another method via:
Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, SysUtils,
  3.   {$IFDEF ReleaseBuild}
  4.   MyUnit in 'units\release\MyUnit.pas'
  5.   {$ELSE}
  6.   MyUnit in 'units\debug\MyUnit.pas'
  7.   {$ENDIF}
  8.   ;

Read the warnings about relative paths carefully.

 

TinyPortal © 2005-2018