1

I am trying to customize an NSIS installer using CMAKE and CPACK. In particular I would like to include in the generated project.nsi an external script.

Something like:

!include "@SCRIPT_PATH@\@[email protected]"

To do so I am following the example shown here: https://gitlab.kitware.com/cmake/community/-/wikis/doc/cpack/NSISAdvancedTips

I have copied the template script file (NSIS.template.in) and added the required commands. I now need to configure the two variables "@SCRIPT_PATH@ and @SCRIPT_NAME@ accordingly.

I tried to set them like standard CMAKE variables

SET(SCRIPT_PATH "somePath")
SET(SCRIPT_NAME "someName")

but the template variables are simply left blank by CPACK

Any clue?

2 Answers 2

1

I have found a working solution.

Start by adding a placeholder in the main NSIS.template.in where you want to insert new commands, for instance:

@NSIS_ADDITIONAL_SCRIPT@

Then you need to configure a template file containing options that will be passed to the CPACK build.

The CPackOptions.cmake contains the following:

SET(NSIS_ADDITIONAL_SCRIPT ${NSIS_ADDITIONAL_SCRIPT})

Then in your CMAKE script before including CPACK you need to set the NSIS_ADDITIONAL_SCRIPT variable (the path is reworked in order to have the correct set of backslashes on window systems)

SET(scriptPath "[path to the script location]" )
FILE(TO_NATIVE_PATH ${scriptPath} scriptPath )
STRING(REPLACE "\\" "\\\\" scriptPath  ${scriptPath} ) 

and finally configure the CPackOption.cmake file (watch out for the correct number of slashes and comas)

SET(NSIS_ADDITIONAL_SCRIPT " \"!include \\\"${scriptPath}\\\" \\n  \" ")
CONFIGURE_FILE( ${CMAKE_SOURCE_DIR}/CMakeConf/CPackOptions.cmake ${PROJECT_BINARY_DIR}/CPackOptions.cmake)

If you do everything correctly:

  • by configuring and generating the CMAKE script you will find the file CPackOptions.cmake in your project build folder.
  • This configured file will be then used by CPACK to fill in the variable added in the NSIStemplate.in.
  • The final generated project.nsi will contain the additional !include instruction
2
  • Does this work for cmake 3.20? I tried, but the variable within the template.in file will be removed and stays blank
    – Simon Pio.
    Commented Jan 16, 2022 at 20:27
  • @SimonPio. It seems like a problem with variables not being escpaed properly. Can you give some details about your problem ? Commented Mar 3, 2023 at 8:15
0

I know this is old but it may help some folks out there:

The solution to your problem is to prefix your variables, SCRIPT_PATH and SCRIPT_NAME by CPACK. So that it will be CPACK_SCRIPT_PATH...

That way when CMake process your CmakeLists it knows that your variables should be set for CPack (in other word when generating CPackConfig.cmake).

Not the answer you're looking for? Browse other questions tagged or ask your own question.