Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2012-03

Conditional compilation depending on Lazarus version?

16 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Reinier Olislagers

Hi list,

I'm sure I must be missing something.

I want to compile some grid drawing stuff depending on whether a grid bug has been fixed - i.e. I want to do something like this:

Had a look here:
http://wiki.lazarus.freepascal.org/IDE_Macros_in_paths_and_filenames ... but that only seems to specify support for FPC version, not Lazarus version.

Ideally, I'd be looking at something like:
  {$IF LAZARUSREVISION>=36180}
  // Note: normally, grid DefaultDrawing property must be off to avoid
duplicate drawing
  // This will result in duplicate drawing but should work.
  DefaultDrawing:=true;
  {$ENDIF}

Is something like this possible, or is there an easier way of doing things?

I realize that the code above will be compiled by FPC, not Lazarus; perhaps it is possible to set an environment variable LAZARUSREVISION and/or LAZARUSVERSION and get these into FPC macros in some way?

Thanks,
Reinier

Reply by email

Mark Morgan Lloyd
to Reinier Olislagers
Reinier Olislagers wrote 25 lines
Hi list, I'm sure I must be missing something. I want to compile some grid drawing stuff depending on whether a grid bug has been fixed - i.e. I want to do something like this: Had a look here: http://wiki.lazarus.freepascal.org/IDE_Macros_in_paths_and_filenames ... but that only seems to specify support for FPC version, not Lazarus version. Ideally, I'd be looking at something like: {$IF LAZARUSREVISION>=36180} // Note: normally, grid DefaultDrawing property must be off to avoid duplicate drawing // This will result in duplicate drawing but should work. DefaultDrawing:=true; {$ENDIF} Is something like this possible, or is there an easier way of doing things? I realize that the code above will be compiled by FPC, not Lazarus; perhaps it is possible to set an environment variable LAZARUSREVISION and/or LAZARUSVERSION and get these into FPC macros in some way?

I don't think so, in the general case. The Lazarus revision number can probably be found as a .inc in the ide directory, and post about 0.9.24 you can get the LCL version at runtime, but folding those back into something that could be used by a conditional will be problematic.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Sven Barth
to Reinier Olislagers: I'm sure I must be missing something. I want to compile some grid drawing stuff depending…
Reinier Olislagers wrote 25 lines
Hi list, I'm sure I must be missing something. I want to compile some grid drawing stuff depending on whether a grid bug has been fixed - i.e. I want to do something like this: Had a look here: http://wiki.lazarus.freepascal.org/IDE_Macros_in_paths_and_filenames ... but that only seems to specify support for FPC version, not Lazarus version. Ideally, I'd be looking at something like: {$IF LAZARUSREVISION>=36180} // Note: normally, grid DefaultDrawing property must be off to avoid duplicate drawing // This will result in duplicate drawing but should work. DefaultDrawing:=true; {$ENDIF} Is something like this possible, or is there an easier way of doing things? I realize that the code above will be compiled by FPC, not Lazarus; perhaps it is possible to set an environment variable LAZARUSREVISION and/or LAZARUSVERSION and get these into FPC macros in some way?

The revision is not possible (when using development versions you should try to use the latest revision), but for version you can use the unit LCLVersion. Just add it to the uses clause and then you can use e.g.:

{$if lcl_fullversion>=93100}
   // code that should work with 0.9.31 or newer
{$endif}

Just take a look at the unit to see which constants are supported (Note: it makes use of the feature that constants can be used in compiler conditionals)

Regards,
Sven

Reply by email

Vincent Snijders
to Mark Morgan Lloyd: I don't think so, in the general case. The Lazarus revision number can probably be found…

Op 22 maart 2012 11:50 heeft Mark Morgan Lloyd
het volgende geschreven:

I don't think so, in the general case. The Lazarus revision number can probably be found as a .inc in the ide directory, and post about 0.9.24 you can get the LCL version at runtime, but folding those back into something that could be used by a conditional will be problematic.

For examles using the version number, see:
http://lazarus-ccr.sourceforge.net/docs/lcl/lclversion/index.html and
http://svn.freepascal.org/svn/lazarus/trunk/examples/lclversion/

Vincent

Reply by email

