Previous | Contents | Next

Chapter 5: Compile Time Commands

5.1 Compiler Utility Commands

These commands are similar to the C preprocessor in terms of purpose and functionality. They allow file inclusion, conditional compilation, executable header packing, and processes execution during the build process. Note: none of these commands allow use of variables.

5.1.1 !include

file

This command will include 'file' as if it was part of the original script. Note that if a file is included in another directory, the current directory is still where the script was compiled from (not where the included file resides). If the compiler can't find the file it will look for it in every include directory. See !addincludedir for more information.

!include WinMessages.nsh
!include Library.nsh
!include MyConfig.nsh
!include ..\MyConfig.nsh

5.1.2 !addincludedir

directory

Adds another include directory to the include directories list. This list is searched when !include is used. This list's initial value is ${NSISDIR}\Include alone.

!addincludedir ..\include
!include something.nsh

5.1.3 !addplugindir

directory

Causes the NSIS compiler to scan the given directory for Plugin DLLs.

!addplugindir myplugin
MyPlugin::SomeFunction

5.1.4 !cd

new_path

This command will change the compiler to the new directory, new_path. new_path can be relative or absolute.

!cd ..\more-scripts\new

5.1.5 !echo

message

This command will echo a message to the user compiling the script.

!echo "hello world"

5.1.6 !error

[message]

This command will issue an error to the script compiler and will stop execution of the script. You can also add a message to this error.

!ifdef VERSION & NOVERSION
  !error "both VERSION and NOVERSION are defined"
!endif

5.1.7 !execute

command

This command will execute 'command' using a call to CreateProcess(). Unlike !system, it does not use the command line processor, so input/output redirection and commands like 'cd', 'dir' and 'type' can not be used. !execute also ignores the return value of the executed command. Currently, the only known advantage of !execute over !system is that it does not give trouble when the current working directory is specified using UNC.

On POSIX platforms, !execute will use system() just like !system.

!execute '"%WINDIR%\notepad.exe" "${NSISDIR}\license.txt"'

5.1.8 !packhdr

tempfile command

This option makes the compiler use an external EXE packer (such as Petite or UPX) to compress the executable header. Specify a temporary file name (such as "temp.dat") and a command line (such as "C:\program files\upx\upx -9 temp.dat") to compress the header.

!packhdr "%TEMP%\exehead.tmp" '"C:\Program Files\UPX\upx.exe" "%TEMP%\exehead.tmp"'

5.1.9 !system

command [compare comparevalue]

This command will execute 'command' using a call to system(), and if the return value compared (using 'compare') to 'comparevalue' is false, execution will halt. 'compare' can be '<' or '>' or '<>' or '='.

!system '"%WINDIR%\notepad.exe" "${NSISDIR}\license.txt"'
!system 'echo !define something > newinclude.nsh'
!include newinclude.nsh
!ifdef something
  !echo "something is defined"
!endif

5.1.10 !warning

[message]

This command will issue a warning to the script compiler. You can also add a message to this warning.

!ifdef USE_DANGEROUS_STUFF
  !warning "using dangerous stuff"
!endif

5.1.11 !verbose

level | push | pop

This command will set the level of verbosity. 4=all, 3=no script, 2=no info, 1=no warnings, 0=none.

Passing push will cause !verbose to push the current verbosity level on a special stack. Passing pop will cause !verbose to pop the current verbosity level from the same stack and use it.

!verbose push
!verbose 1
!include WinMessages.nsh
!verbose pop

5.2 Predefines

You can use these standard predefines to automatically add the build time to the title of development versions, add the date to the version number, etc.

5.2.1 ${__FILE__}

Current script name.

5.2.2 ${__LINE__}

Current line number.

5.2.3 ${__DATE__}

Date when the script started compiling according to the current locale.

5.2.4 ${__TIME__}

Time when the script started compiling according to the current locale.

5.2.5 ${__TIMESTAMP__}

Date & time of the last modification to the script file according to the current locale.

5.3 Read enviroment variables

5.3.1 $%envVarName%

$%envVarName% will be replaced on compile time by the enviroment variable envVarName.

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:

!define symbol1 ${symbol2}

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

Define/conditional compilation related commands:

5.4.1 !define

[/date] gflag [value]

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 (only the define only becomes effective after the !define command).

If /date is used, value will be passed into 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.

!define USE_SOMETHING
!define VERSION 1.2
!define /date NOW "%H:%M:%S %d %b, %Y"

5.4.2 !undef

gflag

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

!define SOMETHING
!undef SOMETHING

5.4.3 !ifdef

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.

!define SOMETHING
!ifdef SOMETHING
  !echo "SOMETHING is defined"
!endif
!undef SOMETHING
!ifdef SOMETHING
  !echo "SOMETHING is defined" # will never be printed
!endif

5.4.4 !ifndef

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

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

5.4.5 !ifmacrodef

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.

!macro SomeMacro
!macroend
!ifmacrodef SomeMacro
  !echo "SomeMacro is defined"
!endif

5.4.6 !ifmacrondef

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

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

5.4.7 !else

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

!ifdef VERSION
OutFile installer-${VERSION}.exe
!else
OutFile installer.exe
!endif

5.4.8 !endif

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

5.4.9 !insertmacro

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.

!macro Print text
  DetailPrint "${text}"
!macroend
!insertmacro Print "some text"
!insertmacro Print "some more text"

5.4.10 !macro

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.

!macro SomeMacro parm1 parm2 parm3
  DetailPrint "${parm1}"
  MessageBox MB_OK "${parm2}"
  File "${parm3}"
!macroend

5.4.11 !macroend

Ends a macro that was started with !macro.

Previous | Contents | Next