Skip to content

Commit

Permalink
Make sourcePaths work as documented
Browse files Browse the repository at this point in the history
`sourcePaths` is only default initialized when both
top level and configuration level `sourcePaths` is empty.

Fixes #1913
  • Loading branch information
omerfirmak committed Nov 27, 2020
1 parent f1815e6 commit 0c4e676
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 38 deletions.
79 changes: 43 additions & 36 deletions source/dub/package_.d
Expand Up @@ -671,55 +671,62 @@ class Package {
}
}

// check for default source folders
immutable hasSP = ("" in bs.sourcePaths) !is null;
immutable hasIP = ("" in bs.importPaths) !is null;
if (!hasSP || !hasIP) {
foreach (defsf; ["source/", "src/"]) {
if (existsFile(m_path ~ defsf)) {
if (!hasSP) bs.sourcePaths[""] ~= defsf;
if (!hasIP) bs.importPaths[""] ~= defsf;
}
}
}

// check for default app_main
string app_main_file;
auto pkg_name = m_info.name.length ? m_info.name : "unknown";
foreach(sf; bs.sourcePaths.get("", null)){
auto p = m_path ~ sf;
if( !existsFile(p) ) continue;
foreach(fil; ["app.d", "main.d", pkg_name ~ "/main.d", pkg_name ~ "/" ~ "app.d"]){
if( existsFile(p ~ fil) ) {
app_main_file = (NativePath(sf) ~ fil).toNativeString();
break;
}
}
}

// generate default configurations if none are defined
if (m_info.configurations.length == 0) {
if (bs.targetType == TargetType.executable) {
BuildSettingsTemplate app_settings;
app_settings.targetType = TargetType.executable;
if (bs.mainSourceFile.empty) app_settings.mainSourceFile = app_main_file;
m_info.configurations ~= ConfigurationInfo("application", app_settings);
} else if (bs.targetType != TargetType.none) {
BuildSettingsTemplate lib_settings;
lib_settings.targetType = bs.targetType == TargetType.autodetect ? TargetType.library : bs.targetType;
lib_settings.targetType = bs.targetType;
m_info.configurations ~= ConfigurationInfo("library", lib_settings);
}
}

if (bs.targetType == TargetType.autodetect) {
if (app_main_file.length) {
lib_settings.excludedSourceFiles[""] ~= app_main_file;
foreach (ref config; m_info.configurations)
{
auto confBS = &config.buildSettings;
// check for default source folders
immutable hasSP = ("" in confBS.sourcePaths) !is null || ("" in bs.sourcePaths) !is null;
immutable hasIP = ("" in bs.importPaths) !is null;

if (!hasSP || !hasIP) {
foreach (defsf; ["source/", "src/"]) {
if (existsFile(m_path ~ defsf)) {
if (!hasSP) confBS.sourcePaths[""] ~= defsf;
if (!hasIP) bs.importPaths[""] ~= defsf;
}
}
}

BuildSettingsTemplate app_settings;
app_settings.targetType = TargetType.executable;
app_settings.mainSourceFile = app_main_file;
m_info.configurations ~= ConfigurationInfo("application", app_settings);
// check for default app_main
string app_main_file;
auto pkg_name = m_info.name.length ? m_info.name : "unknown";
auto sourcePathsComb = bs.sourcePaths.get("", null) ~ confBS.sourcePaths.get("", null);
foreach(sf; sourcePathsComb){
auto p = m_path ~ sf;
if( !existsFile(p) ) continue;
foreach(fil; ["app.d", "main.d", pkg_name ~ "/main.d", pkg_name ~ "/" ~ "app.d"]){
if( existsFile(p ~ fil) ) {
app_main_file = (NativePath(sf) ~ fil).toNativeString();
break;
}
}
}

m_info.configurations ~= ConfigurationInfo("library", lib_settings);
if (confBS.targetType == TargetType.executable) {
if (bs.mainSourceFile.empty && confBS.mainSourceFile.empty)
confBS.mainSourceFile = app_main_file;
} else if (confBS.targetType == TargetType.autodetect) {
if (!bs.mainSourceFile.empty || !app_main_file.empty || !confBS.mainSourceFile.empty) {
confBS.targetType = TargetType.executable;
if (bs.mainSourceFile.empty && confBS.mainSourceFile.empty)
confBS.mainSourceFile = app_main_file;
config.name = "application";
} else {
confBS.targetType = TargetType.library;
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions source/dub/recipe/packagerecipe.d
Expand Up @@ -276,10 +276,11 @@ struct BuildSettingsTemplate {
// collect import files and remove sources
import std.algorithm : copy, setDifference;

auto importFiles = collectFiles(importPaths, "*.{d,di}").sort();
auto newImportFiles = collectFiles(importPaths, "*.{d,di}");
auto importFiles = (dst.importFiles ~ newImportFiles).sort();
immutable nremoved = importFiles.setDifference(sourceFiles).copy(importFiles.release).length;
importFiles = importFiles[0 .. $ - nremoved];
dst.addImportFiles(importFiles.release);
dst.importFiles = importFiles.release;

dst.addStringImportFiles(collectFiles(stringImportPaths, "*"));

Expand Down
Empty file.
Empty file.
15 changes: 15 additions & 0 deletions test/issue1913-sourcePaths/dub.json
@@ -0,0 +1,15 @@
{
"name": "dub-bug",
"configurations": [
{
"name": "app1",
"targetName": "bug-app1",
"sourcePaths": [ "source/app1" ]
},
{
"name": "app2",
"targetName": "bug-app2",
"sourcePaths": [ "source/app2" ]
}
]
}
1 change: 1 addition & 0 deletions test/issue1913-sourcePaths/source/app1/main.d
@@ -0,0 +1 @@
void main () {}
1 change: 1 addition & 0 deletions test/issue1913-sourcePaths/source/app2/main.d
@@ -0,0 +1 @@
void main () {}

0 comments on commit 0c4e676

Please sign in to comment.