Define Macro

From NSIS Wiki
Jump to navigationJump to search
Author: Zinthose (talk, contrib)


Summary

Pure Pre-compiler macro that enhances the !define command.

  • Once defined, value is locked and will not raise an error or be redefined. This is useful for assigning default values.
  • Recursive define trick to allow a define to assign itself.
  • ReDefine macro to reassign a define easily.

Usage

Command List

  • ${Define} _Name _Value
  • ${ReDefine} _Name _Value
  • ${DefineOnce} _Name
  • ${ReDefineOnce} _Name

Example

## Recursive Define trick for one time value setting
    ${DefineOnce} MSI_FilePath
 
    ${MSI_FilePath} `$TEMP`
    DetailPrint `MSI_FilePath = "${MSI_FilePath}"`
 
## Recursive ReDefine trick for one time value setting
    ${ReDefineOnce} MSI_FilePath
 
    ${MSI_FilePath} `$PROGRAMFILES`
    DetailPrint `MSI_FilePath = "${MSI_FilePath}"`
 
## Standard Define
    ${Define} MSI_FileName `Install.msi`
    DetailPrint `MSI_FileName = "${MSI_FileName}"`
 
## Redefine a previously defined value or create a new one
    ${ReDefine} MSI_FileName `Setup.msi`
    DetailPrint `MSI_FileName = "${MSI_FileName}"`
 
    ${ReDefine} MSI_FileName `Deployment.msi`
    DetailPrint `MSI_FileName = "${MSI_FileName}"`

Macro Source

!ifmacrondef _Define
    !ifndef Define
        !define Define `!insertmacro _Define`
        !macro _Define _Name _Value 
            !ifdef ${_Name}
                !if `${${_Name}}` == ``
                    !undef ${_Name}
                    !define ${_Name} `${_Value}`
                !else if `${${_Name}}` == `!insertmacro _Define ${_Name}`
                    !undef ${_Name}
                    !define ${_Name} `${_Value}`
                !endif
            !else
                !define ${_Name} `${_Value}`
            !endif
        !macroend
    !endif
!endif
 
!ifmacrondef _ReDefine
    !ifndef ReDefine
        !define ReDefine `!insertmacro _ReDefine`
        !macro _ReDefine _Name _Value 
            !ifdef ${_Name}
                !undef ${_Name}
            !endif
            !insertmacro _Define ${_Name} `${_Value}`
        !macroend
    !endif
!endif
 
!ifndef DefineOnce
    !define DefineOnce `!insertmacro _DefineOnce`
    !macro _DefineOnce _Name
        ${Define} ${_Name} `${Define} ${_Name}`
    !macroend
!endif
 
!ifndef ReDefineOnce
    !define ReDefineOnce `!insertmacro _ReDefineOnce`
    !macro _ReDefineOnce _Name
        ${ReDefine} ${_Name} `${ReDefine} ${_Name}`
    !macroend
!endif