Test Functions

Test functions return a boolean value that you can test for in the conditional parts of scopes. Test functions can be divided into built-in functions and function libraries.

See also Replace Functions.

Built-in Test Functions

Basic test functions are implemented as built-in functions.

cache(variablename, [set|add|sub] [transient] [super], [source variablename])

This is an internal function that you will typically not need.

CONFIG(config)

This function can be used to test for variables placed into the CONFIG variable. This is the same as regular old style (tmake) scopes, but has the added advantage a second parameter can be passed to test for the active config. As the order of values is important in CONFIG variables (i.e. the last one set will be considered the active config for mutually exclusive values) a second parameter can be used to specify a set of values to consider. For example:

CONFIG = debug
CONFIG += release
CONFIG(release, debug|release):message(Release build!) #will print
CONFIG(debug, debug|release):message(Debug build!) #no print

Because release is considered the active setting (for feature parsing) it will be the CONFIG used to generate the build file. In the common case a second parameter is not needed, but for specific mutual exclusive tests it is invaluable.

contains(variablename, value)

Succeeds if the variable variablename contains the value value; otherwise fails. You can check the return value of this function using a scope.

For example:

contains( drivers, network ) {
    # drivers contains 'network'
    message( "Configuring for network build..." )
    HEADERS += network.h
    SOURCES += network.cpp
}

The contents of the scope are only processed if the drivers variable contains the value, network. If this is the case, the appropriate files are added to the SOURCES and HEADERS variables.

count(variablename, number)

Succeeds if the variable variablename contains a list with the specified number of value; otherwise fails.

This function is used to ensure that declarations inside a scope are only processed if the variable contains the correct number of values; for example:

options = $$find(CONFIG, "debug") $$find(CONFIG, "release")
count(options, 2) {
    message(Both release and debug specified.)
}

debug(level, message)

Checks whether qmake runs at the specified debug level. If yes, it returns true and prints a debug message.

defined(name[, type])

Tests whether the function or variable name is defined. If type is omitted, checks all functions. To check only variables or particular type of functions, specify type. It can have the following values:

  • test only checks test functions
  • replace only checks replace functions
  • var only checks variables

equals(variablename, value)

Tests whether variablename equals the string value.

For example:

TARGET = helloworld
equals(TARGET, "helloworld") {
    message("The target assignment was successful.")
}

error(string)

This function never returns a value. qmake displays the given string to the user, and exits. This function should only be used for unrecoverable errors.

For example:

error(An error has occurred in the configuration process.)

eval(string)

Evaluates the contents of the string using qmake's syntax rules and returns true. Definitions and assignments can be used in the string to modify the values of existing variables or create new definitions.

For example:

eval(TARGET = myapp) {
    message($$TARGET)
}

Note that quotation marks can be used to delimit the string, and that the return value can be discarded if it is not needed.

exists(filename)

Tests whether a file with the given filename exists. If the file exists, the function succeeds; otherwise it fails. If a regular expression is specified for the filename, this function succeeds if any file matches the regular expression specified.

For example:

exists( $(QTDIR)/lib/libqt-mt* ) {
      message( "Configuring for multi-threaded Qt..." )
      CONFIG += thread
}

Note that "/" can be used as a directory separator, regardless of the platform in use.

export(variablename)

Exports the current value of variablename from the local context of a function to the global context.

files(pattern[, recursive=false])

Expands the specified wildcard pattern and returns a list of filenames. If recursive is true, this function descends into subdirectories.

for(iterate, list)

This special test function will cause a loop to be started that iterates over all values in list, setting iterate to each value in turn. As a convenience, if list is 1..10 then iterate will iterate over the values 1 through 10.

The use of an else scope afer a condition line with a for() loop is disallowed.

For example:

LIST = 1 2 3
for(a, LIST):exists(file.$${a}):message(I see a file.$${a}!)

greaterThan(variablename, value)

Tests that the value of variablename is greater than value. First, this function attempts a numerical comparison. If at least one of the operands fails to convert, this function does a string comparison.

