Qbs如何构建规则使用产品

Joh*_*kic 4 qt build-system qbs

我想使用Qbs编译现有项目.该项目已包含在此项目中大量使用的代码转换工具(my_tool).

到目前为止我有(简化):

import qbs 1.0

Project {
    Application {
        name: "my_tool"
        files: "my_tool/main.cpp"
        Depends { name: "cpp" }
    }

    Application {
        name: "my_app"
        Group {
            files: 'main.cpp.in'
            fileTags: ['cpp_in']
        }
        Depends { name: "cpp" }

        Rule {
            inputs: ["cpp_in"]
            Artifact {
                fileName: input.baseName
                fileTags: "cpp"
            }
            prepare: {

                var mytool = /* Reference to my_tool */;

                var cmd = new Command(mytool, input.fileName, output.fileName);
                cmd.description = "Generate\t" + input.baseName;
                cmd.highlight = "codegen";
                return cmd;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如何获取命令的my_tool引用?

Joh*_*kic 7

这个答案是基于Qbs作者Joerg Bornemann的一封电子邮件,他允许我在这里引用它.

usingsRule 的属性允许将产品依赖项中的工件添加到输入.在这种情况下,我们对"应用程序"工件感兴趣.

然后可以访问应用程序列表input.application.

Application {
    name: "my_app"
    Group {
        files: 'main.cpp.in'
        fileTags: ['cpp_in']
    }
    Depends { name: "cpp" }

    // we need this dependency to make sure that my_tool exists before building my_app
    Depends { name: "my_tool" }

    Rule {
        inputs: ["cpp_in"]
        usings: ["application"] // dependent "application" products appear in inputs
        Artifact {
            fileName: input.completeBaseName
            fileTags: "cpp"
        }
        prepare: {
            // inputs["application"] is a list of "application" products
            var mytool = inputs["application"][0].fileName;
            var cmd = new Command(mytool, [inputs["cpp_in"][0].fileName, output.fileName]);
            cmd.description = "Generate\t" + input.baseName;
            cmd.highlight = "codegen";
            return cmd;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)