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