Cmake调用NSIS(一个可执行文件,其实就是一个编译器)编译NSIS脚本问题研究

技术经理说,可以用Cmake当中的add_custom_command,add_custom_target命令来使用。

我初次研究了下,add_custom_command应该用官方文档中说明的第二种形式:

The second signature adds a custom command to a target such as a library or executable. This is useful for performing an operation before or after building the target. The command becomes part of the target and will only execute when the target itself is built. If the target is already built, the command will not execute.

  add_custom_command(TARGET target
PRE_BUILD | PRE_LINK | POST_BUILD
COMMAND command1 [ARGS] [args1...]
[COMMAND command2 [ARGS] [args2...] ...]
[WORKING_DIRECTORY dir]
[COMMENT comment] [VERBATIM])

This defines a new command that will be associated with building the specified target. When the command will happen is determined by which of the following is specified:

  PRE_BUILD - run before all other dependencies
PRE_LINK - run after other dependencies
POST_BUILD - run after the target has been built

find_program()find_path()这些命令估计有用。

NSIS相关:
NSIS.exe是NSIS带图形界面的编译器,实质上MakeNSISW.exe(一个接口)调用了MakeNSIS.exe来编译你的NSIS脚本文件。所以可以使用MakeNSIS
命令行方式来编译脚本文件。


以下是使用MakeNSIS.exe的官方文档:

3.1 MakeNSIS Usage

NSIS installers are generated by using the 'MakeNSIS' program to compile a NSIS script (.NSI) into an installer executable. The NSIS development kit installer sets up your computer so that you can compile a .nsi file by simply right-clicking on it in explorer, and selecting 'compile'.

If you want to use MakeNSIS on the command line, the syntax of the makensis command is:

makensis [option | script.nsi | - [...]]

3.1.1 Options

  • /LICENSE displays a keen license page.
  • The /V switch followed by a number between 0 and 4 will set the verbosity of output accordingly. 0=no output, 1=errors only, 2=warnings and errors, 3=info, warnings, and errors, 4=all output.
  • The /P switch followed by a number between 0 and 5 will set the priority of the compiler process accordingly. 0=idle, 1=below normal, 2=normal (default), 3=above normal, 4=high, 5=realtime.
  • The /O switch followed by a filename tells the compiler to print its log to that file (instead of the screen)
  • /PAUSE makes makensis pause before quitting, which is useful when executing directly from Windows.
  • /NOCONFIG disables inclusion of nsisconf.nsh. Without this parameter, installer defaults are set from nsisconf.nsh.
  • /CMDHELP prints basic usage information for command (if specified), or all commands (if command is not specified).
  • /HDRINFO prints out information on what options were used to compile makensis was compiled with.
  • /NOCD disables the current directory change to that of the .nsi file
  • Using the /D switch one or more times will add to symbols to the globally defined list (See !define).
  • Using the /X switch one or more times will execute the code you specify following it. Example: "/XAutoCloseWindow false"
  • Specifying a dash (-) for the script name will tell makensis to use the standard input as a source.

3.1.2 Notes

  • Parameters are processed by order. makensis /Ddef script.nsi is not the same as makensis script.nsi /Ddef.
  • If multiple scripts are specified, they are treated as one concatenated script.
  • On Windows 95, 98 and NT, below normal and above normal process priorities are not available. On those systems, below normal will actually set priority to idle and above normal will set to high.

3.1.3 Environment variables

makensis checks a number of environment variables that tell it where to locate the things it needs in order to create installers. These variables include:

  • NSISDIR, NSISCONFDIR - Places where nsis data and config files are installed. NSISDIR alters the script variable ${NSISDIR}. See section 4.2.3 for more info.
  • APPDATA (on Windows) or HOME (on other platforms) - Location of the per-user configuration file.

3.1.4 Examples

Basic usage:

makensis.exe myscript.nsi

Quiet mode:

makensis.exe /V1 myscript.nsi

Force compressor:

makensis.exe /X"SetCompressor /FINAL lzma" myscript.nsi

Change script behavior:

makensis.exe /DUSE_UPX /DVERSION=1.337 /DNO_IMAGES myscript.nsi

Parameters order:

makensis /XSection sectioncontents.nsi /XSectionEnd


****************************************************************************** 以下是一个CMakeLists.txt的实例
 1 cmake_minimum_required(VERSION 2.8)
 2 
 3 #set(CMAKE_C_COMPILER "D:\VS2008Release\VC\bin\amd64")
 4 #set(CMAKE_CXX_COMPILER "D:\VS2008Release\VC\bin\amd64")
 5 
 6 PROJECT(NSIS) 
 7 
 8 
 9 set( nsis_dir ${CMAKE_CURRENT_SOURCE_DIR}/NSIS)
