Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

[Prev: qmake Concepts] [Home] [Next: Using Precompiled Headers]

qmake's Advanced Concepts

qmake's Advanced Concepts

The qmake project files we've seen up to now have been very simple, just a list of name = value and name += value lines. qmake provides a lot more power, for example you can use a single project file to produce makefiles for multiple platforms.

Operators

So far, you have seen the = operator and += operator being used in a project file. There are more operators available for use; but some of these should be used carefully as they may change more than you expect them to.

The '=' operator

This operator simply assigns a value to a variable, it is used like this:

    TARGET = myapp

This sets the TARGET variable to myapp. This will remove any previously set TARGET.

The '+=' operator

This operator appends a value to the list of values in a variable. It is used like this:

    DEFINES += QT_DLL

This appends QT_DLL to the list of pre-processor defines to be put in the makefile.

The '-=' operator

This operator removes a value from the list of values in a variable. It is used like this:

    DEFINES -= QT_DLL

This removes QT_DLL from the list of pre-processor defines to be put in the makefile.

The '*=' operator

This operator only adds a value to the list of values in a variable if it doesn't already exist. It is used like this:

    DEFINES *= QT_DLL

QT_DLL will only be added to the list of pre-processor defines if it is not already defined.

The '~=' operator

This operator replaces any values that match the regexp with the specified value. It is used like this:

    DEFINES ~= s/QT_[DT].+/QT

This removes any values in the list that start with QT_D or QT_T with QT.

Scopes

A scope are similar to 'if' statements, if a certain condition is true, the settings inside the scope are processed. A scope is written like this:

    win32 {
        DEFINES += QT_DLL
    }

The above code will add the QT_DLL define to the makefile if qmake is used on a Windows platform. If qmake is used on a different platform than Windows, the define will be ignored. You may also perform single line conditionals/assignments with qmake like this:

    win32:DEFINES += QT_DLL

For example, suppose we want to process something on all platforms except for Windows. We can achieve this by negating the scope like this:

    !win32 {
        DEFINES += QT_DLL
    }

Any entry on the CONFIG line is also a scope. For example, if you write this:

    CONFIG += warn_on

you will have a scope called 'warn_on'. This makes it easy to change the configuration for a project without losing all the custom settings that might be needed for a specific configuration. Since it is possible to put your own values on the CONFIG line, this provides you with a very powerful configuration tool for your makefiles. For example:

    CONFIG += qt warn_on debug
    debug {
        TARGET = myappdebug
    }
    release {
        TARGET = myapp
    }

In the above code, two scopes are created which depend on what is put on the CONFIG line. In the example, debug is on the config line, so the TARGET variable is set to myappdebug. If release was on the config line, then the TARGET variable would be set to myapp.

It is also possible to check for two things before processing some settings. For instance, if you want to check if the platform is Windows and that the thread configuration is set, you would write this:

    win32 {
        thread {
            DEFINES += QT_THREAD_SUPPORT
        }
    }

To save writing many nested scopes, you can nest scopes using a colon like this:

    win32:thread {
        DEFINES += QT_THREAD_SUPPORT
    }

Once a test has been performed you may also do else/elseif operations. With this you may easily write complicated tests. This can be done with the special 'else' scope, it can be combined with other scopes (separated by colons as above) for example:

    win32:thread {
        DEFINES += QT_THREAD_SUPPORT
    } else:debug {
        DEFINES += QT_NOTHREAD_DEBUG
    } else {
        message("Unknown configuration")
    }

Variables

The variables that we have encountered so far are system variables, such as DEFINES, SOURCES and HEADERS. It is possible for you to create your own variables so that you use them in scopes. It's easy to create your own variable; just name it and assign something to it. For example:

    MY_VARIABLE = value

There are no restricitions on what you do to your own variables, as qmake will just ignore them unless it needs to look at them for a scope.

You can also assign the value of a current variable to another variable by prefixing $$ to the variable name. For example:

    MY_DEFINES = $$DEFINES

Now the MY_DEFINES variable contains what is in the DEFINES variable at this point in the project file. This is also equivalent to:

 
    MY_DEFINES = $${DEFINES}

The second notation allows you to adjoin the variable expansion to another value without separating by space. qmake will allow a variable to contain anything (including $(VALUE), which will be placed directly into the Makefile, and allow it to expand as appropriate, usually an environment variable). However, if you require an environment variable to be replaced immediately then you may use the $$() notation. For example:

    MY_DEFINES = $$(ENV_DEFINES)