For example:

ANSWER = 42
greaterThan(ANSWER, 1) {
    message("The answer might be correct.")
}

It is impossible to compare two numbers as strings directly. As a workaround, construct temporary values with a non-numeric prefix and compare these.

For example:

VALUE = 123
TMP_VALUE = x$$VALUE
greaterThan(TMP_VALUE, x456): message("Condition may be true.")

See also lessThan().

if(condition)

This test function evaluates condition. It is used to group boolean expressions.

For example:

if(linux-g++*|macx-g++*):CONFIG(debug, debug|release) {
    message("We are on Linux or Mac OS, and we are in debug mode.")
}

include(filename)

Includes the contents of the file specified by filename into the current project at the point where it is included. This function succeeds if filename is included; otherwise it fails. The included file is processed immediately.

You can check whether the file was included by using this function as the condition for a scope; for example:

include( shared.pri )
OPTIONS = standard custom
!include( options.pri ) {
    message( "No custom build options specified" )
OPTIONS -= custom
}

infile(filename, var, val)

Succeeds if the file filename (when parsed by qmake itself) contains the variable var with a value of val; otherwise fails. If you do not specify a third argument (val), the function will only test whether var has been declared in the file.

isActiveConfig

This is an alias for the CONFIG function.

isEmpty(variablename)

Succeeds if the variable variablename is empty; otherwise fails. This is the equivalent of count( variablename, 0 ).

For example:

isEmpty( CONFIG ) {
CONFIG += warn_on debug
}

isEqual

This is an alias for the equals function.

lessThan(variablename, value)

Tests that the value of variablename is less than value. Works as greaterThan().

For example:

ANSWER = 42
lessThan(ANSWER, 1) {
    message("The answer might be wrong.")
}

load(feature)

This function loads the feature file (.prf) specified by feature, unless the feature has already been loaded.

log(message)

Prints a message on the console. Unlike the message function, neither prepends text nor appends a line break.

See also message().

message(string)

This function simply writes a message to the console. Unlike the error() function, this function allows processing to continue.

message( "This is a message" )

The above line causes "This is a message" to be written to the console. The use of quotation marks is optional.

Note: By default, messages are written out for each Makefile generated by qmake for a given project. If you want to ensure that messages only appear once for each project, test the build_pass variable in conjunction with a scope to filter out messages during builds; for example:

!build_pass:message( "This is a message" )

mkpath(dirPath)

Creates the directory path dirPath. This function is a wrapper around the QDir::makepath function.

requires(condition)

Evaluates condition. If the condition is false, qmake skips this project (and its SUBDIRS) when building.

Note: You can also use the REQUIRES variable for this purpose. However, we recommend using this function, instead.

See also REQUIRES.

system(command)

Executes the given command in a secondary shell. Succeeds if the command returns with a zero exit status; otherwise fails. You can check the return value of this function using a scope:

For example:

system(ls /bin):HAS_BIN=FALSE

See also the replace variant of system().

touch(filename, reference_filename)

Updates the time stamp of filename to match the time stamp of reference_filename.

unset(variablename)

Removes variablename from the current context.

For example:

NARF = zort
unset(NARF)
!defined(NARF, var) {
    message("NARF is not defined.")
}

warning(string)

This function will always succeed, and will display the given string to the user. message() is a synonym for warning().

write_file(filename, [variablename, [mode]])

Writes the values of variablename to a file with the name filename, each value on a separate line. If variablename is not specified, creates an empty file. If mode is append and the file already exists, appends to it instead of replacing it.

Test Function Library

Complex test functions are implemented in a library of .prf files.

packagesExist(packages)

Uses the PKGCONFIG mechanism to determine whether or not the given packages exist at the time of project parsing.

This can be useful to optionally enable or disable features. For example:

packagesExist(sqlite3 QtNetwork QtDeclarative) {
    DEFINES += USE_FANCY_UI
}

And then, in the code:

#ifdef USE_FANCY_UI
    // Use the fancy UI, as we have extra packages available
#endif