Lazarus

Using the Lazarus IDE => General => Topic started by: MarkMLl on November 27, 2021, 10:50:29 pm

Title: "$if declared()" directive
Post by: MarkMLl on November 27, 2021, 10:50:29 pm
Somewhere around v2.0 Lazarus started adding an assignment to its .lpr file which breaks compatibility with older versions. How does one conditionalise it, since the example below doesn't work:

Code: Pascal  [Select][+][-]
  1.   RequireDerivedFormResource:=True;
  2. {$if declared(Application.Scaled) }
  3.   Application.Scaled:=True;
  4. {$endif                           }
  5.   Application.Initialize;
  6.  

MarkMLl
Title: Re: "$if declared()" directive
Post by: wp on November 27, 2021, 11:49:45 pm
I think it came in v1.8. So, you can add LCLVersion to the uses clause of the project file and then put the Application.Scaled into an {$IF LCL_FullVersion >=1080000} directive:
Code: Pascal  [Select][+][-]
  1. begin
  2.   RequireDerivedFormResource:=True;
  3.   {$IF LCL_FullVersion >= 1080000}
  4.   Application.Scaled:=True;
  5.   {$IFEND}
  6.   Application.Initialize;
  7.   Application.CreateForm(TForm1, Form1);
  8.   Application.Run;
  9. end.    
But be warned: Sometimes the project file is rewritten by the IDE, and then the Application.Scaled := True will be re-added.
Title: Re: "$if declared()" directive
Post by: MarkMLl on November 28, 2021, 10:16:05 am
Thanks very much for that, I'll investigate: I admit I didn't know about that define.

I routinely insert e.g. handlers for --help and --version into the .lpr just before that point, and /so/ /far/ haven't had the IDE interfere with it. In the current case the .lpr hadn't been rewritten, it was something I'd coded on a PC with a v2.0 IDE and then moved to a newly-rebuilt Raspberry Pi where the compiler and IDE were a bit older.

Slightly later for the record: that needs an explicit import of LCLVersion.

MarkMLl


Title: Re: "$if declared()" directive
Post by: marcov on November 28, 2021, 06:32:15 pm
Maybe worth to try TApplication.scaled? 
Title: Re: "$if declared()" directive
Post by: MarkMLl on November 28, 2021, 06:58:49 pm
Maybe worth to try TApplication.scaled?

Regrettably not, I get

gui_test.lpr(51,5) Error: Evaluating a conditional compiling expression

Without meaning to criticise either project, I don't know which is worse: that Lazarus has added something without conditionalising it or that FPC complains about an undefined field without supporting the obvious way of excluding it.

MarkMLl
Title: Re: "$if declared()" directive
Post by: PascalDragon on November 30, 2021, 11:02:18 pm
Without meaning to criticise either project, I don't know which is worse: that Lazarus has added something without conditionalising it or that FPC complains about an undefined field without supporting the obvious way of excluding it.

Declared() simply does not allow dots. It only allows identifiers (including generics) currently. I have it somewhere on my ToDo list to add support for dots as well, but we all know the problems with ToDo lists... %)
Title: Re: "$if declared()" directive
Post by: MarkMLl on December 01, 2021, 07:01:22 am
Without meaning to criticise either project, I don't know which is worse: that Lazarus has added something without conditionalising it or that FPC complains about an undefined field without supporting the obvious way of excluding it.

Declared() simply does not allow dots. It only allows identifiers (including generics) currently. I have it somewhere on my ToDo list to add support for dots as well, but we all know the problems with ToDo lists... %)

Indeed. As I said, I wasn't trying to criticise but I don't really know whose responsibility fixing that one is. It would have been easier if additions to automatically-inserted Lazarus code had been marked by a conditional.

MarkMLl
Title: Re: "$if declared()" directive
Post by: PascalDragon on December 01, 2021, 01:53:15 pm
Indeed. As I said, I wasn't trying to criticise but I don't really know whose responsibility fixing that one is. It would have been easier if additions to automatically-inserted Lazarus code had been marked by a conditional.

Lazarus assumes that you're using a project generated with version X with version X or newer (here the version that generates a project with Application.Scaled does provide Application.Scaled as well). In my opinion that is a valid assumption.
Title: Re: "$if declared()" directive
Post by: MarkMLl on December 01, 2021, 02:18:22 pm
Lazarus assumes that you're using a project generated with version X with version X or newer (here the version that generates a project with Application.Scaled does provide Application.Scaled as well). In my opinion that is a valid assumption.

Somebody pointed out earlier in the thread that there is a risk that a given version of Lazarus insert lines into an older project.

I believe I've seen that, but I don't want to put my hand on my heart and say under what circumstances.

But I did say that the /easiest/ would have been if Lazarus conditionalised the addition, even if it were not strictly necessary.

MarkMLl
Title: Re: "$if declared()" directive
Post by: Bart on December 01, 2021, 02:23:25 pm
There are some setting to treat a project with as much backwards compatibility as possible.
Maybe you can file a feature request to have it write that info in ifdef's based on that setting?

Bart
Title: Re: "$if declared()" directive
Post by: MarkMLl on December 01, 2021, 02:49:45 pm
There are some setting to treat a project with as much backwards compatibility as possible.
Maybe you can file a feature request to have it write that info in ifdef's based on that setting?

