How to optionally include certain code for certain features?
Asked Answered
R

2

5

In Inno Setup, I have a main script which is the "core system", meaning everything which is absolutely needed for our software to install/run at all. Additionally, I'm writing script files for each major feature which may or may not be compiled into the installer. At the top of the main script file, I include other script files...

#include "AdditionalOption.iss"
#include "AnotherOption.iss"

When compiling this main script, the person compiling may choose whether or not to compile these certain options in the installer at all (to spare file size for various reasons).

The problem arises when I have specific code in the main script which depends on something in one of these additional scripts. For example...

procedure InitializeWizard();
begin
  //Creates custom wizard page only if "AdditionalOption.iss" is compiled
  CreatePageForAdditionalOption; 
  //Creates custom wizard page only if "AnotherOption.iss" is compiled
  CreatePageForAnotherOption; 
end;

InitializeWizard can only be defined once, but I need to call code in it conditionally depending on whether or not those other scripts were included. Those procedures reside in their appropriate script files, so of course they don't exist if user excluded that other script file.

In Delphi, I can use conditionals like so:

{$DEFINE ADD_OPT}
{$DEFINE ANO_OPT}

procedure InitializeWizard();
begin
  {$IFDEF ADD_OPT}
  CreatePageForAdditionalOption;
  {$ENDIF}
  {$IFDEF ANO_OPT}
  CreatePageForAnotherOption;
  {$ENDIF}
end;

Of course this isn't actually Delphi though. How can I do such a thing in Inno Setup?

Refund answered 3/2, 2014 at 21:34 Comment(0)
H
8

Inno Setup has a Preprocessor which enables you to make use of #ifdef, #else and #endif which you can set via the iscc.exe /D command line param(s). You can define multiple #ifdef's and set them via multiple /D's.

; Command line param => /DADD_OPT
#ifdef ADD_OPT
  ...
#else
  ...
#endif

I've used them to override default values:

; Command line param => /DENVIRONMENT=Prod
#ifdef ENVIRONMENT
  #define Environment ENVIRONMENT
#else
  #define Environment "Beta"
#endif
Hoe answered 3/2, 2014 at 22:44 Comment(4)
Hello. Thanks for the answer. The ansver is correct but I can't find this information on the official site's documents jrsoftware.org/ishelp . Can you please share link too. Thanks again.Looksee
Try this ... jrsoftware.org/ispphelp/index.php?topic=ifdefHoe
@Hoe The link doesn't mention #else though.Polyadelphous
This link has #else info ... jrsoftware.org/ispphelp/topic_if.htmHoe
R
3

A little bit more digging around and I figured it out. Just like my example above...

{$DEFINE ADD_OPT}
{$DEFINE ANO_OPT}

procedure InitializeWizard();
begin
  {$IFDEF ADD_OPT}
  CreatePageForAdditionalOption;
  {$ENDIF}
  {$IFDEF ANO_OPT}
  CreatePageForAnotherOption;
  {$ENDIF}
end;

It can be replicated in Inno Setup like so...

#define Public ADD_OPT
#define Public ANO_OPT

procedure InitializeWizard();
begin
  #ifdef ADD_OPT
  CreatePageForAdditionalOption;
  #endif
  #ifdef ANO_OPT
  CreatePageForAnotherOption;
  #endif
end;
Refund answered 3/2, 2014 at 22:30 Comment(2)
Good stuff ... just spend time doing something very similar.Hoe
Just like @Hoe shown in his answer. Moreover, he mentioned the use of /D parameter with which you can compile your setup conditionally by command line.Punchy

© 2022 - 2024 — McMap. All rights reserved.