c++ - 如何强制具有依赖关系的qbs产品之间同步?

标签 c++ qt-creator qbs

我有一个包含多个产品的项目,其中一些取决于生成的代码(cpp 和 hpp 文件)。生成的代码本身取决于作为 CppApplication 的 code_generator 产品。

我希望如果代码生成器的源发生更改,则重新构建 code_generator,然后始终运行。仅当输出文件发生更改时,它才会对其进行修改(它在内部在临时文件夹中生成它们,并在将它们移动到正确的位置之前使用校验和检查更改)。 文件生成后就可以开始编译其他产品了。

我的问题是,在构建 code_generator.exe 之前,我放置生成文件的 StaticLibrary 就开始构建了。依赖于 StaticLibrary 的其他产品也可以在生成 .hpp 文件之前开始构建。

所以我的问题是:是否有一些机制可用于使产品等待特定依赖项完全构建?

我对任何类型的解决方案持开放态度,生成的代码可以位于任何类型的模块中,我只是尝试使用静态库,因为它看起来更方便。

PS:code_generator 产品生成了很多文件,并接受了一些我在 qbs 中没有精确的输入,因为它始终可以运行,并且可以将其视为输入本身,因为它可以更改。这就是为什么我不确定使用 Rule 对我来说是否真的有趣。

这是我的 qbs 文件的摘录:

    CppApplication
    {
        name: "code_generator"

        consoleApplication: true

        files: [
            "../sources/code_generator/code_generator.cpp",
            "../sources/code_generator/code_generator.hpp",
            ...
        ]

        Depends { name: "default-cpp-configuration" }
        Depends { name: "tinyxml2" }

        destinationDirectory: "../" // @Warning we move the binary to ease his usage by the "generated_code" module
    }

    StaticLibrary    // @Warning as static library to build those sources only once
    {
        name: "generated_code"

        Depends { name: "default-cpp-configuration" }
        Depends { name: "code_generator" }

        Rule {
            multiplex: true
            alwaysRun: true
//            inputs: [project.buildDirectory + "/../code_generator.exe"]

            Artifact { filePath: "generated/common/directx/driver_call_table.cpp"; fileTags: "cpp" }
            Artifact { filePath: "generated/common/directx/driver_call_table.hpp"; fileTags: "hpp" }
            Artifact { filePath: "generated/common/directx/d3d11.def"; fileTags: "def" }
            Artifact { filePath: "generated/common/directx/uuid_helpers.cpp"; fileTags: "cpp" }
            Artifact { filePath: "generated/common/directx/uuid_helpers.hpp"; fileTags: "hpp" }


            prepare: {
                var code_generator_path = project.buildDirectory + "/../code_generator.exe";

                var cmd = new Command(code_generator_path, ["DirectX", "Outdir=${GENERATED_SOURCES_PATH}", "Indir=${CMAKE_SOURCE_DIR}/lib/specs", "CompilerPath=${CMAKE_CXX_COMPILER}", "--preprocess"]);
                cmd.description = "generating sources";
                return cmd;
            }
        }
    }

    CppApplication
    {
        name: "client"

        consoleApplication: true

        files: [
            "../sources/client/main.cpp",
        ]

        Depends { name: "default-cpp-configuration" }
        Depends { name: "generated_code" }
        Depends { name: "openssl" }
    }

最佳答案

产品依赖性只是使产品的工件在依赖产品的规则中可用。它们本身不会引起同步。同步发生在工件级别;否则,并行化将会受到阻碍。您的问题是您的规则没有声明它取决于代码生成器可执行文件。 它应该是这样的(未经测试):

Rule {
    multiplex: true
    inputsFromDependencies: "application" // These are tags, not paths!
    Artifact { 
        filePath: "generated/common/directx/driver_call_table.cpp"
        fileTags: "cpp" 
    }
    Artifact { 
      filePath: "generated/common/directx/driver_call_table.hpp"
      fileTags: "hpp" 
    }
    Artifact { 
      filePath: "generated/common/directx/d3d11.def"
      fileTags: "def" 
    }
    Artifact { 
        filePath: "generated/common/directx/uuid_helpers.cpp"
        fileTags: "cpp" 
    }
    Artifact { 
        filePath: "generated/common/directx/uuid_helpers.hpp"
        fileTags: "hpp" 
    }
    prepare: {
        var code_generator_path = inputs.application[0].filePath;
        var args = [
            "DirectX", "Outdir=${GENERATED_SOURCES_PATH}", 
            "Indir=${CMAKE_SOURCE_DIR}/lib/specs", 
            CompilerPath=${CMAKE_CXX_COMPILER}", "--preprocess"
        ];
        var cmd = new Command(code_generator_path, args);
        cmd.description = "generating sources";
        return cmd;
    }
}

关于c++ - 如何强制具有依赖关系的qbs产品之间同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57588456/

相关文章:

c++ - 在同一类中强制使用 Getter/Setter (C++)

c++ - EnumWindows 不工作

c++ - Qt Creator 可以识别 TODO 和 FIXME 注释吗?

c++ - qt打开一个带有不支持警告图像格式的jpg文件

qt - qbs QML 调试不起作用

c++ - MacOS 上的 Qbs 静态库

c# - 当输出参数是一个类时,如何在 C++ 中调用 COM 方法?

c++ - 至少在 Visual Studio 中,c++11 下 fmod(和其他)的不同行为

c++ - 无法在 Qt Creator 中修改选项卡设置

qt - Qbs 构建规则如何使用产品