Search code examples
cmakensiscpack

How to remove files in the post-installation phase of an NSIS installer generated by CPack


My project's main CMakeLists.txt contains instructions to generate an NSIS installer.

One of these instructions is

set(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "
       nsExec::Exec '\\\"\\\$INSTDIR\\\\etc\\\\configure.cmd\\\" \\\"$INSTDIR\\\"'
       nsExec::Exec '\\\"\\\$INSTDIR\\\\etc\\\\win32_postinst.cmd\\\" \\\"$INSTDIR\\\"'
       Delete '\\\"\\\$INSTDIR\\\\etc\\\\configure.cmd\\\"'
       Delete '\\\"\\\$INSTDIR\\\\etc\\\\win32_postinst.cmd\\\"'
       ")

The goal is to complete some settings based on the installation directory with Windows CMD files. These files are no longer needed once the installation finishes and should be deleted.

In the generated .nsi file, these commands appear at the end of Section "-Core installation" :

        nsExec::Exec '"$INSTDIR\etc\configure.cmd" "$INSTDIR"'
        nsExec::Exec '"$INSTDIR\etc\win32_postinst.cmd" "$INSTDIR"'
        Delete '"$INSTDIR\etc\configure.cmd"'
        Delete '"$INSTDIR\etc\win32_postinst.cmd"'

The two nsExec::Exec commands are executed, but not the two Delete commands. Indeed, the two files configure.cmd and win32_postinst.cmd are still present after the installation completes.

Why is that and what is the correct procedure to have these two files removed?


Solution

  • You have too many quotes for the Delete instructions.

    The instructions that execute something (and take the full command line as a single parameter) needs to be quoted like '"app" param1 "par am 2" param' but commands that just take a single path only needs one pair of quotes; Delete "$InstDir\somefile.txt".

    The NSIS compiler strips out the outermost set of quotes, therefore Delete "$InstDir\somefile.txt" turns into Delete $InstDir\somefile.txt which is what you want (if there are no spaces you don't need the quotes in the first place).