Archive: ifdef environment variable


ifdef environment variable
Hello,

I have been using NSIS for years but I only used a very basic script (and that was good enough for me).

Now, my new nsi script is also simple except one requirement:
If a certain environment variable is set, then use it as
a root path. If not, a fallback path is used as a root path.

Assume the environment variable I'm interested in is "NSIS".
I tried the following (and many variants):

!define NSIS $%NSIS%

!ifdef NSIS
InstallDir $NSIS\foo\bar
!else
InstallDir C:\NSIS\foo\bar
!endif

//

It generated a warning and it didn't work.
Obviously I'm doing something stupid. But I couldn't
find any example that checks if the environment is set.
Of course, if I only use InstallDir C:\NSIS\foo\bar, then everything works fine.

Any suggestions, comments are greatly appreciated!

Best regards,
Aki Niimura


If the variable does not exist the define will be defined as $%NSIS%, without the variable expanded.

Try this:

!define NSIS $%NSIS%
!define "NSIS_CHECK_${NSIS}"

!ifndef NSIS_CHECK_$%NSIS%
InstallDir $%NSIS%\foo\bar
!else
InstallDir C:\NSIS\foo\bar
!endif

Hello Joost Verburg,

Thank you for your prompt response.
I tried but it didn't work as I hoped.

Now I realized a bigger problem.
I want to check the environment variable not at compilation but at the time when the installer is being executed.
Using !ifdef ... !else ... !endif clause is evaluated at compilation not at installation.

Probably this means I need to modify $INSTDIR value using StrCpy or something.

This seems beyond my poor understanding of NSIS tool but I will try tomorrow. I'm sleepy now.

Using an environment variable is a common trick in Unix but it seems not common to Windows Apps.

Aki-


If you want to check it during runtime, it's a lot simpler. Simply use ReadEnvStr or ExpandEnvStrings and check if you got a valid value or not. An invalid value would probably be an empty string. You will indeed have to copy the new value into $INSTDIR using StrCpy. You can do that in .onInit.


Hello,

Thank you for the suggestions.
Finally I made my .nsi script do as I intended.

> If you want to check it during runtime, it's a lot simpler.

Yes, indeed.
All tricks are (1) Use .onInit Function (2) Environment
variable is "" if not set.

I'm very glad this is over now.

Thank you for providing a wonderful tool.

Best regards,
Aki Niimura


For all who are interested in validating whether a environment variable is defined or not at compile time, here is what worked for me:

Function .onInit        #compile time validation

!define VERSION $%VERSION%
!define DOLLAR "$"

!if ${VERSION} == "${DOLLAR}%Version%"
!error "environment variable 'VERSION' not defined!"
!endif

Since ${VERSION} and $%VERSION% would always be the same, also when the environment variable is not defined, you simply define the "$" separately and concatenate them again in the static comparison. Otherwise the expression $%VERSION% always gonna be replaced by the value of the environment variable.