Search code examples
delphidelphi-10-seattle

Compiler define for Design Time package or code to check for execution in IDE


Are there any compiler defines or functions/constants/variables that can be used to tell whether some code is being built for a design time package or is being executed from inside the IDE?

We have some code hooks that we setup in the initialization of a unit which is part of a package. We don't want this code to be executed when installing the package in the IDE, only when it's running as part of our application.

For now I have added a compiler define to the design time package which strips out the code but I was wondering if there was a built in compiler define that indicates that this is part of a design time package or if there is some function/constant that can be checked to see if the code is running inside the IDE. Similar to the old if csDesigning in ComponentState then that people use from inside components.


Solution

  • Are there any compiler defines or functions/constants/variables that can be used to tell whether some code is being built for a design time package or is being executed from inside the IDE?

    No.

    We have some code hooks that we setup in the initialization of a unit which is part of a package. We don't want this code to be executed when installing the package in the IDE, only when it's running as part of our application.

    Then that code does not belong in the unit's initialization section at all. Move it to a separate function that your application code can call during its startup.

    For now I have added a compiler define to the design time package which strips out the code

    That implies that your design-time package is directly compiling your run-time code, which it should not be doing at all. Run-time code and design-time code need to be in separate packages. Run-time code does not belong in a design-time package, and design-time code does not belong in a run-time package.

    I was wondering if there was a built in compiler define that indicates that this is part of a design time package

    There is not. However, if you create separate run-time and design-time packages (as you should be), then your hook code belongs in the run-time package only. The design-time package can require the run-time package (so it has access to your run-time components), and the run-time package can expose a global variable that the design-time package can set. The run-time code can then ignore whatever it needs to ignore if that variable is set.