Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

test if a variable is empty and give default value during qmake

Tags:

qt

qmake

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".

like image 382
UmNyobe Avatar asked Dec 10 '12 15:12

UmNyobe


2 Answers

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...

like image 116
UmNyobe Avatar answered Sep 30 '22 04:09

UmNyobe


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

like image 32
Kankaristo Avatar answered Sep 30 '22 02:09

Kankaristo