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