Recent

Author Topic: Compiler directive when in the IDE?  (Read 2605 times)

QEnnay

  • Full Member
  • ***
  • Posts: 115
Compiler directive when in the IDE?
« 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? :)
Linux-Mint 20.1 x64 + Cinnamon; Lenovo Flex 5 Ryzen 5 4500, 16GB memory
FPC: 3.2.0-1, Lazarus 2.0.12-0, all 64bit

Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: Compiler directive when in the IDE?
« Reply #1 on: July 13, 2020, 05:59:43 pm »
Both in Delphi and in FPC such a directive never existed.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

QEnnay

  • Full Member
  • ***
  • Posts: 115
Re: Compiler directive when in the IDE?
« Reply #2 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.
Linux-Mint 20.1 x64 + Cinnamon; Lenovo Flex 5 Ryzen 5 4500, 16GB memory
FPC: 3.2.0-1, Lazarus 2.0.12-0, all 64bit

dsiders

  • Hero Member
  • *****
  • Posts: 1078
Re: Compiler directive when in the IDE?
« Reply #3 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.  
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: Compiler directive when in the IDE?
« Reply #4 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.



-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Compiler directive when in the IDE?
« Reply #5 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)

jamie

  • Hero Member
  • *****
  • Posts: 6129
Re: Compiler directive when in the IDE?
« Reply #6 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.
The only true wisdom is knowing you know nothing

QEnnay

  • Full Member
  • ***
  • Posts: 115
Re: Compiler directive when in the IDE?
« Reply #7 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

Linux-Mint 20.1 x64 + Cinnamon; Lenovo Flex 5 Ryzen 5 4500, 16GB memory
FPC: 3.2.0-1, Lazarus 2.0.12-0, all 64bit

MarkMLl

  • Hero Member
  • *****
  • Posts: 6683
Re: Compiler directive when in the IDE?
« Reply #8 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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

jamie

  • Hero Member
  • *****
  • Posts: 6129
Re: Compiler directive when in the IDE?
« Reply #9 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..


The only true wisdom is knowing you know nothing

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11446
  • FPC developer.
Re: Compiler directive when in the IDE?
« Reply #10 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"

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Compiler directive when in the IDE?
« Reply #11 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?
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

jamie

  • Hero Member
  • *****
  • Posts: 6129
Re: Compiler directive when in the IDE?
« Reply #12 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.
The only true wisdom is knowing you know nothing

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Compiler directive when in the IDE?
« Reply #13 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
« Last Edit: July 13, 2020, 11:54:13 pm by Warfley »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Re: Compiler directive when in the IDE?
« Reply #14 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:

  • go to Tools -> Codetools Defines Editor
  • go to Edit -> Insert Node Below -> Define Recurse
  • enter the following values:
    • Name: InLazIDE
    • Description: Define InLazIDE everywhere
    • Variable: InLazIDE
    • Value from text: 1

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).

 

TinyPortal © 2005-2018