5.4 Conditional Compilation

The compiler maintains a list of defined symbols, which can be defined using !define or the /D command line switch. These defined symbols can be used for conditional compilation (using !ifdef) or for symbol replacement (a simple form of macros). To replace a symbol with its value, use ${SYMBOL} (if SYMBOL is not defined, no translation will occur). The translation is first-come-first-served, meaning if you do:

  1. !define symbol_one ${symbol_two}

If symbol_two is defined when that line occurs, it will be replaced. Otherwise, any replacing will occur when ${symbol_one} is referenced.

Define/conditional compilation related commands:

5.4.1 !define

  1. [/ifndef | /redef] ([/date|/utcdate] gflag [value]) | (/math gflag val1 OP val2) | (/file gflag filename.txt)

This command will add gflag to the global define list. This will have a similar effect as using the /D switch on the command line (the define only becomes effective after the !define command).

If /date or /utcdate are used, value will be passed to strftime() and the result will be used as the value of gflag. strftime converts special symbols into certain parts of the current time or date. For example, %H will be converted into the current hour in 24-hour format. For a complete list of available symbols, search for strftime on MSDN. On POSIX, you can get the list by using man strftime.

If /math is used, the result of 'val1 OP val2', where OP may be +,-,*,/,%,<<,>>,>>>,&,|,^,~,!,&& or ||, will be used as the value of gflag. Note that val1 AND val2 MUST be integer values!

If /file is used, the entire text file specified (including whitespace and newlines) will be read and stuffed into gflag.

  1. !define USE_SOMETHING
  2. !define VERSION 1.2
  3. !define /date NOW "%H:%M:%S %d %b, %Y"
  4. !define /math RESULT 3 + 10
  5. !define /math REST 15 % ${RESULT}
  6. !define /file BunchaStuff somesourcefile.cpp
  7. !define /redef USE_SOMETHING ${RESULT} ;redefine USE_SOMETHING

5.4.2 !undef

  1. [/noerrors] gflag [...]

Removes an item from the global define list. Note that ${SYMBOL} where SYMBOL is undefined will be translated to "${SYMBOL}".

  1. !define SOMETHING
  2. !undef SOMETHING

5.4.3 !ifdef

  1. gflag [bcheck gflag [...]]

This command, when paired with an !endif command, will tell the compiler whether or not to compile the lines in between the two lines. If gflag is globally defined (using !define or the /D switch), then the contained lines will be compiled. Otherwise, they will be skipped. 'bcheck' can be specified as & (boolean and) or | (boolean or) along with more gflags — precedence is simple, left to right.

  1. !define SOMETHING
  2. !ifdef SOMETHING
  3. !echo "SOMETHING is defined"
  4. !endif
  5. !undef SOMETHING
  6. !ifdef SOMETHING
  7. !echo "SOMETHING is defined" # will never be printed
  8. !endif

5.4.4 !ifndef

  1. gflag [bcheck gflag [...]]]

The opposite of !ifdef. The lines will be compiled when the gflag has not been defined.

5.4.5 !if

  1. [!] value [op value2]
  2. [!] /FileExists "c:\path\file.exe"

This command, when paired with an !endif command, will tell the compiler whether or not to compile the lines in between the two lines. If value is non-zero, or the comparison of value and value2 depending on the operator results in true, the contained lines will be compiled. Otherwise, they will be skipped. op can be either == or != (case-insensitive string comparison), S== or S!= (case-sensitive string comparison), =, <>, <=, <, > or >= (int/hex/float comparison), & (bitwise AND comparison), && or || (boolean comparison). If [!] is set, the result will be flipped from true to false and vice versa.

  1. !if 1 < 0x2
  2. !echo "1 is smaller than 2!!"
  3. !else if ! 3.1 > 1.99
  4. !error "this line should never appear"
  5. !else
  6. !error "neither should this"
  7. !endif
  1. !if /FileExists ".\cert.pfx"
  2. !finalize '".\sign.bat" "%1"'
  3. !endif