This will set MY_DEFINES to the value of the evironment variable ENV_DEFINES as it parses the .pro file. Additionally you may call built-in functions in variable replacing. These functions (not to be confused with Test Functions as enumerated in the next section) are listed below:

join( variablename, glue, before, after )

This will join the value of variablename with glue. If this value is non-empty it will prefix the value with before and suffix it with after. variablename is the only required field, the others will default to empty strings. If you need to encode spaces in glue, before, or after you must quote them.

prompt( question )

This will display question, and read from stdin as a return value.

member( variablename, position )

This will place the value in variablename in position position of the list. If the value of variablename is not long this will return an empty string. variablename is the only required field, if not specified position will default to the first value in the list (0).

find( variablename, substr )

This will place all the values in variablename that match substr. substr may be a regular expression as well, and will be matched accordingly.

    MY_VAR = one two three four
    MY_VAR2 = $$join(MY_VAR, " -L", -L) -Lfive
    MY_VAR3 = $$member(MY_VAR, 2) $$find(MY_VAR, t.*)

MY_VAR2 will contain '-Lone -Ltwo -Lthree -Lfour -Lfive', and MYVAR3 will contains 'three two three'.

system( program_and_args )

This will return the stdout/stderr of the program executed, and parse it as normally expected. You can use this to interrogate information about the platform for example.

    UNAME = $$system(uname -s)
    contains( UNAME, [lL]inux ):message( This looks like Linux ($$UNAME) to me )

Test Functions

qmake provides built-in functions that perform simple, yet powerful tests. These tests may be used in place of scopes (as described above), in some cases it is more usefull to use the test function by itself ignoring its test value.

contains( variablename, value )

If value is in the list of values stored in the variable called variablename, then the settings inside the scope will be processed. For example:

    contains( CONFIG, thread ) {
        DEFINES += QT_THREAD_SUPPORT
    }

If thread is in the list of values for the CONFIG variable, then QT_THREAD_SUPPORT will be added to the list of values in the DEFINES variable.

count( variablename, number )

If number matches the number of values stored in the variable called variablename, then the settings inside the scope will be processed. For example:

    count( DEFINES, 5 ) {
        CONFIG += debug
    }

error( string )

This function outputs the string given and then makes qmake exit. For example:

    error( "An error has occured" )

The text "An error has occured" will be displayed on the console and qmake will exit.

exists( filename )

If the specified file exists, then the settings inside the scope will be processed. For example:

    exists( /local/qt/qmake/main.cpp ) {
        SOURCES += main.cpp
    }

If /local/qt/qmake/main.cpp exists then main.cpp is added to the list of source files.

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

equals( variable, value )

If the specified variable is equal to the value passed the scope will be processed. For example:

    NUMBERS = 1 2 3
    equals( NUMBERS, 3 4 5 ) {
         message("The numbers are equal")
    }

The message will not be displayed because "1 2 3" does not equal "1 2 3". As with all functions you can pass an expanded variable as the value argument (ie, $$NUMBERS).

include( filename )

The contents of filename are included at this point in the project file, so any settings in the specified file will be processed. An example of this is:

 
    include( myotherapp.pro )

Any settings in the myotherapp.pro project file are now processed.

isEmpty( variablename )

This is the equivalent of using count( variablename, 0 ). If the variable called variablename has no elements, then the settings inside the scope will be processed. An example of this is:

    isEmpty( CONFIG ) {
        CONFIG += qt warn_on debug
    }

message( string )

This function simply outputs a message on the console.

    message( "This is a message" )

The text "This is a message" is output to the console and processing of the project file carries on.

system( command )

The specified command is performed and if it returns an exit code of 1, the settings inside the scope are processed. For example:

    system( ls /bin ) {
        SOURCES += bin/main.cpp
        HEADERS += bin/main.h
    }

So if the command ls /bin returns 1 then bin/main.cpp is added to the list of sources and bin/main.h is added to the list of headers.

infile( filename, var, val )

This function will succeed if the file filename (when parsed by qmake itself) contains the variable var with a value of val. You may also not pass in a third argument (val) and the function will only test if var has been assigned to in the file.

[Prev: qmake Concepts] [Home] [Next: Using Precompiled Headers]


Copyright © 2005 TrolltechTrademarks
Qt 3.3.6