\C{comptime} Compile Time Commands

\S0{compcommands} 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 process execution during the build process. Note: None of these commands allow the use of variables.

Number literals support the \c{0b}, \c{0o}, \c{0n} and \c{0x} radix prefixes (base 2, 8, 10 and 16 respectively). Note: The deprecated plain \c{0} octal prefix is also supported in some places but its usage is discouraged.

\S1{include} !include

\# NOTE: \NsisInputCharset define cannot be used in a \c block 

\c [/NONFATAL] [/CHARSET=ACP|OEM|CP#|UTF8|UTF16LE|UTF16BE] 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 \R{addincludedir}{!addincludedir} for more information. If the /nonfatal switch is used and no files are found, a warning will be issued instead of an error. /charset can be used to specify a codepage for plain text files without a BOM.

\c !include WinMessages.nsh
\c !include Library.nsh
\c !include /CHARSET=CP1252 C:\MyConfig.nsi
\c !include ..\MyConfig.nsh
\c !include /NONFATAL file_that_may_exist_or_not.nsh

\S1{addincludedir} !addincludedir

\c directory

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

\c !addincludedir ..\include
\c !include something.nsh

\S1{addplugindir} !addplugindir

\c [/x86-ansi | /x86-unicode] directory

Causes the NSIS compiler to scan the given directory for plug-in DLLs. If you don't specify the plug-in architecture it is assumed to match the current target architecture. If the architecture does not match the installer will probably crash!

\c !addplugindir ..\myplugin
\c MyPlugin::SomeFunction

\S1{appendfile} !appendfile

\# NOTE: \NsisOutputCharset define cannot be used in a \c block 

\c [/CHARSET=ACP|OEM|CP#|UTF8[SIG]|UTF16<LE|BE>[BOM]] [/RawNL] file text

Appends \e{text} to \e{file}. The text is written as ANSI (ACP) unless the file already has a BOM. Using /CHARSET will force a specific character encoding. \c{$\\n}  will be translated to \c{$\\r$\\n} on Windows unless you specify /RawNL.

\c !tempfile FILE
\c !appendfile "${FILE}" "XPStyle on$\n"
\c !appendfile "${FILE}" "Name 'test'$\n"
\c !include "${FILE}"
\c !delfile "${FILE}"
\c !undef FILE

\S1{cd} !cd

\c new_path

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

\c !cd ..\more-scripts\new

\S1{delfile} !delfile

\c [/nonfatal] file

This command deletes a file.

\c !tempfile FILE
\c !delfile "${FILE}"
\c !undef FILE

\S1{echo} !echo

\c message

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

\c !echo "hello world"

\S1{error} !error

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

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

\S1{execute} !execute

\c command [compare comparevalue | symbol]

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

On POSIX platforms, \R{execute}{!execute} will use system() just like \R{system}{!system}.

\c !execute '"$%WINDIR%\notepad.exe" /P "${NSISDIR}\COPYING"'

\S1{makensis} !makensis

\c parameters [compare comparevalue | symbol]

This command will \R{execute}{!execute} a new instance of MakeNSIS with the parameters you specify.

\c !makensis '-DGENERATEUNINST "${__FILE__}"' = 0
\c !system '"signtool" sign ...' = 0

\S1{packhdr} !packhdr

\c tempfile command

This option makes the compiler use an external EXE packer (such as \W{http://www.un4seen.com/petite/}{Petite} or \W{http://upx.sourceforge.net/}{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.

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

\S1{finalize} !finalize

\c command [compare comparevalue]

This option will execute 'command' using a call to system() after the output EXE has been generated. You can typically use it to sign (Authenticode) your installer. If 'command' contains a '%1' it will be replaced by the executables filename.

\c !finalize 'sign.bat "%1" "Product Installer" http://example.com'

\S1{system} !system

\c command [compare comparevalue | symbol]

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

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

\S1{tempfile} !tempfile

\c symbol

This command creates a temporary file. It puts its path into a define, named \e{symbol}.

\c !tempfile PACKHDRTEMP
\c !packhdr "${PACKHDRTEMP}" '"C:\Program Files\UPX\upx.exe" "${PACKHDRTEMP}"'

\c !tempfile FILE
\c !define /date DATE "%H:%M:%S %d %b, %Y"
\c !system 'echo built on ${DATE} > "${FILE}"'
\c !undef DATE
\c File /oname=build.txt "${FILE}"
\c !delfile "${FILE}"
\c !undef FILE

\S1{ppgetdllversion} !getdllversion

\c localfilename define_basename

This is similar to \R{getdllversionlocal}{GetDLLVersionLocal}, only it stores the version number in defines and can therefore be used anywhere, not just inside functions and sections.

\c !getdllversion "$%WINDIR%\Explorer.exe" expv_
\c !echo "Explorer.exe version is ${expv_1}.${expv_2}.${expv_3}.${expv_4}"

\S1{warning} !warning

\c [message]

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

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

\S1{pragma} !pragma

\c warning <enable|disable|default> code
\c warning <push|pop>

The pragma commands allows you to change compiler features and behavior.

\c !pragma warning disable 9000 ; Disable warning about using "Setup.exe" as the name
\c OutFile "Setup.exe"

\S1{verbose} !verbose

\c 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 \R{verbose}{!verbose} to push the current verbosity level on a special stack. Passing pop will cause \R{verbose}{!verbose} to pop the current verbosity level from the same stack and use it.

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

\S0{comppredefines} 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.

\S1{precounter} $\{__COUNTER__\}

Expands to a number (Starting at 0 and incrementing by 1 every time it is used)

\S1{prefile} $\{__FILE__\}

Current script name.

\S1{prefiledir} $\{__FILEDIR__\}

Current script directory.

\S1{preline} $\{__LINE__\}

Current line number.

\S1{predate} $\{__DATE__\}

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

\S1{pretime} $\{__TIME__\}

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

\S1{pretimestamp} $\{__TIMESTAMP__\}

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

\S1{prensisversion} $\{NSIS_VERSION\}

NSIS version used to build the script.

\S1{prensispackedversion} $\{NSIS_PACKEDVERSION\}

NSIS version as a 32-bit number.

\c !if 0x3014000 >= "${NSIS_PACKEDVERSION}"
\c   !error "NSIS 3.15 or higher is required to build this installer!"
\c !endif

\S1{prensischarsize} $\{NSIS_CHAR_SIZE\}

The size of a character code unit (in bytes). 1 in ANSI installers and 2 in Unicode installers.

A \W{http://unicode.org/glossary/#grapheme}{grapheme} cluster consists of a base character plus optional combining characters and diacritics and is defined as one or more code points. One or more code units is required to encode a single code point.

\S1{prensisptrsize} $\{NSIS_PTR_SIZE\}

The size of a pointer (in bytes) in the generated installer.

\S1{preunicodecodepoint} $\{U+1\}...$\{U+10FFFF\}

A Unicode (UCS-4) character.

\c DetailPrint "${U+2115}SIS" # DOUBLE-STRUCK CAPITAL N + "SIS"

\S1{scopepredefines} Scope Predefines

Standard predefines that contain information about the current code scope.

\S2{prescopeglobal} $\{__GLOBAL__\}

Defined in the global scope.

\c Section test
\c   !ifdef ${__GLOBAL__}
\c     !error "this shouldn't be here!"
\c   !endif
\c SectionEnd
\c
\c PageEx instfiles
\c   !ifdef ${__GLOBAL__}
\c     !error "this shouldn't be here!"
\c   !endif
\c PageExEnd

\S2{prescopesection} $\{__SECTION__\}

Defined as the section name, without any prefixes, in \R{ssection}{section} scope.

\c !ifdef __SECTION__
\c   !error "this shouldn't be here!"
\c !endif
\c 
\c Section test
\c   !ifndef __SECTION__
\c     !error "missing predefine!"
\c   !endif
\c
\c   !if ${__SECTION__} != test
\c     !error "wrong predefine value!"
\c   !endif
\c SectionEnd
\c
\c Section !test
\c   !if ${__SECTION__} != test
\c     !error "wrong predefine value!"
\c   !endif
\c SectionEnd
\c
\c Section un.test
\c   !if ${__SECTION__} != test
\c     !error "wrong predefine value!"
\c   !endif
\c SectionEnd

\S2{prescopefunction} $\{__FUNCTION__\}

Defined as the function name, without any prefixes, in \R{ffunction}{function} scope.

\c !ifdef __FUNCTION__
\c   !error "this shouldn't be here!"
\c !endif
\c 
\c Function test
\c   !ifndef __FUNCTION__
\c     !error "missing predefine!"
\c   !endif
\c
\c   !if ${__FUNCTION__} != test
\c     !error "wrong predefine value!"
\c   !endif
\c FunctionEnd
\c
\c Function un.test
\c   !if ${__FUNCTION__} != test
\c     !error "wrong predefine value!"
\c   !endif
\c FunctionEnd

\S2{prescopepageex} $\{__PAGEEX__\}

Defined as the page type in \R{pageex}{PageEx} scope.

\c !ifdef __PAGEEX__
\c   !error "this shouldn't be here!"
\c !endif
\c 
\c PageEx instfiles
\c   !ifndef __PAGEEX__
\c     !error "missing predefine!"
\c   !endif
\c
\c   !if ${__PAGEEX__} != instfiles
\c     !error "wrong page type"
\c   !endif
\c PageExEnd

\S2{prescopeuninstall} $\{__UNINSTALL__\}

Defined in \R{ssection}{section}, \R{ffunction}{function} or \R{pageex}{PageEx} scopes of the uninstaller.

\c !ifdef __UNINSTALL__
\c   !error "this shouldn't be here!"
\c !endif
\c 
\c Function test
\c   !ifdef __UNINSTALL__
\c     !error "this shouldn't be here!"
\c   !endif
\c FunctionEnd
\c 
\c Function un.test
\c   !ifndef __UNINSTALL__
\c     !error "missing predefine!"
\c   !endif
\c FunctionEnd

\S2{prescopemacro} $\{__MACRO__\}

Defined as the name of the current macro.

\S0{compenvvarread} Read environment variables

\S1{compenvvar} $%envVarName%

$%envVarName% will be replaced at compile time by the environment variable envVarName.
