Qbs Quick Reference: Difference between revisions
No edit summary |
(Cleanup) |
||
Line 1: | Line 1: | ||
{{LangSwitch}} | |||
[[Category:Tools::qbs]] | [[Category:Tools::qbs]] | ||
= Introduction = | == Introduction == | ||
Qbs is the next-generation build system "initially introduced in the [https://blog.qt.io/blog/2012/02/15/introducing-qbs/ Qt Labs Blog]. 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. | Qbs is the next-generation build system "initially introduced in the [https://blog.qt.io/blog/2012/02/15/introducing-qbs/ Qt Labs Blog]. 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. | ||
Line 7: | Line 8: | ||
Some things at the time of writing have no equivalent qbs syntax. Bugtracker links are included for missing functionality, where known. | Some things at the time of writing have no equivalent qbs syntax. Bugtracker links are included for missing functionality, where known. | ||
= Qbs Manual = | == Qbs Manual == | ||
The full Qbs Manual is found at http://doc.qt.io/qbs | The full Qbs Manual is found at http://doc.qt.io/qbs | ||
= qbs equivalents = | == qbs equivalents == | ||
== TEMPLATE = app == | === TEMPLATE = app === | ||
Use Application or CppApplication as the product: | Use Application or CppApplication as the product: | ||
Line 36: | Line 37: | ||
== TEMPLATE = lib == | === TEMPLATE = lib === | ||
Use DynamicLibrary as the product: | Use DynamicLibrary as the product: | ||
Line 49: | Line 50: | ||
</code> | </code> | ||
== TARGET = myappname == | === TARGET = myappname === | ||
Use the "name" property, see the TEMPLATE example above. | Use the "name" property, see the TEMPLATE example above. | ||
== HEADERS, SOURCES, FORMS, RESOURCES == | === HEADERS, SOURCES, FORMS, RESOURCES === | ||
Include in files section, eg | Include in files section, eg | ||
Line 62: | Line 63: | ||
qbs will use file taggers to figure out what kind of file it is dealing with. | qbs will use file taggers to figure out what kind of file it is dealing with. | ||
== CONFIG = console == | === CONFIG = console === | ||
<code>Application { | <code>Application { | ||
Line 72: | Line 73: | ||
</code> | </code> | ||
== CONFIG = designer_defines == | === CONFIG = designer_defines === | ||
<code>DynamicLibrary { | <code>DynamicLibrary { | ||
Line 83: | Line 84: | ||
</code> | </code> | ||
== QT = modulename == | === QT = modulename === | ||
Add an appropriate Depends section to the product. For example: | Add an appropriate Depends section to the product. For example: | ||
Line 96: | Line 97: | ||
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. | 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. | ||
== DEFINES = MACRO == | === DEFINES = MACRO === | ||
Use the following: Note that in order to reference cpp.defines you must specify a dependency on the cpp module. | Use the following: Note that in order to reference cpp.defines you must specify a dependency on the cpp module. | ||
Line 149: | Line 150: | ||
</code> | </code> | ||
== INCLUDEPATH = dir == | === INCLUDEPATH = dir === | ||
<code>cpp.includePaths: [ '..', 'some/other/dir']</code> | <code>cpp.includePaths: [ '..', 'some/other/dir']</code> | ||
== CONFIG -= Qt == | === CONFIG -= Qt === | ||
Just don't declare Qt as a dependency. Probably you'd want: | Just don't declare Qt as a dependency. Probably you'd want: | ||
Line 159: | Line 160: | ||
<code>Depends { name: "cpp" }</code> | <code>Depends { name: "cpp" }</code> | ||
== RC_FILE == | === RC_FILE === | ||
Just add the file to the "files" list. | Just add the file to the "files" list. | ||
== QMAKE_INFO_PLIST == | === QMAKE_INFO_PLIST === | ||
Set the cpp.infoPlistFile property. | Set the cpp.infoPlistFile property. | ||
== ICON == | === ICON === | ||
Not yet implemented. See [https://bugreports.qt.io/browse/QBS-73 QBS-73]. | Not yet implemented. See [https://bugreports.qt.io/browse/QBS-73 QBS-73]. | ||
== TEMPLATE = subdirs == | === TEMPLATE = subdirs === | ||
Inside a "Project" item, use "references": | Inside a "Project" item, use "references": | ||
Line 183: | Line 184: | ||
</code> | </code> | ||
== DESTDIR == | === DESTDIR === | ||
Use the destinationDirectory property: | Use the destinationDirectory property: | ||
Line 197: | Line 198: | ||
Using it is not recommended, however; you should rather use the installation mechanism (see below). | Using it is not recommended, however; you should rather use the installation mechanism (see below). | ||
== message(), warning(), error() == | === message(), warning(), error() === | ||
You can use the JavaScript function print for printing messages and throw exceptions on the right hand side of property bindings. | You can use the JavaScript function print for printing messages and throw exceptions on the right hand side of property bindings. | ||
Line 214: | Line 215: | ||
== Others not mentioned above == | === Others not mentioned above === | ||
Either I've missed them, or they're not yet implemented. | Either I've missed them, or they're not yet implemented. | ||
= .pro and .pri = | == .pro and .pri == | ||
The top-level .qbs file contains the "Project" 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. | The top-level .qbs file contains the "Project" 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. | ||
Line 252: | Line 253: | ||
</code> | </code> | ||
= Conditionals = | == Conditionals == | ||
Instead of the qmake syntax of "windows { … }" or "macx:…", you specify a "condition" property in the relevant block. Conditionally-compiled files should be collected in a "Group" block, while platform-specific properties should go in a "Properties" block rather than being put in the main (outer) block: | Instead of the qmake syntax of "windows { … }" or "macx:…", you specify a "condition" property in the relevant block. Conditionally-compiled files should be collected in a "Group" block, while platform-specific properties should go in a "Properties" block rather than being put in the main (outer) block: | ||
Line 274: | Line 275: | ||
See the DEFINES section above for important information about how conditionals and cpp.defines interact. | See the DEFINES section above for important information about how conditionals and cpp.defines interact. | ||
= C++ compiler options = | == 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: | 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: | ||
Line 292: | Line 293: | ||
Note that setting things like cflags directly is discouraged (because they are compiler-dependent), and higher-level alternatives like cpp.optimization: "fast" should be used if available. | Note that setting things like cflags directly is discouraged (because they are compiler-dependent), and higher-level alternatives like cpp.optimization: "fast" should be used if available. | ||
= Installing files = | == Installing files == | ||
Create a group containing the files, and set qbs.install and qbs.installDir: | Create a group containing the files, and set qbs.install and qbs.installDir: | ||
Line 320: | Line 321: | ||
the qbs.installRoot property on the command line. | the qbs.installRoot property on the command line. | ||
= Command-line examples = | == Command-line examples == | ||
64-bit: | 64-bit: | ||
Line 326: | Line 327: | ||
</code> | </code> | ||
= "Magic" variables = | == "Magic" variables == | ||
Variables defined in various scopes, which may not be obvious: | Variables defined in various scopes, which may not be obvious: | ||
== qbs == | === qbs === | ||
This has lots of useful things in, such as: targetOS ("windows", "linux", "osx", …); buildVariant ("debug", "release"); architecture ("x86", "x86_64", …) | This has lots of useful things in, such as: targetOS ("windows", "linux", "osx", …); buildVariant ("debug", "release"); architecture ("x86", "x86_64", …) | ||
== project == | === project === | ||
Valid anywhere in your project, needed to refer to project properties from within a product: | Valid anywhere in your project, needed to refer to project properties from within a product: | ||
Line 347: | Line 348: | ||
</code> | </code> | ||
== buildDirectory == | === 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. | 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. | ||
It can also be explicitly specified via the -d option. | It can also be explicitly specified via the -d option. | ||
== Module names == | === Module names === | ||
Modules that are declared as dependencies can be referred to by their name and their properties accessed. For example: | Modules that are declared as dependencies can be referred to by their name and their properties accessed. For example: | ||
Line 362: | Line 363: | ||
</code> | </code> | ||
== Inside custom javascript == | === Inside custom javascript === | ||
Generally when writing custom JavaScript logic, things are referenced through the top-level "product" variable. In particular, to get the product's value of the property "baz" of a module "foo", use product.moduleProperty("foo", "baz"). For list values that get automatically combined, like includePaths, use product.moduleProperties("foo", "baz"). | Generally when writing custom JavaScript logic, things are referenced through the top-level "product" variable. In particular, to get the product's value of the property "baz" of a module "foo", use product.moduleProperty("foo", "baz"). For list values that get automatically combined, like includePaths, use product.moduleProperties("foo", "baz"). |
Revision as of 21:32, 27 June 2015
Introduction
Qbs is the next-generation build system "initially introduced in the Qt Labs Blog. 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.
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 {
name: "helloworld"
files: "main.cpp"
…
}
This is roughly equivalent to:
Product {
name: "helloworld"
type: "application"
files: "main.cpp"
Depends { name: "cpp" }
…
}
TEMPLATE = lib
Use DynamicLibrary as the product:
DynamicLibrary {
name: "mydll"
files: ["stuff.cpp"]
Depends { name: "cpp" }
…
}
TARGET = myappname
Use the "name" property, see the TEMPLATE example above.
HEADERS, SOURCES, FORMS, RESOURCES
Include in files section, eg
files: ['thing.h', 'thing.cpp', 'thing.ui', 'myapp.qrc']
qbs will use file taggers to figure out what kind of file it is dealing with.
CONFIG = console
Application {
name: "helloworld"
files: "main.cpp"
consoleApplication: true
…
}
CONFIG = designer_defines
DynamicLibrary {
name: "myplugin"
files: ["foo.cpp", …]
Depends { name: "cpp" }
cpp.defines: ["QDESIGNER_EXPORT_WIDGETS"]
…
}
QT = modulename
Add an appropriate Depends section to the product. For example:
Product {
Depends { name: "Qt.core" }
// …or…
Depends { name: "Qt"; submodules: ["core", "gui", "network"] }
}
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.
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' }
…
cpp.defines: ['SUPPORT_COOL_STUFF']
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 {
Depends { name: 'cpp' }
…
cpp.platformDefines: ['MY_SPECIAL_DEFINE', 'UNICODE']
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 {
Depends { name: 'cpp' }
…
cpp.defines: ['MACRO_EVERYWHERE'] // This is defined for all files in this product (unless a group overrides it!)
Group {
cpp.defines: outer.concat('MACRO_GROUP')
files: groupFile.cpp
// MACRO_GROUP is only defined in groupFile.cpp
// MACRO_EVERYWHERE is also defined in groupFile.cpp because of the outer.concat
}
}
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 {
Depends { name: 'cpp' }
…
Group {
condition: supportFoobar === true
files: fooFile.cpp
}
property stringList commonDefines: ["ONE", "TWO"]
Properties {
condition: supportFoobar === true
cpp.defines: commonDefines.concat("FOOBAR_SUPPORTED")
}
Properties {
cpp.defines: commonDefines // else case for the Properties chain
}
}
INCLUDEPATH = dir
cpp.includePaths: [ '..', 'some/other/dir']
CONFIG -= Qt
Just don't declare Qt as a dependency. Probably you'd want:
Depends { name: "cpp" }
RC_FILE
Just add the file to the "files" list.
QMAKE_INFO_PLIST
Set the cpp.infoPlistFile property.
ICON
Not yet implemented. See QBS-73.
TEMPLATE = subdirs
Inside a "Project" item, use "references":
Project {
references: [
"app/app.qbs",
"lib/lib.qbs"
]
}
DESTDIR
Use the destinationDirectory property:
DynamicLibrary {
name: "mydll"
destinationDirectory: "libDir"
…
}
Using it is not recommended, however; you should rather use the installation mechanism (see below).
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 {
name: {
print("—-> now evaluating the product name");
return "theName";
}
Depends {name: "cpp"}
cpp.includePath: {
throw "I don't know. Something bad happened."
return [];
}
}
Others not mentioned above
Either I've missed them, or they're not yet implemented.
.pro and .pri
The top-level .qbs file contains the "Project" 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:
—CrazyProduct.qbs—
import qbs.base 1.0
Product {
property string craziness: "low"
}
—hellocrazyworld.qbs—
CrazyProduct {
craziness: "enormous"
name: "hellocrazyworld"
// …
}
.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:
import qbs.base 1.0
import "../CrazyProduct.qbs" as CrazyProduct
CrazyProduct {
craziness: "enormous"
name: "hellocrazyworld"
// …
}
Conditionals
Instead of the qmake syntax of "windows { … }" or "macx:…", you specify a "condition" property in the relevant block. Conditionally-compiled files should be collected in a "Group" block, while platform-specific properties should go in a "Properties" block rather than being put in the main (outer) block:
Group {
condition: qbs.targetOS == "windows"
files: [
"harddiskdeleter_win.cpp",
"blowupmonitor_win.cpp",
"setkeyboardonfire_win.cpp"
]
}
Properties {
condition: qbs.targetOS == "linux"
cpp.defines: outer.concat([ "USE_BUILTIN_DESTRUCTORS"])
}
See the DEFINES section above for important information about how conditionals and cpp.defines interact.
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:
cpp.optimization: "none" // or "fast"
cpp.debugInformation: true
cpp.staticLibraries: "libraryName"
cpp.dynamicLibraries: "libraryName"
cpp.frameworks: "osxFrameworkName"
cpp.precompiledHeader: "myheader.pch"
cpp.warningLevel: "all" // or "none", "default"
cpp.treatWarningsAsErrors: true
cpp.cxxLanguageVersion // E.g. "c++11"
Note that setting things like cflags directly is discouraged (because they are compiler-dependent), and higher-level alternatives like cpp.optimization: "fast" should be used if available.
Installing files
Create a group containing the files, and set qbs.install and qbs.installDir:
Group {
qbs.install: true
qbs.installDir: "lib/myproj/"
files: [
"Menu.qml",
"SomeImportantFile.bin"
]
}
For files generated by the build (e.g. an executable), you need to match them by their file tag:
Group {
qbs.install: true
qbs.installDir: "bin"
fileTagsFilter: "application"
}
By default, installation happens automatically when building. The default installation root is called "install_root" and is located at the top level of the build directory. It can be overwritten by setting the qbs.installRoot property on the command line.
Command-line examples
64-bit:
qbs -f /path/to/project.qbs --products productname qbs.architecture:x86_64
"Magic" variables
Variables defined in various scopes, which may not be obvious:
qbs
This has lots of useful things in, such as: targetOS ("windows", "linux", "osx", …); buildVariant ("debug", "release"); architecture ("x86", "x86_64", …)
project
Valid anywhere in your project, needed to refer to project properties from within a product:
Project {
property string version: "1.0"
Product {
cpp.defines: ["PROJECT_VERSION=" + project.version]
}
}
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. It can also be explicitly specified via the -d option.
Module names
Modules that are declared as dependencies can be referred to by their name and their properties accessed. For example:
Product {
Depends { name: "Qt.quick" }
Qt.quick.qmlDebugging: false
}
Inside custom javascript
Generally when writing custom JavaScript logic, things are referenced through the top-level "product" variable. In particular, to get the product's value of the property "baz" of a module "foo", use product.moduleProperty("foo", "baz"). For list values that get automatically combined, like includePaths, use product.moduleProperties("foo", "baz").
Group {
condition: qbs.targetOS === "windows"
files: [ "hcf.cpp" ]
}
and
Group {
files: {
if (product.moduleProperty("qbs", "targetOS") === "windows") {
return [ "hcf.cpp" ];
} else {
return [];
}
}
}