5.4.6 !ifmacrodef

  1. gflag [bcheck gflag [...]]]

This command, when paired with an !endif command, will tell the compiler whether or not to compile the lines in between the two lines. If the macro gflag exists, then the contained lines will be compiled. Otherwise, they will be skipped. 'bcheck' can be specified as & (boolean and) or | (boolean or) along with more gflags — precedence is simple, left to right.

  1. !macro SomeMacro
  2. !macroend
  3. !ifmacrodef SomeMacro
  4. !echo "SomeMacro is defined"
  5. !endif

5.4.7 !ifmacrondef

  1. gflag [bcheck gflag [...]]]

The opposite of !ifmacrodef. The lines will be compiled when the macro gflag does not exist.

5.4.8 !else

  1. [if|ifdef|ifndef|ifmacrodef|ifmacrondef [...]]

This command allows to easily insert different code when different defines or macros are set. You can create blocks like !ifdef/!else/!endif, !ifdef/!else ifdef/!else/!endif etc.

  1. !ifdef VERSION
  2. OutFile installer-${VERSION}.exe
  3. !else
  4. OutFile installer.exe
  5. !endif

5.4.9 !endif

This command closes a block started with !if, !ifdef, !ifndef, !ifmacrodef or !ifmacrondef.

5.4.10 !insertmacro

  1. macro_name [parameter] [...]

Inserts the contents of a macro that was created with !macro. If the macro was created with parameters, then you must pass as many parameters to the macro as it requires.

  1. !macro Print text
  2. DetailPrint "${text}"
  3. !macroend
  4. !insertmacro Print "some text"
  5. !insertmacro Print "some more text"

5.4.11 !macro

  1. macro_name [parameter][...]

Creates a macro named 'macro_name'. All lines between the !macro and the !macroend will be saved. To insert the macro later on, use !insertmacro. !macro definitions can have one or more parameters defined. The parameters may be accessed the same way a !define would (e.g. ${PARMNAME}) from inside the macro.

  1. !macro SomeMacro parm1 parm2 parm3
  2. DetailPrint "${parm1}"
  3. MessageBox MB_OK "${parm2}"
  4. File "${parm3}"
  5. !macroend

5.4.12 !macroend

Ends a macro that was started with !macro.

5.4.13 !macroundef

  1. macro_name

Deletes a macro.

5.4.14 !searchparse

  1. [/ignorecase] [/noerrors] [/file] source_string_or_file substring_start OUTPUTSYMBOL1 [substring [OUTPUTSYMBOL2 [substring ...]]]

Parses source_string_or_file (which is treated as a string, or as a filename if /file is set), looking for substring_start. If substring_start is found, then OUTPUTSYMBOL1 is defined to the rest of the string (minus any other substring that may be found). Any number of OUTPUTSYMBOLx may be specified, and the final substring is optional.

If /noerrors is specified, matching less than the full number of strings is allowed (all OUTPUTSYMBOLx after the not-found substring will be ignored).

If /file is specified, the file is treated as a series of lines. The file is searched until all substrings are matched. If /noerrors is specified and not all strings are matched, the first line with the most symbols matched is used.

  1. # search filename.cpp for a line '#define APP_VERSION "2.5"' and set ${VER_MAJOR} to 2, ${VER_MINOR} to 5.
  2. !searchparse /file filename.cpp `#define APP_VERSION "` VER_MAJOR `.` VER_MINOR `"`

5.4.15 !searchreplace

  1. [/ignorecase] symbol_out source_string searchfor replacewith

Searches source_string, looking for searchfor and replacing all instances of it with replacewith. Unlike !define, !searchreplace allows you to redefine symbol_out without warning or error.

  1. # defines ${blah} to "i like ponies"
  2. !searchreplace blah "i love ponies" "love" "like"