Lazarus

Programming => General => Topic started by: QEnnay on July 13, 2020, 05:32:26 pm

Title: Compiler directive when in the IDE?
Post by: QEnnay on July 13, 2020, 05:32:26 pm
Hi, OK I have searched but not sure of the search-term I am searching for.

I need a Compiler Directive where code is only active/available when I am in the IDE and making changes.

I have searched many things including "directive design" and "directive IDE" and scoured the FPC-wiki. The directive eludes me, IF it even exists.

I remember having something like it in my Delphi days, but cannot remember that either.

Help? :)
Title: Re: Compiler directive when in the IDE?
Post by: Thaddy on July 13, 2020, 05:59:43 pm
Both in Delphi and in FPC such a directive never existed.
Title: Re: Compiler directive when in the IDE?
Post by: QEnnay on July 13, 2020, 06:16:42 pm
Both in Delphi and in FPC such a directive never existed.

Never say, "never" I will find it.

Maybe "Directive" is not the correct word,  but there is a way to prevent code being executed or compiled unless it is within the IDE.
Title: Re: Compiler directive when in the IDE?
Post by: dsiders on July 13, 2020, 06:22:44 pm
Hi, OK I have searched but not sure of the search-term I am searching for.

I need a Compiler Directive where code is only active/available when I am in the IDE and making changes.

I have searched many things including "directive design" and "directive IDE" and scoured the FPC-wiki. The directive eludes me, IF it even exists.

I remember having something like it in my Delphi days, but cannot remember that either.

Help? :)

Perhaps you're looking for:

Code: Pascal  [Select][+][-]
  1. {$define  DEBUG_SOMETHING}
  2.  
  3. // ...
  4.  
  5. {$ifdef DEBUG_SOMETHING}
  6.   // include debugging code here... not limited to design-time though
  7. {$endif}
  8.  

Of course you can always use the ComponentState property:

Code: Pascal  [Select][+][-]
  1. if (csDesigning in AnObject.ComponentState) then
  2. begin
  3.   // design-time code here
  4. end;
  5.  
Title: Re: Compiler directive when in the IDE?
Post by: ASBzone on July 13, 2020, 06:30:46 pm
Both in Delphi and in FPC such a directive never existed.

Never say, "never" I will find it.

Maybe "Directive" is not the correct word,  but there is a way to prevent code being executed or compiled unless it is within the IDE.


I'm not sure how you would like that to work.   Can you give us an example of what you are hoping to accomplish or prevent?

Your code is human readable language that will also be interpreted by some system (compiler, etc) to generate machine readable instructions.   There's nothing in those instructions that will prevent someone else who gets the code from (a) compiling it themselves, from the IDE or a command prompt, or (b) reading it themselves and removing any restrictions that say "don't compile here".

Are you trying to protect the code as in intellectual property?
Are you trying to protect the code as in revision control?

You said "Never say, "never" I will find it."

It must first exist before it can be found.



Title: Re: Compiler directive when in the IDE?
Post by: Warfley on July 13, 2020, 06:38:38 pm
Such a thing can not exist, because FPC and Lazarus are both standalone projects. FPC does not know about Lazarus. Lazarus just calls FPC with a given set of arguments, so if you know that set of arguments you can always compile the source exactly like Lazarus does (which you can get by going into ProjectOptions and klick the show options button on the bottom)
Title: Re: Compiler directive when in the IDE?
Post by: jamie on July 13, 2020, 08:41:34 pm
Hmm. Let's see. How about when the app running under a debugger?
I believe you can detect that and maybe add a little code to examine the path of where it's being run from.
Title: Re: Compiler directive when in the IDE?
Post by: QEnnay on July 13, 2020, 09:24:09 pm
The last Delphi used was D5-Ent.

The Directive (or whatever) had something like "DesignIDE" or along those lines.

It allowed the block of code to run only when the IDE was active. I've done a work around, and I just have to remember to comment it out before moving the final program to be used.

You could have it show passwords in plain text for logging into a Server test stuff etc.

The "designIDE" thing was handy as it prevented debugging stuff going out with the final program when you forgot to UNDEF stuff. Maybe I am the only one who forgets stuff so it never carried over to FPC/Lazarus. LOL

Title: Re: Compiler directive when in the IDE?
Post by: MarkMLl on July 13, 2020, 10:29:15 pm
That rings a bell. I think that Delphi had a library routine which allowed the code to determine whether it was being run in the context of the IDE, probably by working out whether it was being run by a debugger.