It's too late since it would be unreasonable to expect a fix to be backported.

MarkMLl
Title: Re: "$if declared()" directive
Post by: Bart on December 01, 2021, 05:29:11 pm
At least it would not happen if you open the project in trunk then.

Bart
Title: Re: "$if declared()" directive
Post by: MarkMLl on December 02, 2021, 01:03:17 pm
I notice that the

Code: Pascal  [Select][+][-]
  1.   RequireDerivedFormResource:=True;
  2.  

line started being added about v1.0. Purely out of curiosity, can anybody say what version of FPC was first able to evaluate the

Code: Pascal  [Select][+][-]
  1.   {$IF LCL_FullVersion >= ...
  2.  

directive?

Please note that I'm not asking anybody to go looking for an answer, I can trawl through compiler release notes as easily as anybody. But if anybody knew from memory I'd be interested.

MarkMLl
Title: Re: "$if declared()" directive
Post by: PascalDragon on December 02, 2021, 01:53:57 pm
Purely out of curiosity, can anybody say what version of FPC was first able to evaluate the

Code: Pascal  [Select][+][-]
  1.   {$IF LCL_FullVersion >= ...
  2.  

directive?

In mode Delphi since some time in the 2.0.x series, in mode ObjFPC since 2.2.0 and for all modes since 3.0.

Please note that I'm not asking anybody to go looking for an answer, I can trawl through compiler release notes as easily as anybody.

It can very well be that this isn't mentioned in the release notes. Not everything is mentioned there...
Title: Re: "$if declared()" directive
Post by: AlexTP on December 02, 2021, 02:05:23 pm
Yes, it is not mentioned at https://wiki.freepascal.org/FPC_New_Features_3.0.0 as I see. I don't know how to 'formulate it' in the wiki.
Title: Re: "$if declared()" directive
Post by: MarkMLl on December 02, 2021, 02:45:55 pm
In mode Delphi since some time in the 2.0.x series, in mode ObjFPC since 2.2.0 and for all modes since 3.0.
...

It can very well be that this isn't mentioned in the release notes. Not everything is mentioned there...

Allowing that the context- at least of the thread as started- is .lpr files which are normally IDE-generated I presume mode ObjFPC applies.

The oldest FPC that I can possibly see myself running is 2.2.4 in order to run Lazarus building a GTK v1 app. I can't realistically see any reason I'd want to do that.

I do have one elderly 32-bit laptop which I use on occasion for testing comms hardware; because of old display hardware there's a complex kernel -> libraries -> apps progression which limits me to something like Lazarus 1.6.4 + FPC 3.0.2 on it hence the original question.

I think it's reasonable to assume that nobody will be running anything older, unless I (or anybody else) starts looking at e.g. SunOS/Solaris again where the available variant was- I think- limited to GTK1. I've done it in the past but really don't know how that squares with the versions above since Jonas et al. did various SPARC fixes around 2.7.1... it might be that I was able to use 2.7 or 3.0 on that platform but had to disable various packages.

A bit of looking at IDE sources earlier suggests that the other automatically-inserted line started appearing around Lazarus 1.0.0; in view of the combinations of Lazarus and FPC which realistically work together it is probably always safe to edit the .lpr to read

Code: Pascal  [Select][+][-]
  1. begin
  2. {$if LCL_FULLVERSION >= 1000000 }
  3.   RequireDerivedFormResource:=True;
  4. {$ifend                         }
  5. {$if LCL_FULLVERSION >= 1080000 }
  6.   Application.Scaled:=True;
  7. {$ifend                         }
  8.   Application.Initialize;
  9. ...
  10.  

Anything substantially older than Lazarus 1.0 is likely to have other problems caused by tabbed control and Synedit changes, plus possibly a change in the resource directive. But I'd be surprised if anybody seriously tried that and expected code that old to be portable without significant issues.

Thanks also to Alex.

MarkMLl
Title: Re: "$if declared()" directive
Post by: PascalDragon on December 03, 2021, 03:46:38 pm
Yes, it is not mentioned at https://wiki.freepascal.org/FPC_New_Features_3.0.0 as I see. I don't know how to 'formulate it' in the wiki.

3.0.0 would be the wrong place anyway, as the original feature was added in 2.0.x, but only for mode Delphi.
Title: Re: "$if declared()" directive
Post by: MarkMLl on February 19, 2022, 02:19:17 pm
Declared() simply does not allow dots. It only allows identifiers (including generics) currently. I have it somewhere on my ToDo list to add support for dots as well, but we all know the problems with ToDo lists... %)

I note that (as of 3.2.2) it does however accept a unit name:

Code: Pascal  [Select][+][-]
  1. { if declared(TMFRC522.PCD_PerformSelfTest) }  // Fails due to no dot etc. support
  2. {$if declared(mfrc522_diag) } // <----- Unit name (works)
  3. { if declared(TMFRC522H) }    // <----- Class helper in unit (works)
  4.       if mfrc522.PCD_PerformSelfTest() then
  5.         WriteLn('Selftest OK')
  6.       else
  7.         WriteLn('Selftest failed');
  8. {$endif                            }
  9.  

which is actually quite a comfortable way of handling things.

MarkMLl
TinyPortal © 2005-2018