2
votes

NSIS is driving me crazy, I am not able to do something really straightforward. Following string comparison with if statement is not working. Could someone tell me what I am doing wrong?

!ifndef PRODUCT_NAME
    !define PRODUCT_NAME "$%PRODUCT_NAME%"
!endif

DetailPrint "TEST - ${PRODUCT_NAME}"
${If} ${PRODUCT_NAME} == "DEMO_NOT_FOR_RETAIL"
    DetailPrint "DEMO"
    File /r ${LOCAL_DIST_DIR_OUTPUT}\bin\win64
${Else}
    DetailPrint "NOT DEMO"
${EndIf}

Whatever what is the value of ${PRODUCT_NAME}, it enters in the if statement. PRODUCT_NAME is a environment variable set by my shell script just before calling my nsis script

EDIT As suggested by Anders, I tried (learnt) to use variables instead of define :

Var /Global PRODUCT_NAME
StrCpy $PRODUCT_NAME "$%PRODUCT_NAME%"

DetailPrint "TEST - $PRODUCT_NAME"
${If} $PRODUCT_NAME == "DEMO_NOT_FOR_RETAIL"
    DetailPrint "DEMO"
    File /r ${LOCAL_DIST_DIR_OUTPUT}\bin\win64
${Else}
    DetailPrint "NOT DEMO"
${EndIf}

Unfortunaly, does not work. See traces :

 Var: "PRODUCT_NAME"
StrCpy $PRODUCT_NAME "sdk" () ()
DetailPrint: "TEST - $PRODUCT_NAME"
!insertmacro: _If
!insertmacro: _PushLogic
!insertmacro: _PushScope
!define: "_Logic"="_LogicLib_Label_53"
!insertmacro: end of _PushScope
!insertmacro: _IncreaseCounter
!define: "_LOGICLIB_COUNTER"="53"
!undef: "LOGICLIB_COUNTER"
!define: "LOGICLIB_COUNTER"="54"
!undef: "_LOGICLIB_COUNTER"
!insertmacro: end of _IncreaseCounter
!insertmacro: end of _PushLogic
!define: "_LogicLib_Label_53If"=""
!define: "_LogicLib_Label_53Else"="_LogicLib_Label_54"
!insertmacro: _IncreaseCounter
!define: "_LOGICLIB_COUNTER"="54"
!undef: "LOGICLIB_COUNTER"
!define: "LOGICLIB_COUNTER"="55"
!undef: "_LOGICLIB_COUNTER"
!insertmacro: end of _IncreaseCounter
!define: "_c=true"=""
!insertmacro: _==
StrCmp "$PRODUCT_NAME" "DEMO_NOT_FOR_RETAIL" equal=, nonequal=_LogicLib_Label_54
2
What have you defined PRODUCT_NAME as? - Anders
@Anders PRODUCT_NAME=sdk in this case - peterphonic
Are you sure you are not mixing defines and variables? - Anders
@Anders might be, I dont know! I added code above. In the case it is a define, how do I check the value in a if statement? - peterphonic
@Anders My traces : StrCmp "sdk" "DEMO_NOT_FOR_RETAIL" equal=, nonequal=_LogicLib_Label_54. I feel that it is not normal that nonequl doesn't go to the else, is in it? - peterphonic

2 Answers

2
votes

Your code is a little bit strange, defines (${something}) are set at compile-time and you can compare them at compile-time with !if. If installing 64-bit things should be determined at run-time on the end users machine you have to use a variable ($something). While it is perfectly legal to use a define in a ${If} statement like your example does I would not really recommend it because it adds code that will never execute.

!include LogicLib.nsh


Section

; Using a define:
!define PRODUCT_NAME "blahblahDEMO_NOT_FOR_RETAIL"
!if "${PRODUCT_NAME}" == "DEMO_NOT_FOR_RETAIL"
    DetailPrint "DEMO"
!else
    DetailPrint "NOT DEMO"
!endif


; Using a variable:
Var /Global PRODUCT_NAME
StrCpy $PRODUCT_NAME "blahblahDEMO_NOT_FOR_RETAIL"

${If} $PRODUCT_NAME == "DEMO_NOT_FOR_RETAIL"
    DetailPrint "DEMO"
${Else}
    DetailPrint "NOT DEMO"
${EndIf}
SectionEnd

If you want to stick with the design you currently have you can actually view the generated comparison instruction and the values used at compile time by setting LOGICLIB_VERBOSITY to 4:

!define LOGICLIB_VERBOSITY 4
!include LogicLib.nsh

!define PRODUCT_NAME "somethingthatmakestheifstatementfalse"

Section
DetailPrint "|${PRODUCT_NAME}|"
${If} ${PRODUCT_NAME} == "DEMO_NOT_FOR_RETAIL"
    DetailPrint "DEMO"
${Else}
    DetailPrint "NOT DEMO"
${EndIf}
SectionEnd

As part of the compiler output I get this:

StrCmp "somethingthatmakestheifstatementfalse" "DEMO_NOT_FOR_RETAIL" equal=, nonequal=_LogicLib_ElseLabel_1

I still can't explain why it seems to always evaluate to true on your end.

0
votes

Thank you Anders! The advice to separate compiler-time if's from install-time if's was a life saver! This is a comment, not an answer, but Stack Overflow does not permit code formatting in Comments, and I am supplying a comment in case someone is Googling any of these or similar questions (as I was doing):

  • "NSIS Else statement not working"
  • "NSIS Else clause always executed"

I was using {If} on a compile-time define item but it was acting odd:

{If} MY_DEFINE:
  do_this
{Else}
  do_that
{EndIf}

When MY_DEFINE was defined, the above would do_this but it would also always do_that! It seems obvious now to separate compile-time & installer-time logic but NSIS is a tool that I rarely use & I have to re-learn it every time. This fixed it, per Anders' advice:

!if MY_DEFINE:
  do_this
!else
  do_that
!endif