10 add_custom_target(Setup ALL MakeNSIS.exe /V1 MyScript.nsi
11                     COMMENT begin Setup buiding...
12                     WORKING_DIRECTORY ${nsis_dir})

 

结论:
用add_custom_target()命令来实现此需求。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要编写一个可被其他 CMake 文件 `find_package()` 调用的包,需要完成以下步骤: 1. 创建一个文件夹用于存放你的包,例如 `MyPackage`。 2. 在 `MyPackage` 文件夹中创建 `CMakeLists.txt` 文件,用于编写构建逻辑。 3. 在 `MyPackage` 文件夹中创建一个 `MyPackageConfig.cmake.in` 文件,该文件定义了包的名称、版本号、安装路径等信息。 4. 在 `MyPackage` 文件夹中创建一个 `MyPackageTargets.cmake` 文件,该文件定义了包的编译目标。 5. 在 `MyPackage` 文件夹中创建一个 `include` 文件夹,用于存放包的公共头文件。 6. 在 `MyPackage` 文件夹中创建一个 `src` 文件夹,用于存放包的源代码。 下面是一个示例 `CMakeLists.txt` 文件: ```cmake cmake_minimum_required(VERSION 3.5) project(MyPackage VERSION 1.0.0 LANGUAGES CXX) # 设置包的安装路径 if(NOT CMAKE_INSTALL_PREFIX) set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Install path prefix" FORCE) endif() # 添加头文件路径 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) # 添加源码路径 add_subdirectory(src) # 安装包的配置文件 configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/MyPackageConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/MyPackageConfig.cmake" @ONLY ) # 安装包的配置文件和版本文件 install( FILES "${CMAKE_CURRENT_BINARY_DIR}/MyPackageConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/MyPackageConfigVersion.cmake" DESTINATION lib/cmake/MyPackage ) # 安装包的公共头文件 install( DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/" DESTINATION include ) # 导出包的编译目标 install( EXPORT MyPackageTargets FILE MyPackageTargets.cmake NAMESPACE MyPackage:: DESTINATION lib/cmake/MyPackage ) ``` `MyPackageConfig.cmake.in` 文件示例: ```cmake @PACKAGE_INIT@ set_and_check(MYPACKAGE_INCLUDE_DIR "@PACKAGE_INCLUDE_DIR@") set_and_check(MYPACKAGE_LIBRARY_DIR "@PACKAGE_LIBRARY_DIR@") set_and_check(MYPACKAGE_LIBRARIES "@PACKAGE_LIBRARIES@") set_and_check(MYPACKAGE_VERSION "@PACKAGE_VERSION@") include("${CMAKE_CURRENT_LIST_DIR}/MyPackageTargets.cmake") # 导出包的头文件路径和编译目标 set(MYPACKAGE_INCLUDE_DIRS "${MYPACKAGE_INCLUDE_DIR}") set(MYPACKAGE_TARGETS MyPackage::MyPackage) # 导出包的版本信息 set(MYPACKAGE_VERSION_MAJOR "@PACKAGE_VERSION_MAJOR@") set(MYPACKAGE_VERSION_MINOR "@PACKAGE_VERSION_MINOR@") set(MYPACKAGE_VERSION_PATCH "@PACKAGE_VERSION_PATCH@") set(MYPACKAGE_VERSION "${MYPACKAGE_VERSION_MAJOR}.${MYPACKAGE_VERSION_MINOR}.${MYPACKAGE_VERSION_PATCH}") ``` `MyPackageTargets.cmake` 文件示例: ```cmake add_library(MyPackage::MyPackage INTERFACE IMPORTED) set_target_properties(MyPackage::MyPackage PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${MYPACKAGE_INCLUDE_DIR}" INTERFACE_LINK_DIRECTORIES "${MYPACKAGE_LIBRARY_DIR}" ) target_link_libraries(MyPackage::MyPackage INTERFACE "${MYPACKAGE_LIBRARIES}") ``` 完成上述步骤后,你的包就可以被其他 CMake 文件 `find_package()` 调用了。例如,在其他项目中使用该包的方法如下: ```cmake find_package(MyPackage REQUIRED) # 使用 MyPackage 的头文件和库 target_include_directories(my_target PUBLIC ${MyPackage_INCLUDE_DIRS}) target_link_libraries(my_target PUBLIC MyPackage::MyPackage) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值