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