Reinier Olislagers
to Sven Barth: The revision is not possible (when using development versions you should try to use the…
Sven Barth wrote 12 lines
Am 22.03.2012 11:32, schrieb Reinier Olislagers: The revision is not possible (when using development versions you should try to use the latest revision), but for version you can use the unit LCLVersion. Just add it to the uses clause and then you can use e.g.: {$if lcl_fullversion>=93100} // code that should work with 0.9.31 or newer {$endif} Just take a look at the unit to see which constants are supported (Note: it makes use of the feature that constants can be used in compiler conditionals)

Great, thanks, that works!

Reply by email

Mark Morgan Lloyd
to Reinier Olislagers
Reinier Olislagers wrote 15 lines
On 22-3-2012 11:57, Sven Barth wrote: > Am 22.03.2012 11:32, schrieb Reinier Olislagers: > The revision is not possible (when using development versions you should > try to use the latest revision), but for version you can use the unit > LCLVersion. Just add it to the uses clause and then you can use e.g.: > > {$if lcl_fullversion>=93100} > // code that should work with 0.9.31 or newer > {$endif} > > Just take a look at the unit to see which constants are supported (Note: > it makes use of the feature that constants can be used in compiler > conditionals) Great, thanks, that works!

At that point if there's an ./ide/revision.inc file containing

// Created by Svn2RevisionInc
const RevisionStr = '35880M';

can it be included into a unit to make it visible at compile time?

And is there a way of getting $i to do something intelligent if the file it's looking for doesn't exist?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Mark Morgan Lloyd
to Sven Barth: The revision is not possible (when using development versions you should try to use the…
Sven Barth wrote 13 lines
Am 22.03.2012 11:32, schrieb Reinier Olislagers: > I realize that the code above will be compiled by FPC, not Lazarus; > perhaps it is possible to set an environment variable LAZARUSREVISION > and/or LAZARUSVERSION and get these into FPC macros in some way? The revision is not possible (when using development versions you should try to use the latest revision), but for version you can use the unit LCLVersion. Just add it to the uses clause and then you can use e.g.: {$if lcl_fullversion>=93100} // code that should work with 0.9.31 or newer {$endif}

For the record, can you easily say what version of FPC introduced this capability?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Sven Barth
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 19 lines
Sven Barth wrote: > Am 22.03.2012 11:32, schrieb Reinier Olislagers: >> I realize that the code above will be compiled by FPC, not Lazarus; >> perhaps it is possible to set an environment variable LAZARUSREVISION >> and/or LAZARUSVERSION and get these into FPC macros in some way? > > The revision is not possible (when using development versions you > should try to use the latest revision), but for version you can use > the unit LCLVersion. Just add it to the uses clause and then you can > use e.g.: > > {$if lcl_fullversion>=93100} > // code that should work with 0.9.31 or newer > {$endif} For the record, can you easily say what version of FPC introduced this capability?

According to a svn blame for the corresponding code this is in there at least since 2005 (when FPC switched from CVS to SVN and thus no older history is available to the public)

Regards,
Sven

Reply by email

Mark Morgan Lloyd
to Sven Barth
Sven Barth wrote 24 lines
Am 22.03.2012 13:28, schrieb Mark Morgan Lloyd: > Sven Barth wrote: >> Am 22.03.2012 11:32, schrieb Reinier Olislagers: > >>> I realize that the code above will be compiled by FPC, not Lazarus; >>> perhaps it is possible to set an environment variable LAZARUSREVISION >>> and/or LAZARUSVERSION and get these into FPC macros in some way? >> >> The revision is not possible (when using development versions you >> should try to use the latest revision), but for version you can use >> the unit LCLVersion. Just add it to the uses clause and then you can >> use e.g.: >> >> {$if lcl_fullversion>=93100} >> // code that should work with 0.9.31 or newer >> {$endif} > > For the record, can you easily say what version of FPC introduced this > capability? > According to a svn blame for the corresponding code this is in there at least since 2005 (when FPC switched from CVS to SVN and thus no older history is available to the public)

Which probably makes it safe for 2.2.4, which corresponds approximately to 0.9.24.1 by which time the new-format lcl_fullversion etc. were available.

I was looking at these versions a few days ago, because I've got one project which I'm trying to keep compatible with GTK v1. Being to base coding decisions relating to e.g. Synedit capabilities on Lazarus rather than FPC version, without having to go to RTTI, is obviously a major advantage.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Sven Barth
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 29 lines
Sven Barth wrote: > Am 22.03.2012 13:28, schrieb Mark Morgan Lloyd: >> Sven Barth wrote: >>> Am 22.03.2012 11:32, schrieb Reinier Olislagers: >> >>>> I realize that the code above will be compiled by FPC, not Lazarus; >>>> perhaps it is possible to set an environment variable LAZARUSREVISION >>>> and/or LAZARUSVERSION and get these into FPC macros in some way? >>> >>> The revision is not possible (when using development versions you >>> should try to use the latest revision), but for version you can use >>> the unit LCLVersion. Just add it to the uses clause and then you can >>> use e.g.: >>> >>> {$if lcl_fullversion>=93100} >>> // code that should work with 0.9.31 or newer >>> {$endif} >> >> For the record, can you easily say what version of FPC introduced this >> capability? >> > > According to a svn blame for the corresponding code this is in there > at least since 2005 (when FPC switched from CVS to SVN and thus no > older history is available to the public) Which probably makes it safe for 2.2.4, which corresponds approximately to 0.9.24.1 by which time the new-format lcl_fullversion etc. were available.

These constants were introduced EXACTLY for the purpose to use them in ifdefs. So yes, they are safe to use in this way ;)

Regards,
Sven

Reply by email

cobines
to Mark Morgan Lloyd: At that point if there's an ./ide/revision.inc file containing // Created by…

2012/3/22 Mark Morgan Lloyd :

Show quoted text 6 lines
At that point if there's an ./ide/revision.inc file containing // Created by Svn2RevisionInc const RevisionStr = '35880M'; can it be included into a unit to make it visible at compile time?

You can add "$(LazarusDir)\ide" to the include paths of the project. Then this works:

{$I revision.inc}
const
  revIntroducedNewFeature = '55555';
begin
  {$IF (RevisionStr >= revIntroducedNewFeature)}
  UseNewFeature;
  {$ENDIF}
end;
cobines

Reply by email

Mark Morgan Lloyd
to cobines
cobines wrote 21 lines
2012/3/22 Mark Morgan Lloyd : > At that point if there's an ./ide/revision.inc file containing > > // Created by Svn2RevisionInc > const RevisionStr = '35880M'; > > can it be included into a unit to make it visible at compile time? You can add "$(LazarusDir)\ide" to the include paths of the project. Then this works: {$I revision.inc} const revIntroducedNewFeature = '55555'; begin {$IF (RevisionStr >= revIntroducedNewFeature)} UseNewFeature; {$ENDIF} end;

True, although what I was really wondering was whether it could be pulled in purely by referencing a unit, rather than having $I all over the place. And there's still the issue of recovering gracefully if the include file isn't available or the version is non-numeric, i.e. has an appended M or reads "exported" because Lazarus wasn't built from SVN.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Mark Morgan Lloyd
to Sven Barth: These constants were introduced EXACTLY for the purpose to use them in ifdefs. So yes,…
Sven Barth wrote 33 lines
Am 22.03.2012 14:10, schrieb Mark Morgan Lloyd: > Sven Barth wrote: >> Am 22.03.2012 13:28, schrieb Mark Morgan Lloyd: >>> Sven Barth wrote: >>>> Am 22.03.2012 11:32, schrieb Reinier Olislagers: >>> >>>>> I realize that the code above will be compiled by FPC, not Lazarus; >>>>> perhaps it is possible to set an environment variable LAZARUSREVISION >>>>> and/or LAZARUSVERSION and get these into FPC macros in some way? >>>> >>>> The revision is not possible (when using development versions you >>>> should try to use the latest revision), but for version you can use >>>> the unit LCLVersion. Just add it to the uses clause and then you can >>>> use e.g.: >>>> >>>> {$if lcl_fullversion>=93100} >>>> // code that should work with 0.9.31 or newer >>>> {$endif} >>> >>> For the record, can you easily say what version of FPC introduced this >>> capability? >>> >> >> According to a svn blame for the corresponding code this is in there >> at least since 2005 (when FPC switched from CVS to SVN and thus no >> older history is available to the public) > > Which probably makes it safe for 2.2.4, which corresponds approximately > to 0.9.24.1 by which time the new-format lcl_fullversion etc. were > available. These constants were introduced EXACTLY for the purpose to use them in ifdefs. So yes, they are safe to use in this way ;)

I've been having a bit of a play with this. Using the example above, am I correct in saying that this works

{$if lcl_fullversion>=93100}
   // code that should work with 0.9.31 or newer
{$endif}

but this doesn't

{$ifdef lcl_fullversion }
   // code that should work with any version that defines lcl_fullversion
{$endif}

As a variation of the latter, am I correct in saying that ifdef can't be used to check whether a function or procedure is implemented in some imported unit?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Sven Barth
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 53 lines
Sven Barth wrote: > Am 22.03.2012 14:10, schrieb Mark Morgan Lloyd: >> Sven Barth wrote: >>> Am 22.03.2012 13:28, schrieb Mark Morgan Lloyd: >>>> Sven Barth wrote: >>>>> Am 22.03.2012 11:32, schrieb Reinier Olislagers: >>>> >>>>>> I realize that the code above will be compiled by FPC, not Lazarus; >>>>>> perhaps it is possible to set an environment variable >>>>>> LAZARUSREVISION >>>>>> and/or LAZARUSVERSION and get these into FPC macros in some way? >>>>> >>>>> The revision is not possible (when using development versions you >>>>> should try to use the latest revision), but for version you can use >>>>> the unit LCLVersion. Just add it to the uses clause and then you can >>>>> use e.g.: >>>>> >>>>> {$if lcl_fullversion>=93100} >>>>> // code that should work with 0.9.31 or newer >>>>> {$endif} >>>> >>>> For the record, can you easily say what version of FPC introduced this >>>> capability? >>>> >>> >>> According to a svn blame for the corresponding code this is in there >>> at least since 2005 (when FPC switched from CVS to SVN and thus no >>> older history is available to the public) >> >> Which probably makes it safe for 2.2.4, which corresponds approximately >> to 0.9.24.1 by which time the new-format lcl_fullversion etc. were >> available. > > These constants were introduced EXACTLY for the purpose to use them in > ifdefs. So yes, they are safe to use in this way ;) I've been having a bit of a play with this. Using the example above, am I correct in saying that this works {$if lcl_fullversion>=93100} // code that should work with 0.9.31 or newer {$endif} but this doesn't {$ifdef lcl_fullversion } // code that should work with any version that defines lcl_fullversion {$endif} As a variation of the latter, am I correct in saying that ifdef can't be used to check whether a function or procedure is implemented in some imported unit?

$ifdef does only work with defines (those done with -dXXX or {$define ...}), but not with identifiers/symbols (lcl_fullversion is a symbol). If you want to check for the existance of a symbol you need to use this:

{$if declared(lcl_fullversion) }
// code that should work with any version that defines lcl_fullversion
{$endif}
// Note: if you need Delphi compatibility you need to use {$ifend}
instead of {$endif} here

Regards,
Sven

Reply by email

Mark Morgan Lloyd
to Sven Barth
Sven Barth wrote 16 lines
On 24.03.2012 19:35, Mark Morgan Lloyd wrote: > As a variation of the latter, am I correct in saying that ifdef can't be > used to check whether a function or procedure is implemented in some > imported unit? > $ifdef does only work with defines (those done with -dXXX or {$define ...}), but not with identifiers/symbols (lcl_fullversion is a symbol). If you want to check for the existance of a symbol you need to use this: {$if declared(lcl_fullversion) } // code that should work with any version that defines lcl_fullversion {$endif} // Note: if you need Delphi compatibility you need to use {$ifend} instead of {$endif} here

Ah /ha/! So in other words I can use something like

{$if declared(SerGetRI) }
   WriteLn('Has SerGetRI');
{$endif }
{$if declared(SerGetCD) }
   WriteLn('Has SerGetCD');
{$endif }

to check whether the updated serial.pp that I attached to bug 18946 is actually available, rather than having to scratch around looking for it when I don't actually need to monitor CD.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Sven Barth
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 32 lines
Sven Barth wrote: > On 24.03.2012 19:35, Mark Morgan Lloyd wrote: >> As a variation of the latter, am I correct in saying that ifdef can't be >> used to check whether a function or procedure is implemented in some >> imported unit? >> > > $ifdef does only work with defines (those done with -dXXX or {$define > ...}), but not with identifiers/symbols (lcl_fullversion is a symbol). > If you want to check for the existance of a symbol you need to use this: > > {$if declared(lcl_fullversion) } > // code that should work with any version that defines lcl_fullversion > {$endif} > // Note: if you need Delphi compatibility you need to use {$ifend} > instead of {$endif} here Ah /ha/! So in other words I can use something like {$if declared(SerGetRI) } WriteLn('Has SerGetRI'); {$endif } {$if declared(SerGetCD) } WriteLn('Has SerGetCD'); {$endif } to check whether the updated serial.pp that I attached to bug 18946 is actually available, rather than having to scratch around looking for it when I don't actually need to monitor CD.

Yes, that should work that way.

Regards,
Sven

Reply by email