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