Qbs Quick Reference

From Qt Wiki
Revision as of 14:22, 23 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search


[toc align_right="yes" depth="2"]

Introduction

Qbs is the next-generation build system "initially introduced in the Qt Labs Blog":https://blog.qt.io/blog/2012/02/15/introducing-qbs/. This page is intended as a quick guide to porting project files from qmake .pro syntax to.qbs. It is not intended to supplant the official documentation, rather to be a quick summary of the current status of qbs functionality with a focus on how to port from qmake.

Some things at the time of writing have no equivalent qbs syntax. Bugtracker links are included for missing functionality, where known. And finally this is based on my incomplete current understanding of qbs, so there may be errors or indeed things which have changed by the time you read this :)

Qbs Manual

The full Qbs Manual is found at http://doc.qt.io/qbs

qbs equivalents

TEMPLATE = app

Use Application or CppApplication as the product:

CppApplication {<br /> name: &quot;helloworld&amp;quot;<br /> files: &quot;main.cpp&amp;quot;<br /> <br />}<br />

This is roughly equivalent to:

Product {<br /> name: &quot;helloworld&amp;quot;<br /> type: &quot;application&amp;quot;<br /> files: &quot;main.cpp&amp;quot;<br /> Depends { name: &quot;cpp&amp;quot; }<br /> <br />}<br />

except that in the first example, the type is "applicationbundle&quot; on OSX.

TEMPLATE = lib

Use DynamicLibrary as the product:

<br />DynamicLibrary {<br /> name: &quot;mydll&amp;quot;<br /> files: [&quot;stuff.cpp&amp;quot;]<br /> Depends { name: &quot;cpp&amp;quot; }<br /> <br />}<br />

TARGET = myappname

Use the "name&quot; property, see the TEMPLATE example above.

HEADERS, SOURCES, FORMS, RESOURCES

Include in files section, eg

files: ['thing.h', 'thing.cpp', 'thing.ui', 'myapp.qrc']<br />

qbs will use file taggers to figure out what kind of file it is dealing with.

== CONFIG = console


Application {<br /> name: &quot;helloworld&amp;quot;<br /> files: &quot;main.cpp&amp;quot;<br /> consoleApplication: true<br /> <br />}<br />


h2. CONFIG= designer_defines ==

DynamicLibrary {<br /> name: &quot;myplugin&amp;quot;<br /> files: [&quot;foo.cpp&amp;quot;, ]<br /> Depends { name: &quot;cpp&amp;quot; }<br /> cpp.defines: [&quot;QDESIGNER_EXPORT_WIDGETS&amp;quot;]<br /> <br />}<br />

== QT = modulename
Add an appropriate Depends section to the product. For example:


Product {<br /> Depends { name: &quot;Qt.core&amp;quot; }<br /> // …or…<br /> Depends { name: &quot;Qt&amp;quot;; submodules: [&quot;core&amp;quot;, &quot;gui&amp;quot;, &quot;network&amp;quot;] }<br />}<br />


Both forms are equivalent, the first form being quicker to type if you depend on just one module and the second more flexible if you have more complex dependencies.
h2. DEFINES= MACRO ==

Use the following: Note that in order to reference cpp.defines you must specify a dependency on the cpp module.

Depends { name: 'cpp' }<br /><br />cpp.defines: ['SUPPORT_COOL_STUFF']<br />

The cpp module might define default preprocessor macros. For example on Windows UNICODE is predefined.
These are stored in the property cpp.platformDefines.
To override these macros do:

Product {<br /> Depends { name: 'cpp' }<br /> <br /> cpp.platformDefines: ['MY_SPECIAL_DEFINE', 'UNICODE']<br />

To add macros within a group, you need to use outer.concat rather than base.concat(), because you are adding additional macros to what is specified in the outer scope:

Product {<br /> Depends { name: 'cpp' }<br /> <br /> cpp.defines: ['MACRO_EVERYWHERE'] // This is defined for all files in this product (unless a group overrides it!)<br /> Group {<br /> cpp.defines: outer.concat('MACRO_GROUP')<br /> files: groupFile.cpp<br /> // MACRO_GROUP is only defined in groupFile.cpp<br /> // MACRO_EVERYWHERE is also defined in groupFile.cpp because of the outer.concat<br /> }<br />}<br />

cpp.defines statements inside a group only apply to the files in that group - therefore you cannot use a group to include a bunch of files and globally-visible macros - the macros must go in a Properties block at the same level as the group if they need to be visible to files outside the group:

Product {<br /> Depends { name: 'cpp' }<br /> <br /> Group {<br /> condition: supportFoobar === true<br /> files: fooFile.cpp<br /> }

property stringList commonDefines: [&quot;ONE&amp;quot;, &quot;TWO&amp;quot;]<br /> Properties {<br /> condition: supportFoobar === true<br /> cpp.defines: commonDefines.concat(&quot;FOOBAR_SUPPORTED&amp;quot;)<br /> }<br /> Properties {<br /> cpp.defines: commonDefines // else case for the Properties chain<br /> }<br />}<br />

== INCLUDEPATH = dir


cpp.includePaths: [ '..', 'some/other/dir']<code>
<br />h2. CONFIG <s>= Qt
<br />Just don't declare Qt as a dependency. Probably you'd want:
<br />

Depends { name: "cpp&quot; }


h2. RC_FILE
Just add the file to the "files&quot; list.
h2. QMAKE_INFO_PLIST
Set the cpp.infoPlistFile property.
h2. ICON,
Not yet implemented. See "QBS-73&quot;:https://bugreports.qt.io/browse/QBS-73.
h2. TEMPLATE = subdirs
Inside a "Project&quot; item, use "references&quot;:


Project {<br /> references: [<br /> &quot;app/app.qbs&amp;quot;,<br /> &quot;lib/lib.qbs&amp;quot;<br /> ]<br />}<br />


h2. DESTDIR
Use the destinationDirectory property:


<br />DynamicLibrary {<br /> name: &quot;mydll&amp;quot;<br /> destinationDirectory: &quot;libDir&amp;quot;<br /> <br />}<br />


h2. message(), warning(), error()
You can use the JavaScript function print for printing messages and throw exceptions on the right hand side of property bindings.


Product {<br /> name: {<br /> print(&quot;</s>&amp;gt; now evaluating the product name&amp;quot;);<br /> return &quot;theName&amp;quot;;<br /> }<br /> Depends {name: &quot;cpp&amp;quot;}<br /> cpp.includePath: {<br /> throw &quot;I don't know. Something bad happened.&quot;<br /> return [];<br /> }<br />}


h2. Others not mentioned above
Either I've missed them, or they're not yet implemented.
h1. .pro and .pri
The top-level .qbs file contains the "Project&quot; definition. A project can contain multiple products, so you may find that multiple .pro files can be expressed in a single .qbs. The subdirs pattern will typically convert to a single .qbs containing references to multiple .qbs files. Each .qbs file would then define a single product or sub-project.
.qbs files can also be used like .pri files in that a top-level .qbs can include sections defined in another .qbs. For example:


<br /> CrazyProduct.qbs<br /> import qbs.base 1.0
<br /> Product {<br /> property string craziness: &quot;low&amp;quot;<br /> }
<br /> hellocrazyworld.qbs<br /> CrazyProduct {<br /> craziness: &quot;enormous&amp;quot;<br /> name: &quot;hellocrazyworld&amp;quot;<br /> // …<br /> }<br />


.qbs files in the same directory as the top-level .qbs file are picked up automatically. Others must be explicitly imported and named using an "import … as …" statement:


<br />import qbs.base 1.0<br />import &quot;../CrazyProduct.qbs&amp;quot; as CrazyProduct<br />CrazyProduct {<br /> craziness: &quot;enormous&amp;quot;<br /> name: &quot;hellocrazyworld&amp;quot;<br /> // …<br />}<br />


h1. Conditionals
Instead of the qmake syntax of "windows { … }" or "macx:…", you specify a "condition&quot; property in the relevant block. Conditionally-compiled files should be collected in a "Group&quot; block, while platform-specific properties should go in a "Properties&quot; block rather than being put in the main (outer) block:


<br />Group {<br /> condition: qbs.targetOS == &quot;windows&amp;quot;<br /> files: [<br /> &quot;harddiskdeleter_win.cpp&amp;quot;,<br /> &quot;blowupmonitor_win.cpp&amp;quot;,<br /> &quot;setkeyboardonfire_win.cpp&amp;quot;<br /> ]<br />}
<br />Properties {<br /> condition: qbs.targetOS == &quot;linux&amp;quot;<br /> cpp.defines: outer.concat([ &quot;USE_BUILTIN_DESTRUCTORS&amp;quot;])<br />}<br />


See the DEFINES section above for important information about how conditionals and cpp.defines interact.
h1. C+ compiler options ==

Here is a selection of options that are supported. The full list can be found in share/qbs/modules/cpp/CppModule.qbs in the qbs source tree, these are some of the more useful:

<br />cpp.optimization: &quot;none&amp;quot; // or &quot;fast&amp;quot;<br />cpp.debugInformation: true<br />cpp.staticLibraries: &quot;libraryName&amp;quot;<br />cpp.dynamicLibraries: &quot;libraryName&amp;quot;<br />cpp.frameworks: &quot;osxFrameworkName&amp;quot;<br />cpp.precompiledHeader: &quot;myheader.pch&amp;quot;<br />cpp.warningLevel: &quot;all&amp;quot; // or &quot;none&amp;quot;, &quot;default&amp;quot;<br />cpp.treatWarningsAsErrors: true<br />

Note that setting things like cflags directly is discouraged (because they are compiler-dependent), and higher-level alternatives like cpp.optimization: "fast&quot; should be used if available.

Installing files

Create a group containing the files, and set qbs.install and qbs.installDir:

<br />Group {<br /> qbs.install: true<br /> qbs.installDir: &quot;lib/myproj/&amp;quot;<br /> files: [<br /> &quot;Menu.qml&amp;quot;,<br /> &quot;SomeImportantFile.bin&amp;quot;<br /> ]<br />}<br />

For files generated by the build (e.g. an executable), you need to match them by their file tag:

<br />Group {<br /> qbs.install: true<br /> qbs.installDir: &quot;bin&amp;quot;<br /> fileTagsFilter: &quot;application&amp;quot;<br />}<br />

The installation happens by invoking the "qbs install&quot; command:

<br />$ qbs install install-root /tmp/myProject<br />

Command-line examples

64-bit:

qbs <s>f /path/to/project.qbs products productname qbs.architecture:x86_64<br />


h1. "Magic&quot; variables
Variables defined in various scopes, which may not be obvious:
h2. qbs
This has lots of useful things in, such as: targetOS ("windows&quot;, "linux&quot;, "macx&quot;, …); buildVariant ("debug&quot;, "release&quot;); architecture ("x86&quot;, "x86_64&quot;, …)
h2. project
Valid anywhere in your project, needed to refer to project properties from within a product:


Project {<br /> property string version: &quot;1.0&amp;quot;
<br /> Product {<br /> cpp.defines: [&quot;PROJECT_VERSION=&quot; + project.version]<br /> }<br />}<br />


h2. buildDirectory
The top-level build directory. By default will be a subdirectory in the directory where you invoked qbs from, whose name is derived from the current profile.
h2. Module names
Modules that are declared as dependencies can be referred to by their name and their properties accessed for example:

Product {<br /> Depends { name: &quot;Qt.quick&amp;quot; }<br /> Qt.quick.qmlDebugging: false<br />}<br />

Inside custom javascript

Generally when writing custom JavaScript logic, things are referenced through the top-level "product&quot; variable. In particular, to get the product's value of the property "baz&quot; of a module "foo&quot;, use product.moduleProperty("foo&quot;, "baz&quot;). For list values that get automatically combined, like includePaths, use product.moduleProperties("foo&quot;, "baz&quot;).

Group {<br /> condition: qbs.targetOS === &quot;windows&amp;quot;<br /> files: [ &quot;hcf.cpp&amp;quot; ]<br />}<br />

and

Group {
files: {
if (product.moduleProperty("qbs&quot;, "targetOS&quot;) === "windows&quot;) {
return [ "hcf.cpp&quot; ];
} else {
return [];
}
}
}