diff --git a/changelog/auto-exclude-main-from-other-configs.dd b/changelog/auto-exclude-main-from-other-configs.dd new file mode 100644 index 0000000..3398a84 --- /dev/null +++ b/changelog/auto-exclude-main-from-other-configs.dd @@ -0,0 +1,12 @@ +Dub will now automatically exclude `mainSourceFile` from other configurations + +By default, Dub uses all files it can find under its `sourcePaths`. +However, a common pattern when dealing with multiple targets is to use +configurations to represent said targets. In the case those targets are executables, +users would be forced to add main files from other configurations to the +`excludedSourceFiles` list, or store the main in a different directory outside of +the sourcePaths. + +To simplify this workflow, Dub will now exclude files listed in mainSourceFile +for other configuration. In case this is not desirable, the files need to be manually +added to the `sourceFiles` list. diff --git a/source/dub/package_.d b/source/dub/package_.d index c7c7750..98c295b 100644 --- a/source/dub/package_.d +++ b/source/dub/package_.d @@ -118,6 +118,7 @@ checkDubRequirements(); fillWithDefaults(); + mutuallyExcludeMainFiles(); } /** Searches the given directory for package recipe files. @@ -740,6 +741,30 @@ cnames[c.name] = true; } } + + /// Exclude files listed in mainSourceFile for other configurations unless they are listed in sourceFiles + private void mutuallyExcludeMainFiles() + { + string[] allMainFiles; + foreach (ref config; m_info.configurations) + if (!config.buildSettings.mainSourceFile.empty()) + allMainFiles ~= config.buildSettings.mainSourceFile; + + if (allMainFiles.length == 0) + return; + + foreach (ref config; m_info.configurations) { + import std.algorithm.searching : canFind; + auto bs = &config.buildSettings; + auto otherMainFiles = allMainFiles.filter!(elem => (elem != bs.mainSourceFile)).array; + + if (bs.sourceFiles.length == 0) + bs.excludedSourceFiles[""] ~= otherMainFiles; + else + foreach (suffix, arr; bs.sourceFiles) + bs.excludedSourceFiles[suffix] ~= otherMainFiles.filter!(elem => !canFind(arr, elem)).array; + } + } } private string determineVersionFromSCM(NativePath path) diff --git a/test/mutex-main-1/.no_run b/test/mutex-main-1/.no_run new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/mutex-main-1/.no_run diff --git a/test/mutex-main-1/.no_test b/test/mutex-main-1/.no_test new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/mutex-main-1/.no_test diff --git a/test/mutex-main-1/dub.json b/test/mutex-main-1/dub.json new file mode 100644 index 0000000..882df72 --- /dev/null +++ b/test/mutex-main-1/dub.json @@ -0,0 +1,24 @@ +{ + "description": "A minimal D application.", + "name": "mutex-main", + "targetType": "executable", + + "configurations": [ + { + "name": "app", + "targetName": "app", + "mainSourceFile": "source/app.d" + }, + { + "name": "app2", + "targetName": "app2", + "mainSourceFile": "source/app2.d" + }, + { + "name": "failapp", + "targetName": "failapp", + "mainSourceFile": "source/app.d", + "sourceFiles": ["source/app2.d"] + } + ] +} diff --git a/test/mutex-main-1/source/app.d b/test/mutex-main-1/source/app.d new file mode 100644 index 0000000..0d545d4 --- /dev/null +++ b/test/mutex-main-1/source/app.d @@ -0,0 +1,8 @@ +module app; + +import std.stdio; + +void main() +{ + writeln("Edit source/app.d to start your project."); +} diff --git a/test/mutex-main-1/source/app2.d b/test/mutex-main-1/source/app2.d new file mode 100644 index 0000000..3524f0b --- /dev/null +++ b/test/mutex-main-1/source/app2.d @@ -0,0 +1,8 @@ +module app2; + +import std.stdio; + +void main() +{ + writeln("Edit source/app2.d to start your project."); +} diff --git a/test/mutex-main-2/.no_run b/test/mutex-main-2/.no_run new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/mutex-main-2/.no_run diff --git a/test/mutex-main-2/.no_test b/test/mutex-main-2/.no_test new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/mutex-main-2/.no_test diff --git a/test/mutex-main-2/dub.json b/test/mutex-main-2/dub.json new file mode 100644 index 0000000..6ca022b --- /dev/null +++ b/test/mutex-main-2/dub.json @@ -0,0 +1,24 @@ +{ + "description": "A minimal D application.", + "name": "mutex-main", + "targetType": "executable", + + "configurations": [ + { + "name": "app2", + "targetName": "app2", + "mainSourceFile": "source/app2.d" + }, + { + "name": "app", + "targetName": "app", + "mainSourceFile": "source/app.d" + }, + { + "name": "failapp", + "targetName": "failapp", + "mainSourceFile": "source/app.d", + "sourceFiles": ["source/app2.d"] + } + ] +} diff --git a/test/mutex-main-2/source/app.d b/test/mutex-main-2/source/app.d new file mode 100644 index 0000000..0d545d4 --- /dev/null +++ b/test/mutex-main-2/source/app.d @@ -0,0 +1,8 @@ +module app; + +import std.stdio; + +void main() +{ + writeln("Edit source/app.d to start your project."); +} diff --git a/test/mutex-main-2/source/app2.d b/test/mutex-main-2/source/app2.d new file mode 100644 index 0000000..3524f0b --- /dev/null +++ b/test/mutex-main-2/source/app2.d @@ -0,0 +1,8 @@ +module app2; + +import std.stdio; + +void main() +{ + writeln("Edit source/app2.d to start your project."); +} diff --git a/test/mutex-main-3/.fail_build b/test/mutex-main-3/.fail_build new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/mutex-main-3/.fail_build diff --git a/test/mutex-main-3/.no_run b/test/mutex-main-3/.no_run new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/mutex-main-3/.no_run diff --git a/test/mutex-main-3/.no_test b/test/mutex-main-3/.no_test new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/mutex-main-3/.no_test diff --git a/test/mutex-main-3/dub.json b/test/mutex-main-3/dub.json new file mode 100644 index 0000000..c5db1fa --- /dev/null +++ b/test/mutex-main-3/dub.json @@ -0,0 +1,24 @@ +{ + "description": "A minimal D application.", + "name": "mutex-main", + "targetType": "executable", + + "configurations": [ + { + "name": "failapp", + "targetName": "failapp", + "mainSourceFile": "source/app.d", + "sourceFiles": ["source/app2.d"] + }, + { + "name": "app", + "targetName": "app", + "mainSourceFile": "source/app.d", + }, + { + "name": "app2", + "targetName": "app2", + "mainSourceFile": "source/app2.d" + } + ] +} diff --git a/test/mutex-main-3/source/app.d b/test/mutex-main-3/source/app.d new file mode 100644 index 0000000..0d545d4 --- /dev/null +++ b/test/mutex-main-3/source/app.d @@ -0,0 +1,8 @@ +module app; + +import std.stdio; + +void main() +{ + writeln("Edit source/app.d to start your project."); +} diff --git a/test/mutex-main-3/source/app2.d b/test/mutex-main-3/source/app2.d new file mode 100644 index 0000000..3524f0b --- /dev/null +++ b/test/mutex-main-3/source/app2.d @@ -0,0 +1,8 @@ +module app2; + +import std.stdio; + +void main() +{ + writeln("Edit source/app2.d to start your project."); +}