Lazarus does of course have "Build modes", and another possibility would be having something ask the local version control system for its opinion as to the checkout state of a file (might no longer be relevant to e.g. Git). And for what it's worth I use things like

Code: [Select]
{$ifopt C+ Predicated on assertions generating code }
  SelfAddressList();                    (* These here for testing during        *)
  BroadcastAddressList()                (* development.                         *)
{$endif }

MarkMLl
Title: Re: Compiler directive when in the IDE?
Post by: jamie on July 13, 2020, 10:35:12 pm
are you referring to the "csDesigning" in the control state ?

basically is set by the IDE if its part of the OI designer mode..


Title: Re: Compiler directive when in the IDE?
Post by: marcov on July 13, 2020, 10:49:08 pm
Afaik Delphi does not have such directive, because it is simply not a compiletime decision since a binary produced in the IDE can be started outside it.

Thus, in Delphi this is a runtime decision, made by checking the pointer "debughook"
Title: Re: Compiler directive when in the IDE?
Post by: sash on July 13, 2020, 11:09:41 pm
Maybe "Directive" is not the correct word,  but there is a way to prevent code being executed or compiled unless it is within the IDE.

I didn't understand two things, consequently:
1. What do you mean by "code": a text being edited in text editor (IDE) or compiled binary? Because these two are different entities.
2. Why someone needs this? What are you trying to achieve, use case?
Title: Re: Compiler directive when in the IDE?
Post by: jamie on July 13, 2020, 11:16:30 pm
I am having problems in following this thread...

As for debugging detection which is one way to determine if the app is running under the debugger is to use the "IsDebuggerPresent"

 I've used that to fast exist one of my apps after it leads the user off on a stray a little.
Title: Re: Compiler directive when in the IDE?
Post by: Warfley on July 13, 2020, 11:48:09 pm
From what you describe I think you are looking for build modes.

In Lazarus go to Project Options and select Compiler Options.
On the top you will find a label "Build Modes" with a drop down next to it and  a button "...". Click that button.

In the newly opened dialog now click "Create Debug and Release modes" in the upper left corner, this will automatically create two new modes, Debug and Release, with sensible configurations for each. You can now close the dialog by clicking "Ok".
Next go to "Custom Options" in the "Project Settings". Make sure "Debug" is selected above in the Build Mode dropdown (it is visible on every entry under "Compiler Options" and press the "Defines" button on the left.
In the newly opend dialog enter "DEBUG" into the Edit at the top and click add. Make sure the checkbox in the List that now was added is checked. Click Ok and in the "Custom Options" Memo "-dDEBUG" should have appeared. If that is the case you can close the Project Options by clicking Ok.

Now you have two new Build modes, Debug and Release, and during Debug, the Definition DEBUG will be set, meaning if you compile your application in this mode, the following Directive will evaluate to true:
Code: Pascal  [Select][+][-]
  1. {$IFDEF DEBUG}
  2.   WriteLn('Debug only code');
  3. {$ENDIF}

To change the build mode, left of the run icon (the little green arrow) you find a button with a gear on it, the tooltip reads "Change Build mode" This icon consits of the main button and a little down arrow to the right of it (directly left of the Run without Debugger symbol). If you click this (the arrow down button), a drop down will appear where you can choose your build mode.

When you run your application from Lazarus, the selected mode there will be used to build. So you want to use Debug, because you want to debug it while in Lazarus.

To now publish your application go to Menu -> Run -> Compile Many Modes. Select "Release" and click Ok. This way the Release build is done without changing the current build mode, so whenever you use the Lazarus Run feature (e.g. the green arrow) still Debug will be executed.

You can test this with this little Application:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. begin
  6.   {$IFDEF DEBUG}
  7.   WriteLn('Debug Mode');
  8.   {$ELSE}
  9.   WriteLn('Release Mode');
  10.   {$ENDIF}
  11. end.  
As Debug is selected as current build mode, running the Application from Lazarus will print out Debug Mode. If you go to Run -> Build Many Modes -> Release and execute the produced executable (by navigating to the folder it is compiled to and execute it, e.g. by double click) it will print Release Mode.

Note: If you want to have a clear distinction which build is the Debug and which is the Release build, simply go to Project Options -> Paths. Now select Release in the dropdown above and under "Target file name (-o)" select something like "bin/$(TargetCPU)-$(TargetOS)/Release/projectName", then select Debug in the drop down and add "bin/$(TargetCPU)-$(TargetOS)/Debug/projectName" to the "Target file name" field. This way whenever you are building your app, the executable for Debug will go into PROJECTDIR/bin/arch-OS/Debug and when you build release with Build many modes it will go into "PROJECTDIR/bin/arch-OS/Release" (where arch-OS could be for example X86_64-win64 on windows 64 bit). This way you don't accedentally override your release build with a debug build

Note 2: Changing the DropDown in Build Modes, changes the currently selected build mode. So when doing changes to Release in the Project Options, you want to change it back to Debug afterwards

Note 3: While you are editing, Lazarus will automatically deduce which ifdef will be compiled and what not, meaning it won't be able to give you help (sensible auto completion and stuff) while you are working in the else branch of the DEBUG ifdefs. So if you are writing code for this, you probably want to temporarily switch build modes while editing, so you can use the full features of the IDE
Title: Re: Compiler directive when in the IDE?
Post by: PascalDragon on July 14, 2020, 09:38:06 am
Hi, OK I have searched but not sure of the search-term I am searching for.

I need a Compiler Directive where code is only active/available when I am in the IDE and making changes.

I have searched many things including "directive design" and "directive IDE" and scoured the FPC-wiki. The directive eludes me, IF it even exists.

I remember having something like it in my Delphi days, but cannot remember that either.

Help? :)

