13

How can you test if a variable is empty or not defined in a qmake .pro file? I want to be able to set up a default value if the variable is not defined.

I tried

eval("VARIABLE" = ""){
     VARIABLE = test
}

eval("VARIABLE" = ""){
     message(variable is empty)
}

but I still get the message "variable is empty".

UmNyobe
  • 22,539
  • 9
  • 61
  • 90

2 Answers2

19

there is already the function isEmpty I didn't spot:

isEmpty(VARIABLE){
  VARIABLE = test
}    
isEmpty(VARIABLE ){
  message(variable is empty)
}

I don't understand why eval didnt work thought...

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • It treats the quotation marks as literals for paths and tries to escape them in Qt 5.6. It does the same thing to parentheses if you don't wrap variables intended to be paths with curly-braces and the `$$` on the outside-left. – kayleeFrye_onDeck May 21 '16 at 00:26
8

Like your own answer says, isEmpty(VARIABLE) does what you want:

isEmpty(VARIABLE) {
    ...
}

The qmake language has no equivalent of an equals operator (==), but you can compare things like this:

equals(VARIABLE, foo) {
    ...
}

You can also check if a variable contains a substring, using a regular expression:

contains(VARIABLE, .*foo.*) {
    ...
}

The reason why eval() didn't work, is that it executes the statement within it and returns true if the statement succeeded.

So by doing this:

eval(VARIABLE = "") {
    ...
}

...you are actually assigning "" to VARIABLE, making the variable empty and entering the block.

More about test functions: http://qt-project.org/doc/qt-5/qmake-test-function-reference.html

Kankaristo
  • 2,515
  • 2
  • 20
  • 16