Search code examples
cmakewixcpack

How to get current user environment variable while installing a cMake + WIX installer


I'm using cMake + Wix to generate my installer. My program must be installed in C:\Users\UserName\MyProgram of any computer.

The problem is that when I generate an installer with cPack, I am setting the installation directory like this:

set(CPACK_WIX_SKIP_PROGRAM_FOLDER TRUE)
set(CPACK_PACKAGE_INSTALL_DIRECTORY $ENV{USERPROFILE}\\${CPACK_PACKAGE_VENDOR}\\${CPACK_PACKAGE_NAME})

When I generate the installer and give it to the client to install my program, the program tries to install under the "old" username (the username of the machine where the installer was generated).

C:\Users\OldUserName\MyProgram

This username will not be recognized on the machine that is being installed since it does not exist, since it is the user of the machine where the installer was generated, not the current one.

Why is this happening? Because it is stored as an installation path. I don't know how to tell WIX to take the "Current User" of the machine where the installer is running to generate the installation path.


Solution

  • I do not have the wix + cpack pipeline setup, but found this question interesting. Maybe these information might help.

    • I think CPACK_WIX_SKIP_PROGRAM_FOLDER might not be the correct thing to use. ref

    Note Installers created with this feature do not take differences between the system on which the installer is created and the system on which the installer might be used into account. It is therefore possible that the installer e.g. might try to install onto a drive that is unavailable or unintended or a path that does not follow the localization or convention of the system on which the installation is performed.

    • For windows, in the general case, CPACK_SET_DESTDIR should be set to false and CPACK_PACKAGING_INSTALL_PREFIX can be used to set the installation root ref.
    • But also for the general CPack way (CPACK_PACKAGING_INSTALL_PREFIX), it will only be evaluated at configure time ref. It is also possible to not evaluate/escape CMake variables, but that will not help here, because I would expect wix to not be able to handle CMake paths. So I don’t know if the default of CPACK_PACKAGING_INSTALL_PREFIX, which should be \user will already work or if that does not apply for windows.
    • Here ref they purpose to use %APPDATA% in the path, I checked that CMake will not evaluate that directly, but I am not sure if wix can handle it.