bushmills
bushmills

Reputation: 673

QMake: test functions do not work as expected

In QtCreator 4.2.0 I try to use one *.pro file for building binaries for multiple hardware configurations.

In Build & Run => Build Settings => Build Enviroment I define the enviroment variable TARGET like as follows:

In the pro-file I use the following test functions:

equals($$TARGET,"bbb")
{
    message("setting include paths for bbb"))
    message($$TARGET)
}

equals($$TARGET,"laptop")
{
    message("setting include paths for laptop.")
    message($$TARGET)
}

contains($$TARGET,"*bbb*")
{
    message("setting include paths for bbb"))
    message($$TARGET)
}

contains($$TARGET,"*laptop*")
{
    message("setting include paths for laptop.")
    message($$TARGET)
}

And I get this output when running qmake:

Project MESSAGE: setting include paths for bbb
Project MESSAGE: bbb
Project MESSAGE: setting include paths for laptop.
Project MESSAGE: bbb
Project MESSAGE: setting include paths for bbb
Project MESSAGE: bbb
Project MESSAGE: setting include paths for laptop.
Project MESSAGE: bbb
Project MESSAGE: setting include paths for bbb

This makes no sense to me an I can't figure what I'm doing wrong here. Why are the parts after testing fro laptop executed?

By the way, I solved my problem by using Scopes. This works perfect for me:

CONFIG += $$(TARGET_HW)
desktop {
    message("setting include paths for laptop.")
}

cetec {
    message("setting include paths for cetec."))
}

But I'm still interested in the correct way of using test functions.

Upvotes: 2

Views: 933

Answers (3)

Former contributor
Former contributor

Reputation: 2576

There are so many issues in your question, that there is place for another answer, adding to the previous correct ones:

  1. as @daru says, you need to open the brace in the same line as the test function.

  2. as @p-a-o-l-o says, contains and equals syntax require variable names as first arguments, without $$.

  3. TARGET is an internal variable, that contains the base name of the project file by default. It becomes the name of the executable or library you are building.

  4. You may use an environment variable named TARGET, but then you should assign it to a qmake variable name with another name.

sample code:

TGT=$$(TARGET)

equals(TGT,"bbb") {
    message("$$TGT equals bbb"))
    message(TGT=$$TGT)
}

equals(TGT,"laptop") {
    message("$$TGT equals laptop")
    message(TGT=$$TGT)
}

contains(TGT,"bbb") {
    message("$$TGT contains bbb"))
    message(TGT=$$TGT)
}

contains(TGT,"top") {
    message("$$TGT contains top")
    message(TGT=$$TGT)
}

Upvotes: 2

daru
daru

Reputation: 76

The opening brace must written on the same line as the condition (https://doc.qt.io/qt-5/qmake-language.html#scope-syntax).

Upvotes: 2

p-a-o-l-o
p-a-o-l-o

Reputation: 10047

I provide the correct syntax for the first test, as an example:

equals(TARGET,"bbb") {
    message("setting include paths for bbb"))
    message($$TARGET)
}

Please notice:

  1. The curly brace is on the same line of the test.
  2. The variable tested has no dollar signs, just the variable name

Upvotes: 3

Related Questions