I usually do this:


Then you can use {$ifdef InLazIDE} to hide code from the IDE. I usually use this when working with new language features that the IDE does not yet support (like when I implemented generic functions).
Title: Re: Compiler directive when in the IDE?
Post by: MarkMLl on July 14, 2020, 10:07:32 am
in the drop down and add "bin/$(TargetCPU)-$(TargetOS)/Debug/projectName" to the "Target file name" field. This way whenever you are building

Is the mode name available as a $(something) for use in this context?

MarkMLl
Title: Re: Compiler directive when in the IDE?
Post by: Warfley on July 14, 2020, 10:28:37 am
There actually is $(BuildMode) (https://wiki.lazarus.freepascal.org/IDE_Macros_in_paths_and_filenames#Environment)

Funny, I'm doing this hardcoding now for years and never bothered to check if there is such a switch.

That said, if you have multiple Release and Debug Buildmodes (for different architectures) I like the arch-os/Debug and arch-os/Release structure more (compared to for example bin/ReleaseX86_64Linux or bin/Debugi386Windows or so) and as this is a one-time effort, I am probably sticking with my current system (as I nearly always build for Windows and Linux)
Title: Re: Compiler directive when in the IDE?
Post by: MarkMLl on July 14, 2020, 10:47:38 am
There actually is $(BuildMode) (https://wiki.lazarus.freepascal.org/IDE_Macros_in_paths_and_filenames#Environment)

Don't act so surprised- I was asking, not challenging :-)

OK, it's set to "Default" if no explicit modes are set up so something like

SomeApp-$(TargetCPU)-$(TargetOS)-$(LCLWidgetType)-$(BuildMode)

will never be left with a trailing dash. This is something that I really ought to get into the habit of using, although I habitually leave as much range checking etc. in programs as possible.

MarkMLl
Title: Re: Compiler directive when in the IDE?
Post by: Warfley on July 14, 2020, 02:01:34 pm
Don't act so surprised- I was asking, not challenging :-)
I actually was suprised, I've been using build modes for nearly every project for the past 4 years now and never even thought about the existence of such a switch

This is something that I really ought to get into the habit of using, although I habitually leave as much range checking etc. in programs as possible.

MarkMLl
Yeah, this makes everything easier, for your gitignore you now only need to add the bin directory, especially when cross compiling you basically can iterate over the subdirectories in /bin to pack your releases.
But it can also be annoying at times because, when you add a new Unit in a new folder, you get asked if you want to add this unit, and if you say yes, it is only added to the currently selected build mode. This then results in, when building release mode, suddenly nothing can be found because you created the buildmodes at the very beginning and only added things to the debug mode.

But it's a great feature which doesn't get talked about enough in beginners guides and stuff. It took me years to find out that a. it existed and b. how useful it is
TinyPortal © 2005-2018