3

我至少可以想到一些用例或场景:

  • 构建多个示例程序的“示例”配置。
  • 具有客户端程序和主机程序的项目。
  • 具有一个主程序和一些相关实用程序(例如:gcc)的项目。

更一般地说,如果项目有多个具有相同或相似依赖集的输出可执行文件,那么一次构建它们可能是有意义的。用户决定运行哪个可执行文件比弄清楚如何使 DUB 创建他们想要的可执行文件更容易(他们可能不是熟悉 DUB 的 D 开发人员)。作为一名 D 开发人员,这对我来说也很方便,因为我可以运行更少的构建命令来获得我想要的全部内容。

例如,我可能有这样的项目布局:

dub.json
.gitignore
source/common/foo.d               (Code called by all examples)
source/examples/simple_example.d  (Build input with main function)
source/examples/complex_example.d (Build input with main function)
source/examples/clever_example.d  (Build input with main function)
bin/simple_example.exe            (Output executable)
bin/complex_example.exe           (Output executable)
bin/clever_example.exe            (Output executable)

另一个项目可能如下所示:

dub.json
.gitignore
source/common/netcode.d           (Code called by all programs)
source/common/logic.d             (Code called by all programs)
source/executables/host-daemon.d  (Does privileged things for the server)
source/executables/server.d       (Unprivileged network endpoint)
source/executables/client.d       (Queries the server)
bin/host-daemon                   (Output executable)
bin/server                        (Output executable)
bin/client                        (Output executable)

在任一项目中,我都希望通过一次 DUB 调用来构建所有可执行文件。理想情况下,由于输入和输出的相互关联性,这一切都将通过一个 dub.json 文件进行管理。

似乎subPackages可能能够做到这一点,但从一个 dub.json 文件管理它“通常不鼓励”:

子目录 /component1 和 /component2 然后包含普通包,可以从外部项目中称为“mylib:component1”和“mylib:component2”。要引用同一存储库中的子包,请使用“*”版本说明符。

也可以在根包文件中定义子包,但注意是generally discouraged将多个子包的源代码放在同一个源文件夹中。这样做可能会导致对“依赖项”部分中未明确说明的子包的隐藏依赖项。这些隐藏的依赖关系可能会导致与某些可能难以理解的构建模式或依赖关系树相结合的构建错误。

如上所述,DUB 可以一次构建多个可执行文件吗?如果可以,最推荐的方式是什么?为什么?

4

2 回答 2

3

我设法使用配置来做到这一点,没有子包。

首先,在所有主要函数前面加上versions说明符。

假设您有 3 个不同的文件,每个文件都有自己的 main.js 文件。

一.d:

....
versions(one)
void main() {
    ....
}

二.d:

....
versions(two)
void main() {
    ....
}

三.d:

....
versions(three)
void main() {
    ....
}

在你的 dub 工程文件中(我这里使用的是 sdl 格式),你可以定义三个配置,每个配置定义不同的版本,输出到不同的输出文件。

配音.sdl:

....
configuration "one" {
    versions "one"
    targetType "executable"
    targetName "one"
}

configuration "two" {
    versions "two"
    targetType "executable"
    targetName "two"
}

configuration "three" {
    versions "three"
    targetType "executable"
    targetName "three"
}

如果您只是调用dub build,它将使用第一个配置,但您可以传递不同的配置:

dub build --config=two
于 2017-08-26T09:57:41.733 回答
0

“子包旨在从外部模块化您的包”

相反,您应该能够创建多个配置来执行您想要的操作,就像dub 本身所做的那样(它定义了多个库,但您可以轻松地执行多个可执行文件)。

我不确定是否有命令可以一次构建所有配置。--combined也许(文档不清楚,但我认为它实际上是用于使用单个编译器调用构建所有源文件,而不是一个一个地生成目标文件)。

于 2017-06-23T20:10:10.330 回答