[Qbs] Need help with setting up a code generator

Christian Kandeler christian.kandeler at qt.io
Mon Dec 11 13:27:28 CET 2017


On Mon, 11 Dec 2017 12:38:03 +0100
Ola Røer Thorsen <ola at silentwings.no> wrote:

> The code generator (a commandline tool) takes a set of C header files (.h)
> in a given directory, and another set of template files (names matching
> .tpl, .h and .cpp) in another directory. 

StaticLibrary {
    Depends { name: "cpp" }
    Group { 
        name: "header inputs"
        files: ["dir1/*.h"]
        fileTags: ["generator.in.headers"]
    }
    Group { 
        name: "template inputs"
        prefix: "dir2/"
        files: ["*.tpl", "*.h", "*.cpp]
        fileTags: ["generator.in.templates"]
    }    

> The output is a set of C++ .h and
> .cpp files in an output directory. The number of output files is not the
> same as the number of input files.

    Rule {
        // See https://doc.qt.io/qbs/rule-item.html for details.
        multiplex: true // Probably. Not entirely clear from your description
        inputs: ["generator.in.headers", "generator.in.templates"]

        // I assume the number of output files is dynamic and depends on
        // the number and/or contents of the input files.
        outputFileTags: ["h", "cpp"]
        outputArtifacts: {
            // The following two are arrays of artifact objects.
            // Their file paths are available via the filePath property.
            var headerInputs = inputs["generator.in.headers"];
            var templateInputs = inputs["generator.in.templates"];

            var outList = [];
            // Do what you need to do here to calculate the output 
            // from the input artifacts. The return value is a list of
            // objects with properties filePath and fileTags.
            return outList;
        }
        prepare: {
            var generatorArgs = [];
            // Generate the command-line arguments for the generator tool
            // from the inputs and outputs variables.
            var cmd = new Command("generator.exe", generatorArgs);
            cmd.description = "Running generator";
            return [cmd];
        }

    // Use this property in the outputArtifacts script above.
    property string generatedIncludesDir: product.buildDirectory + "/includes"

> The resulting .h and .cpp files are to be built into a static library. Also
> those header files must be available in the includepath to those needing
> it.

    Export {
        Depends { name: "cpp" }
        cpp.includePaths: product.generatedIncludesDir
    }
}

> Whenever any of the C headers or template files are modified, the code
> generator is to be run again.

This is basic built-in functionality.

> I'd really appreciate some hints or examples on how this can be done, I'm
> currently stuck trying to figure it out.

Hope this helps,
Christian



More information about the Qbs mailing list