预定义宏(Predefine)即无需用户赋值就可直接使用的宏
NSIS脚本中的预定义宏有如下六种:
1、${__FILE__}:Current script name.
输出当前脚本的名称(如Galatea.nsi)
2、${__LINE__}:Current line number.
输出当前代码在NSIS脚本中的行数
3、${__DATE__}:Date when the script started compiling according to the current locale.
输出当前日期(如2016/4/1)
4、${__TIME__}:Time when the script started compiling according to the current locale.
输出当前时间(如22:47:10)
5、${__TIMESTAMP__}:Date & time of the last modification to the script file according to the current locale.
输出当前日期和时间(如2016年4月1日 22:47:09),注意这里面有两个参数,因此如果直接使用
!echo ${__TIMESTAMP__}
在编译时就会报错:
!echo expects 1 parameters, got 2. Usage: !echo message Error in script "E:\NSIS_Test\galatea.nsi" on line 14 -- aborting creation process
因此应换一种写法来打印时间戳
!echo "${__TIMESTAMP__}"
6、${NSIS_VERSION}:NSIS version used to build the script.
当前NSIS编译器版本号(如v2.46)
用一段代码演示一下这六个预定义宏的功能:
!define DEBUG_PATH "E:\NSIS_Test\TmpProgram" !define OUTPUT_PATH "E:\NSIS_Test\Output" Name "NSIS_Predefines_Test" Caption "NSIS_Predefines_Test" Function .onInit ;TODO - 这里输入要测试的代码 !echo '------Predefines(START)------' !echo ${__FILE__} !echo ${__LINE__} !echo ${__DATE__} !echo ${__TIME__} !echo "${__TIMESTAMP__}" !echo ${NSIS_VERSION} !echo '------Predefines(FINISH)------' FunctionEnd OutFile "Galatea.exe" Section "My Program" SetOutPath ${OUTPUT_PATH} File /r "${DEBUG_PATH}\*.*" SectionEnd
这段代码编译时命令行的输出为:
Command line: "D:\NSIS\makensis.exe" /NOTIFYHWND 67520 "E:\NSIS_Test\galatea.nsi" MakeNSIS v2.46 - Copyright 1995-2009 Contributors See the file COPYING for license details. Credits can be found in the Users Manual. Processing config: Processing plugin dlls: "D:\NSIS\Plugins\*.dll" ( ... 此处省略若干行 ... ) !define: "MUI_INSERT_NSISCONF"="" Changing directory to: "E:\NSIS_Test" Processing script file: "E:\NSIS_Test\galatea.nsi" !define: "DEBUG_PATH"="E:\NSIS_Test\TmpProgram" !define: "OUTPUT_PATH"="E:\NSIS_Test\Output" Name: "NSIS_Predefines_Test" Caption: "NSIS_Predefines_Test" Function: ".onInit" ( ↓↓↓ 最重要的输出看这里 ↓↓↓ ) ------Predefines(START)------ (E:\NSIS_Test\galatea.nsi:9) galatea.nsi (E:\NSIS_Test\galatea.nsi:10) 11 (E:\NSIS_Test\galatea.nsi:11) 2016/4/1 (E:\NSIS_Test\galatea.nsi:12) 23:08:34 (E:\NSIS_Test\galatea.nsi:13) 2016年4月1日 23:08:34 (E:\NSIS_Test\galatea.nsi:14) v2.46 (E:\NSIS_Test\galatea.nsi:15) ------Predefines(FINISH)------ (E:\NSIS_Test\galatea.nsi:16) ( ↑↑↑ 最重要的输出看这里 ↑↑↑ ) FunctionEnd OutFile: "Galatea.exe" Section: "My Program" SetOutPath: "E:\NSIS_Test\Output" File: "MyProgram.exe" [compress] 0 bytes File: "ReadMe.txt" [compress] 6 bytes File: Returning to: "E:\NSIS_Test\TmpProgram" SectionEnd Processed 1 file, writing output: Processing pages... Done! Removing unused resources... Done! Generating language tables... Done! Output: "E:\NSIS_Test\Galatea.exe" Install: 2 pages (128 bytes), 1 section (1048 bytes), 7 instructions (196 bytes), 34 strings (602 bytes), 1 language table (198 bytes). Using zlib compression. EXE header size: 33280 / 35840 bytes Install code: 689 / 2500 bytes Install data: 14 / 14 bytes CRC (0xDB1FEB17): 4 / 4 bytes Total size: 33987 / 38358 bytes (88.6%)
另外,还有五个与范围有关的预定义宏(Scope Predefines):
1、${__GLOBAL__}:Defined in the global scope.
全局定义的宏,可用预处理命令 !ifdef、!ifndef、!endif 判断是否定义
2、${__SECTION__}:Defined as the section name, without any prefixes, in section scope.
如当前语句在一个Section块中,打印Section块名,可用于与其他字符串比较
3、${__FUNCTION__}:Defined as the function name, without any prefixes, in function scope.
如当前语句在一个Function块中,打印Function块名,可用于与其他字符串比较
4、${__PAGEEX__}:Defined as the page type in PageEx scope.
如当前语句在一个PageEx块中,打印PageEx块名,可用于与其他字符串比较
5、${__UNINSTALL__}:Defined in section, function or PageEx scopes of the uninstaller.
在卸载程序的Section、Function、RageEx中定义的宏,可用预处理命令 !ifdef、!ifndef、!endif 判断是否定义
注:本文写作过程中参考了NSIS官方使用手册 NSIS.chm
END