3

I need to generate two exe files that have some common source codes. What is the best way to do it with dub?

I tried to do like this, but got error message about only one main function allowed.

Here is my dub.json:

{
    "name": "code1",
    "authors": [ "Suliman" ],
    "description": "A minimal D application.",
    "copyright": "Copyright © 2016, Suliman",
    "license": "proprietary",
    "subPackages": [
    {
        "name": "App1",
        "targetName": "App1",
        "description": "App1",
        "targetType": "executable",
        "excludedSourceFiles" : ["source/App2/*"],
        "excludedSourceFiles" : ["source/app2.d"]
    },

    {
        "name": "App2",
        "targetName": "App2",
        "description": "App2",
        "targetType": "executable",
        "excludedSourceFiles" : ["source/App1/*"],
        "excludedSourceFiles" : ["source/app1.d"]
    }]
} 
Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145

1 Answers1

7

Your dub.json will work, but you need to explicitly tell it to build one of the subpackages with dub build :App1 or dub build :App2 (where :App1 is a shortcut for code1:App1).

Separate configurations may be more appropriate here:

"configurations": [
    {
        "name": "App1",
        "targetType": "executable",
        "mainSourceFile": "source/app1.d",
        "excludedSourceFiles": [ "source/app2.d", "source/App2/*" ],
        "targetName": "app1"
    },
    {
        "name": "App2",
        "targetType": "executable",
        "mainSourceFile": "source/app2.d",
        "excludedSourceFiles": [ "source/app1.d", "source/App1/*" ],
        "targetName": "app2"
    }
]

dub build --config=App1 will produce app1, dub build --config=App2 will produce app2

A plain dub build will default to App1.

Note that you need excludedSourceFiles so dub doesn't see a duplicate main.

The docs recommend against using subpackages for this purpose:

It is also possible to define the sub packages within the root package file, but note that it is generally discouraged to put the source code of multiple sub packages into the same source folder. Doing so can lead to hidden dependencies to sub packages that haven't been explicitly stated in the "dependencies" section. These hidden dependencies can then result in build errors in conjunction with certain build modes or dependency trees that may be hard to understand.

I realized you were using dub.json, so I put the json format above. For reference, here's the dub.sdl format I posted earlier.

configuration "App1" {
    targetType "executable"
    mainSourceFile "source/app1.d"
    excludedSourceFiles "source/app2.d" "source/App2/*"
    targetName "app1"
}

configuration "App2" {
    targetType "executable"
    mainSourceFile "source/app2.d"
    excludedSourceFiles "source/app1.d" "source/App1/*"
    targetName "app2"
}
Max Alibaev
  • 681
  • 7
  • 17
rcorre
  • 6,477
  • 3
  • 28
  • 33
  • It seems to compile always just one app at time. Is there a way co compile many targets simultaneously? (like with cmake). E.g. I have a game engine, with many different tests/demos/examples and I want to check the all (without recompiling). – Prokop Hapala Oct 20 '19 at 17:55
  • Prokop, this is still a work in progress, but targetType "none" in the parent project, might be workable for you, but I'm afraid it only works for subpackages. I have not tried it myself yet. See https://github.com/dlang/dub/pull/1364 and its referenced PRs for more details – Jasmine Hegman Jun 02 '20 at 04:31