Module Item

A Module item is a collection of properties and language items that are used for building a product if the product has a dependency on the module. The following (somewhat artificial) module pre-processes text files by removing certain characters from them:

import qbs
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 = product.moduleProperty("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"
    ]
}

Of course, normally the pre-processed files would not be the target artifacts of the product, but rather serve as inputs to a different rule that will often come from a different module.

How you make your own modules available to Qbs is explained here.

Module Properties

PropertyTypeDefaultDescription
additionalProductFileTagsstring listempty listThe elements of this list will be added to the type property of a product that has a dependency on the module.
conditionbooltrueControls whether the module is enabled. If this property is false, the surrounding Module item will not be considered in the module look-up.
presentbooltrueThis property is read-only. Its value is false if and only if the respective Depends item had its required property set to false and the module was not found.
setupBuildEnvironmentscriptundefinedScript for setting up the environment in which the project is built. Use the putEnv and getEnv functions to alter the environment. The return value of this script is ignored.
setupRunEnvironmentscriptsetupBuildEnvironmentScript for setting up the environment in which the project is run.
validatescriptundefinedScript 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.