Difference between revisions of "Conditional Compiler Options"

From Lazarus wiki
Jump to navigationJump to search
 
(29 intermediate revisions by 4 users not shown)
Line 1: Line 1:
This page collects ideas of how to implement conditional compiler options in lazarus.
+
{{Conditional_Compiler_Options}}
  
First it should be collected the cases, where conditional compiler options are needed.
+
==Overview==
 +
Since Lazarus 0.9.29 you can define build modes and conditional compiler options. These options depend on current target OS, CPU, widgetset and project's build mode.
  
=Case studies=
+
For conditionals see here [[Macros and Conditionals]].
  
==Conditional Package Requirement: VirtualTreeView / multilog==
+
You can set the build macros for a package via [[IDE_Window: Compiler Options#Build_Macros|package editor / Compiler Options / Build Macros]].
  
Make it possible to define a package requirement as optional (Should be
+
You can set the build macros for a project via [[IDE Window: Compiler Options#Build_Macros|Project / Project Options / Compiler Options / Build Macros]].
available both to package and program).
 
Concrete situation: VirtualTreeView requires multilog package to help
 
debug code. In a release it is not desired to have such dependency.  
 
Currently there are two possibilities:
 
  
* in the release remove the dependency from the package, and then add again to reenable debugging.
+
==Target specific units - Doable via FPC directives, no Lazarus extras needed==
* enclose the units of the package with {$IFNDEF MYRELEASE} and add -dMYRELEASE to the project and all package compiler options (or fpc.cfg).
+
Concrete situation: LCL extensions package has a unit (OleUtils) that implements TOLEStream. It only makes sense in Windows. Currently the unit is added in all widgetsets but all code is wrapped around a ifdef Windows define so is seen as a dummy unit in other widgetsets.
 
 
An idea is to have groups of conditional requirements. Each group would
 
have  a define value associated to be used at compile time (e.g:
 
'DEBUG_CODE') and child packages. Each group would have an enabled flag.
 
If group is enabled, the child packages are added as requirements and
 
the define value passed with -d to the compiler.
 
 
 
*Disadvantage: Other packages will use the same Debug/Release differences. In big projects normally only a small part should be set verbose (into debugging mode).
 
 
 
==Target specific units==
 
 
 
Make units packages/projects optional according to OS.
 
Concrete situation: LCL extensions package has a unit (OleUtils) that  
 
implements TOLEStream. It only makes sense in Windows. Currently the unit  
 
is added in all widgetsets but all code is wrapped around a ifdef  
 
Windows define so is seen as a dummy unit in other widgetsets.
 
  
 
Notes:  
 
Notes:  
*All units should be added to the package, independent if they are used. If a unit should not always be compiled in (not added to the uses section of the package), just unset the 'uses unit' checkbox.
+
*All units should be added to the package, independent if they are used. If a unit should not always be compiled (not added to the uses section of the package), just unset the 'uses unit' checkbox and add for example {$IFDEF win32}uses oleutils{$ENDIF} to a package unit.
** If the "uses unit" flag is not set, the unit will not be compiled. Nor in win32 (where is needed) neither in the other OS-es. It will be compiled '''only''' if another unit in the package uses it. So you must add for example {$IFDEF win32}uses oleutils.pas{$ENDIF} somewhere to one of the package units.
+
*You can put units and include files specific to targets into sub directories as demonstrated in the Lazarus and FPC sources (units/$(TargetOS)).
*You can put units specific to targets into sub directories as demonstrated in the Lazarus and FPC sources (units/$(TargetOS)).
+
*Since 0.9.31 multiple files with the same name can be added to packages.
*Conclusion: There is no real need for conditional options for single units, as this is already possible.
 
 
 
==Special build modes==
 
 
 
Any application will desire different options for at least 2 possibilities:
 
 
 
* Debug - which should be default
 
* Release
 
 
 
So, any application should already come with those 2 options, and legacy applications could have them added automatically.
 
 
 
To have a trully flexible system any number of compilation modes should be supported (maybe >= 1 to have at least one). One idea to implement this system would be having any mode use all options set on the dialog, but also have a list of which options it will override to a custom value.
 
 
 
An extra idea is having the build options available on the Project -> Build menu
 
 
 
We could have: Project -> Build mode -> Debug; Release; Etc (submenu with all build modes), like Xcode has.
 
 
 
==Arbitrary number of build modes - Virtual Magnifying Glass==
 
 
 
One case where an arbitrary number of build modes would be excelent is the Virtual Magnifying Glass. Because Mac OS X requires special build options which can't be used on other operating systems, the magnifier could have the following options:
 
  
* Debug
+
+=What will not be implemented==
* Release
+
*A global Debug/Verbose buildmode. Almost no one wants to increase verbosity of all packages, nor debug all packages. But you can define one for your own packages.
* Mac Qt4 Debug
+
*Conditional units. This is already flexible doable via FPC directives and IDE macros.
* Mac Qt4 Release
 
* Mac Carbon Debug
 
* Mac Carbon Release
 
  
The Mac Qt4 options need to add "-framework Qt4Intf -lobjc" to the extra linker options and active the extra linker options.
+
==Future development / Wishes==
 +
*Use a package only under one target, e.g. only under Windows and OS X, but not under Linux or BSD.
  
The Mac Carbon options need to add "-framework carbon" to the extra linker options and active the extra linker options.
+
{{Template:Directives, Defines and Conditionals}}
 +
[[Category:Conditionals]]

Latest revision as of 15:27, 25 March 2017

English (en) français (fr)

Overview

Since Lazarus 0.9.29 you can define build modes and conditional compiler options. These options depend on current target OS, CPU, widgetset and project's build mode.

For conditionals see here Macros and Conditionals.

You can set the build macros for a package via package editor / Compiler Options / Build Macros.

You can set the build macros for a project via Project / Project Options / Compiler Options / Build Macros.

Target specific units - Doable via FPC directives, no Lazarus extras needed

Concrete situation: LCL extensions package has a unit (OleUtils) that implements TOLEStream. It only makes sense in Windows. Currently the unit is added in all widgetsets but all code is wrapped around a ifdef Windows define so is seen as a dummy unit in other widgetsets.

Notes:

  • All units should be added to the package, independent if they are used. If a unit should not always be compiled (not added to the uses section of the package), just unset the 'uses unit' checkbox and add for example {$IFDEF win32}uses oleutils{$ENDIF} to a package unit.
  • You can put units and include files specific to targets into sub directories as demonstrated in the Lazarus and FPC sources (units/$(TargetOS)).
  • Since 0.9.31 multiple files with the same name can be added to packages.

+=What will not be implemented==

  • A global Debug/Verbose buildmode. Almost no one wants to increase verbosity of all packages, nor debug all packages. But you can define one for your own packages.
  • Conditional units. This is already flexible doable via FPC directives and IDE macros.

Future development / Wishes

  • Use a package only under one target, e.g. only under Windows and OS X, but not under Linux or BSD.
Directives, definitions and conditionals definitions
global compiler directives • local compiler directives

Conditional Compiler Options • Conditional compilation • Macros and Conditionals • Platform defines
$IF