Module

Represents a collection of properties and items that can be loaded into a product. More...

Properties

Detailed Description

A Module item is a collection of properties and language items. It contributes to building a product if the product has a dependency on the module. Modules may contain the following items:

When a product expresses a dependency on a module, Qbs will create an instance of the module item in the scope of the product. The product can then read and write properties from and to the loaded module, respectively.

Modules in different products are isolated from each other, just as products cannot access each other's properties. However, products can use the Export item to pass dependencies and properties of modules to other dependent products.

The following (somewhat artificial) module pre-processes text files by removing certain characters from them. The module's name is txt_processor.

 import qbs.FileInfo
 import qbs.TextFile

 Module {
     property stringList unwantedCharacters: []

     FileTagger {
         patterns: ["*.raw"]
         fileTags: ["raw-txt"]
     }

     Rule {
         inputs: ["raw-txt"]

         Artifact {
             filePath: FileInfo.relativePath(input.filePath, product.sourceDirectory) +
                        "/" + input.fileName + ".processed"
             fileTags: ["processed-txt"]
         }

         prepare: {
             var cmd = new JavaScriptCommand();
             cmd.description = "Processing " + input.fileName;
             cmd.sourceCode = function() {
                 var inFile = new TextFile(input.filePath, TextFile.ReadOnly);
                 var content = inFile.readAll();
                 inFile.close();
                 var unwantedChars = input.txt_processor.unwantedCharacters;
                 for (var c in unwantedChars)
                     content = content.replace(unwantedChars[c], "");
                 var outFile = new TextFile(output.filePath, TextFile.WriteOnly);
                 outFile.write(content);
                 outFile.close();
             };
             return cmd;
         }
     }
 }

And this is how a Product would use the module:

 Product {
     type: "processed-txt"
     Depends { name: "txt_processor" }
     txt_processor.unwantedCharacters: ["\r"]
     files: [
         "file1.raw",
         "file2.raw"
     ]
 }

The resulting files are tagged with processed-txt and might be consumed by a rule in another module. That is possible if another rule has processed-txt in its inputs property.

For more information about how you make your own modules available to Qbs, see Custom Modules and Items.

Accessing Product and Module Properties

When defining a property in a module item, the right-hand side expression is a binding. Bindings may reference other properties of:

  • the current module
  • other modules that this module depends on
  • the dependent product

Please note that this applies to bindings in modules only. Property access in rules and other nested items is different.

Accessing Properties of the Current Module

Sibling properties in the same module can be accessed directly by their name:

 Module {
     property stringList windowsDefaults: ["\r"]
     property stringList unwantedCharacters: windowsDefaults
 }

Properties of the Dependent Modules

When a module loads another module through a Depends element, it can access properties of the other module through its name. Assuming there was a module OtherModule with a property otherProperty, such an access would look like this:

 Module {
     Depends { name: "OtherModule" }
     property string myProperty: "something-" + OtherModule.otherProperty
 }

Accessing Properties of the Dependent Product

 Module {
     property bool featureEnabled:
             (product.type.contains("application")) ?  true : false
 }

Special Property Values

For every property defined in a module, Qbs provides the following special built-in values:

base

This value is useful when making use of inheritance. It stands for the value of the respective property in the item one level up in the inheritance chain. For instance:

 Product { // defined in MyProduct.qbs
     Depends { name: "mymodule" }
     mymodule.someProperty: ["value1"]
 }
 ------ some other file ------
 MyProduct {
     mymodule.someProperty: base.concat(["value2"]) // => ["value1", "value2"]
 }

original

This is the value of the property in the module itself (possibly overridden from a profile or the command line). Use it to set a module property conditionally:

 Module { // This is mymodule
     property string aProperty: "z"
 }
 ----------
 Product {
     Depends { name: "mymodule" }
     Depends { name: "myothermodule" }
     mymodule.aProperty: myothermodule.anotherProperty === "x" ? "y" : original // => "y" if myothermodule.anotherProperty is "x", "z" otherwise

outer

This value is used in nested items, where it refers to the value of the respective property in the surrounding item. It is only valid in Group and Properties items:

 Product {
     Depends { name: "mymodule" }
     mymodule.someProperty: ["value1"]
     Group {
         name: "special files"
         files: ["somefile1", "somefile2"]
         mymodule.someProperty: outer.concat(["value"]) // => ["value1", "value2"]
     }
 }

Dependency Parameters

Modules can declare dependency parameters. Those parameters can be set within Depends items. Rules of the module can read the parameters of dependencies and act accordingly.

In the following example, the module foo declares the parameter ignore. A dependency to bar then sets the parameter foo.ignore to true. A rule in foo ignores all dependencies that have foo.ignore set to true.

 Module {    // Definition of module 'foo'.
     Parameter { property bool ignore }
     Rule {
         ...
         prepare: {
             for (i in product.dependencies) {
                 var dep = product.dependencies[i];
                 if (dep.foo.ignore)
                     continue;
                 // Do something with the dependency.
             }
         }
     }
     ...
 }
 ----------
 Product {
     Depends { name: "foo" }
     Depends { name: "bar"; foo.ignore: true }
 }

Property Documentation

additionalProductTypes : stringList

A list of elements that will be added to the type property of a product that has a dependency on the module.

Default: []


condition : bool

Whether the module is enabled. If this property is false, the surrounding Module item will not be considered in the module look-up.

Default: true


[read-only] present : bool

This property is false if and only if the respective Depends item had its required property set to false and the module was not found.

Default: true


priority : int

The priority of this module instance. If there is more than one module instance available for a module name, the module with the highest priority is chosen.

Default: 0


setupBuildEnvironment : script

A script for setting up the environment in which a product is built.

The code in this script is treated as a function with the signature function(project, product).

Use the Environment functions to alter the environment.

The return value of this script is ignored.

Default: Undefined


setupRunEnvironment : script

A script for setting up the environment in which a product is run.

The code in this script is treated as a function with the signature function(project, product, config).

The config parameter is a list of arbitrary strings that can be passed via the run command. The values supported by specific modules are listed in their respective documentation.

Use the Environment functions to alter the environment.

The return value of this script is ignored.

Default: Undefined


validate : script

A script that is run after the module is loaded. It can be used to check property values and throw errors in unexpected cases. The return value is ignored.

Default: Undefined


version : string

The module's version. It consists of integer values separated by dots. You can check for specific values of this property in a Depends item.