1 /** 2 Representing a full project, with a root Package and several dependencies. 3 4 Copyright: © 2012-2013 Matthias Dondorff, 2012-2016 Sönke Ludwig 5 License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. 6 Authors: Matthias Dondorff, Sönke Ludwig 7 */ 8 module dub.project; 9 10 import dub.compilers.compiler; 11 import dub.dependency; 12 import dub.description; 13 import dub.generators.generator; 14 import dub.internal.utils; 15 import dub.internal.vibecompat.core.file; 16 import dub.internal.vibecompat.data.json; 17 import dub.internal.vibecompat.inet.path; 18 import dub.internal.logging; 19 import dub.package_; 20 import dub.packagemanager; 21 import dub.recipe.selection; 22 23 import dub.internal.configy.Read; 24 25 import std.algorithm; 26 import std.array; 27 import std.conv : to; 28 import std.datetime; 29 import std.encoding : sanitize; 30 import std.exception : enforce; 31 import std.string; 32 33 /** 34 Represents a full project, a root package with its dependencies and package 35 selection. 36 37 All dependencies must be available locally so that the package dependency 38 graph can be built. Use `Project.reinit` if necessary for reloading 39 dependencies after more packages are available. 40 */ 41 class Project { 42 private { 43 PackageManager m_packageManager; 44 Package m_rootPackage; 45 Package[] m_dependencies; 46 Package[][Package] m_dependees; 47 SelectedVersions m_selections; 48 string[] m_missingDependencies; 49 string[string] m_overriddenConfigs; 50 } 51 52 /** Loads a project. 53 54 Params: 55 package_manager = Package manager instance to use for loading 56 dependencies 57 project_path = Path of the root package to load 58 pack = An existing `Package` instance to use as the root package 59 */ 60 this(PackageManager package_manager, NativePath project_path) 61 { 62 Package pack; 63 auto packageFile = Package.findPackageFile(project_path); 64 if (packageFile.empty) { 65 logWarn("There was no package description found for the application in '%s'.", project_path.toNativeString()); 66 pack = new Package(PackageRecipe.init, project_path); 67 } else { 68 pack = package_manager.getOrLoadPackage(project_path, packageFile, false, StrictMode.Warn); 69 } 70 71 this(package_manager, pack); 72 } 73 74 /// ditto 75 this(PackageManager package_manager, Package pack) 76 { 77 m_packageManager = package_manager; 78 m_rootPackage = pack; 79 80 auto selverfile = (m_rootPackage.path ~ SelectedVersions.defaultFile).toNativeString(); 81 if (existsFile(selverfile)) { 82 // TODO: Remove `StrictMode.Warn` after v1.40 release 83 // The default is to error, but as the previous parser wasn't 84 // complaining, we should first warn the user. 85 auto selected = parseConfigFileSimple!Selected(selverfile, StrictMode.Warn); 86 m_selections = !selected.isNull() ? 87 new SelectedVersions(selected.get()) : new SelectedVersions(); 88 } else m_selections = new SelectedVersions; 89 90 reinit(); 91 } 92 93 /** List of all resolved dependencies. 94 95 This includes all direct and indirect dependencies of all configurations 96 combined. Optional dependencies that were not chosen are not included. 97 */ 98 @property const(Package[]) dependencies() const { return m_dependencies; } 99 100 /// The root package of the project. 101 @property inout(Package) rootPackage() inout { return m_rootPackage; } 102 103 /// The versions to use for all dependencies. Call reinit() after changing these. 104 @property inout(SelectedVersions) selections() inout { return m_selections; } 105 106 /// Package manager instance used by the project. 107 @property inout(PackageManager) packageManager() inout { return m_packageManager; } 108 109 /** Determines if all dependencies necessary to build have been collected. 110 111 If this function returns `false`, it may be necessary to add more entries 112 to `selections`, or to use `Dub.upgrade` to automatically select all 113 missing dependencies. 114 */ 115 bool hasAllDependencies() const { return m_missingDependencies.length == 0; } 116 117 /// Sorted list of missing dependencies. 118 string[] missingDependencies() { return m_missingDependencies; } 119 120 /** Allows iteration of the dependency tree in topological order 121 */ 122 int delegate(int delegate(ref Package)) getTopologicalPackageList(bool children_first = false, Package root_package = null, string[string] configs = null) 123 { 124 // ugly way to avoid code duplication since inout isn't compatible with foreach type inference 125 return cast(int delegate(int delegate(ref Package)))(cast(const)this).getTopologicalPackageList(children_first, root_package, configs); 126 } 127 /// ditto 128 int delegate(int delegate(ref const Package)) getTopologicalPackageList(bool children_first = false, in Package root_package = null, string[string] configs = null) 129 const { 130 const(Package) rootpack = root_package ? root_package : m_rootPackage; 131 132 int iterator(int delegate(ref const Package) del) 133 { 134 int ret = 0; 135 bool[const(Package)] visited; 136 void perform_rec(in Package p){ 137 if( p in visited ) return; 138 visited[p] = true; 139 140 if( !children_first ){ 141 ret = del(p); 142 if( ret ) return; 143 } 144 145 auto cfg = configs.get(p.name, null); 146 147 PackageDependency[] deps; 148 if (!cfg.length) deps = p.getAllDependencies(); 149 else { 150 auto depmap = p.getDependencies(cfg); 151 deps = depmap.byKey.map!(k => PackageDependency(k, depmap[k])).array; 152 } 153 deps.sort!((a, b) => a.name < b.name); 154 155 foreach (d; deps) { 156 auto dependency = getDependency(d.name, true); 157 assert(dependency || d.spec.optional, 158 format("Non-optional dependency '%s' of '%s' not found in dependency tree!?.", d.name, p.name)); 159 if(dependency) perform_rec(dependency); 160 if( ret ) return; 161 } 162 163 if( children_first ){ 164 ret = del(p); 165 if( ret ) return; 166 } 167 } 168 perform_rec(rootpack); 169 return ret; 170 } 171 172 return &iterator; 173 } 174 175 /** Retrieves a particular dependency by name. 176 177 Params: 178 name = (Qualified) package name of the dependency 179 is_optional = If set to true, will return `null` for unsatisfiable 180 dependencies instead of throwing an exception. 181 */ 182 inout(Package) getDependency(string name, bool is_optional) 183 inout { 184 foreach(dp; m_dependencies) 185 if( dp.name == name ) 186 return dp; 187 if (!is_optional) throw new Exception("Unknown dependency: "~name); 188 else return null; 189 } 190 191 /** Returns the name of the default build configuration for the specified 192 target platform. 193 194 Params: 195 platform = The target build platform 196 allow_non_library_configs = If set to true, will use the first 197 possible configuration instead of the first "executable" 198 configuration. 199 */ 200 string getDefaultConfiguration(in BuildPlatform platform, bool allow_non_library_configs = true) 201 const { 202 auto cfgs = getPackageConfigs(platform, null, allow_non_library_configs); 203 return cfgs[m_rootPackage.name]; 204 } 205 206 /** Overrides the configuration chosen for a particular package in the 207 dependency graph. 208 209 Setting a certain configuration here is equivalent to removing all 210 but one configuration from the package. 211 212 Params: 213 package_ = The package for which to force selecting a certain 214 dependency 215 config = Name of the configuration to force 216 */ 217 void overrideConfiguration(string package_, string config) 218 { 219 auto p = getDependency(package_, true); 220 enforce(p !is null, 221 format("Package '%s', marked for configuration override, is not present in dependency graph.", package_)); 222 enforce(p.configurations.canFind(config), 223 format("Package '%s' does not have a configuration named '%s'.", package_, config)); 224 m_overriddenConfigs[package_] = config; 225 } 226 227 /** Adds a test runner configuration for the root package. 228 229 Params: 230 settings = The generator settings to use 231 generate_main = Whether to generate the main.d file 232 base_config = Optional base configuration 233 custom_main_file = Optional path to file with custom main entry point 234 235 Returns: 236 Name of the added test runner configuration, or null for base configurations with target type `none` 237 */ 238 string addTestRunnerConfiguration(in GeneratorSettings settings, bool generate_main = true, string base_config = "", NativePath custom_main_file = NativePath()) 239 { 240 if (base_config.length == 0) { 241 // if a custom main file was given, favor the first library configuration, so that it can be applied 242 if (!custom_main_file.empty) base_config = getDefaultConfiguration(settings.platform, false); 243 // else look for a "unittest" configuration 244 if (!base_config.length && rootPackage.configurations.canFind("unittest")) base_config = "unittest"; 245 // if not found, fall back to the first "library" configuration 246 if (!base_config.length) base_config = getDefaultConfiguration(settings.platform, false); 247 // if still nothing found, use the first executable configuration 248 if (!base_config.length) base_config = getDefaultConfiguration(settings.platform, true); 249 } 250 251 BuildSettings lbuildsettings = settings.buildSettings.dup; 252 addBuildSettings(lbuildsettings, settings, base_config, null, true); 253 254 if (lbuildsettings.targetType == TargetType.none) { 255 logInfo(`Configuration '%s' has target type "none". Skipping test runner configuration.`, base_config); 256 return null; 257 } 258 259 if (lbuildsettings.targetType == TargetType.executable && base_config == "unittest") { 260 if (!custom_main_file.empty) logWarn("Ignoring custom main file."); 261 return base_config; 262 } 263 264 if (lbuildsettings.sourceFiles.empty) { 265 logInfo(`No source files found in configuration '%s'. Falling back to default configuration for test runner.`, base_config); 266 if (!custom_main_file.empty) logWarn("Ignoring custom main file."); 267 return getDefaultConfiguration(settings.platform); 268 } 269 270 const config = format("%s-test-%s", rootPackage.name.replace(".", "-").replace(":", "-"), base_config); 271 logInfo(`Generating test runner configuration '%s' for '%s' (%s).`, config, base_config, lbuildsettings.targetType); 272 273 BuildSettingsTemplate tcinfo = rootPackage.recipe.getConfiguration(base_config).buildSettings.dup; 274 tcinfo.targetType = TargetType.executable; 275 276 // set targetName unless specified explicitly in unittest base configuration 277 if (tcinfo.targetName.empty || base_config != "unittest") 278 tcinfo.targetName = config; 279 280 auto mainfil = tcinfo.mainSourceFile; 281 if (!mainfil.length) mainfil = rootPackage.recipe.buildSettings.mainSourceFile; 282 283 string custommodname; 284 if (!custom_main_file.empty) { 285 import std.path; 286 tcinfo.sourceFiles[""] ~= custom_main_file.relativeTo(rootPackage.path).toNativeString(); 287 tcinfo.importPaths[""] ~= custom_main_file.parentPath.toNativeString(); 288 custommodname = custom_main_file.head.name.baseName(".d"); 289 } 290 291 // prepare the list of tested modules 292 293 string[] import_modules; 294 if (settings.single) 295 lbuildsettings.importPaths ~= NativePath(mainfil).parentPath.toNativeString; 296 bool firstTimePackage = true; 297 foreach (file; lbuildsettings.sourceFiles) { 298 if (file.endsWith(".d")) { 299 auto fname = NativePath(file).head.name; 300 NativePath msf = NativePath(mainfil); 301 if (msf.absolute) 302 msf = msf.relativeTo(rootPackage.path); 303 if (!settings.single && NativePath(file).relativeTo(rootPackage.path) == msf) { 304 logWarn("Excluding main source file %s from test.", mainfil); 305 tcinfo.excludedSourceFiles[""] ~= mainfil; 306 continue; 307 } 308 if (fname == "package.d") { 309 if (firstTimePackage) { 310 firstTimePackage = false; 311 logDiagnostic("Excluding package.d file from test due to https://issues.dlang.org/show_bug.cgi?id=11847"); 312 } 313 continue; 314 } 315 import_modules ~= dub.internal.utils.determineModuleName(lbuildsettings, NativePath(file), rootPackage.path); 316 } 317 } 318 319 NativePath mainfile; 320 if (settings.tempBuild) 321 mainfile = getTempFile("dub_test_root", ".d"); 322 else { 323 import dub.generators.build : computeBuildName; 324 mainfile = packageCache(settings.cache, this.rootPackage) ~ 325 format("code/%s/dub_test_root.d", 326 computeBuildName(config, settings, import_modules)); 327 } 328 329 auto escapedMainFile = mainfile.toNativeString().replace("$", "$$"); 330 tcinfo.sourceFiles[""] ~= escapedMainFile; 331 tcinfo.mainSourceFile = escapedMainFile; 332 if (!settings.tempBuild) { 333 // add the directory containing dub_test_root.d to the import paths 334 tcinfo.importPaths[""] ~= NativePath(escapedMainFile).parentPath.toNativeString(); 335 } 336 337 if (generate_main && (settings.force || !existsFile(mainfile))) { 338 ensureDirectory(mainfile.parentPath); 339 340 const runnerCode = custommodname.length ? 341 format("import %s;", custommodname) : DefaultTestRunnerCode; 342 const content = TestRunnerTemplate.format( 343 import_modules, import_modules, runnerCode); 344 writeFile(mainfile, content); 345 } 346 347 rootPackage.recipe.configurations ~= ConfigurationInfo(config, tcinfo); 348 349 return config; 350 } 351 352 /** Performs basic validation of various aspects of the package. 353 354 This will emit warnings to `stderr` if any discouraged names or 355 dependency patterns are found. 356 */ 357 void validate() 358 { 359 bool isSDL = !m_rootPackage.recipePath.empty 360 && m_rootPackage.recipePath.head.name.endsWith(".sdl"); 361 362 // some basic package lint 363 m_rootPackage.warnOnSpecialCompilerFlags(); 364 string nameSuggestion() { 365 string ret; 366 ret ~= `Please modify the "name" field in %s accordingly.`.format(m_rootPackage.recipePath.toNativeString()); 367 if (!m_rootPackage.recipe.buildSettings.targetName.length) { 368 if (isSDL) { 369 ret ~= ` You can then add 'targetName "%s"' to keep the current executable name.`.format(m_rootPackage.name); 370 } else { 371 ret ~= ` You can then add '"targetName": "%s"' to keep the current executable name.`.format(m_rootPackage.name); 372 } 373 } 374 return ret; 375 } 376 if (m_rootPackage.name != m_rootPackage.name.toLower()) { 377 logWarn(`DUB package names should always be lower case. %s`, nameSuggestion()); 378 } else if (!m_rootPackage.recipe.name.all!(ch => ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9' || ch == '-' || ch == '_')) { 379 logWarn(`DUB package names may only contain alphanumeric characters, ` 380 ~ `as well as '-' and '_'. %s`, nameSuggestion()); 381 } 382 enforce(!m_rootPackage.name.canFind(' '), "Aborting due to the package name containing spaces."); 383 384 foreach (d; m_rootPackage.getAllDependencies()) 385 if (d.spec.isExactVersion && d.spec.version_.isBranch) { 386 string suggestion = isSDL 387 ? format(`dependency "%s" repository="git+<git url>" version="<commit>"`, d.name) 388 : format(`"%s": {"repository": "git+<git url>", "version": "<commit>"}`, d.name); 389 logWarn("Dependency '%s' depends on git branch '%s', which is deprecated.", 390 d.name.color(Mode.bold), d.spec.version_.toString.color(Mode.bold)); 391 logWarnTag("", "Specify the git repository and commit hash in your %s:", 392 (isSDL ? "dub.sdl" : "dub.json").color(Mode.bold)); 393 logWarnTag("", "%s", suggestion.color(Mode.bold)); 394 } 395 396 // search for orphan sub configurations 397 void warnSubConfig(string pack, string config) { 398 logWarn("The sub configuration directive \"%s\" -> [%s] " 399 ~ "references a package that is not specified as a dependency " 400 ~ "and will have no effect.", pack.color(Mode.bold), config.color(Color.blue)); 401 } 402 403 void checkSubConfig(string pack, string config) { 404 auto p = getDependency(pack, true); 405 if (p && !p.configurations.canFind(config)) { 406 logWarn("The sub configuration directive \"%s\" -> [%s] " 407 ~ "references a configuration that does not exist.", 408 pack.color(Mode.bold), config.color(Color.red)); 409 } 410 } 411 auto globalbs = m_rootPackage.getBuildSettings(); 412 foreach (p, c; globalbs.subConfigurations) { 413 if (p !in globalbs.dependencies) warnSubConfig(p, c); 414 else checkSubConfig(p, c); 415 } 416 foreach (c; m_rootPackage.configurations) { 417 auto bs = m_rootPackage.getBuildSettings(c); 418 foreach (p, subConf; bs.subConfigurations) { 419 if (p !in bs.dependencies && p !in globalbs.dependencies) 420 warnSubConfig(p, subConf); 421 else checkSubConfig(p, subConf); 422 } 423 } 424 425 // check for version specification mismatches 426 bool[Package] visited; 427 void validateDependenciesRec(Package pack) { 428 // perform basic package linting 429 pack.simpleLint(); 430 431 foreach (d; pack.getAllDependencies()) { 432 auto basename = getBasePackageName(d.name); 433 d.spec.visit!( 434 (NativePath path) { /* Valid */ }, 435 (Repository repo) { /* Valid */ }, 436 (VersionRange vers) { 437 if (m_selections.hasSelectedVersion(basename)) { 438 auto selver = m_selections.getSelectedVersion(basename); 439 if (d.spec.merge(selver) == Dependency.invalid) { 440 logWarn(`Selected package %s %s does not match the dependency specification %s in package %s. Need to "%s"?`, 441 basename.color(Mode.bold), selver, vers, pack.name.color(Mode.bold), "dub upgrade".color(Mode.bold)); 442 } 443 } 444 }, 445 ); 446 447 auto deppack = getDependency(d.name, true); 448 if (deppack in visited) continue; 449 visited[deppack] = true; 450 if (deppack) validateDependenciesRec(deppack); 451 } 452 } 453 validateDependenciesRec(m_rootPackage); 454 } 455 456 /// Reloads dependencies. 457 void reinit() 458 { 459 m_dependencies = null; 460 m_missingDependencies = []; 461 462 Package resolveSubPackage(Package p, string subname, bool silentFail) { 463 return subname.length ? m_packageManager.getSubPackage(p, subname, silentFail) : p; 464 } 465 466 void collectDependenciesRec(Package pack, int depth = 0) 467 { 468 auto indent = replicate(" ", depth); 469 logDebug("%sCollecting dependencies for %s", indent, pack.name); 470 indent ~= " "; 471 472 foreach (dep; pack.getAllDependencies()) { 473 Dependency vspec = dep.spec; 474 Package p; 475 476 auto basename = getBasePackageName(dep.name); 477 auto subname = getSubPackageName(dep.name); 478 479 // non-optional and optional-default dependencies (if no selections file exists) 480 // need to be satisfied 481 bool is_desired = !vspec.optional || m_selections.hasSelectedVersion(basename) || (vspec.default_ && m_selections.bare); 482 483 if (dep.name == m_rootPackage.basePackage.name) { 484 vspec = Dependency(m_rootPackage.version_); 485 p = m_rootPackage.basePackage; 486 } else if (basename == m_rootPackage.basePackage.name) { 487 vspec = Dependency(m_rootPackage.version_); 488 try p = m_packageManager.getSubPackage(m_rootPackage.basePackage, subname, false); 489 catch (Exception e) { 490 logDiagnostic("%sError getting sub package %s: %s", indent, dep.name, e.msg); 491 if (is_desired) m_missingDependencies ~= dep.name; 492 continue; 493 } 494 } else if (m_selections.hasSelectedVersion(basename)) { 495 vspec = m_selections.getSelectedVersion(basename); 496 p = vspec.visit!( 497 (NativePath path_) { 498 auto path = path_.absolute ? path_ : m_rootPackage.path ~ path_; 499 auto tmp = m_packageManager.getOrLoadPackage(path, NativePath.init, true); 500 return resolveSubPackage(tmp, subname, true); 501 }, 502 (Repository repo) { 503 auto tmp = m_packageManager.loadSCMPackage(basename, repo); 504 return resolveSubPackage(tmp, subname, true); 505 }, 506 (VersionRange range) { 507 // See `dub.recipe.selection : SelectedDependency.fromYAML` 508 assert(range.isExactVersion()); 509 return m_packageManager.getPackage(dep.name, vspec.version_); 510 }, 511 ); 512 } else if (m_dependencies.canFind!(d => getBasePackageName(d.name) == basename)) { 513 auto idx = m_dependencies.countUntil!(d => getBasePackageName(d.name) == basename); 514 auto bp = m_dependencies[idx].basePackage; 515 vspec = Dependency(bp.path); 516 p = resolveSubPackage(bp, subname, false); 517 } else { 518 logDiagnostic("%sVersion selection for dependency %s (%s) of %s is missing.", 519 indent, basename, dep.name, pack.name); 520 } 521 522 // We didn't find the package 523 if (p is null) 524 { 525 if (!vspec.repository.empty) { 526 p = m_packageManager.loadSCMPackage(basename, vspec.repository); 527 resolveSubPackage(p, subname, false); 528 } else if (!vspec.path.empty && is_desired) { 529 NativePath path = vspec.path; 530 if (!path.absolute) path = pack.path ~ path; 531 logDiagnostic("%sAdding local %s in %s", indent, dep.name, path); 532 p = m_packageManager.getOrLoadPackage(path, NativePath.init, true); 533 if (p.parentPackage !is null) { 534 logWarn("%sSub package %s must be referenced using the path to it's parent package.", indent, dep.name); 535 p = p.parentPackage; 536 } 537 p = resolveSubPackage(p, subname, false); 538 enforce(p.name == dep.name, 539 format("Path based dependency %s is referenced with a wrong name: %s vs. %s", 540 path.toNativeString(), dep.name, p.name)); 541 } else { 542 logDiagnostic("%sMissing dependency %s %s of %s", indent, dep.name, vspec, pack.name); 543 if (is_desired) m_missingDependencies ~= dep.name; 544 continue; 545 } 546 } 547 548 if (!m_dependencies.canFind(p)) { 549 logDiagnostic("%sFound dependency %s %s", indent, dep.name, vspec.toString()); 550 m_dependencies ~= p; 551 if (basename == m_rootPackage.basePackage.name) 552 p.warnOnSpecialCompilerFlags(); 553 collectDependenciesRec(p, depth+1); 554 } 555 556 m_dependees[p] ~= pack; 557 //enforce(p !is null, "Failed to resolve dependency "~dep.name~" "~vspec.toString()); 558 } 559 } 560 collectDependenciesRec(m_rootPackage); 561 m_missingDependencies.sort(); 562 } 563 564 /// Returns the name of the root package. 565 @property string name() const { return m_rootPackage ? m_rootPackage.name : "app"; } 566 567 /// Returns the names of all configurations of the root package. 568 @property string[] configurations() const { return m_rootPackage.configurations; } 569 570 /// Returns the names of all built-in and custom build types of the root package. 571 /// The default built-in build type is the first item in the list. 572 @property string[] builds() const { return builtinBuildTypes ~ m_rootPackage.customBuildTypes; } 573 574 /// Returns a map with the configuration for all packages in the dependency tree. 575 string[string] getPackageConfigs(in BuildPlatform platform, string config, bool allow_non_library = true) 576 const { 577 struct Vertex { string pack, config; } 578 struct Edge { size_t from, to; } 579 580 Vertex[] configs; 581 Edge[] edges; 582 string[][string] parents; 583 parents[m_rootPackage.name] = null; 584 foreach (p; getTopologicalPackageList()) 585 foreach (d; p.getAllDependencies()) 586 parents[d.name] ~= p.name; 587 588 size_t createConfig(string pack, string config) { 589 foreach (i, v; configs) 590 if (v.pack == pack && v.config == config) 591 return i; 592 assert(pack !in m_overriddenConfigs || config == m_overriddenConfigs[pack]); 593 logDebug("Add config %s %s", pack, config); 594 configs ~= Vertex(pack, config); 595 return configs.length-1; 596 } 597 598 bool haveConfig(string pack, string config) { 599 return configs.any!(c => c.pack == pack && c.config == config); 600 } 601 602 size_t createEdge(size_t from, size_t to) { 603 auto idx = edges.countUntil(Edge(from, to)); 604 if (idx >= 0) return idx; 605 logDebug("Including %s %s -> %s %s", configs[from].pack, configs[from].config, configs[to].pack, configs[to].config); 606 edges ~= Edge(from, to); 607 return edges.length-1; 608 } 609 610 void removeConfig(size_t i) { 611 logDebug("Eliminating config %s for %s", configs[i].config, configs[i].pack); 612 auto had_dep_to_pack = new bool[configs.length]; 613 auto still_has_dep_to_pack = new bool[configs.length]; 614 615 edges = edges.filter!((e) { 616 if (e.to == i) { 617 had_dep_to_pack[e.from] = true; 618 return false; 619 } else if (configs[e.to].pack == configs[i].pack) { 620 still_has_dep_to_pack[e.from] = true; 621 } 622 if (e.from == i) return false; 623 return true; 624 }).array; 625 626 configs[i] = Vertex.init; // mark config as removed 627 628 // also remove any configs that cannot be satisfied anymore 629 foreach (j; 0 .. configs.length) 630 if (j != i && had_dep_to_pack[j] && !still_has_dep_to_pack[j]) 631 removeConfig(j); 632 } 633 634 bool isReachable(string pack, string conf) { 635 if (pack == configs[0].pack && configs[0].config == conf) return true; 636 foreach (e; edges) 637 if (configs[e.to].pack == pack && configs[e.to].config == conf) 638 return true; 639 return false; 640 //return (pack == configs[0].pack && conf == configs[0].config) || edges.canFind!(e => configs[e.to].pack == pack && configs[e.to].config == config); 641 } 642 643 bool isReachableByAllParentPacks(size_t cidx) { 644 bool[string] r; 645 foreach (p; parents[configs[cidx].pack]) r[p] = false; 646 foreach (e; edges) { 647 if (e.to != cidx) continue; 648 if (auto pp = configs[e.from].pack in r) *pp = true; 649 } 650 foreach (bool v; r) if (!v) return false; 651 return true; 652 } 653 654 string[] allconfigs_path; 655 656 void determineDependencyConfigs(in Package p, string c) 657 { 658 string[][string] depconfigs; 659 foreach (d; p.getAllDependencies()) { 660 auto dp = getDependency(d.name, true); 661 if (!dp) continue; 662 663 string[] cfgs; 664 if (auto pc = dp.name in m_overriddenConfigs) cfgs = [*pc]; 665 else { 666 auto subconf = p.getSubConfiguration(c, dp, platform); 667 if (!subconf.empty) cfgs = [subconf]; 668 else cfgs = dp.getPlatformConfigurations(platform); 669 } 670 cfgs = cfgs.filter!(c => haveConfig(d.name, c)).array; 671 672 // if no valid configuration was found for a dependency, don't include the 673 // current configuration 674 if (!cfgs.length) { 675 logDebug("Skip %s %s (missing configuration for %s)", p.name, c, dp.name); 676 return; 677 } 678 depconfigs[d.name] = cfgs; 679 } 680 681 // add this configuration to the graph 682 size_t cidx = createConfig(p.name, c); 683 foreach (d; p.getAllDependencies()) 684 foreach (sc; depconfigs.get(d.name, null)) 685 createEdge(cidx, createConfig(d.name, sc)); 686 } 687 688 // create a graph of all possible package configurations (package, config) -> (sub-package, sub-config) 689 void determineAllConfigs(in Package p) 690 { 691 auto idx = allconfigs_path.countUntil(p.name); 692 enforce(idx < 0, format("Detected dependency cycle: %s", (allconfigs_path[idx .. $] ~ p.name).join("->"))); 693 allconfigs_path ~= p.name; 694 scope (exit) allconfigs_path.length--; 695 696 // first, add all dependency configurations 697 foreach (d; p.getAllDependencies) { 698 auto dp = getDependency(d.name, true); 699 if (!dp) continue; 700 determineAllConfigs(dp); 701 } 702 703 // for each configuration, determine the configurations usable for the dependencies 704 if (auto pc = p.name in m_overriddenConfigs) 705 determineDependencyConfigs(p, *pc); 706 else 707 foreach (c; p.getPlatformConfigurations(platform, p is m_rootPackage && allow_non_library)) 708 determineDependencyConfigs(p, c); 709 } 710 if (config.length) createConfig(m_rootPackage.name, config); 711 determineAllConfigs(m_rootPackage); 712 713 // successively remove configurations until only one configuration per package is left 714 bool changed; 715 do { 716 // remove all configs that are not reachable by all parent packages 717 changed = false; 718 foreach (i, ref c; configs) { 719 if (c == Vertex.init) continue; // ignore deleted configurations 720 if (!isReachableByAllParentPacks(i)) { 721 logDebug("%s %s NOT REACHABLE by all of (%s):", c.pack, c.config, parents[c.pack]); 722 removeConfig(i); 723 changed = true; 724 } 725 } 726 727 // when all edges are cleaned up, pick one package and remove all but one config 728 if (!changed) { 729 foreach (p; getTopologicalPackageList()) { 730 size_t cnt = 0; 731 foreach (i, ref c; configs) 732 if (c.pack == p.name && ++cnt > 1) { 733 logDebug("NON-PRIMARY: %s %s", c.pack, c.config); 734 removeConfig(i); 735 } 736 if (cnt > 1) { 737 changed = true; 738 break; 739 } 740 } 741 } 742 } while (changed); 743 744 // print out the resulting tree 745 foreach (e; edges) logDebug(" %s %s -> %s %s", configs[e.from].pack, configs[e.from].config, configs[e.to].pack, configs[e.to].config); 746 747 // return the resulting configuration set as an AA 748 string[string] ret; 749 foreach (c; configs) { 750 if (c == Vertex.init) continue; // ignore deleted configurations 751 assert(ret.get(c.pack, c.config) == c.config, format("Conflicting configurations for %s found: %s vs. %s", c.pack, c.config, ret[c.pack])); 752 logDebug("Using configuration '%s' for %s", c.config, c.pack); 753 ret[c.pack] = c.config; 754 } 755 756 // check for conflicts (packages missing in the final configuration graph) 757 void checkPacksRec(in Package pack) { 758 auto pc = pack.name in ret; 759 enforce(pc !is null, "Could not resolve configuration for package "~pack.name); 760 foreach (p, dep; pack.getDependencies(*pc)) { 761 auto deppack = getDependency(p, dep.optional); 762 if (deppack) checkPacksRec(deppack); 763 } 764 } 765 checkPacksRec(m_rootPackage); 766 767 return ret; 768 } 769 770 /** 771 * Fills `dst` with values from this project. 772 * 773 * `dst` gets initialized according to the given platform and config. 774 * 775 * Params: 776 * dst = The BuildSettings struct to fill with data. 777 * gsettings = The generator settings to retrieve the values for. 778 * config = Values of the given configuration will be retrieved. 779 * root_package = If non null, use it instead of the project's real root package. 780 * shallow = If true, collects only build settings for the main package (including inherited settings) and doesn't stop on target type none and sourceLibrary. 781 */ 782 void addBuildSettings(ref BuildSettings dst, in GeneratorSettings gsettings, string config, in Package root_package = null, bool shallow = false) 783 const { 784 import dub.internal.utils : stripDlangSpecialChars; 785 786 auto configs = getPackageConfigs(gsettings.platform, config); 787 788 foreach (pkg; this.getTopologicalPackageList(false, root_package, configs)) { 789 auto pkg_path = pkg.path.toNativeString(); 790 dst.addVersions(["Have_" ~ stripDlangSpecialChars(pkg.name)]); 791 792 assert(pkg.name in configs, "Missing configuration for "~pkg.name); 793 logDebug("Gathering build settings for %s (%s)", pkg.name, configs[pkg.name]); 794 795 auto psettings = pkg.getBuildSettings(gsettings.platform, configs[pkg.name]); 796 if (psettings.targetType != TargetType.none) { 797 if (shallow && pkg !is m_rootPackage) 798 psettings.sourceFiles = null; 799 processVars(dst, this, pkg, psettings, gsettings); 800 if (!gsettings.single && psettings.importPaths.empty) 801 logWarn(`Package %s (configuration "%s") defines no import paths, use {"importPaths": [...]} or the default package directory structure to fix this.`, pkg.name, configs[pkg.name]); 802 if (psettings.mainSourceFile.empty && pkg is m_rootPackage && psettings.targetType == TargetType.executable) 803 logWarn(`Executable configuration "%s" of package %s defines no main source file, this may cause certain build modes to fail. Add an explicit "mainSourceFile" to the package description to fix this.`, configs[pkg.name], pkg.name); 804 } 805 if (pkg is m_rootPackage) { 806 if (!shallow) { 807 enforce(psettings.targetType != TargetType.none, "Main package has target type \"none\" - stopping build."); 808 enforce(psettings.targetType != TargetType.sourceLibrary, "Main package has target type \"sourceLibrary\" which generates no target - stopping build."); 809 } 810 dst.targetType = psettings.targetType; 811 dst.targetPath = psettings.targetPath; 812 dst.targetName = psettings.targetName; 813 if (!psettings.workingDirectory.empty) 814 dst.workingDirectory = processVars(psettings.workingDirectory, this, pkg, gsettings, true, [dst.environments, dst.buildEnvironments]); 815 if (psettings.mainSourceFile.length) 816 dst.mainSourceFile = processVars(psettings.mainSourceFile, this, pkg, gsettings, true, [dst.environments, dst.buildEnvironments]); 817 } 818 } 819 820 // always add all version identifiers of all packages 821 foreach (pkg; this.getTopologicalPackageList(false, null, configs)) { 822 auto psettings = pkg.getBuildSettings(gsettings.platform, configs[pkg.name]); 823 dst.addVersions(psettings.versions); 824 } 825 } 826 827 /** Fills `dst` with build settings specific to the given build type. 828 829 Params: 830 dst = The `BuildSettings` instance to add the build settings to 831 gsettings = Target generator settings 832 for_root_package = Selects if the build settings are for the root 833 package or for one of the dependencies. Unittest flags will 834 only be added to the root package. 835 */ 836 void addBuildTypeSettings(ref BuildSettings dst, in GeneratorSettings gsettings, bool for_root_package = true) 837 { 838 bool usedefflags = !(dst.requirements & BuildRequirement.noDefaultFlags); 839 if (usedefflags) { 840 BuildSettings btsettings; 841 m_rootPackage.addBuildTypeSettings(btsettings, gsettings.platform, gsettings.buildType); 842 843 if (!for_root_package) { 844 // don't propagate unittest switch to dependencies, as dependent 845 // unit tests aren't run anyway and the additional code may 846 // cause linking to fail on Windows (issue #640) 847 btsettings.removeOptions(BuildOption.unittests); 848 } 849 850 processVars(dst, this, m_rootPackage, btsettings, gsettings); 851 } 852 } 853 854 /// Outputs a build description of the project, including its dependencies. 855 ProjectDescription describe(GeneratorSettings settings) 856 { 857 import dub.generators.targetdescription; 858 859 // store basic build parameters 860 ProjectDescription ret; 861 ret.rootPackage = m_rootPackage.name; 862 ret.configuration = settings.config; 863 ret.buildType = settings.buildType; 864 ret.compiler = settings.platform.compiler; 865 ret.architecture = settings.platform.architecture; 866 ret.platform = settings.platform.platform; 867 868 // collect high level information about projects (useful for IDE display) 869 auto configs = getPackageConfigs(settings.platform, settings.config); 870 ret.packages ~= m_rootPackage.describe(settings.platform, settings.config); 871 foreach (dep; m_dependencies) 872 ret.packages ~= dep.describe(settings.platform, configs[dep.name]); 873 874 foreach (p; getTopologicalPackageList(false, null, configs)) 875 ret.packages[ret.packages.countUntil!(pp => pp.name == p.name)].active = true; 876 877 if (settings.buildType.length) { 878 // collect build target information (useful for build tools) 879 auto gen = new TargetDescriptionGenerator(this); 880 try { 881 gen.generate(settings); 882 ret.targets = gen.targetDescriptions; 883 ret.targetLookup = gen.targetDescriptionLookup; 884 } catch (Exception e) { 885 logDiagnostic("Skipping targets description: %s", e.msg); 886 logDebug("Full error: %s", e.toString().sanitize); 887 } 888 } 889 890 return ret; 891 } 892 893 private string[] listBuildSetting(string attributeName)(ref GeneratorSettings settings, 894 string config, ProjectDescription projectDescription, Compiler compiler, bool disableEscaping) 895 { 896 return listBuildSetting!attributeName(settings, getPackageConfigs(settings.platform, config), 897 projectDescription, compiler, disableEscaping); 898 } 899 900 private string[] listBuildSetting(string attributeName)(ref GeneratorSettings settings, 901 string[string] configs, ProjectDescription projectDescription, Compiler compiler, bool disableEscaping) 902 { 903 if (compiler) 904 return formatBuildSettingCompiler!attributeName(settings, configs, projectDescription, compiler, disableEscaping); 905 else 906 return formatBuildSettingPlain!attributeName(settings, configs, projectDescription); 907 } 908 909 // Output a build setting formatted for a compiler 910 private string[] formatBuildSettingCompiler(string attributeName)(ref GeneratorSettings settings, 911 string[string] configs, ProjectDescription projectDescription, Compiler compiler, bool disableEscaping) 912 { 913 import std.process : escapeShellFileName; 914 import std.path : dirSeparator; 915 916 assert(compiler); 917 918 auto targetDescription = projectDescription.lookupTarget(projectDescription.rootPackage); 919 auto buildSettings = targetDescription.buildSettings; 920 921 string[] values; 922 switch (attributeName) 923 { 924 case "dflags": 925 case "linkerFiles": 926 case "mainSourceFile": 927 case "importFiles": 928 values = formatBuildSettingPlain!attributeName(settings, configs, projectDescription); 929 break; 930 931 case "lflags": 932 case "sourceFiles": 933 case "injectSourceFiles": 934 case "versions": 935 case "debugVersions": 936 case "importPaths": 937 case "stringImportPaths": 938 case "options": 939 auto bs = buildSettings.dup; 940 bs.dflags = null; 941 942 // Ensure trailing slash on directory paths 943 auto ensureTrailingSlash = (string path) => path.endsWith(dirSeparator) ? path : path ~ dirSeparator; 944 static if (attributeName == "importPaths") 945 bs.importPaths = bs.importPaths.map!(ensureTrailingSlash).array(); 946 else static if (attributeName == "stringImportPaths") 947 bs.stringImportPaths = bs.stringImportPaths.map!(ensureTrailingSlash).array(); 948 949 compiler.prepareBuildSettings(bs, settings.platform, BuildSetting.all & ~to!BuildSetting(attributeName)); 950 values = bs.dflags; 951 break; 952 953 case "libs": 954 auto bs = buildSettings.dup; 955 bs.dflags = null; 956 bs.lflags = null; 957 bs.sourceFiles = null; 958 bs.targetType = TargetType.none; // Force Compiler to NOT omit dependency libs when package is a library. 959 960 compiler.prepareBuildSettings(bs, settings.platform, BuildSetting.all & ~to!BuildSetting(attributeName)); 961 962 if (bs.lflags) 963 values = compiler.lflagsToDFlags( bs.lflags ); 964 else if (bs.sourceFiles) 965 values = compiler.lflagsToDFlags( bs.sourceFiles ); 966 else 967 values = bs.dflags; 968 969 break; 970 971 default: assert(0); 972 } 973 974 // Escape filenames and paths 975 if(!disableEscaping) 976 { 977 switch (attributeName) 978 { 979 case "mainSourceFile": 980 case "linkerFiles": 981 case "injectSourceFiles": 982 case "copyFiles": 983 case "importFiles": 984 case "stringImportFiles": 985 case "sourceFiles": 986 case "importPaths": 987 case "stringImportPaths": 988 return values.map!(escapeShellFileName).array(); 989 990 default: 991 return values; 992 } 993 } 994 995 return values; 996 } 997 998 // Output a build setting without formatting for any particular compiler 999 private string[] formatBuildSettingPlain(string attributeName)(ref GeneratorSettings settings, string[string] configs, ProjectDescription projectDescription) 1000 { 1001 import std.path : buildNormalizedPath, dirSeparator; 1002 import std.range : only; 1003 1004 string[] list; 1005 1006 enforce(attributeName == "targetType" || projectDescription.lookupRootPackage().targetType != TargetType.none, 1007 "Target type is 'none'. Cannot list build settings."); 1008 1009 static if (attributeName == "targetType") 1010 if (projectDescription.rootPackage !in projectDescription.targetLookup) 1011 return ["none"]; 1012 1013 auto targetDescription = projectDescription.lookupTarget(projectDescription.rootPackage); 1014 auto buildSettings = targetDescription.buildSettings; 1015 1016 string[] substituteCommands(Package pack, string[] commands, CommandType type) 1017 { 1018 auto env = makeCommandEnvironmentVariables(type, pack, this, settings, buildSettings); 1019 return processVars(this, pack, settings, commands, false, env); 1020 } 1021 1022 // Return any BuildSetting member attributeName as a range of strings. Don't attempt to fixup values. 1023 // allowEmptyString: When the value is a string (as opposed to string[]), 1024 // is empty string an actual permitted value instead of 1025 // a missing value? 1026 auto getRawBuildSetting(Package pack, bool allowEmptyString) { 1027 auto value = __traits(getMember, buildSettings, attributeName); 1028 1029 static if( attributeName.endsWith("Commands") ) 1030 return substituteCommands(pack, value, mixin("CommandType.", attributeName[0 .. $ - "Commands".length])); 1031 else static if( is(typeof(value) == string[]) ) 1032 return value; 1033 else static if( is(typeof(value) == string) ) 1034 { 1035 auto ret = only(value); 1036 1037 // only() has a different return type from only(value), so we 1038 // have to empty the range rather than just returning only(). 1039 if(value.empty && !allowEmptyString) { 1040 ret.popFront(); 1041 assert(ret.empty); 1042 } 1043 1044 return ret; 1045 } 1046 else static if( is(typeof(value) == string[string]) ) 1047 return value.byKeyValue.map!(a => a.key ~ "=" ~ a.value); 1048 else static if( is(typeof(value) == enum) ) 1049 return only(value); 1050 else static if( is(typeof(value) == Flags!BuildRequirement) ) 1051 return only(cast(BuildRequirement) cast(int) value.values); 1052 else static if( is(typeof(value) == Flags!BuildOption) ) 1053 return only(cast(BuildOption) cast(int) value.values); 1054 else 1055 static assert(false, "Type of BuildSettings."~attributeName~" is unsupported."); 1056 } 1057 1058 // Adjust BuildSetting member attributeName as needed. 1059 // Returns a range of strings. 1060 auto getFixedBuildSetting(Package pack) { 1061 // Is relative path(s) to a directory? 1062 enum isRelativeDirectory = 1063 attributeName == "importPaths" || attributeName == "stringImportPaths" || 1064 attributeName == "targetPath" || attributeName == "workingDirectory"; 1065 1066 // Is relative path(s) to a file? 1067 enum isRelativeFile = 1068 attributeName == "sourceFiles" || attributeName == "linkerFiles" || 1069 attributeName == "importFiles" || attributeName == "stringImportFiles" || 1070 attributeName == "copyFiles" || attributeName == "mainSourceFile" || 1071 attributeName == "injectSourceFiles"; 1072 1073 // For these, empty string means "main project directory", not "missing value" 1074 enum allowEmptyString = 1075 attributeName == "targetPath" || attributeName == "workingDirectory"; 1076 1077 enum isEnumBitfield = 1078 attributeName == "requirements" || attributeName == "options"; 1079 1080 enum isEnum = attributeName == "targetType"; 1081 1082 auto values = getRawBuildSetting(pack, allowEmptyString); 1083 string fixRelativePath(string importPath) { return buildNormalizedPath(pack.path.toString(), importPath); } 1084 static string ensureTrailingSlash(string path) { return path.endsWith(dirSeparator) ? path : path ~ dirSeparator; } 1085 1086 static if(isRelativeDirectory) { 1087 // Return full paths for the paths, making sure a 1088 // directory separator is on the end of each path. 1089 return values.map!(fixRelativePath).map!(ensureTrailingSlash); 1090 } 1091 else static if(isRelativeFile) { 1092 // Return full paths. 1093 return values.map!(fixRelativePath); 1094 } 1095 else static if(isEnumBitfield) 1096 return bitFieldNames(values.front); 1097 else static if (isEnum) 1098 return [values.front.to!string]; 1099 else 1100 return values; 1101 } 1102 1103 foreach(value; getFixedBuildSetting(m_rootPackage)) { 1104 list ~= value; 1105 } 1106 1107 return list; 1108 } 1109 1110 // The "compiler" arg is for choosing which compiler the output should be formatted for, 1111 // or null to imply "list" format. 1112 private string[] listBuildSetting(ref GeneratorSettings settings, string[string] configs, 1113 ProjectDescription projectDescription, string requestedData, Compiler compiler, bool disableEscaping) 1114 { 1115 // Certain data cannot be formatter for a compiler 1116 if (compiler) 1117 { 1118 switch (requestedData) 1119 { 1120 case "target-type": 1121 case "target-path": 1122 case "target-name": 1123 case "working-directory": 1124 case "string-import-files": 1125 case "copy-files": 1126 case "extra-dependency-files": 1127 case "pre-generate-commands": 1128 case "post-generate-commands": 1129 case "pre-build-commands": 1130 case "post-build-commands": 1131 case "pre-run-commands": 1132 case "post-run-commands": 1133 case "environments": 1134 case "build-environments": 1135 case "run-environments": 1136 case "pre-generate-environments": 1137 case "post-generate-environments": 1138 case "pre-build-environments": 1139 case "post-build-environments": 1140 case "pre-run-environments": 1141 case "post-run-environments": 1142 enforce(false, "--data="~requestedData~" can only be used with `--data-list` or `--data-list --data-0`."); 1143 break; 1144 1145 case "requirements": 1146 enforce(false, "--data=requirements can only be used with `--data-list` or `--data-list --data-0`. Use --data=options instead."); 1147 break; 1148 1149 default: break; 1150 } 1151 } 1152 1153 import std.typetuple : TypeTuple; 1154 auto args = TypeTuple!(settings, configs, projectDescription, compiler, disableEscaping); 1155 switch (requestedData) 1156 { 1157 case "target-type": return listBuildSetting!"targetType"(args); 1158 case "target-path": return listBuildSetting!"targetPath"(args); 1159 case "target-name": return listBuildSetting!"targetName"(args); 1160 case "working-directory": return listBuildSetting!"workingDirectory"(args); 1161 case "main-source-file": return listBuildSetting!"mainSourceFile"(args); 1162 case "dflags": return listBuildSetting!"dflags"(args); 1163 case "lflags": return listBuildSetting!"lflags"(args); 1164 case "libs": return listBuildSetting!"libs"(args); 1165 case "linker-files": return listBuildSetting!"linkerFiles"(args); 1166 case "source-files": return listBuildSetting!"sourceFiles"(args); 1167 case "inject-source-files": return listBuildSetting!"injectSourceFiles"(args); 1168 case "copy-files": return listBuildSetting!"copyFiles"(args); 1169 case "extra-dependency-files": return listBuildSetting!"extraDependencyFiles"(args); 1170 case "versions": return listBuildSetting!"versions"(args); 1171 case "debug-versions": return listBuildSetting!"debugVersions"(args); 1172 case "import-paths": return listBuildSetting!"importPaths"(args); 1173 case "string-import-paths": return listBuildSetting!"stringImportPaths"(args); 1174 case "import-files": return listBuildSetting!"importFiles"(args); 1175 case "string-import-files": return listBuildSetting!"stringImportFiles"(args); 1176 case "pre-generate-commands": return listBuildSetting!"preGenerateCommands"(args); 1177 case "post-generate-commands": return listBuildSetting!"postGenerateCommands"(args); 1178 case "pre-build-commands": return listBuildSetting!"preBuildCommands"(args); 1179 case "post-build-commands": return listBuildSetting!"postBuildCommands"(args); 1180 case "pre-run-commands": return listBuildSetting!"preRunCommands"(args); 1181 case "post-run-commands": return listBuildSetting!"postRunCommands"(args); 1182 case "environments": return listBuildSetting!"environments"(args); 1183 case "build-environments": return listBuildSetting!"buildEnvironments"(args); 1184 case "run-environments": return listBuildSetting!"runEnvironments"(args); 1185 case "pre-generate-environments": return listBuildSetting!"preGenerateEnvironments"(args); 1186 case "post-generate-environments": return listBuildSetting!"postGenerateEnvironments"(args); 1187 case "pre-build-environments": return listBuildSetting!"preBuildEnvironments"(args); 1188 case "post-build-environments": return listBuildSetting!"postBuildEnvironments"(args); 1189 case "pre-run-environments": return listBuildSetting!"preRunEnvironments"(args); 1190 case "post-run-environments": return listBuildSetting!"postRunEnvironments"(args); 1191 case "requirements": return listBuildSetting!"requirements"(args); 1192 case "options": return listBuildSetting!"options"(args); 1193 1194 default: 1195 enforce(false, "--data="~requestedData~ 1196 " is not a valid option. See 'dub describe --help' for accepted --data= values."); 1197 } 1198 1199 assert(0); 1200 } 1201 1202 /// Outputs requested data for the project, optionally including its dependencies. 1203 string[] listBuildSettings(GeneratorSettings settings, string[] requestedData, ListBuildSettingsFormat list_type) 1204 { 1205 import dub.compilers.utils : isLinkerFile; 1206 1207 auto projectDescription = describe(settings); 1208 auto configs = getPackageConfigs(settings.platform, settings.config); 1209 PackageDescription packageDescription; 1210 foreach (pack; projectDescription.packages) { 1211 if (pack.name == projectDescription.rootPackage) 1212 packageDescription = pack; 1213 } 1214 1215 if (projectDescription.rootPackage in projectDescription.targetLookup) { 1216 // Copy linker files from sourceFiles to linkerFiles 1217 auto target = projectDescription.lookupTarget(projectDescription.rootPackage); 1218 foreach (file; target.buildSettings.sourceFiles.filter!(f => isLinkerFile(settings.platform, f))) 1219 target.buildSettings.addLinkerFiles(file); 1220 1221 // Remove linker files from sourceFiles 1222 target.buildSettings.sourceFiles = 1223 target.buildSettings.sourceFiles 1224 .filter!(a => !isLinkerFile(settings.platform, a)) 1225 .array(); 1226 projectDescription.lookupTarget(projectDescription.rootPackage) = target; 1227 } 1228 1229 Compiler compiler; 1230 bool no_escape; 1231 final switch (list_type) with (ListBuildSettingsFormat) { 1232 case list: break; 1233 case listNul: no_escape = true; break; 1234 case commandLine: compiler = settings.compiler; break; 1235 case commandLineNul: compiler = settings.compiler; no_escape = true; break; 1236 1237 } 1238 1239 auto result = requestedData 1240 .map!(dataName => listBuildSetting(settings, configs, projectDescription, dataName, compiler, no_escape)); 1241 1242 final switch (list_type) with (ListBuildSettingsFormat) { 1243 case list: return result.map!(l => l.join("\n")).array(); 1244 case listNul: return result.map!(l => l.join("\0")).array; 1245 case commandLine: return result.map!(l => l.join(" ")).array; 1246 case commandLineNul: return result.map!(l => l.join("\0")).array; 1247 } 1248 } 1249 1250 /** Saves the currently selected dependency versions to disk. 1251 1252 The selections will be written to a file named 1253 `SelectedVersions.defaultFile` ("dub.selections.json") within the 1254 directory of the root package. Any existing file will get overwritten. 1255 */ 1256 void saveSelections() 1257 { 1258 assert(m_selections !is null, "Cannot save selections for non-disk based project (has no selections)."); 1259 if (m_selections.hasSelectedVersion(m_rootPackage.basePackage.name)) 1260 m_selections.deselectVersion(m_rootPackage.basePackage.name); 1261 1262 auto path = m_rootPackage.path ~ SelectedVersions.defaultFile; 1263 if (m_selections.dirty || !existsFile(path)) 1264 m_selections.save(path); 1265 } 1266 1267 deprecated bool isUpgradeCacheUpToDate() 1268 { 1269 return false; 1270 } 1271 1272 deprecated Dependency[string] getUpgradeCache() 1273 { 1274 return null; 1275 } 1276 } 1277 1278 1279 /// Determines the output format used for `Project.listBuildSettings`. 1280 enum ListBuildSettingsFormat { 1281 list, /// Newline separated list entries 1282 listNul, /// NUL character separated list entries (unescaped) 1283 commandLine, /// Formatted for compiler command line (one data list per line) 1284 commandLineNul, /// NUL character separated list entries (unescaped, data lists separated by two NUL characters) 1285 } 1286 1287 deprecated("Use `dub.packagemanager : PlacementLocation` instead") 1288 public alias PlacementLocation = dub.packagemanager.PlacementLocation; 1289 1290 void processVars(ref BuildSettings dst, in Project project, in Package pack, 1291 BuildSettings settings, in GeneratorSettings gsettings, bool include_target_settings = false) 1292 { 1293 string[string] processVerEnvs(in string[string] targetEnvs, in string[string] defaultEnvs) 1294 { 1295 string[string] retEnv; 1296 foreach (k, v; targetEnvs) 1297 retEnv[k] = v; 1298 foreach (k, v; defaultEnvs) { 1299 if (k !in targetEnvs) 1300 retEnv[k] = v; 1301 } 1302 return processVars(project, pack, gsettings, retEnv); 1303 } 1304 dst.addEnvironments(processVerEnvs(settings.environments, gsettings.buildSettings.environments)); 1305 dst.addBuildEnvironments(processVerEnvs(settings.buildEnvironments, gsettings.buildSettings.buildEnvironments)); 1306 dst.addRunEnvironments(processVerEnvs(settings.runEnvironments, gsettings.buildSettings.runEnvironments)); 1307 dst.addPreGenerateEnvironments(processVerEnvs(settings.preGenerateEnvironments, gsettings.buildSettings.preGenerateEnvironments)); 1308 dst.addPostGenerateEnvironments(processVerEnvs(settings.postGenerateEnvironments, gsettings.buildSettings.postGenerateEnvironments)); 1309 dst.addPreBuildEnvironments(processVerEnvs(settings.preBuildEnvironments, gsettings.buildSettings.preBuildEnvironments)); 1310 dst.addPostBuildEnvironments(processVerEnvs(settings.postBuildEnvironments, gsettings.buildSettings.postBuildEnvironments)); 1311 dst.addPreRunEnvironments(processVerEnvs(settings.preRunEnvironments, gsettings.buildSettings.preRunEnvironments)); 1312 dst.addPostRunEnvironments(processVerEnvs(settings.postRunEnvironments, gsettings.buildSettings.postRunEnvironments)); 1313 1314 auto buildEnvs = [dst.environments, dst.buildEnvironments]; 1315 1316 dst.addDFlags(processVars(project, pack, gsettings, settings.dflags, false, buildEnvs)); 1317 dst.addLFlags(processVars(project, pack, gsettings, settings.lflags, false, buildEnvs)); 1318 dst.addLibs(processVars(project, pack, gsettings, settings.libs, false, buildEnvs)); 1319 dst.addSourceFiles(processVars!true(project, pack, gsettings, settings.sourceFiles, true, buildEnvs)); 1320 dst.addImportFiles(processVars(project, pack, gsettings, settings.importFiles, true, buildEnvs)); 1321 dst.addStringImportFiles(processVars(project, pack, gsettings, settings.stringImportFiles, true, buildEnvs)); 1322 dst.addInjectSourceFiles(processVars!true(project, pack, gsettings, settings.injectSourceFiles, true, buildEnvs)); 1323 dst.addCopyFiles(processVars(project, pack, gsettings, settings.copyFiles, true, buildEnvs)); 1324 dst.addExtraDependencyFiles(processVars(project, pack, gsettings, settings.extraDependencyFiles, true, buildEnvs)); 1325 dst.addVersions(processVars(project, pack, gsettings, settings.versions, false, buildEnvs)); 1326 dst.addDebugVersions(processVars(project, pack, gsettings, settings.debugVersions, false, buildEnvs)); 1327 dst.addVersionFilters(processVars(project, pack, gsettings, settings.versionFilters, false, buildEnvs)); 1328 dst.addDebugVersionFilters(processVars(project, pack, gsettings, settings.debugVersionFilters, false, buildEnvs)); 1329 dst.addImportPaths(processVars(project, pack, gsettings, settings.importPaths, true, buildEnvs)); 1330 dst.addStringImportPaths(processVars(project, pack, gsettings, settings.stringImportPaths, true, buildEnvs)); 1331 dst.addRequirements(settings.requirements); 1332 dst.addOptions(settings.options); 1333 1334 // commands are substituted in dub.generators.generator : runBuildCommands 1335 dst.addPreGenerateCommands(settings.preGenerateCommands); 1336 dst.addPostGenerateCommands(settings.postGenerateCommands); 1337 dst.addPreBuildCommands(settings.preBuildCommands); 1338 dst.addPostBuildCommands(settings.postBuildCommands); 1339 dst.addPreRunCommands(settings.preRunCommands); 1340 dst.addPostRunCommands(settings.postRunCommands); 1341 1342 if (include_target_settings) { 1343 dst.targetType = settings.targetType; 1344 dst.targetPath = processVars(settings.targetPath, project, pack, gsettings, true, buildEnvs); 1345 dst.targetName = settings.targetName; 1346 if (!settings.workingDirectory.empty) 1347 dst.workingDirectory = processVars(settings.workingDirectory, project, pack, gsettings, true, buildEnvs); 1348 if (settings.mainSourceFile.length) 1349 dst.mainSourceFile = processVars(settings.mainSourceFile, project, pack, gsettings, true, buildEnvs); 1350 } 1351 } 1352 1353 string[] processVars(bool glob = false)(in Project project, in Package pack, in GeneratorSettings gsettings, in string[] vars, bool are_paths = false, in string[string][] extraVers = null) 1354 { 1355 auto ret = appender!(string[])(); 1356 processVars!glob(ret, project, pack, gsettings, vars, are_paths, extraVers); 1357 return ret.data; 1358 } 1359 void processVars(bool glob = false)(ref Appender!(string[]) dst, in Project project, in Package pack, in GeneratorSettings gsettings, in string[] vars, bool are_paths = false, in string[string][] extraVers = null) 1360 { 1361 static if (glob) 1362 alias process = processVarsWithGlob!(Project, Package); 1363 else 1364 alias process = processVars!(Project, Package); 1365 foreach (var; vars) 1366 dst.put(process(var, project, pack, gsettings, are_paths, extraVers)); 1367 } 1368 1369 string processVars(Project, Package)(string var, in Project project, in Package pack, in GeneratorSettings gsettings, bool is_path, in string[string][] extraVers = null) 1370 { 1371 var = var.expandVars!(varName => getVariable(varName, project, pack, gsettings, extraVers)); 1372 if (!is_path) 1373 return var; 1374 auto p = NativePath(var); 1375 if (!p.absolute) 1376 return (pack.path ~ p).toNativeString(); 1377 else 1378 return p.toNativeString(); 1379 } 1380 string[string] processVars(bool glob = false)(in Project project, in Package pack, in GeneratorSettings gsettings, in string[string] vars, in string[string][] extraVers = null) 1381 { 1382 string[string] ret; 1383 processVars!glob(ret, project, pack, gsettings, vars, extraVers); 1384 return ret; 1385 } 1386 void processVars(bool glob = false)(ref string[string] dst, in Project project, in Package pack, in GeneratorSettings gsettings, in string[string] vars, in string[string][] extraVers) 1387 { 1388 static if (glob) 1389 alias process = processVarsWithGlob!(Project, Package); 1390 else 1391 alias process = processVars!(Project, Package); 1392 foreach (k, var; vars) 1393 dst[k] = process(var, project, pack, gsettings, false, extraVers); 1394 } 1395 1396 private string[] processVarsWithGlob(Project, Package)(string var, in Project project, in Package pack, in GeneratorSettings gsettings, bool is_path, in string[string][] extraVers) 1397 { 1398 assert(is_path, "can't glob something that isn't a path"); 1399 string res = processVars(var, project, pack, gsettings, is_path, extraVers); 1400 // Find the unglobbed prefix and iterate from there. 1401 size_t i = 0; 1402 size_t sepIdx = 0; 1403 loop: while (i < res.length) { 1404 switch_: switch (res[i]) 1405 { 1406 case '*', '?', '[', '{': break loop; 1407 case '/': sepIdx = i; goto default; 1408 version (Windows) { case '\\': sepIdx = i; goto default; } 1409 default: ++i; break switch_; 1410 } 1411 } 1412 if (i == res.length) //no globbing found in the path 1413 return [res]; 1414 import std.file : dirEntries, SpanMode; 1415 import std.path : buildNormalizedPath, globMatch, isAbsolute, relativePath; 1416 auto cwd = gsettings.toolWorkingDirectory.toNativeString; 1417 auto path = res[0 .. sepIdx]; 1418 bool prependCwd = false; 1419 if (!isAbsolute(path)) 1420 { 1421 prependCwd = true; 1422 path = buildNormalizedPath(cwd, path); 1423 } 1424 1425 return dirEntries(path, SpanMode.depth) 1426 .map!(de => prependCwd 1427 ? de.name.relativePath(cwd) 1428 : de.name) 1429 .filter!(name => globMatch(name, res)) 1430 .array; 1431 } 1432 /// Expand variables using `$VAR_NAME` or `${VAR_NAME}` syntax. 1433 /// `$$` escapes itself and is expanded to a single `$`. 1434 private string expandVars(alias expandVar)(string s) 1435 { 1436 import std.functional : not; 1437 1438 auto result = appender!string; 1439 1440 static bool isVarChar(char c) 1441 { 1442 import std.ascii; 1443 return isAlphaNum(c) || c == '_'; 1444 } 1445 1446 while (true) 1447 { 1448 auto pos = s.indexOf('$'); 1449 if (pos < 0) 1450 { 1451 result.put(s); 1452 return result.data; 1453 } 1454 result.put(s[0 .. pos]); 1455 s = s[pos + 1 .. $]; 1456 enforce(s.length > 0, "Variable name expected at end of string"); 1457 switch (s[0]) 1458 { 1459 case '$': 1460 result.put("$"); 1461 s = s[1 .. $]; 1462 break; 1463 case '{': 1464 pos = s.indexOf('}'); 1465 enforce(pos >= 0, "Could not find '}' to match '${'"); 1466 result.put(expandVar(s[1 .. pos])); 1467 s = s[pos + 1 .. $]; 1468 break; 1469 default: 1470 pos = s.representation.countUntil!(not!isVarChar); 1471 if (pos < 0) 1472 pos = s.length; 1473 result.put(expandVar(s[0 .. pos])); 1474 s = s[pos .. $]; 1475 break; 1476 } 1477 } 1478 } 1479 1480 unittest 1481 { 1482 string[string] vars = 1483 [ 1484 "A" : "a", 1485 "B" : "b", 1486 ]; 1487 1488 string expandVar(string name) { auto p = name in vars; enforce(p, name); return *p; } 1489 1490 assert(expandVars!expandVar("") == ""); 1491 assert(expandVars!expandVar("x") == "x"); 1492 assert(expandVars!expandVar("$$") == "$"); 1493 assert(expandVars!expandVar("x$$") == "x$"); 1494 assert(expandVars!expandVar("$$x") == "$x"); 1495 assert(expandVars!expandVar("$$$$") == "$$"); 1496 assert(expandVars!expandVar("x$A") == "xa"); 1497 assert(expandVars!expandVar("x$$A") == "x$A"); 1498 assert(expandVars!expandVar("$A$B") == "ab"); 1499 assert(expandVars!expandVar("${A}$B") == "ab"); 1500 assert(expandVars!expandVar("$A${B}") == "ab"); 1501 assert(expandVars!expandVar("a${B}") == "ab"); 1502 assert(expandVars!expandVar("${A}b") == "ab"); 1503 1504 import std.exception : assertThrown; 1505 assertThrown(expandVars!expandVar("$")); 1506 assertThrown(expandVars!expandVar("${}")); 1507 assertThrown(expandVars!expandVar("$|")); 1508 assertThrown(expandVars!expandVar("x$")); 1509 assertThrown(expandVars!expandVar("$X")); 1510 assertThrown(expandVars!expandVar("${")); 1511 assertThrown(expandVars!expandVar("${X")); 1512 1513 // https://github.com/dlang/dmd/pull/9275 1514 assert(expandVars!expandVar("$${DUB_EXE:-dub}") == "${DUB_EXE:-dub}"); 1515 } 1516 1517 /// Expands the variables in the input string with the same rules as command 1518 /// variables inside custom dub commands. 1519 /// 1520 /// Params: 1521 /// s = the input string where environment variables in form `$VAR` should be replaced 1522 /// throwIfMissing = if true, throw an exception if the given variable is not found, 1523 /// otherwise replace unknown variables with the empty string. 1524 string expandEnvironmentVariables(string s, bool throwIfMissing = true) 1525 { 1526 import std.process : environment; 1527 1528 return expandVars!((v) { 1529 auto ret = environment.get(v); 1530 if (ret is null && throwIfMissing) 1531 throw new Exception("Specified environment variable `$" ~ v ~ "` is not set"); 1532 return ret; 1533 })(s); 1534 } 1535 1536 // Keep the following list up-to-date if adding more build settings variables. 1537 /// List of variables that can be used in build settings 1538 package(dub) immutable buildSettingsVars = [ 1539 "ARCH", "PLATFORM", "PLATFORM_POSIX", "BUILD_TYPE" 1540 ]; 1541 1542 private string getVariable(Project, Package)(string name, in Project project, in Package pack, in GeneratorSettings gsettings, in string[string][] extraVars = null) 1543 { 1544 import dub.internal.utils : getDUBExePath; 1545 import std.process : environment, escapeShellFileName; 1546 import std.uni : asUpperCase; 1547 1548 NativePath path; 1549 if (name == "PACKAGE_DIR") 1550 path = pack.path; 1551 else if (name == "ROOT_PACKAGE_DIR") 1552 path = project.rootPackage.path; 1553 1554 if (name.endsWith("_PACKAGE_DIR")) { 1555 auto pname = name[0 .. $-12]; 1556 foreach (prj; project.getTopologicalPackageList()) 1557 if (prj.name.asUpperCase.map!(a => a == '-' ? '_' : a).equal(pname)) 1558 { 1559 path = prj.path; 1560 break; 1561 } 1562 } 1563 1564 if (!path.empty) 1565 { 1566 // no trailing slash for clean path concatenation (see #1392) 1567 path.endsWithSlash = false; 1568 return path.toNativeString(); 1569 } 1570 1571 if (name == "DUB") { 1572 return getDUBExePath(gsettings.platform.compilerBinary).toNativeString(); 1573 } 1574 1575 if (name == "ARCH") { 1576 foreach (a; gsettings.platform.architecture) 1577 return a; 1578 return ""; 1579 } 1580 1581 if (name == "PLATFORM") { 1582 import std.algorithm : filter; 1583 foreach (p; gsettings.platform.platform.filter!(p => p != "posix")) 1584 return p; 1585 foreach (p; gsettings.platform.platform) 1586 return p; 1587 return ""; 1588 } 1589 1590 if (name == "PLATFORM_POSIX") { 1591 import std.algorithm : canFind; 1592 if (gsettings.platform.platform.canFind("posix")) 1593 return "posix"; 1594 foreach (p; gsettings.platform.platform) 1595 return p; 1596 return ""; 1597 } 1598 1599 if (name == "BUILD_TYPE") return gsettings.buildType; 1600 1601 if (name == "DFLAGS" || name == "LFLAGS") 1602 { 1603 auto buildSettings = pack.getBuildSettings(gsettings.platform, gsettings.config); 1604 if (name == "DFLAGS") 1605 return join(buildSettings.dflags," "); 1606 else if (name == "LFLAGS") 1607 return join(buildSettings.lflags," "); 1608 } 1609 1610 import std.range; 1611 foreach (aa; retro(extraVars)) 1612 if (auto exvar = name in aa) 1613 return *exvar; 1614 1615 auto envvar = environment.get(name); 1616 if (envvar !is null) return envvar; 1617 1618 throw new Exception("Invalid variable: "~name); 1619 } 1620 1621 1622 unittest 1623 { 1624 static struct MockPackage 1625 { 1626 this(string name) 1627 { 1628 this.name = name; 1629 version (Posix) 1630 path = NativePath("/pkgs/"~name); 1631 else version (Windows) 1632 path = NativePath(`C:\pkgs\`~name); 1633 // see 4d4017c14c, #268, and #1392 for why this all package paths end on slash internally 1634 path.endsWithSlash = true; 1635 } 1636 string name; 1637 NativePath path; 1638 BuildSettings getBuildSettings(in BuildPlatform platform, string config) const 1639 { 1640 return BuildSettings(); 1641 } 1642 } 1643 1644 static struct MockProject 1645 { 1646 MockPackage rootPackage; 1647 inout(MockPackage)[] getTopologicalPackageList() inout 1648 { 1649 return _dependencies; 1650 } 1651 private: 1652 MockPackage[] _dependencies; 1653 } 1654 1655 MockProject proj = { 1656 rootPackage: MockPackage("root"), 1657 _dependencies: [MockPackage("dep1"), MockPackage("dep2")] 1658 }; 1659 auto pack = MockPackage("test"); 1660 GeneratorSettings gsettings; 1661 enum isPath = true; 1662 1663 import std.path : dirSeparator; 1664 1665 static NativePath woSlash(NativePath p) { p.endsWithSlash = false; return p; } 1666 // basic vars 1667 assert(processVars("Hello $PACKAGE_DIR", proj, pack, gsettings, !isPath) == "Hello "~woSlash(pack.path).toNativeString); 1668 assert(processVars("Hello $ROOT_PACKAGE_DIR", proj, pack, gsettings, !isPath) == "Hello "~woSlash(proj.rootPackage.path).toNativeString.chomp(dirSeparator)); 1669 assert(processVars("Hello $DEP1_PACKAGE_DIR", proj, pack, gsettings, !isPath) == "Hello "~woSlash(proj._dependencies[0].path).toNativeString); 1670 // ${VAR} replacements 1671 assert(processVars("Hello ${PACKAGE_DIR}"~dirSeparator~"foobar", proj, pack, gsettings, !isPath) == "Hello "~(pack.path ~ "foobar").toNativeString); 1672 assert(processVars("Hello $PACKAGE_DIR"~dirSeparator~"foobar", proj, pack, gsettings, !isPath) == "Hello "~(pack.path ~ "foobar").toNativeString); 1673 // test with isPath 1674 assert(processVars("local", proj, pack, gsettings, isPath) == (pack.path ~ "local").toNativeString); 1675 assert(processVars("foo/$$ESCAPED", proj, pack, gsettings, isPath) == (pack.path ~ "foo/$ESCAPED").toNativeString); 1676 assert(processVars("$$ESCAPED", proj, pack, gsettings, !isPath) == "$ESCAPED"); 1677 // test other env variables 1678 import std.process : environment; 1679 environment["MY_ENV_VAR"] = "blablabla"; 1680 assert(processVars("$MY_ENV_VAR", proj, pack, gsettings, !isPath) == "blablabla"); 1681 assert(processVars("${MY_ENV_VAR}suffix", proj, pack, gsettings, !isPath) == "blablablasuffix"); 1682 assert(processVars("$MY_ENV_VAR-suffix", proj, pack, gsettings, !isPath) == "blablabla-suffix"); 1683 assert(processVars("$MY_ENV_VAR:suffix", proj, pack, gsettings, !isPath) == "blablabla:suffix"); 1684 assert(processVars("$MY_ENV_VAR$MY_ENV_VAR", proj, pack, gsettings, !isPath) == "blablablablablabla"); 1685 environment.remove("MY_ENV_VAR"); 1686 } 1687 1688 /** Holds and stores a set of version selections for package dependencies. 1689 1690 This is the runtime representation of the information contained in 1691 "dub.selections.json" within a package's directory. 1692 */ 1693 final class SelectedVersions { 1694 private { 1695 enum FileVersion = 1; 1696 Selected m_selections; 1697 bool m_dirty = false; // has changes since last save 1698 bool m_bare = true; 1699 } 1700 1701 /// Default file name to use for storing selections. 1702 enum defaultFile = "dub.selections.json"; 1703 1704 /// Constructs a new empty version selection. 1705 public this(uint version_ = FileVersion) @safe pure nothrow @nogc 1706 { 1707 this.m_selections = Selected(version_); 1708 } 1709 1710 /// Constructs a new non-empty version selection. 1711 public this(Selected data) @safe pure nothrow @nogc 1712 { 1713 this.m_selections = data; 1714 this.m_bare = false; 1715 } 1716 1717 /** Constructs a new version selection from JSON data. 1718 1719 The structure of the JSON document must match the contents of the 1720 "dub.selections.json" file. 1721 */ 1722 deprecated("Pass a `dub.recipe.selection : Selected` directly") 1723 this(Json data) 1724 { 1725 deserialize(data); 1726 m_dirty = false; 1727 } 1728 1729 /** Constructs a new version selections from an existing JSON file. 1730 */ 1731 deprecated("JSON deserialization is deprecated") 1732 this(NativePath path) 1733 { 1734 auto json = jsonFromFile(path); 1735 deserialize(json); 1736 m_dirty = false; 1737 m_bare = false; 1738 } 1739 1740 /// Returns a list of names for all packages that have a version selection. 1741 @property string[] selectedPackages() const { return m_selections.versions.keys; } 1742 1743 /// Determines if any changes have been made after loading the selections from a file. 1744 @property bool dirty() const { return m_dirty; } 1745 1746 /// Determine if this set of selections is still empty (but not `clear`ed). 1747 @property bool bare() const { return m_bare && !m_dirty; } 1748 1749 /// Removes all selections. 1750 void clear() 1751 { 1752 m_selections.versions = null; 1753 m_dirty = true; 1754 } 1755 1756 /// Duplicates the set of selected versions from another instance. 1757 void set(SelectedVersions versions) 1758 { 1759 m_selections.fileVersion = versions.m_selections.fileVersion; 1760 m_selections.versions = versions.m_selections.versions.dup; 1761 m_dirty = true; 1762 } 1763 1764 /// Selects a certain version for a specific package. 1765 void selectVersion(string package_id, Version version_) 1766 { 1767 if (auto pdep = package_id in m_selections.versions) { 1768 if (*pdep == Dependency(version_)) 1769 return; 1770 } 1771 m_selections.versions[package_id] = Dependency(version_); 1772 m_dirty = true; 1773 } 1774 1775 /// Selects a certain path for a specific package. 1776 void selectVersion(string package_id, NativePath path) 1777 { 1778 if (auto pdep = package_id in m_selections.versions) { 1779 if (*pdep == Dependency(path)) 1780 return; 1781 } 1782 m_selections.versions[package_id] = Dependency(path); 1783 m_dirty = true; 1784 } 1785 1786 /// Selects a certain Git reference for a specific package. 1787 void selectVersion(string package_id, Repository repository) 1788 { 1789 const dependency = Dependency(repository); 1790 if (auto pdep = package_id in m_selections.versions) { 1791 if (*pdep == dependency) 1792 return; 1793 } 1794 m_selections.versions[package_id] = dependency; 1795 m_dirty = true; 1796 } 1797 1798 deprecated("Move `spec` inside of the `repository` parameter and call `selectVersion`") 1799 void selectVersionWithRepository(string package_id, Repository repository, string spec) 1800 { 1801 this.selectVersion(package_id, Repository(repository.remote(), spec)); 1802 } 1803 1804 /// Removes the selection for a particular package. 1805 void deselectVersion(string package_id) 1806 { 1807 m_selections.versions.remove(package_id); 1808 m_dirty = true; 1809 } 1810 1811 /// Determines if a particular package has a selection set. 1812 bool hasSelectedVersion(string packageId) 1813 const { 1814 return (packageId in m_selections.versions) !is null; 1815 } 1816 1817 /** Returns the selection for a particular package. 1818 1819 Note that the returned `Dependency` can either have the 1820 `Dependency.path` property set to a non-empty value, in which case this 1821 is a path based selection, or its `Dependency.version_` property is 1822 valid and it is a version selection. 1823 */ 1824 Dependency getSelectedVersion(string packageId) 1825 const { 1826 enforce(hasSelectedVersion(packageId)); 1827 return m_selections.versions[packageId]; 1828 } 1829 1830 /** Stores the selections to disk. 1831 1832 The target file will be written in JSON format. Usually, `defaultFile` 1833 should be used as the file name and the directory should be the root 1834 directory of the project's root package. 1835 */ 1836 void save(NativePath path) 1837 { 1838 Json json = serialize(); 1839 auto result = appender!string(); 1840 1841 assert(json.type == Json.Type.object); 1842 assert(json.length == 2); 1843 assert(json["versions"].type != Json.Type.undefined); 1844 1845 result.put("{\n\t\"fileVersion\": "); 1846 result.writeJsonString(json["fileVersion"]); 1847 result.put(",\n\t\"versions\": {"); 1848 auto vers = json["versions"].get!(Json[string]); 1849 bool first = true; 1850 foreach (k; vers.byKey.array.sort()) { 1851 if (!first) result.put(","); 1852 else first = false; 1853 result.put("\n\t\t"); 1854 result.writeJsonString(Json(k)); 1855 result.put(": "); 1856 result.writeJsonString(vers[k]); 1857 } 1858 result.put("\n\t}\n}\n"); 1859 path.writeFile(result.data); 1860 m_dirty = false; 1861 m_bare = false; 1862 } 1863 1864 deprecated("Use `dub.dependency : Dependency.toJson(true)`") 1865 static Json dependencyToJson(Dependency d) 1866 { 1867 return d.toJson(true); 1868 } 1869 1870 deprecated("JSON deserialization is deprecated") 1871 static Dependency dependencyFromJson(Json j) 1872 { 1873 if (j.type == Json.Type..string) 1874 return Dependency(Version(j.get!string)); 1875 else if (j.type == Json.Type.object && "path" in j) 1876 return Dependency(NativePath(j["path"].get!string)); 1877 else if (j.type == Json.Type.object && "repository" in j) 1878 return Dependency(Repository(j["repository"].get!string, 1879 enforce("version" in j, "Expected \"version\" field in repository version object").get!string)); 1880 else throw new Exception(format("Unexpected type for dependency: %s", j)); 1881 } 1882 1883 Json serialize() 1884 const { 1885 Json json = serializeToJson(m_selections); 1886 Json serialized = Json.emptyObject; 1887 serialized["fileVersion"] = m_selections.fileVersion; 1888 serialized["versions"] = Json.emptyObject; 1889 foreach (p, dep; m_selections.versions) 1890 serialized["versions"][p] = dep.toJson(true); 1891 return serialized; 1892 } 1893 1894 deprecated("JSON deserialization is deprecated") 1895 private void deserialize(Json json) 1896 { 1897 const fileVersion = json["fileVersion"].get!int; 1898 enforce(fileVersion == FileVersion, "Mismatched dub.selections.json version: " ~ to!string(fileVersion) ~ " vs. " ~ to!string(FileVersion)); 1899 clear(); 1900 m_selections.fileVersion = fileVersion; 1901 scope(failure) clear(); 1902 foreach (string p, dep; json["versions"]) 1903 m_selections.versions[p] = dependencyFromJson(dep); 1904 } 1905 } 1906 1907 /// The template code from which the test runner is generated 1908 private immutable TestRunnerTemplate = q{ 1909 module dub_test_root; 1910 1911 import std.typetuple; 1912 1913 %-(static import %s; 1914 %); 1915 1916 alias allModules = TypeTuple!( 1917 %-(%s, %) 1918 ); 1919 1920 %s 1921 }; 1922 1923 /// The default test runner that gets used if none is provided 1924 private immutable DefaultTestRunnerCode = q{ 1925 version(D_BetterC) { 1926 extern(C) int main() { 1927 foreach (module_; allModules) { 1928 foreach (unitTest; __traits(getUnitTests, module_)) { 1929 unitTest(); 1930 } 1931 } 1932 import core.stdc.stdio : puts; 1933 puts("All unit tests have been run successfully."); 1934 return 0; 1935 } 1936 } else { 1937 void main() { 1938 version (D_Coverage) { 1939 } else { 1940 import std.stdio : writeln; 1941 writeln("All unit tests have been run successfully."); 1942 } 1943 } 1944 shared static this() { 1945 version (Have_tested) { 1946 import tested; 1947 import core.runtime; 1948 import std.exception; 1949 Runtime.moduleUnitTester = () => true; 1950 enforce(runUnitTests!allModules(new ConsoleTestResultWriter), "Unit tests failed."); 1951 } 1952 } 1953 } 1954 };