1 /**
2  * Code to manage a D checkout and its dependencies.
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <vladimir@thecybershadow.net>
12  */
13 
14 module ae.sys.d.manager;
15 
16 import std.algorithm;
17 import std.array;
18 import std.conv;
19 import std.datetime;
20 import std.exception;
21 import std.file;
22 import std.json : parseJSON;
23 import std.path;
24 import std.process : spawnProcess, wait, escapeShellCommand;
25 import std.range;
26 import std.regex;
27 import std..string;
28 import std.typecons;
29 
30 import ae.net.github.rest;
31 import ae.sys.d.cache;
32 import ae.sys.d.repo;
33 import ae.sys.file;
34 import ae.sys.git;
35 import ae.utils.aa;
36 import ae.utils.array;
37 import ae.utils.digest;
38 import ae.utils.json;
39 import ae.utils.meta;
40 import ae.utils.regex;
41 
42 alias ensureDirExists = ae.sys.file.ensureDirExists;
43 
44 version (Windows)
45 {
46 	import ae.sys.install.dmc;
47 	import ae.sys.install.msys;
48 	import ae.sys.install.vs;
49 
50 	import ae.sys.windows.misc;
51 
52 	extern(Windows) void SetErrorMode(int);
53 }
54 
55 import ae.sys.install.dmd;
56 import ae.sys.install.git;
57 import ae.sys.install.kindlegen;
58 
59 static import std.process;
60 
61 /// Class which manages a D checkout and its dependencies.
62 class DManager : ICacheHost
63 {
64 	// **************************** Configuration ****************************
65 
66 	struct Config /// DManager configuration.
67 	{
68 		struct Build /// Build configuration
69 		{
70 			struct Components
71 			{
72 				bool[string] enable;
73 
74 				string[] getEnabledComponentNames()
75 				{
76 					foreach (componentName; enable.byKey)
77 						enforce(allComponents.canFind(componentName), "Unknown component: " ~ componentName);
78 					return allComponents
79 						.filter!(componentName =>
80 							enable.get(componentName, defaultComponents.canFind(componentName)))
81 						.array
82 						.dup;
83 				}
84 
85 				Component.CommonConfig common;
86 				DMD.Config dmd;
87 				Website.Config website;
88 			}
89 			Components components;
90 
91 			/// Additional environment variables.
92 			/// Supports %VAR% expansion - see applyEnv.
93 			string[string] environment;
94 
95 			/// Optional cache key.
96 			/// Can be used to force a rebuild and bypass the cache for one build.
97 			string cacheKey;
98 		}
99 		Build build; /// ditto
100 
101 		/// Machine-local configuration
102 		/// These settings should not affect the build output.
103 		struct Local
104 		{
105 			/// URL of D git repository hosting D components.
106 			/// Defaults to (and must have the layout of) D.git:
107 			/// https://github.com/CyberShadow/D-dot-git
108 			string repoUrl = "https://bitbucket.org/cybershadow/d.git";
109 
110 			/// Location for the checkout, temporary files, etc.
111 			string workDir;
112 
113 			/// If present, passed to GNU make via -j parameter.
114 			/// Can also be "auto" or "unlimited".
115 			string makeJobs;
116 
117 			/// Don't get latest updates from GitHub.
118 			bool offline;
119 
120 			/// How to cache built files.
121 			string cache;
122 
123 			/// Maximum execution time, in seconds, of any single
124 			/// command.
125 			int timeout;
126 
127 			/// API token to access the GitHub REST API (optional).
128 			string githubToken;
129 		}
130 		Local local; /// ditto
131 	}
132 	Config config; /// ditto
133 
134 	// Behavior options that generally depend on the host program.
135 
136 	/// Automatically re-clone the repository in case
137 	/// "git reset --hard" fails.
138 	bool autoClean;
139 
140 	/// Whether to verify working tree state
141 	/// to make sure we don't clobber user changes
142 	bool verifyWorkTree;
143 
144 	/// Whether we should cache failed builds.
145 	bool cacheFailures = true;
146 
147 	/// Current build environment.
148 	struct Environment
149 	{
150 		struct Deps /// Configuration for software dependencies
151 		{
152 			string dmcDir;   /// Where dmc.zip is unpacked.
153 			string vsDir;    /// Where Visual Studio is installed
154 			string sdkDir;   /// Where the Windows SDK is installed
155 			string hostDC;   /// Host D compiler (for DDMD bootstrapping)
156 		}
157 		Deps deps; /// ditto
158 
159 		/// Calculated local environment, incl. dependencies
160 		string[string] vars;
161 	}
162 
163 	/// Get a specific subdirectory of the work directory.
164 	@property string subDir(string name)() { return buildPath(config.local.workDir, name); }
165 
166 	alias repoDir    = subDir!"repo";        /// The git repository location.
167 	alias buildDir   = subDir!"build";       /// The build directory.
168 	alias dlDir      = subDir!"dl";          /// The directory for downloaded software.
169 	alias tmpDir     = subDir!"tmp";         /// Directory for $TMPDIR etc.
170 	alias homeDir    = subDir!"home";        /// Directory for $HOME.
171 	alias binDir     = subDir!"bin" ;        /// For wrapper scripts.
172 	alias githubDir  = subDir!"github-cache";/// For the GitHub API cache.
173 
174 	/// This number increases with each incompatible change to cached data.
175 	enum cacheVersion = 3;
176 
177 	string cacheEngineDir(string engineName)
178 	{
179 		// Keep compatibility with old cache paths
180 		string engineDirName =
181 			engineName.isOneOf("directory", "true") ? "cache"      :
182 			engineName.isOneOf("", "none", "false") ? "temp-cache" :
183 			"cache-" ~ engineName;
184 		return buildPath(
185 			config.local.workDir,
186 			engineDirName,
187 			"v%d".format(cacheVersion),
188 		);
189 	}
190 
191 	version (Windows)
192 	{
193 		enum string binExt = ".exe";
194 		enum configFileName = "sc.ini";
195 	}
196 	else
197 	{
198 		enum string binExt = "";
199 		enum configFileName = "dmd.conf";
200 	}
201 
202 	static bool needConfSwitch() { return exists(std.process.environment.get("HOME", null).buildPath(configFileName)); }
203 
204 	// **************************** Repositories *****************************
205 
206 	class DManagerRepository : ManagedRepository
207 	{
208 		this()
209 		{
210 			this.offline = config.local.offline;
211 			this.verify = this.outer.verifyWorkTree;
212 		}
213 
214 		override void log(string s) { return this.outer.log(s); }
215 	}
216 
217 	class MetaRepository : DManagerRepository
218 	{
219 		protected override Repository getRepo()
220 		{
221 			needGit();
222 
223 			if (!repoDir.exists)
224 			{
225 				log("Cloning initial repository...");
226 				atomic!performClone(config.local.repoUrl, repoDir);
227 			}
228 
229 			return Repository(repoDir);
230 		}
231 
232 		static void performClone(string url, string target)
233 		{
234 			import ae.sys.cmd;
235 			run(["git", "clone", url, target]);
236 		}
237 
238 		override void performCheckout(string hash)
239 		{
240 			super.performCheckout(hash);
241 			submodules = null;
242 		}
243 
244 		string[string][string] submoduleCache;
245 
246 		string[string] getSubmoduleCommits(string head)
247 		{
248 			auto pcacheEntry = head in submoduleCache;
249 			if (pcacheEntry)
250 				return (*pcacheEntry).dup;
251 
252 			string[string] result;
253 			foreach (line; git.query("ls-tree", head).splitLines())
254 			{
255 				auto parts = line.split();
256 				if (parts.length == 4 && parts[1] == "commit")
257 					result[parts[3]] = parts[2];
258 			}
259 			assert(result.length, "No submodules found");
260 			submoduleCache[head] = result;
261 			return result.dup;
262 		}
263 
264 		/// Get the submodule state for all commits in the history.
265 		/// Returns: result[commitHash][submoduleName] == submoduleCommitHash
266 		string[string][string] getSubmoduleHistory(string[] refs)
267 		{
268 			auto marksFile = buildPath(config.local.workDir, "temp", "marks.txt");
269 			ensurePathExists(marksFile);
270 			scope(exit) if (marksFile.exists) marksFile.remove();
271 			log("Running fast-export...");
272 			auto fastExportData = git.query([
273 				"fast-export",
274 				"--full-tree",
275 				"--no-data",
276 				"--export-marks=" ~ marksFile.absolutePath,
277 				] ~ refs
278 			);
279 
280 			log("Parsing fast-export marks...");
281 
282 			auto markLines = marksFile.readText.strip.splitLines;
283 			auto marks = new string[markLines.length];
284 			foreach (line; markLines)
285 			{
286 				auto parts = line.split(' ');
287 				auto markIndex = parts[0][1..$].to!int-1;
288 				marks[markIndex] = parts[1];
289 			}
290 
291 			log("Parsing fast-export data...");
292 
293 			string[string][string] result;
294 			foreach (i, commitData; fastExportData.split("deleteall\n")[1..$])
295 				result[marks[i]] = commitData
296 					.matchAll(re!(`^M 160000 ([0-9a-f]{40}) (\S+)$`, "m"))
297 					.map!(m => tuple(m.captures[2], m.captures[1]))
298 					.assocArray
299 				;
300 			return result;
301 		}
302 	}
303 
304 	class SubmoduleRepository : DManagerRepository
305 	{
306 		string dir;
307 
308 		protected override Repository getRepo()
309 		{
310 			getMetaRepo().git; // ensure meta-repository is cloned
311 			return Repository(dir);
312 		}
313 
314 		override void needHead(string hash)
315 		{
316 			if (!autoClean)
317 				super.needHead(hash);
318 			else
319 			try
320 				super.needHead(hash);
321 			catch (RepositoryCleanException e)
322 			{
323 				log("Error during repository cleanup.");
324 
325 				log("Nuking %s...".format(dir));
326 				rmdirRecurse(dir);
327 
328 				auto name = baseName(dir);
329 				auto gitDir = buildPath(dirName(dir), ".git", "modules", name);
330 				log("Nuking %s...".format(gitDir));
331 				rmdirRecurse(gitDir);
332 
333 				log("Updating submodule...");
334 				getMetaRepo().git.run(["submodule", "update", name]);
335 
336 				reset();
337 
338 				log("Trying again...");
339 				super.needHead(hash);
340 			}
341 		}
342 	}
343 
344 	/// The meta-repository, which contains the sub-project submodules.
345 	private MetaRepository metaRepo;
346 
347 	MetaRepository getMetaRepo() /// ditto
348 	{
349 		if (!metaRepo)
350 			metaRepo = new MetaRepository;
351 		return metaRepo;
352 	}
353 
354 	/// Sub-project repositories.
355 	private SubmoduleRepository[string] submodules;
356 
357 	ManagedRepository getSubmodule(string name) /// ditto
358 	{
359 		assert(name, "This component is not associated with a submodule");
360 		if (name !in submodules)
361 		{
362 			enforce(name in getMetaRepo().getSubmoduleCommits(getMetaRepo().getRef("origin/master")),
363 				"Unknown submodule: " ~ name);
364 
365 			auto path = buildPath(metaRepo.git.path, name);
366 			auto gitPath = buildPath(path, ".git");
367 
368 			if (!gitPath.exists)
369 			{
370 				log("Initializing and updating submodule %s...".format(name));
371 				getMetaRepo().git.run(["submodule", "update", "--init", name]);
372 			}
373 
374 			submodules[name] = new SubmoduleRepository();
375 			submodules[name].dir = path;
376 		}
377 
378 		return submodules[name];
379 	}
380 
381 	// ***************************** Components ******************************
382 
383 	/// Base class for a D component.
384 	class Component
385 	{
386 		/// Name of this component, as registered in DManager.components AA.
387 		string name;
388 
389 		/// Corresponding subproject repository name.
390 		@property abstract string submoduleName();
391 		@property ManagedRepository submodule() { return getSubmodule(submoduleName); }
392 
393 		/// Configuration applicable to multiple (not all) components.
394 		// Note: don't serialize this structure whole!
395 		// Only serialize used fields.
396 		struct CommonConfig
397 		{
398 			version (Windows)
399 				enum defaultModel = "32";
400 			else
401 			version (D_LP64)
402 				enum defaultModel = "64";
403 			else
404 				enum defaultModel = "32";
405 
406 			/// Target comma-separated models ("32", "64", and on Windows, "32mscoff").
407 			/// Controls the models of the built Phobos and Druntime libraries.
408 			string model = defaultModel;
409 
410 			@property string[] models() { return model.split(","); }
411 			@property void models(string[] value) { this.model = value.join(","); }
412 
413 			string[] makeArgs; /// Additional make parameters,
414 			                   /// e.g. "HOST_CC=g++48"
415 		}
416 
417 		/// A string description of this component's configuration.
418 		abstract @property string configString();
419 
420 		/// Commit in the component's repo from which to build this component.
421 		@property string commit() { return incrementalBuild ? "incremental" : getComponentCommit(name); }
422 
423 		/// The components the source code of which this component depends on.
424 		/// Used for calculating the cache key.
425 		@property abstract string[] sourceDependencies();
426 
427 		/// The components the state and configuration of which this component depends on.
428 		/// Used for calculating the cache key.
429 		@property abstract string[] dependencies();
430 
431 		/// This metadata is saved to a .json file,
432 		/// and is also used to calculate the cache key.
433 		struct Metadata
434 		{
435 			int cacheVersion;
436 			string name;
437 			string commit;
438 			string configString;
439 			string[] sourceDepCommits;
440 			Metadata[] dependencyMetadata;
441 			@JSONOptional string cacheKey;
442 		}
443 
444 		Metadata getMetadata() /// ditto
445 		{
446 			return Metadata(
447 				cacheVersion,
448 				name,
449 				commit,
450 				configString,
451 				sourceDependencies.map!(
452 					dependency => getComponent(dependency).commit
453 				).array(),
454 				dependencies.map!(
455 					dependency => getComponent(dependency).getMetadata()
456 				).array(),
457 				config.build.cacheKey,
458 			);
459 		}
460 
461 		void saveMetaData(string target)
462 		{
463 			std.file.write(buildPath(target, "digger-metadata.json"), getMetadata().toJson());
464 			// Use a separate file to avoid double-encoding JSON
465 			std.file.write(buildPath(target, "digger-config.json"), configString);
466 		}
467 
468 		/// Calculates the cache key, which should be unique and immutable
469 		/// for the same source, build parameters, and build algorithm.
470 		string getBuildID()
471 		{
472 			auto configBlob = getMetadata().toJson() ~ configString;
473 			return "%s-%s-%s".format(
474 				name,
475 				commit,
476 				configBlob.getDigestString!MD5().toLower(),
477 			);
478 		}
479 
480 		@property string sourceDir() { return submodule.git.path; }
481 
482 		/// Directory to which built files are copied to.
483 		/// This will then be atomically added to the cache.
484 		protected string stageDir;
485 
486 		/// Prepare the source checkout for this component.
487 		/// Usually needed by other components.
488 		void needSource(bool needClean = false)
489 		{
490 			tempError++; scope(success) tempError--;
491 
492 			if (incrementalBuild)
493 				return;
494 			if (!submoduleName)
495 				return;
496 
497 			bool needHead;
498 			if (needClean)
499 				needHead = true;
500 			else
501 			{
502 				// It's OK to run tests with a dirty worktree (i.e. after a build).
503 				needHead = commit != submodule.getHead();
504 			}
505 
506 			if (needHead)
507 			{
508 				foreach (component; getSubmoduleComponents(submoduleName))
509 					component.haveBuild = false;
510 				submodule.needHead(commit);
511 			}
512 			submodule.clean = false;
513 		}
514 
515 		private bool haveBuild;
516 
517 		/// Build the component in-place, as needed,
518 		/// without moving the built files anywhere.
519 		void needBuild(bool clean = true)
520 		{
521 			if (haveBuild) return;
522 			scope(success) haveBuild = true;
523 
524 			log("needBuild: " ~ getBuildID());
525 
526 			needSource(clean);
527 
528 			prepareEnv();
529 
530 			log("Building " ~ getBuildID());
531 			performBuild();
532 			log(getBuildID() ~ " built OK!");
533 		}
534 
535 		/// Set up / clean the build environment.
536 		private void prepareEnv()
537 		{
538 			// Nuke any additional directories cloned by makefiles
539 			if (!incrementalBuild)
540 			{
541 				getMetaRepo().git.run(["clean", "-ffdx"]);
542 
543 				foreach (dir; [tmpDir, homeDir])
544 				{
545 					if (dir.exists && !dir.dirEntries(SpanMode.shallow).empty)
546 						log("Clearing %s ...".format(dir));
547 					dir.recreateEmptyDirectory();
548 				}
549 			}
550 
551 			// Set up compiler wrappers.
552 			recreateEmptyDirectory(binDir);
553 			version (linux)
554 			{
555 				foreach (cc; ["cc", "gcc", "c++", "g++"])
556 				{
557 					auto fileName = binDir.buildPath(cc);
558 					write(fileName, q"EOF
559 #!/bin/sh
560 set -eu
561 
562 tool=$(basename "$0")
563 next=/usr/bin/$tool
564 tmpdir=${TMP:-/tmp}
565 flagfile=$tmpdir/nopie-flag-$tool
566 
567 if [ ! -e "$flagfile" ]
568 then
569 	echo 'Testing for -no-pie...' 1>&2
570 	testfile=$tmpdir/test-$$.c
571 	echo 'int main(){return 0;}' > $testfile
572 	if $next -no-pie -c -o$testfile.o $testfile
573 	then
574 		printf "%s" "-no-pie" > "$flagfile".$$.tmp
575 		mv "$flagfile".$$.tmp "$flagfile"
576 	else
577 		touch "$flagfile"
578 	fi
579 	rm -f "$testfile" "$testfile.o"
580 fi
581 
582 exec "$next" $(cat "$flagfile") "$@"
583 EOF");
584 					setAttributes(fileName, octal!755);
585 				}
586 			}
587 		}
588 
589 		private bool haveInstalled;
590 
591 		/// Build and "install" the component to buildDir as necessary.
592 		void needInstalled()
593 		{
594 			if (haveInstalled) return;
595 			scope(success) haveInstalled = true;
596 
597 			auto buildID = getBuildID();
598 			log("needInstalled: " ~ buildID);
599 
600 			needCacheEngine();
601 			if (cacheEngine.haveEntry(buildID))
602 			{
603 				log("Cache hit!");
604 				if (cacheEngine.listFiles(buildID).canFind(unbuildableMarker))
605 					throw new Exception(buildID ~ " was cached as unbuildable");
606 			}
607 			else
608 			{
609 				log("Cache miss.");
610 
611 				auto tempDir = buildPath(config.local.workDir, "temp");
612 				if (tempDir.exists)
613 					tempDir.removeRecurse();
614 				stageDir = buildPath(tempDir, buildID);
615 				stageDir.mkdirRecurse();
616 
617 				bool failed = false;
618 				tempError = 0;
619 
620 				// Save the results to cache, failed or not
621 				void saveToCache()
622 				{
623 					// Use a separate function to work around
624 					// "cannot put scope(success) statement inside scope(exit)"
625 
626 					int currentTempError = tempError;
627 
628 					// Treat cache errors an environmental errors
629 					// (for when needInstalled is invoked to build a dependency)
630 					tempError++; scope(success) tempError--;
631 
632 					// tempDir might be removed by a dependency's build failure.
633 					if (!tempDir.exists)
634 						log("Not caching %s dependency build failure.".format(name));
635 					else
636 					// Don't cache failed build results due to temporary/environment problems
637 					if (failed && currentTempError > 0)
638 					{
639 						log("Not caching %s build failure due to temporary/environment error.".format(name));
640 						rmdirRecurse(tempDir);
641 					}
642 					else
643 					// Don't cache failed build results during delve
644 					if (failed && !cacheFailures)
645 					{
646 						log("Not caching failed %s build.".format(name));
647 						rmdirRecurse(tempDir);
648 					}
649 					else
650 					if (cacheEngine.haveEntry(buildID))
651 					{
652 						// Can happen due to force==true
653 						log("Already in cache.");
654 						rmdirRecurse(tempDir);
655 					}
656 					else
657 					{
658 						log("Saving to cache.");
659 						saveMetaData(stageDir);
660 						cacheEngine.add(buildID, stageDir);
661 						rmdirRecurse(tempDir);
662 					}
663 				}
664 
665 				scope (exit)
666 					saveToCache();
667 
668 				// An incomplete build is useless, nuke the directory
669 				// and create a new one just for the "unbuildable" marker.
670 				scope (failure)
671 				{
672 					failed = true;
673 					if (stageDir.exists)
674 					{
675 						rmdirRecurse(stageDir);
676 						mkdir(stageDir);
677 						buildPath(stageDir, unbuildableMarker).touch();
678 					}
679 				}
680 
681 				needBuild();
682 
683 				performStage();
684 			}
685 
686 			install();
687 		}
688 
689 		/// Build the component in-place, without moving the built files anywhere.
690 		void performBuild() {}
691 
692 		/// Place resulting files to stageDir
693 		void performStage() {}
694 
695 		/// Update the environment post-install, to allow
696 		/// building components that depend on this one.
697 		void updateEnv(ref Environment env) {}
698 
699 		/// Copy build results from cacheDir to buildDir
700 		void install()
701 		{
702 			log("Installing " ~ getBuildID());
703 			needCacheEngine().extract(getBuildID(), buildDir, de => !de.baseName.startsWith("digger-"));
704 		}
705 
706 		/// Prepare the dependencies then run the component's tests.
707 		void test()
708 		{
709 			log("Testing " ~ getBuildID());
710 
711 			needSource();
712 
713 			submodule.clean = false;
714 			performTest();
715 			log(getBuildID() ~ " tests OK!");
716 		}
717 
718 		/// Run the component's tests.
719 		void performTest() {}
720 
721 	protected final:
722 		// Utility declarations for component implementations
723 
724 		string modelSuffix(string model) { return model == "32" ? "" : model; }
725 		version (Windows)
726 		{
727 			enum string makeFileName = "win32.mak";
728 			string makeFileNameModel(string model)
729 			{
730 				if (model == "32mscoff")
731 					model = "64";
732 				return "win"~model~".mak";
733 			}
734 			enum string binExt = ".exe";
735 		}
736 		else
737 		{
738 			enum string makeFileName = "posix.mak";
739 			string makeFileNameModel(string model) { return "posix.mak"; }
740 			enum string binExt = "";
741 		}
742 
743 		version (Windows)
744 			enum platform = "windows";
745 		else
746 		version (linux)
747 			enum platform = "linux";
748 		else
749 		version (OSX)
750 			enum platform = "osx";
751 		else
752 		version (FreeBSD)
753 			enum platform = "freebsd";
754 		else
755 			static assert(false);
756 
757 		/// Returns the command for the make utility.
758 		string[] getMake(ref const Environment env)
759 		{
760 			version (FreeBSD)
761 				enum makeProgram = "gmake"; // GNU make
762 			else
763 			version (Posix)
764 				enum makeProgram = "make"; // GNU make
765 			else
766 				enum makeProgram = "make"; // DigitalMars make
767 			return [env.vars.get("MAKE", makeProgram)];
768 		}
769 
770 		/// Returns the path to the built dmd executable.
771 		@property string dmd() { return buildPath(buildDir, "bin", "dmd" ~ binExt).absolutePath(); }
772 
773 		/// Escape a path for d_do_test's very "special" criteria.
774 		/// Spaces must be escaped, but there must be no double-quote at the end.
775 		private static string dDoTestEscape(string str)
776 		{
777 			return str.replaceAll(re!`\\([^\\ ]*? [^\\]*)(?=\\)`, `\"$1"`);
778 		}
779 
780 		unittest
781 		{
782 			assert(dDoTestEscape(`C:\Foo boo bar\baz quuz\derp.exe`) == `C:\"Foo boo bar"\"baz quuz"\derp.exe`);
783 		}
784 
785 		string[] getPlatformMakeVars(ref const Environment env, string model, bool quote = true)
786 		{
787 			string[] args;
788 
789 			args ~= "MODEL=" ~ model;
790 
791 			version (Windows)
792 				if (model != "32")
793 				{
794 					args ~= "VCDIR="  ~ env.deps.vsDir.buildPath("VC").absolutePath();
795 					args ~= "SDKDIR=" ~ env.deps.sdkDir.absolutePath();
796 
797 					// Work around https://github.com/dlang/druntime/pull/2438
798 					auto quoteStr = quote ? `"` : ``;
799 					args ~= "CC=" ~ quoteStr ~ env.deps.vsDir.buildPath("VC", "bin", msvcModelDir(model), "cl.exe").absolutePath() ~ quoteStr;
800 					args ~= "LD=" ~ quoteStr ~ env.deps.vsDir.buildPath("VC", "bin", msvcModelDir(model), "link.exe").absolutePath() ~ quoteStr;
801 					args ~= "AR=" ~ quoteStr ~ env.deps.vsDir.buildPath("VC", "bin", msvcModelDir(model), "lib.exe").absolutePath() ~ quoteStr;
802 				}
803 
804 			return args;
805 		}
806 
807 		@property string[] gnuMakeArgs()
808 		{
809 			string[] args;
810 			if (config.local.makeJobs)
811 			{
812 				if (config.local.makeJobs == "auto")
813 				{
814 					import std.parallelism, std.conv;
815 					args ~= "-j" ~ text(totalCPUs);
816 				}
817 				else
818 				if (config.local.makeJobs == "unlimited")
819 					args ~= "-j";
820 				else
821 					args ~= "-j" ~ config.local.makeJobs;
822 			}
823 			return args;
824 		}
825 
826 		@property string[] dMakeArgs()
827 		{
828 			version (Windows)
829 				return null; // On Windows, DigitalMars make is used for all makefiles except the dmd test suite
830 			else
831 				return gnuMakeArgs;
832 		}
833 
834 		/// Older versions did not use the posix.mak/win32.mak convention.
835 		static string findMakeFile(string dir, string fn)
836 		{
837 			version (OSX)
838 				if (!dir.buildPath(fn).exists && dir.buildPath("osx.mak").exists)
839 					return "osx.mak";
840 			version (Posix)
841 				if (!dir.buildPath(fn).exists && dir.buildPath("linux.mak").exists)
842 					return "linux.mak";
843 			return fn;
844 		}
845 
846 		void needCC(ref Environment env, string model, string dmcVer = null)
847 		{
848 			version (Windows)
849 			{
850 				needDMC(env, dmcVer); // We need DMC even for 64-bit builds (for DM make)
851 				if (model != "32")
852 					needVC(env, model);
853 			}
854 		}
855 
856 		void run(const(string)[] args, in string[string] newEnv, string dir)
857 		{
858 			// Apply user environment
859 			auto env = applyEnv(newEnv, config.build.environment);
860 
861 			// Temporarily apply PATH from newEnv to our process,
862 			// so process creation lookup can use it.
863 			string oldPath = std.process.environment["PATH"];
864 			scope (exit) std.process.environment["PATH"] = oldPath;
865 			std.process.environment["PATH"] = env["PATH"];
866 
867 			// Apply timeout setting
868 			if (config.local.timeout)
869 				args = ["timeout", config.local.timeout.text] ~ args;
870 
871 			foreach (name, value; env)
872 				log("Environment: " ~ name ~ "=" ~ value);
873 			log("Working directory: " ~ dir);
874 			log("Running: " ~ escapeShellCommand(args));
875 
876 			auto status = spawnProcess(args, env, std.process.Config.newEnv, dir).wait();
877 			enforce(status == 0, "Command %s failed with status %d".format(args, status));
878 		}
879 	}
880 
881 	/// The dmd executable
882 	final class DMD : Component
883 	{
884 		@property override string submoduleName  () { return "dmd"; }
885 		@property override string[] sourceDependencies() { return []; }
886 		@property override string[] dependencies() { return []; }
887 
888 		struct Config
889 		{
890 			/// Whether to build a debug DMD.
891 			/// Debug builds are faster to build,
892 			/// but run slower.
893 			@JSONOptional bool debugDMD = false;
894 
895 			/// Whether to build a release DMD.
896 			/// Mutually exclusive with debugDMD.
897 			@JSONOptional bool releaseDMD = false;
898 
899 			/// Model for building DMD itself (on Windows).
900 			/// Can be used to build a 64-bit DMD, to avoid 4GB limit.
901 			@JSONOptional string dmdModel = CommonConfig.defaultModel;
902 
903 			/// How to build DMD versions written in D.
904 			/// We can either download a pre-built binary DMD
905 			/// package, or build an  earlier version from source
906 			/// (e.g. starting with the last C++-only version.)
907 			struct Bootstrap
908 			{
909 				/// Whether to download a pre-built D version,
910 				/// or build one from source. If set, then build
911 				/// from source according to the value of ver,
912 				@JSONOptional bool fromSource = false;
913 
914 				/// Version specification.
915 				/// When building from source, syntax can be defined
916 				/// by outer application (see parseSpec method);
917 				/// When the bootstrapping compiler is not built from source,
918 				/// it is understood as a version number, such as "v2.070.2",
919 				/// which also doubles as a tag name.
920 				/// By default (when set to null), an appropriate version
921 				/// is selected automatically.
922 				@JSONOptional string ver = null;
923 
924 				/// Build configuration for the compiler used for bootstrapping.
925 				/// If not set, then use the default build configuration.
926 				/// Used when fromSource is set.
927 				@JSONOptional DManager.Config.Build* build;
928 			}
929 			@JSONOptional Bootstrap bootstrap; /// ditto
930 
931 			/// Use Visual C++ to build DMD instead of DMC.
932 			/// Currently, this is a hack, as msbuild will consult the system
933 			/// registry and use the system-wide installation of Visual Studio.
934 			/// Only relevant for older versions, as newer versions are written in D.
935 			@JSONOptional bool useVC;
936 		}
937 
938 		@property override string configString()
939 		{
940 			static struct FullConfig
941 			{
942 				Config config;
943 				string[] makeArgs;
944 
945 				// Include the common models as well as the DMD model (from config).
946 				// Necessary to ensure the correct sc.ini is generated on Windows
947 				// (we don't want to pull in MSVC unless either DMD or Phobos are
948 				// built as 64-bit, but also we can't reuse a DMD build with 32-bit
949 				// DMD and Phobos for a 64-bit Phobos build because it won't have
950 				// the VC vars set up in its sc.ini).
951 				// Possibly refactor the compiler configuration to a separate
952 				// component in the future to avoid the inefficiency of rebuilding
953 				// DMD just to generate a different sc.ini.
954 				@JSONOptional string commonModel = Component.CommonConfig.defaultModel;
955 			}
956 
957 			return FullConfig(
958 				config.build.components.dmd,
959 				config.build.components.common.makeArgs,
960 				config.build.components.common.model,
961 			).toJson();
962 		}
963 
964 		@property string vsConfiguration() { return config.build.components.dmd.debugDMD ? "Debug" : "Release"; }
965 		@property string vsPlatform     () { return config.build.components.dmd.dmdModel == "64" ? "x64" : "Win32"; }
966 
967 		override void performBuild()
968 		{
969 			// We need an older DMC for older DMD versions
970 			string dmcVer = null;
971 			auto idgen = buildPath(sourceDir, "src", "idgen.c");
972 			if (idgen.exists && idgen.readText().indexOf(`{ "alignof" },`) >= 0)
973 				dmcVer = "850";
974 
975 			auto env = baseEnvironment;
976 			needCC(env, config.build.components.dmd.dmdModel, dmcVer); // Need VC too for VSINSTALLDIR
977 
978 			auto srcDir = buildPath(sourceDir, "src");
979 			string dmdMakeFileName = findMakeFile(srcDir, makeFileName);
980 			string dmdMakeFullName = srcDir.buildPath(dmdMakeFileName);
981 
982 			if (buildPath(sourceDir, "src", "idgen.d").exists ||
983 			    buildPath(sourceDir, "src", "ddmd", "idgen.d").exists ||
984 			    buildPath(sourceDir, "src", "ddmd", "mars.d").exists ||
985 			    buildPath(sourceDir, "src", "dmd", "mars.d").exists)
986 			{
987 				// Need an older DMD for bootstrapping.
988 				string dmdVer = "v2.067.1";
989 				if (sourceDir.buildPath("test/compilable/staticforeach.d").exists)
990 					dmdVer = "v2.068.0";
991 				version (Windows)
992 					if (config.build.components.dmd.dmdModel != Component.CommonConfig.defaultModel)
993 						dmdVer = "v2.070.2"; // dmd/src/builtin.d needs core.stdc.math.fabsl. 2.068.2 generates a dmd which crashes on building Phobos
994 				if (sourceDir.buildPath("src/dmd/backend/dvec.d").exists) // 2.079 is needed since 2.080
995 					dmdVer = "v2.079.0";
996 				needDMD(env, dmdVer);
997 
998 				// Go back to our commit (in case we bootstrapped from source).
999 				needSource(true);
1000 				submodule.clean = false;
1001 			}
1002 
1003 			if (config.build.components.dmd.useVC) // Mostly obsolete, see useVC ddoc
1004 			{
1005 				version (Windows)
1006 				{
1007 					needVC(env, config.build.components.dmd.dmdModel);
1008 
1009 					env.vars["PATH"] = env.vars["PATH"] ~ pathSeparator ~ env.deps.hostDC.dirName;
1010 
1011 					auto solutionFile = `dmd_msc_vs10.sln`;
1012 					if (!exists(srcDir.buildPath(solutionFile)))
1013 						solutionFile = `vcbuild\dmd.sln`;
1014 					if (!exists(srcDir.buildPath(solutionFile)))
1015 						throw new Exception("Can't find Visual Studio solution file");
1016 
1017 					return run(["msbuild", "/p:Configuration=" ~ vsConfiguration, "/p:Platform=" ~ vsPlatform, solutionFile], env.vars, srcDir);
1018 				}
1019 				else
1020 					throw new Exception("Can only use Visual Studio on Windows");
1021 			}
1022 
1023 			version (Windows)
1024 				auto scRoot = env.deps.dmcDir.absolutePath();
1025 
1026 			string modelFlag = config.build.components.dmd.dmdModel;
1027 			if (dmdMakeFullName.readText().canFind("MODEL=-m32"))
1028 				modelFlag = "-m" ~ modelFlag;
1029 
1030 			version (Windows)
1031 			{
1032 				auto m = dmdMakeFullName.readText();
1033 				m = m
1034 					// A make argument is insufficient,
1035 					// because of recursive make invocations
1036 					.replace(`CC=\dm\bin\dmc`, `CC=dmc`)
1037 					.replace(`SCROOT=$D\dm`, `SCROOT=` ~ scRoot)
1038 					// Debug crashes in build.d
1039 					.replaceAll(re!(`^(	\$\(HOST_DC\) .*) (build\.d)$`, "m"), "$1 -g $2")
1040 				;
1041 				dmdMakeFullName.write(m);
1042 			}
1043 			else
1044 			{
1045 				auto m = dmdMakeFullName.readText();
1046 				m = m
1047 					// Fix hard-coded reference to gcc as linker
1048 					.replace(`gcc -m32 -lstdc++`, `g++ -m32 -lstdc++`)
1049 					.replace(`gcc $(MODEL) -lstdc++`, `g++ $(MODEL) -lstdc++`)
1050 					// Fix compilation of older versions of go.c with GCC 6
1051 					.replace(`-Wno-deprecated`, `-Wno-deprecated -Wno-narrowing`)
1052 				;
1053 				// Fix pthread linker error
1054 				version (linux)
1055 					m = m.replace(`-lpthread`, `-pthread`);
1056 				dmdMakeFullName.write(m);
1057 			}
1058 
1059 			submodule.saveFileState("src/" ~ dmdMakeFileName);
1060 
1061 			version (Windows)
1062 			{
1063 				auto buildDFileName = "build.d";
1064 				auto buildDPath = srcDir.buildPath(buildDFileName);
1065 				if (buildDPath.exists)
1066 				{
1067 					auto buildD = buildDPath.readText();
1068 					buildD = buildD
1069 						// https://github.com/dlang/dmd/pull/10491
1070 						// Needs WBEM PATH entry, and also fails under Wine as its wmic outputs UTF-16.
1071 						.replace(`["wmic", "OS", "get", "OSArchitecture"].execute.output`, isWin64 ? `"64-bit"` : `"32-bit"`)
1072 					;
1073 					buildDPath.write(buildD);
1074 					submodule.saveFileState("src/" ~ buildDFileName);
1075 				}
1076 			}
1077 
1078 			// Fix compilation error of older DMDs with glibc >= 2.25
1079 			version (linux)
1080 			{{
1081 				auto fn = srcDir.buildPath("root", "port.c");
1082 				if (fn.exists)
1083 				{
1084 					fn.write(fn.readText
1085 						.replace(`#include <bits/mathdef.h>`, `#include <complex.h>`)
1086 						.replace(`#include <bits/nan.h>`, `#include <math.h>`)
1087 					);
1088 					submodule.saveFileState(fn.relativePath(sourceDir));
1089 				}
1090 			}}
1091 
1092 			// Fix alignment issue in older DMDs with GCC >= 7
1093 			// See https://issues.dlang.org/show_bug.cgi?id=17726
1094 			version (Posix)
1095 			{
1096 				foreach (fn; [srcDir.buildPath("tk", "mem.c"), srcDir.buildPath("ddmd", "tk", "mem.c")])
1097 					if (fn.exists)
1098 					{
1099 						fn.write(fn.readText.replace(
1100 								// `#if defined(__llvm__) && (defined(__GNUC__) || defined(__clang__))`,
1101 								// `#if defined(__GNUC__) || defined(__clang__)`,
1102 								`numbytes = (numbytes + 3) & ~3;`,
1103 								`numbytes = (numbytes + 0xF) & ~0xF;`
1104 						));
1105 						submodule.saveFileState(fn.relativePath(sourceDir));
1106 					}
1107 			}
1108 
1109 			string[] extraArgs, targets;
1110 			version (Posix)
1111 			{
1112 				if (config.build.components.dmd.debugDMD)
1113 					extraArgs ~= "DEBUG=1";
1114 				if (config.build.components.dmd.releaseDMD)
1115 					extraArgs ~= "ENABLE_RELEASE=1";
1116 			}
1117 			else
1118 			{
1119 				if (config.build.components.dmd.debugDMD)
1120 					targets ~= [];
1121 				else
1122 				if (config.build.components.dmd.releaseDMD && dmdMakeFullName.readText().canFind("reldmd"))
1123 					targets ~= ["reldmd"];
1124 				else
1125 					targets ~= ["dmd"];
1126 			}
1127 
1128 			version (Windows)
1129 			{
1130 				if (config.build.components.dmd.dmdModel != CommonConfig.defaultModel)
1131 				{
1132 					dmdMakeFileName = "win64.mak";
1133 					dmdMakeFullName = srcDir.buildPath(dmdMakeFileName);
1134 					enforce(dmdMakeFullName.exists, "dmdModel not supported for this DMD version");
1135 					extraArgs ~= "DMODEL=-m" ~ config.build.components.dmd.dmdModel;
1136 					if (config.build.components.dmd.dmdModel == "32mscoff")
1137 					{
1138 						auto objFiles = dmdMakeFullName.readText().splitLines().filter!(line => line.startsWith("OBJ_MSVC="));
1139 						enforce(!objFiles.empty, "Can't find OBJ_MSVC in win64.mak");
1140 						extraArgs ~= "OBJ_MSVC=" ~ objFiles.front.findSplit("=")[2].split().filter!(obj => obj != "ldfpu.obj").join(" ");
1141 					}
1142 				}
1143 			}
1144 
1145 			// Avoid HOST_DC reading ~/dmd.conf
1146 			string hostDC = env.deps.hostDC;
1147 			version (Posix)
1148 			if (hostDC && needConfSwitch())
1149 			{
1150 				auto dcProxy = buildPath(config.local.workDir, "host-dc-proxy.sh");
1151 				std.file.write(dcProxy, escapeShellCommand(["exec", hostDC, "-conf=" ~ buildPath(dirName(hostDC), configFileName)]) ~ ` "$@"`);
1152 				setAttributes(dcProxy, octal!755);
1153 				hostDC = dcProxy;
1154 			}
1155 
1156 			run(getMake(env) ~ [
1157 					"-f", dmdMakeFileName,
1158 					"MODEL=" ~ modelFlag,
1159 					"HOST_DC=" ~ hostDC,
1160 				] ~ config.build.components.common.makeArgs ~ dMakeArgs ~ extraArgs ~ targets,
1161 				env.vars, srcDir
1162 			);
1163 		}
1164 
1165 		override void performStage()
1166 		{
1167 			if (config.build.components.dmd.useVC)
1168 			{
1169 				foreach (ext; [".exe", ".pdb"])
1170 					cp(
1171 						buildPath(sourceDir, "src", "vcbuild", vsPlatform, vsConfiguration, "dmd_msc" ~ ext),
1172 						buildPath(stageDir , "bin", "dmd" ~ ext),
1173 					);
1174 			}
1175 			else
1176 			{
1177 				string dmdPath = buildPath(sourceDir, "generated", platform, "release", config.build.components.dmd.dmdModel, "dmd" ~ binExt);
1178 				if (!dmdPath.exists)
1179 					dmdPath = buildPath(sourceDir, "src", "dmd" ~ binExt); // legacy
1180 				enforce(dmdPath.exists && dmdPath.isFile, "Can't find built DMD executable");
1181 
1182 				cp(
1183 					dmdPath,
1184 					buildPath(stageDir , "bin", "dmd" ~ binExt),
1185 				);
1186 			}
1187 
1188 			version (Windows)
1189 			{
1190 				auto env = baseEnvironment;
1191 				needCC(env, config.build.components.dmd.dmdModel);
1192 				foreach (model; config.build.components.common.models)
1193 					needCC(env, model);
1194 
1195 				auto ini = q"EOS
1196 [Environment]
1197 LIB=%@P%\..\lib
1198 DFLAGS="-I%@P%\..\import"
1199 DMC=__DMC__
1200 LINKCMD=%DMC%\link.exe
1201 EOS"
1202 				.replace("__DMC__", env.deps.dmcDir.buildPath(`bin`).absolutePath())
1203 			;
1204 
1205 				if (env.deps.vsDir && env.deps.sdkDir)
1206 				{
1207 					ini ~= q"EOS
1208 
1209 [Environment64]
1210 LIB=%@P%\..\lib
1211 DFLAGS=%DFLAGS% -L/OPT:NOICF
1212 VSINSTALLDIR=__VS__\
1213 VCINSTALLDIR=%VSINSTALLDIR%VC\
1214 PATH=%PATH%;%VCINSTALLDIR%\bin\__MODELDIR__;%VCINSTALLDIR%\bin
1215 WindowsSdkDir=__SDK__
1216 LINKCMD=%VCINSTALLDIR%\bin\__MODELDIR__\link.exe
1217 LIB=%LIB%;%VCINSTALLDIR%\lib\amd64
1218 LIB=%LIB%;%WindowsSdkDir%\Lib\x64
1219 
1220 [Environment32mscoff]
1221 LIB=%@P%\..\lib
1222 DFLAGS=%DFLAGS% -L/OPT:NOICF
1223 VSINSTALLDIR=__VS__\
1224 VCINSTALLDIR=%VSINSTALLDIR%VC\
1225 PATH=%PATH%;%VCINSTALLDIR%\bin
1226 WindowsSdkDir=__SDK__
1227 LINKCMD=%VCINSTALLDIR%\bin\link.exe
1228 LIB=%LIB%;%VCINSTALLDIR%\lib
1229 LIB=%LIB%;%WindowsSdkDir%\Lib
1230 EOS"
1231 						.replace("__VS__"      , env.deps.vsDir .absolutePath())
1232 						.replace("__SDK__"     , env.deps.sdkDir.absolutePath())
1233 						.replace("__MODELDIR__", msvcModelDir("64"))
1234 					;
1235 				}
1236 
1237 				buildPath(stageDir, "bin", configFileName).write(ini);
1238 			}
1239 			else version (OSX)
1240 			{
1241 				auto ini = q"EOS
1242 [Environment]
1243 DFLAGS="-I%@P%/../import" "-L-L%@P%/../lib"
1244 EOS";
1245 				buildPath(stageDir, "bin", configFileName).write(ini);
1246 			}
1247 			else version (linux)
1248 			{
1249 				auto ini = q"EOS
1250 [Environment32]
1251 DFLAGS="-I%@P%/../import" "-L-L%@P%/../lib" -L--export-dynamic
1252 
1253 [Environment64]
1254 DFLAGS="-I%@P%/../import" "-L-L%@P%/../lib" -L--export-dynamic -fPIC
1255 EOS";
1256 				buildPath(stageDir, "bin", configFileName).write(ini);
1257 			}
1258 			else
1259 			{
1260 				auto ini = q"EOS
1261 [Environment]
1262 DFLAGS="-I%@P%/../import" "-L-L%@P%/../lib" -L--export-dynamic
1263 EOS";
1264 				buildPath(stageDir, "bin", configFileName).write(ini);
1265 			}
1266 		}
1267 
1268 		override void updateEnv(ref Environment env)
1269 		{
1270 			// Add the DMD we built for Phobos/Druntime/Tools
1271 			env.vars["PATH"] = buildPath(buildDir, "bin").absolutePath() ~ pathSeparator ~ env.vars["PATH"];
1272 		}
1273 
1274 		override void performTest()
1275 		{
1276 			foreach (dep; ["dmd", "druntime", "phobos"])
1277 				getComponent(dep).needBuild(true);
1278 
1279 			foreach (model; config.build.components.common.models)
1280 			{
1281 				auto env = baseEnvironment;
1282 				version (Windows)
1283 				{
1284 					// In this order so it uses the MSYS make
1285 					needCC(env, model);
1286 					needMSYS(env);
1287 
1288 					disableCrashDialog();
1289 				}
1290 
1291 				auto makeArgs = getMake(env) ~ config.build.components.common.makeArgs ~ getPlatformMakeVars(env, model) ~ gnuMakeArgs;
1292 				version (Windows)
1293 				{
1294 					makeArgs ~= ["OS=win" ~ model[0..2], "SHELL=bash"];
1295 					if (model == "32")
1296 					{
1297 						auto extrasDir = needExtras();
1298 						// The autotester seems to pass this via environment. Why does that work there???
1299 						makeArgs ~= "LIB=" ~ extrasDir.buildPath("localextras-windows", "dmd2", "windows", "lib") ~ `;..\..\phobos`;
1300 					}
1301 					else
1302 					{
1303 						// Fix path for d_do_test and its special escaping (default is the system VS2010 install)
1304 						// We can't use the same syntax in getPlatformMakeVars because win64.mak uses "CC=\$(CC32)"\""
1305 						auto cl = env.deps.vsDir.buildPath("VC", "bin", msvcModelDir(model), "cl.exe");
1306 						foreach (ref arg; makeArgs)
1307 							if (arg.startsWith("CC="))
1308 								arg = "CC=" ~ dDoTestEscape(cl);
1309 					}
1310 				}
1311 
1312 				version (test)
1313 				{
1314 					// Only try a few tests during CI runs, to check for
1315 					// platform integration and correct invocation.
1316 					// For this purpose, the C++ ABI tests will do nicely.
1317 					makeArgs ~= [
1318 					//	"test_results/runnable/cppa.d.out", // https://github.com/dlang/dmd/pull/5686
1319 						"test_results/runnable/cpp_abi_tests.d.out",
1320 						"test_results/runnable/cabi1.d.out",
1321 					];
1322 				}
1323 
1324 				run(makeArgs, env.vars, sourceDir.buildPath("test"));
1325 			}
1326 		}
1327 	}
1328 
1329 	/// Phobos import files.
1330 	/// In older versions of D, Druntime depended on Phobos modules.
1331 	final class PhobosIncludes : Component
1332 	{
1333 		@property override string submoduleName() { return "phobos"; }
1334 		@property override string[] sourceDependencies() { return []; }
1335 		@property override string[] dependencies() { return []; }
1336 		@property override string configString() { return null; }
1337 
1338 		override void performStage()
1339 		{
1340 			foreach (f; ["std", "etc", "crc32.d"])
1341 				if (buildPath(sourceDir, f).exists)
1342 					cp(
1343 						buildPath(sourceDir, f),
1344 						buildPath(stageDir , "import", f),
1345 					);
1346 		}
1347 	}
1348 
1349 	/// Druntime. Installs only import files, but builds the library too.
1350 	final class Druntime : Component
1351 	{
1352 		@property override string submoduleName    () { return "druntime"; }
1353 		@property override string[] sourceDependencies() { return ["phobos", "phobos-includes"]; }
1354 		@property override string[] dependencies() { return ["dmd"]; }
1355 
1356 		@property override string configString()
1357 		{
1358 			static struct FullConfig
1359 			{
1360 				string model;
1361 				string[] makeArgs;
1362 			}
1363 
1364 			return FullConfig(
1365 				config.build.components.common.model,
1366 				config.build.components.common.makeArgs,
1367 			).toJson();
1368 		}
1369 
1370 		override void performBuild()
1371 		{
1372 			getComponent("phobos").needSource();
1373 			getComponent("dmd").needSource();
1374 			getComponent("dmd").needInstalled();
1375 			getComponent("phobos-includes").needInstalled();
1376 
1377 			foreach (model; config.build.components.common.models)
1378 			{
1379 				auto env = baseEnvironment;
1380 				needCC(env, model);
1381 
1382 				mkdirRecurse(sourceDir.buildPath("import"));
1383 				mkdirRecurse(sourceDir.buildPath("lib"));
1384 
1385 				setTimes(sourceDir.buildPath("src", "rt", "minit.obj"), Clock.currTime(), Clock.currTime()); // Don't rebuild
1386 				submodule.saveFileState("src/rt/minit.obj");
1387 
1388 				runMake(env, model, "import");
1389 				runMake(env, model);
1390 			}
1391 		}
1392 
1393 		override void performStage()
1394 		{
1395 			cp(
1396 				buildPath(sourceDir, "import"),
1397 				buildPath(stageDir , "import"),
1398 			);
1399 		}
1400 
1401 		override void performTest()
1402 		{
1403 			getComponent("druntime").needBuild(true);
1404 			getComponent("dmd").needInstalled();
1405 
1406 			foreach (model; config.build.components.common.models)
1407 			{
1408 				auto env = baseEnvironment;
1409 				needCC(env, model);
1410 				runMake(env, model, "unittest");
1411 			}
1412 		}
1413 
1414 		private final void runMake(ref Environment env, string model, string target = null)
1415 		{
1416 			// Work around https://github.com/dlang/druntime/pull/2438
1417 			bool quotePaths = !(isVersion!"Windows" && model != "32" && sourceDir.buildPath("win64.mak").readText().canFind(`"$(CC)"`));
1418 
1419 			string[] args =
1420 				getMake(env) ~
1421 				["-f", makeFileNameModel(model)] ~
1422 				(target ? [target] : []) ~
1423 				["DMD=" ~ dmd] ~
1424 				config.build.components.common.makeArgs ~
1425 				getPlatformMakeVars(env, model, quotePaths) ~
1426 				dMakeArgs;
1427 			run(args, env.vars, sourceDir);
1428 		}
1429 	}
1430 
1431 	/// Phobos library and imports.
1432 	final class Phobos : Component
1433 	{
1434 		@property override string submoduleName    () { return "phobos"; }
1435 		@property override string[] sourceDependencies() { return []; }
1436 		@property override string[] dependencies() { return ["druntime", "dmd"]; }
1437 
1438 		@property override string configString()
1439 		{
1440 			static struct FullConfig
1441 			{
1442 				string model;
1443 				string[] makeArgs;
1444 			}
1445 
1446 			return FullConfig(
1447 				config.build.components.common.model,
1448 				config.build.components.common.makeArgs,
1449 			).toJson();
1450 		}
1451 
1452 		string[] targets;
1453 
1454 		override void performBuild()
1455 		{
1456 			getComponent("dmd").needSource();
1457 			getComponent("dmd").needInstalled();
1458 			getComponent("druntime").needBuild();
1459 
1460 			targets = null;
1461 
1462 			foreach (model; config.build.components.common.models)
1463 			{
1464 				// Clean up old object files with mismatching model.
1465 				// Necessary for a consecutive 32/64 build.
1466 				version (Windows)
1467 				{
1468 					foreach (de; dirEntries(sourceDir.buildPath("etc", "c", "zlib"), "*.obj", SpanMode.shallow))
1469 					{
1470 						auto data = cast(ubyte[])read(de.name);
1471 
1472 						string fileModel;
1473 						if (data.length < 4)
1474 							fileModel = "invalid";
1475 						else
1476 						if (data[0] == 0x80)
1477 							fileModel = "32"; // OMF
1478 						else
1479 						if (data[0] == 0x01 && data[0] == 0x4C)
1480 							fileModel = "32mscoff"; // COFF - IMAGE_FILE_MACHINE_I386
1481 						else
1482 						if (data[0] == 0x86 && data[0] == 0x64)
1483 							fileModel = "64"; // COFF - IMAGE_FILE_MACHINE_AMD64
1484 						else
1485 							fileModel = "unknown";
1486 
1487 						if (fileModel != model)
1488 						{
1489 							log("Cleaning up object file '%s' with mismatching model (file is %s, building %s)".format(de.name, fileModel, model));
1490 							remove(de.name);
1491 						}
1492 					}
1493 				}
1494 
1495 				auto env = baseEnvironment;
1496 				needCC(env, model);
1497 
1498 				string phobosMakeFileName = findMakeFile(sourceDir, makeFileNameModel(model));
1499 				string phobosMakeFullName = sourceDir.buildPath(phobosMakeFileName);
1500 
1501 				version (Windows)
1502 				{
1503 					auto lib = "phobos%s.lib".format(modelSuffix(model));
1504 					runMake(env, model, lib);
1505 					enforce(sourceDir.buildPath(lib).exists);
1506 					targets ~= ["phobos%s.lib".format(modelSuffix(model))];
1507 				}
1508 				else
1509 				{
1510 					string[] makeArgs;
1511 					if (phobosMakeFullName.readText().canFind("DRUNTIME = $(DRUNTIME_PATH)/lib/libdruntime-$(OS)$(MODEL).a") &&
1512 						getComponent("druntime").sourceDir.buildPath("lib").dirEntries(SpanMode.shallow).walkLength == 0 &&
1513 						exists(getComponent("druntime").sourceDir.buildPath("generated")))
1514 					{
1515 						auto dir = getComponent("druntime").sourceDir.buildPath("generated");
1516 						auto aFile  = dir.dirEntries("libdruntime.a", SpanMode.depth);
1517 						if (!aFile .empty) makeArgs ~= ["DRUNTIME="   ~ aFile .front];
1518 						auto soFile = dir.dirEntries("libdruntime.so.a", SpanMode.depth);
1519 						if (!soFile.empty) makeArgs ~= ["DRUNTIMESO=" ~ soFile.front];
1520 					}
1521 					runMake(env, model, makeArgs);
1522 					targets ~= sourceDir
1523 						.buildPath("generated")
1524 						.dirEntries(SpanMode.depth)
1525 						.filter!(de => de.name.endsWith(".a") || de.name.endsWith(".so"))
1526 						.map!(de => de.name.relativePath(sourceDir))
1527 						.array()
1528 					;
1529 				}
1530 			}
1531 		}
1532 
1533 		override void performStage()
1534 		{
1535 			assert(targets.length, "Phobos stage without build");
1536 			foreach (lib; targets)
1537 				cp(
1538 					buildPath(sourceDir, lib),
1539 					buildPath(stageDir , "lib", lib.baseName()),
1540 				);
1541 		}
1542 
1543 		override void performTest()
1544 		{
1545 			getComponent("druntime").needBuild(true);
1546 			getComponent("phobos").needBuild(true);
1547 			getComponent("dmd").needInstalled();
1548 
1549 			foreach (model; config.build.components.common.models)
1550 			{
1551 				auto env = baseEnvironment;
1552 				needCC(env, model);
1553 				version (Windows)
1554 				{
1555 					getComponent("curl").needInstalled();
1556 					getComponent("curl").updateEnv(env);
1557 
1558 					// Patch out std.datetime unittest to work around Digger test
1559 					// suite failure on AppVeyor due to Windows time zone changes
1560 					auto stdDateTime = buildPath(sourceDir, "std", "datetime.d");
1561 					if (stdDateTime.exists && !stdDateTime.readText().canFind("Altai Standard Time"))
1562 					{
1563 						auto m = stdDateTime.readText();
1564 						m = m
1565 							.replace(`assert(tzName !is null, format("TZName which is missing: %s", winName));`, ``)
1566 							.replace(`assert(tzDatabaseNameToWindowsTZName(tzName) !is null, format("TZName which failed: %s", tzName));`, `{}`)
1567 							.replace(`assert(windowsTZNameToTZDatabaseName(tzName) !is null, format("TZName which failed: %s", tzName));`, `{}`)
1568 						;
1569 						stdDateTime.write(m);
1570 						submodule.saveFileState("std/datetime.d");
1571 					}
1572 
1573 					if (model == "32")
1574 						getComponent("extras").needInstalled();
1575 				}
1576 				runMake(env, model, "unittest");
1577 			}
1578 		}
1579 
1580 		private final void runMake(ref Environment env, string model, string[] makeArgs...)
1581 		{
1582 			// Work around https://github.com/dlang/druntime/pull/2438
1583 			bool quotePaths = !(isVersion!"Windows" && model != "32" && sourceDir.buildPath("win64.mak").readText().canFind(`"$(CC)"`));
1584 
1585 			string[] args =
1586 				getMake(env) ~
1587 				["-f", makeFileNameModel(model)] ~
1588 				makeArgs ~
1589 				["DMD=" ~ dmd] ~
1590 				config.build.components.common.makeArgs ~
1591 				getPlatformMakeVars(env, model, quotePaths) ~
1592 				dMakeArgs;
1593 			run(args, env.vars, sourceDir);
1594 		}
1595 	}
1596 
1597 	/// The rdmd build tool by itself.
1598 	/// It predates the tools package.
1599 	final class RDMD : Component
1600 	{
1601 		@property override string submoduleName() { return "tools"; }
1602 		@property override string[] sourceDependencies() { return []; }
1603 		@property override string[] dependencies() { return ["dmd", "druntime", "phobos"]; }
1604 
1605 		@property string model() { return config.build.components.common.models.get(0); }
1606 
1607 		@property override string configString()
1608 		{
1609 			static struct FullConfig
1610 			{
1611 				string model;
1612 			}
1613 
1614 			return FullConfig(
1615 				this.model,
1616 			).toJson();
1617 		}
1618 
1619 		override void performBuild()
1620 		{
1621 			foreach (dep; ["dmd", "druntime", "phobos", "phobos-includes"])
1622 				getComponent(dep).needInstalled();
1623 
1624 			auto env = baseEnvironment;
1625 			needCC(env, this.model);
1626 
1627 			// Just build rdmd
1628 			bool needModel; // Need -mXX switch?
1629 
1630 			if (sourceDir.buildPath("posix.mak").exists)
1631 				needModel = true; // Known to be needed for recent versions
1632 
1633 			string[] args;
1634 			if (needConfSwitch())
1635 				args ~= ["-conf=" ~ buildPath(buildDir , "bin", configFileName)];
1636 			args ~= ["rdmd"];
1637 
1638 			if (!needModel)
1639 				try
1640 					run([dmd] ~ args, env.vars, sourceDir);
1641 				catch (Exception e)
1642 					needModel = true;
1643 
1644 			if (needModel)
1645 				run([dmd, "-m" ~ this.model] ~ args, env.vars, sourceDir);
1646 		}
1647 
1648 		override void performStage()
1649 		{
1650 			cp(
1651 				buildPath(sourceDir, "rdmd" ~ binExt),
1652 				buildPath(stageDir , "bin", "rdmd" ~ binExt),
1653 			);
1654 		}
1655 
1656 		override void performTest()
1657 		{
1658 			auto env = baseEnvironment;
1659 			version (Windows)
1660 				needDMC(env); // Need DigitalMars Make
1661 
1662 			string[] args;
1663 			if (sourceDir.buildPath(makeFileName).readText.canFind("\ntest_rdmd"))
1664 				args = getMake(env) ~ ["-f", makeFileName, "test_rdmd", "DFLAGS=-g -m" ~ model] ~ config.build.components.common.makeArgs ~ getPlatformMakeVars(env, model) ~ dMakeArgs;
1665 			else
1666 			{
1667 				// Legacy (before makefile rules)
1668 
1669 				args = ["dmd", "-m" ~ this.model, "-run", "rdmd_test.d"];
1670 				if (sourceDir.buildPath("rdmd_test.d").readText.canFind("modelSwitch"))
1671 					args ~= "--model=" ~ this.model;
1672 				else
1673 				{
1674 					version (Windows)
1675 						if (this.model != "32")
1676 						{
1677 							// Can't test rdmd on non-32-bit Windows until compiler model matches Phobos model.
1678 							// rdmd_test does not use -m when building rdmd, thus linking will fail
1679 							// (because of model mismatch with the phobos we built).
1680 							log("Can't test rdmd with model " ~ this.model ~ ", skipping");
1681 							return;
1682 						}
1683 				}
1684 			}
1685 
1686 			foreach (dep; ["dmd", "druntime", "phobos", "phobos-includes"])
1687 				getComponent(dep).needInstalled();
1688 
1689 			getComponent("dmd").updateEnv(env);
1690 			run(args, env.vars, sourceDir);
1691 		}
1692 	}
1693 
1694 	/// Tools package with all its components, including rdmd.
1695 	final class Tools : Component
1696 	{
1697 		@property override string submoduleName() { return "tools"; }
1698 		@property override string[] sourceDependencies() { return []; }
1699 		@property override string[] dependencies() { return ["dmd", "druntime", "phobos"]; }
1700 
1701 		@property string model() { return config.build.components.common.models.get(0); }
1702 
1703 		@property override string configString()
1704 		{
1705 			static struct FullConfig
1706 			{
1707 				string model;
1708 				string[] makeArgs;
1709 			}
1710 
1711 			return FullConfig(
1712 				this.model,
1713 				config.build.components.common.makeArgs,
1714 			).toJson();
1715 		}
1716 
1717 		override void performBuild()
1718 		{
1719 			getComponent("dmd").needSource();
1720 			foreach (dep; ["dmd", "druntime", "phobos"])
1721 				getComponent(dep).needInstalled();
1722 
1723 			auto env = baseEnvironment;
1724 			needCC(env, this.model);
1725 
1726 			run(getMake(env) ~ ["-f", makeFileName, "DMD=" ~ dmd] ~ config.build.components.common.makeArgs ~ getPlatformMakeVars(env, this.model) ~ dMakeArgs, env.vars, sourceDir);
1727 		}
1728 
1729 		override void performStage()
1730 		{
1731 			foreach (os; buildPath(sourceDir, "generated").dirEntries(SpanMode.shallow))
1732 				foreach (de; os.buildPath(this.model).dirEntries(SpanMode.shallow))
1733 					if (de.extension == binExt)
1734 						cp(de, buildPath(stageDir, "bin", de.baseName));
1735 		}
1736 	}
1737 
1738 	/// Website (dlang.org). Only buildable on POSIX.
1739 	final class Website : Component
1740 	{
1741 		@property override string submoduleName() { return "dlang.org"; }
1742 		@property override string[] sourceDependencies() { return ["druntime", "phobos", "dub"]; }
1743 		@property override string[] dependencies() { return ["dmd", "druntime", "phobos", "rdmd"]; }
1744 
1745 		struct Config
1746 		{
1747 			/// Do not include timestamps, line numbers, or other
1748 			/// volatile dynamic content in generated .ddoc files.
1749 			/// Improves cache efficiency and allows meaningful diffs.
1750 			bool diffable = false;
1751 
1752 			deprecated alias noDateTime = diffable;
1753 		}
1754 
1755 		@property override string configString()
1756 		{
1757 			static struct FullConfig
1758 			{
1759 				Config config;
1760 			}
1761 
1762 			return FullConfig(
1763 				config.build.components.website,
1764 			).toJson();
1765 		}
1766 
1767 		/// Get the latest version of DMD at the time.
1768 		/// Needed for the makefile's "LATEST" parameter.
1769 		string getLatest()
1770 		{
1771 			auto dmd = getComponent("dmd").submodule;
1772 
1773 			auto t = dmd.git.query(["log", "--pretty=format:%ct"]).splitLines.map!(to!int).filter!(n => n > 0).front;
1774 
1775 			foreach (line; dmd.git.query(["log", "--decorate=full", "--tags", "--pretty=format:%ct%d"]).splitLines())
1776 				if (line.length > 10 && line[0..10].to!int < t)
1777 					if (line[10..$].startsWith(" (") && line.endsWith(")"))
1778 					{
1779 						foreach (r; line[12..$-1].split(", "))
1780 							if (r.skipOver("tag: refs/tags/"))
1781 								if (r.match(re!`^v2\.\d\d\d(\.\d)?$`))
1782 									return r[1..$];
1783 					}
1784 			throw new Exception("Can't find any DMD version tags at this point!");
1785 		}
1786 
1787 		private enum Target { build, test }
1788 
1789 		private void make(Target target)
1790 		{
1791 			foreach (dep; ["dmd", "druntime", "phobos"])
1792 			{
1793 				auto c = getComponent(dep);
1794 				c.needInstalled();
1795 
1796 				// Need DMD source because https://github.com/dlang/phobos/pull/4613#issuecomment-266462596
1797 				// Need Druntime/Phobos source because we are building its documentation from there.
1798 				c.needSource();
1799 			}
1800 			foreach (dep; ["tools", "dub"]) // for changelog; also tools for changed.d
1801 				getComponent(dep).needSource();
1802 
1803 			auto env = baseEnvironment;
1804 
1805 			version (Windows)
1806 				throw new Exception("The dlang.org website is only buildable on POSIX platforms.");
1807 			else
1808 			{
1809 				getComponent("dmd").updateEnv(env);
1810 
1811 				// Need an in-tree build for SYSCONFDIR.imp, which is
1812 				// needed to parse .d files for the DMD API
1813 				// documentation.
1814 				getComponent("dmd").needBuild(target == Target.test);
1815 
1816 				needKindleGen(env);
1817 
1818 				foreach (dep; dependencies)
1819 					getComponent(dep).submodule.clean = false;
1820 
1821 				auto makeFullName = sourceDir.buildPath(makeFileName);
1822 				auto makeSrc = makeFullName.readText();
1823 				makeSrc
1824 					// https://github.com/D-Programming-Language/dlang.org/pull/1011
1825 					.replace(": modlist.d", ": modlist.d $(DMD)")
1826 					// https://github.com/D-Programming-Language/dlang.org/pull/1017
1827 					.replace("dpl-docs: ${DUB} ${STABLE_DMD}\n\tDFLAGS=", "dpl-docs: ${DUB} ${STABLE_DMD}\n\t${DUB} upgrade --missing-only --root=${DPL_DOCS_PATH}\n\tDFLAGS=")
1828 					.toFile(makeFullName)
1829 				;
1830 				submodule.saveFileState(makeFileName);
1831 
1832 				// Retroactive OpenSSL 1.1.0 fix
1833 				// See https://github.com/dlang/dlang.org/pull/1654
1834 				auto dubJson = sourceDir.buildPath("dpl-docs/dub.json");
1835 				dubJson
1836 					.readText()
1837 					.replace(`"versions": ["VibeCustomMain"]`, `"versions": ["VibeCustomMain", "VibeNoSSL"]`)
1838 					.toFile(dubJson);
1839 				submodule.saveFileState("dpl-docs/dub.json");
1840 				scope(exit) submodule.saveFileState("dpl-docs/dub.selections.json");
1841 
1842 				string latest = null;
1843 				if (!sourceDir.buildPath("VERSION").exists)
1844 				{
1845 					latest = getLatest();
1846 					log("LATEST=" ~ latest);
1847 				}
1848 				else
1849 					log("VERSION file found, not passing LATEST parameter");
1850 
1851 				string[] diffable = null;
1852 
1853 				auto pdf = makeSrc.indexOf("pdf") >= 0 ? ["pdf"] : [];
1854 
1855 				string[] targets =
1856 					[
1857 						config.build.components.website.diffable
1858 						? makeSrc.indexOf("dautotest") >= 0
1859 							? ["dautotest"]
1860 							: ["all", "verbatim"] ~ pdf ~ (
1861 								makeSrc.indexOf("diffable-intermediaries") >= 0
1862 								? ["diffable-intermediaries"]
1863 								: ["dlangspec.html"])
1864 						: ["all", "verbatim", "kindle"] ~ pdf,
1865 						["test"]
1866 					][target];
1867 
1868 				if (config.build.components.website.diffable)
1869 				{
1870 					if (makeSrc.indexOf("DIFFABLE") >= 0)
1871 						diffable = ["DIFFABLE=1"];
1872 					else
1873 						diffable = ["NODATETIME=nodatetime.ddoc"];
1874 
1875 					env.vars["SOURCE_DATE_EPOCH"] = "0";
1876 				}
1877 
1878 				auto args =
1879 					getMake(env) ~
1880 					[ "-f", makeFileName ] ~
1881 					diffable ~
1882 					(latest ? ["LATEST=" ~ latest] : []) ~
1883 					targets ~
1884 					gnuMakeArgs;
1885 				run(args, env.vars, sourceDir);
1886 			}
1887 		}
1888 
1889 		override void performBuild()
1890 		{
1891 			make(Target.build);
1892 		}
1893 
1894 		override void performTest()
1895 		{
1896 			make(Target.test);
1897 		}
1898 
1899 		override void performStage()
1900 		{
1901 			foreach (item; ["web", "dlangspec.tex", "dlangspec.html"])
1902 			{
1903 				auto src = buildPath(sourceDir, item);
1904 				auto dst = buildPath(stageDir , item);
1905 				if (src.exists)
1906 					cp(src, dst);
1907 			}
1908 		}
1909 	}
1910 
1911 	/// Extras not built from source (DigitalMars and third-party tools and libraries)
1912 	final class Extras : Component
1913 	{
1914 		@property override string submoduleName() { return null; }
1915 		@property override string[] sourceDependencies() { return []; }
1916 		@property override string[] dependencies() { return []; }
1917 		@property override string configString() { return null; }
1918 
1919 		override void performBuild()
1920 		{
1921 			needExtras();
1922 		}
1923 
1924 		override void performStage()
1925 		{
1926 			auto extrasDir = needExtras();
1927 
1928 			void copyDir(string source, string target)
1929 			{
1930 				source = buildPath(extrasDir, "localextras-" ~ platform, "dmd2", platform, source);
1931 				target = buildPath(stageDir, target);
1932 				if (source.exists)
1933 					cp(source, target);
1934 			}
1935 
1936 			copyDir("bin", "bin");
1937 			foreach (model; config.build.components.common.models)
1938 				copyDir("bin" ~ model, "bin");
1939 			copyDir("lib", "lib");
1940 
1941 			version (Windows)
1942 				foreach (model; config.build.components.common.models)
1943 					if (model == "32")
1944 					{
1945 						// The version of snn.lib bundled with DMC will be newer.
1946 						Environment env;
1947 						needDMC(env);
1948 						cp(buildPath(env.deps.dmcDir, "lib", "snn.lib"), buildPath(stageDir, "lib", "snn.lib"));
1949 					}
1950 		}
1951 	}
1952 
1953 	/// libcurl DLL and import library for Windows.
1954 	final class Curl : Component
1955 	{
1956 		@property override string submoduleName() { return null; }
1957 		@property override string[] sourceDependencies() { return []; }
1958 		@property override string[] dependencies() { return []; }
1959 		@property override string configString() { return null; }
1960 
1961 		override void performBuild()
1962 		{
1963 			version (Windows)
1964 				needCurl();
1965 			else
1966 				log("Not on Windows, skipping libcurl download");
1967 		}
1968 
1969 		override void performStage()
1970 		{
1971 			version (Windows)
1972 			{
1973 				auto curlDir = needCurl();
1974 
1975 				void copyDir(string source, string target)
1976 				{
1977 					source = buildPath(curlDir, "dmd2", "windows", source);
1978 					target = buildPath(stageDir, target);
1979 					if (source.exists)
1980 						cp(source, target);
1981 				}
1982 
1983 				foreach (model; config.build.components.common.models)
1984 				{
1985 					auto suffix = model == "64" ? "64" : "";
1986 					copyDir("bin" ~ suffix, "bin");
1987 					copyDir("lib" ~ suffix, "lib");
1988 				}
1989 			}
1990 			else
1991 				log("Not on Windows, skipping libcurl install");
1992 		}
1993 
1994 		override void updateEnv(ref Environment env)
1995 		{
1996 			env.vars["PATH"] = buildPath(buildDir, "bin").absolutePath() ~ pathSeparator ~ env.vars["PATH"];
1997 		}
1998 	}
1999 
2000 	/// The Dub package manager and build tool
2001 	final class Dub : Component
2002 	{
2003 		@property override string submoduleName() { return "dub"; }
2004 		@property override string[] sourceDependencies() { return []; }
2005 		@property override string[] dependencies() { return []; }
2006 		@property override string configString() { return null; }
2007 
2008 		override void performBuild()
2009 		{
2010 			auto env = baseEnvironment;
2011 			run([dmd, "-i", "-run", "build.d"], env.vars, sourceDir);
2012 		}
2013 
2014 		override void performStage()
2015 		{
2016 			cp(
2017 				buildPath(sourceDir, "bin", "dub" ~ binExt),
2018 				buildPath(stageDir , "bin", "dub" ~ binExt),
2019 			);
2020 		}
2021 	}
2022 
2023 	private int tempError;
2024 
2025 	private Component[string] components;
2026 
2027 	Component getComponent(string name)
2028 	{
2029 		if (name !in components)
2030 		{
2031 			Component c;
2032 
2033 			switch (name)
2034 			{
2035 				case "dmd":
2036 					c = new DMD();
2037 					break;
2038 				case "phobos-includes":
2039 					c = new PhobosIncludes();
2040 					break;
2041 				case "druntime":
2042 					c = new Druntime();
2043 					break;
2044 				case "phobos":
2045 					c = new Phobos();
2046 					break;
2047 				case "rdmd":
2048 					c = new RDMD();
2049 					break;
2050 				case "tools":
2051 					c = new Tools();
2052 					break;
2053 				case "website":
2054 					c = new Website();
2055 					break;
2056 				case "extras":
2057 					c = new Extras();
2058 					break;
2059 				case "curl":
2060 					c = new Curl();
2061 					break;
2062 				case "dub":
2063 					c = new Dub();
2064 					break;
2065 				default:
2066 					throw new Exception("Unknown component: " ~ name);
2067 			}
2068 
2069 			c.name = name;
2070 			return components[name] = c;
2071 		}
2072 
2073 		return components[name];
2074 	}
2075 
2076 	Component[] getSubmoduleComponents(string submoduleName)
2077 	{
2078 		return components
2079 			.byValue
2080 			.filter!(component => component.submoduleName == submoduleName)
2081 			.array();
2082 	}
2083 
2084 	// ***************************** GitHub API ******************************
2085 
2086 	GitHub github;
2087 
2088 	ref GitHub needGitHub()
2089 	{
2090 		if (github is GitHub.init)
2091 		{
2092 			github.log = &this.log;
2093 			github.token = config.local.githubToken;
2094 			github.cache = new class GitHub.ICache
2095 			{
2096 				final string cacheFileName(string key)
2097 				{
2098 					return githubDir.buildPath(getDigestString!MD5(key).toLower());
2099 				}
2100 
2101 				string get(string key)
2102 				{
2103 					auto fn = cacheFileName(key);
2104 					return fn.exists ? fn.readText : null;
2105 				}
2106 
2107 				void put(string key, string value)
2108 				{
2109 					githubDir.ensureDirExists;
2110 					std.file.write(cacheFileName(key), value);
2111 				}
2112 			};
2113 		}
2114 		return github;
2115 	}
2116 
2117 	// **************************** Customization ****************************
2118 
2119 	/// Fetch latest D history.
2120 	/// Return true if any updates were fetched.
2121 	bool update()
2122 	{
2123 		return getMetaRepo().update();
2124 	}
2125 
2126 	struct SubmoduleState
2127 	{
2128 		string[string] submoduleCommits;
2129 	}
2130 
2131 	/// Begin customization, starting at the specified commit.
2132 	SubmoduleState begin(string commit)
2133 	{
2134 		log("Starting at meta repository commit " ~ commit);
2135 		return SubmoduleState(getMetaRepo().getSubmoduleCommits(commit));
2136 	}
2137 
2138 	alias MergeMode = ManagedRepository.MergeMode;
2139 
2140 	/// Applies a merge onto the given SubmoduleState.
2141 	void merge(ref SubmoduleState submoduleState, string submoduleName, string[2] branch, MergeMode mode)
2142 	{
2143 		log("Merging %s commits %s..%s".format(submoduleName, branch[0], branch[1]));
2144 		enforce(submoduleName in submoduleState.submoduleCommits, "Unknown submodule: " ~ submoduleName);
2145 		auto submodule = getSubmodule(submoduleName);
2146 		auto head = submoduleState.submoduleCommits[submoduleName];
2147 		auto result = submodule.getMerge(head, branch, mode);
2148 		submoduleState.submoduleCommits[submoduleName] = result;
2149 	}
2150 
2151 	/// Removes a merge from the given SubmoduleState.
2152 	void unmerge(ref SubmoduleState submoduleState, string submoduleName, string[2] branch, MergeMode mode)
2153 	{
2154 		log("Unmerging %s commits %s..%s".format(submoduleName, branch[0], branch[1]));
2155 		enforce(submoduleName in submoduleState.submoduleCommits, "Unknown submodule: " ~ submoduleName);
2156 		auto submodule = getSubmodule(submoduleName);
2157 		auto head = submoduleState.submoduleCommits[submoduleName];
2158 		auto result = submodule.getUnMerge(head, branch, mode);
2159 		submoduleState.submoduleCommits[submoduleName] = result;
2160 	}
2161 
2162 	/// Reverts a commit from the given SubmoduleState.
2163 	/// parent is the 1-based mainline index (as per `man git-revert`),
2164 	/// or 0 if commit is not a merge commit.
2165 	void revert(ref SubmoduleState submoduleState, string submoduleName, string[2] branch, MergeMode mode)
2166 	{
2167 		log("Reverting %s commits %s..%s".format(submoduleName, branch[0], branch[1]));
2168 		enforce(submoduleName in submoduleState.submoduleCommits, "Unknown submodule: " ~ submoduleName);
2169 		auto submodule = getSubmodule(submoduleName);
2170 		auto head = submoduleState.submoduleCommits[submoduleName];
2171 		auto result = submodule.getRevert(head, branch, mode);
2172 		submoduleState.submoduleCommits[submoduleName] = result;
2173 	}
2174 
2175 	/// Returns the commit hash for the given pull request # (base and tip).
2176 	/// The result can then be used with addMerge/removeMerge.
2177 	string[2] getPull(string submoduleName, int pullNumber)
2178 	{
2179 		auto tip = getSubmodule(submoduleName).getPullTip(pullNumber);
2180 		auto pull = needGitHub().query("https://api.github.com/repos/%s/%s/pulls/%d"
2181 			.format("dlang", submoduleName, pullNumber)).data.parseJSON;
2182 		auto base = pull["base"]["sha"].str;
2183 		return [base, tip];
2184 	}
2185 
2186 	/// Returns the commit hash for the given branch (optionally GitHub fork).
2187 	/// The result can then be used with addMerge/removeMerge.
2188 	string[2] getBranch(string submoduleName, string user, string base, string tip)
2189 	{
2190 		return getSubmodule(submoduleName).getBranch(user, base, tip);
2191 	}
2192 
2193 	// ****************************** Building *******************************
2194 
2195 	private SubmoduleState submoduleState;
2196 	private bool incrementalBuild;
2197 
2198 	@property string cacheEngineName()
2199 	{
2200 		if (incrementalBuild)
2201 			return "none";
2202 		else
2203 			return config.local.cache;
2204 	}
2205 
2206 	private string getComponentCommit(string componentName)
2207 	{
2208 		auto submoduleName = getComponent(componentName).submoduleName;
2209 		auto commit = submoduleState.submoduleCommits.get(submoduleName, null);
2210 		enforce(commit, "Unknown commit to build for component %s (submodule %s)"
2211 			.format(componentName, submoduleName));
2212 		return commit;
2213 	}
2214 
2215 	static const string[] defaultComponents = ["dmd", "druntime", "phobos-includes", "phobos", "rdmd"];
2216 	static const string[] additionalComponents = ["tools", "website", "extras", "curl", "dub"];
2217 	static const string[] allComponents = defaultComponents ~ additionalComponents;
2218 
2219 	/// Build the specified components according to the specified configuration.
2220 	void build(SubmoduleState submoduleState, bool incremental = false)
2221 	{
2222 		auto componentNames = config.build.components.getEnabledComponentNames();
2223 		log("Building components %-(%s, %)".format(componentNames));
2224 
2225 		this.components = null;
2226 		this.submoduleState = submoduleState;
2227 		this.incrementalBuild = incremental;
2228 
2229 		if (buildDir.exists)
2230 			buildDir.removeRecurse();
2231 		enforce(!buildDir.exists);
2232 
2233 		scope(exit) if (cacheEngine) cacheEngine.finalize();
2234 
2235 		foreach (componentName; componentNames)
2236 			getComponent(componentName).needInstalled();
2237 	}
2238 
2239 	/// Shortcut for begin + build
2240 	void buildRev(string rev)
2241 	{
2242 		auto submoduleState = begin(rev);
2243 		build(submoduleState);
2244 	}
2245 
2246 	/// Simply check out the source code for the given submodules.
2247 	void checkout(SubmoduleState submoduleState)
2248 	{
2249 		auto componentNames = config.build.components.getEnabledComponentNames();
2250 		log("Checking out components %-(%s, %)".format(componentNames));
2251 
2252 		this.components = null;
2253 		this.submoduleState = submoduleState;
2254 		this.incrementalBuild = false;
2255 
2256 		foreach (componentName; componentNames)
2257 			getComponent(componentName).needSource(true);
2258 	}
2259 
2260 	/// Rerun build without cleaning up any files.
2261 	void rebuild()
2262 	{
2263 		build(SubmoduleState(null), true);
2264 	}
2265 
2266 	/// Run all tests for the current checkout (like rebuild).
2267 	void test(bool incremental = true)
2268 	{
2269 		auto componentNames = config.build.components.getEnabledComponentNames();
2270 		log("Testing components %-(%s, %)".format(componentNames));
2271 
2272 		if (incremental)
2273 		{
2274 			this.components = null;
2275 			this.submoduleState = SubmoduleState(null);
2276 			this.incrementalBuild = true;
2277 		}
2278 
2279 		foreach (componentName; componentNames)
2280 			getComponent(componentName).test();
2281 	}
2282 
2283 	bool isCached(SubmoduleState submoduleState)
2284 	{
2285 		this.components = null;
2286 		this.submoduleState = submoduleState;
2287 
2288 		needCacheEngine();
2289 		foreach (componentName; config.build.components.getEnabledComponentNames())
2290 			if (!cacheEngine.haveEntry(getComponent(componentName).getBuildID()))
2291 				return false;
2292 		return true;
2293 	}
2294 
2295 	/// Returns the isCached state for all commits in the history of the given ref.
2296 	bool[string] getCacheState(string[string][string] history)
2297 	{
2298 		log("Enumerating cache entries...");
2299 		auto cacheEntries = needCacheEngine().getEntries().toSet();
2300 
2301 		this.components = null;
2302 		auto componentNames = config.build.components.getEnabledComponentNames();
2303 		auto components = componentNames.map!(componentName => getComponent(componentName)).array;
2304 		auto requiredSubmodules = components
2305 			.map!(component => chain(component.name.only, component.sourceDependencies, component.dependencies))
2306 			.joiner
2307 			.map!(componentName => getComponent(componentName).submoduleName)
2308 			.array.sort().uniq().array
2309 		;
2310 
2311 		log("Collating cache state...");
2312 		bool[string] result;
2313 		foreach (commit, submoduleCommits; history)
2314 		{
2315 			import ae.utils.meta : I;
2316 			this.submoduleState.submoduleCommits = submoduleCommits;
2317 			result[commit] =
2318 				requiredSubmodules.all!(submoduleName => submoduleName in submoduleCommits) &&
2319 				componentNames.all!(componentName =>
2320 					getComponent(componentName).I!(component =>
2321 						component.getBuildID() in cacheEntries
2322 					)
2323 				);
2324 		}
2325 		return result;
2326 	}
2327 
2328 	/// ditto
2329 	bool[string] getCacheState(string[] refs)
2330 	{
2331 		auto history = getMetaRepo().getSubmoduleHistory(refs);
2332 		return getCacheState(history);
2333 	}
2334 
2335 	// **************************** Dependencies *****************************
2336 
2337 	private void needInstaller()
2338 	{
2339 		Installer.logger = &log;
2340 		Installer.installationDirectory = dlDir;
2341 	}
2342 
2343 	/// Pull in a built DMD as configured.
2344 	/// Note that this function invalidates the current repository state.
2345 	void needDMD(ref Environment env, string dmdVer)
2346 	{
2347 		tempError++; scope(success) tempError--;
2348 
2349 		// User setting overrides autodetection
2350 		if (config.build.components.dmd.bootstrap.ver)
2351 			dmdVer = config.build.components.dmd.bootstrap.ver;
2352 
2353 		if (config.build.components.dmd.bootstrap.fromSource)
2354 		{
2355 			log("Bootstrapping DMD " ~ dmdVer);
2356 
2357 			auto bootstrapBuildConfig = config.build.components.dmd.bootstrap.build;
2358 
2359 			// Back up and clear component state
2360 			enum backupTemplate = q{
2361 				auto VARBackup = this.VAR;
2362 				this.VAR = typeof(VAR).init;
2363 				scope(exit) this.VAR = VARBackup;
2364 			};
2365 			mixin(backupTemplate.replace(q{VAR}, q{components}));
2366 			mixin(backupTemplate.replace(q{VAR}, q{config}));
2367 			mixin(backupTemplate.replace(q{VAR}, q{submoduleState}));
2368 
2369 			config.local = configBackup.local;
2370 			if (bootstrapBuildConfig)
2371 				config.build = *bootstrapBuildConfig;
2372 
2373 			// Disable building rdmd in the bootstrap compiler by default
2374 			if ("rdmd" !in config.build.components.enable)
2375 				config.build.components.enable["rdmd"] = false;
2376 
2377 			build(parseSpec(dmdVer));
2378 
2379 			log("Built bootstrap DMD " ~ dmdVer ~ " successfully.");
2380 
2381 			auto bootstrapDir = buildPath(config.local.workDir, "bootstrap");
2382 			if (bootstrapDir.exists)
2383 				bootstrapDir.removeRecurse();
2384 			ensurePathExists(bootstrapDir);
2385 			rename(buildDir, bootstrapDir);
2386 
2387 			env.deps.hostDC = buildPath(bootstrapDir, "bin", "dmd" ~ binExt);
2388 		}
2389 		else
2390 		{
2391 			import std.ascii;
2392 			log("Preparing DMD " ~ dmdVer);
2393 			enforce(dmdVer.startsWith("v"), "Invalid DMD version spec for binary bootstrap. Did you forget to " ~
2394 				((dmdVer.length && dmdVer[0].isDigit && dmdVer.contains('.')) ? "add a leading 'v'" : "enable fromSource") ~ "?");
2395 			needInstaller();
2396 			auto dmdInstaller = new DMDInstaller(dmdVer[1..$]);
2397 			dmdInstaller.requireLocal(false);
2398 			env.deps.hostDC = dmdInstaller.exePath("dmd").absolutePath();
2399 		}
2400 
2401 		log("hostDC=" ~ env.deps.hostDC);
2402 	}
2403 
2404 	void needKindleGen(ref Environment env)
2405 	{
2406 		needInstaller();
2407 		kindleGenInstaller.requireLocal(false);
2408 		env.vars["PATH"] = kindleGenInstaller.directory ~ pathSeparator ~ env.vars["PATH"];
2409 	}
2410 
2411 	version (Windows)
2412 	void needMSYS(ref Environment env)
2413 	{
2414 		needInstaller();
2415 		MSYS.msysCORE.requireLocal(false);
2416 		MSYS.libintl.requireLocal(false);
2417 		MSYS.libiconv.requireLocal(false);
2418 		MSYS.libtermcap.requireLocal(false);
2419 		MSYS.libregex.requireLocal(false);
2420 		MSYS.coreutils.requireLocal(false);
2421 		MSYS.bash.requireLocal(false);
2422 		MSYS.make.requireLocal(false);
2423 		MSYS.grep.requireLocal(false);
2424 		MSYS.sed.requireLocal(false);
2425 		MSYS.diffutils.requireLocal(false);
2426 		env.vars["PATH"] = MSYS.bash.directory.buildPath("bin") ~ pathSeparator ~ env.vars["PATH"];
2427 	}
2428 
2429 	/// Get DMD unbuildable extras
2430 	/// (proprietary DigitalMars utilities, 32-bit import libraries)
2431 	string needExtras()
2432 	{
2433 		import ae.utils.meta : I, singleton;
2434 
2435 		static class DExtrasInstaller : Installer
2436 		{
2437 			@property override string name() { return "dmd-localextras"; }
2438 			string url = "http://semitwist.com/download/app/dmd-localextras.7z";
2439 
2440 			override void installImpl(string target)
2441 			{
2442 				url
2443 					.I!save()
2444 					.I!unpackTo(target);
2445 			}
2446 
2447 			static this()
2448 			{
2449 				urlDigests["http://semitwist.com/download/app/dmd-localextras.7z"] = "ef367c2d25d4f19f45ade56ab6991c726b07d3d9";
2450 			}
2451 		}
2452 
2453 		alias extrasInstaller = singleton!DExtrasInstaller;
2454 
2455 		needInstaller();
2456 		extrasInstaller.requireLocal(false);
2457 		return extrasInstaller.directory;
2458 	}
2459 
2460 	/// Get libcurl for Windows (DLL and import libraries)
2461 	version (Windows)
2462 	string needCurl()
2463 	{
2464 		import ae.utils.meta : I, singleton;
2465 
2466 		static class DCurlInstaller : Installer
2467 		{
2468 			@property override string name() { return "libcurl-" ~ curlVersion; }
2469 			string curlVersion = "7.47.1";
2470 			@property string url() { return "http://downloads.dlang.org/other/libcurl-" ~ curlVersion ~ "-WinSSL-zlib-x86-x64.zip"; }
2471 
2472 			override void installImpl(string target)
2473 			{
2474 				url
2475 					.I!save()
2476 					.I!unpackTo(target);
2477 			}
2478 
2479 			static this()
2480 			{
2481 				urlDigests["http://downloads.dlang.org/other/libcurl-7.47.1-WinSSL-zlib-x86-x64.zip"] = "4b8a7bb237efab25a96588093ae51994c821e097";
2482 			}
2483 		}
2484 
2485 		alias curlInstaller = singleton!DCurlInstaller;
2486 
2487 		needInstaller();
2488 		curlInstaller.requireLocal(false);
2489 		return curlInstaller.directory;
2490 	}
2491 
2492 	version (Windows)
2493 	void needDMC(ref Environment env, string ver = null)
2494 	{
2495 		tempError++; scope(success) tempError--;
2496 
2497 		needInstaller();
2498 
2499 		auto dmc = ver ? new LegacyDMCInstaller(ver) : dmcInstaller;
2500 		if (!dmc.installedLocally)
2501 			log("Preparing DigitalMars C++ " ~ ver);
2502 		dmc.requireLocal(false);
2503 		env.deps.dmcDir = dmc.directory;
2504 
2505 		auto binPath = buildPath(env.deps.dmcDir, `bin`).absolutePath();
2506 		log("DMC=" ~ binPath);
2507 		env.vars["DMC"] = binPath;
2508 		env.vars["PATH"] = binPath ~ pathSeparator ~ env.vars.get("PATH", null);
2509 	}
2510 
2511 	version (Windows)
2512 	auto getVSInstaller()
2513 	{
2514 		needInstaller();
2515 		return vs2013community;
2516 	}
2517 
2518 	version (Windows)
2519 	static string msvcModelStr(string model, string str32, string str64)
2520 	{
2521 		switch (model)
2522 		{
2523 			case "32":
2524 				throw new Exception("Shouldn't need VC for 32-bit builds");
2525 			case "64":
2526 				return str64;
2527 			case "32mscoff":
2528 				return str32;
2529 			default:
2530 				throw new Exception("Unknown model: " ~ model);
2531 		}
2532 	}
2533 
2534 	version (Windows)
2535 	static string msvcModelDir(string model, string dir64 = "x86_amd64")
2536 	{
2537 		return msvcModelStr(model, null, dir64);
2538 	}
2539 
2540 	version (Windows)
2541 	void needVC(ref Environment env, string model)
2542 	{
2543 		tempError++; scope(success) tempError--;
2544 
2545 		auto vs = getVSInstaller();
2546 
2547 		// At minimum, we want the C compiler (cl.exe) and linker (link.exe).
2548 		vs["vc_compilercore86"].requireLocal(false); // Contains both x86 and x86_amd64 cl.exe
2549 		vs["vc_compilercore86res"].requireLocal(false); // Contains clui.dll needed by cl.exe
2550 
2551 		// Include files. Needed when using VS to build either DMD or Druntime.
2552 		vs["vc_librarycore86"].requireLocal(false); // Contains include files, e.g. errno.h needed by Druntime
2553 
2554 		// C runtime. Needed for all programs built with VC.
2555 		vs[msvcModelStr(model, "vc_libraryDesktop_x86", "vc_libraryDesktop_x64")].requireLocal(false); // libcmt.lib
2556 
2557 		// XP-compatible import libraries.
2558 		vs["win_xpsupport"].requireLocal(false); // shell32.lib
2559 
2560 		// MSBuild, for the useVC option
2561 		if (config.build.components.dmd.useVC)
2562 			vs["Msi_BuildTools_MSBuild_x86"].requireLocal(false); // msbuild.exe
2563 
2564 		env.deps.vsDir  = vs.directory.buildPath("Program Files (x86)", "Microsoft Visual Studio 12.0").absolutePath();
2565 		env.deps.sdkDir = vs.directory.buildPath("Program Files", "Microsoft SDKs", "Windows", "v7.1A").absolutePath();
2566 
2567 		env.vars["PATH"] ~= pathSeparator ~ vs.modelBinPaths(msvcModelDir(model)).map!(path => vs.directory.buildPath(path).absolutePath()).join(pathSeparator);
2568 		env.vars["VisualStudioVersion"] = "12"; // Work-around for problem fixed in dmd 38da6c2258c0ff073b0e86e0a1f6ba190f061e5e
2569 		env.vars["VSINSTALLDIR"] = env.deps.vsDir ~ dirSeparator; // ditto
2570 		env.vars["VCINSTALLDIR"] = env.deps.vsDir.buildPath("VC") ~ dirSeparator;
2571 		env.vars["INCLUDE"] = env.deps.vsDir.buildPath("VC", "include") ~ ";" ~ env.deps.sdkDir.buildPath("Include");
2572 		env.vars["LIB"] = env.deps.vsDir.buildPath("VC", "lib", msvcModelDir(model, "amd64")) ~ ";" ~ env.deps.sdkDir.buildPath("Lib", msvcModelDir(model, "x64"));
2573 		env.vars["WindowsSdkDir"] = env.deps.sdkDir ~ dirSeparator;
2574 		env.vars["Platform"] = "x64";
2575 		env.vars["LINKCMD64"] = env.deps.vsDir.buildPath("VC", "bin", msvcModelDir(model), "link.exe"); // Used by dmd
2576 		env.vars["MSVC_CC"] = env.deps.vsDir.buildPath("VC", "bin", msvcModelDir(model), "cl.exe"); // For the msvc-dmc wrapper
2577 		env.vars["MSVC_AR"] = env.deps.vsDir.buildPath("VC", "bin", msvcModelDir(model), "lib.exe"); // For the msvc-lib wrapper
2578 		env.vars["CL"] = "-D_USING_V110_SDK71_"; // Work around __userHeader macro redifinition VS bug
2579 	}
2580 
2581 	private void needGit()
2582 	{
2583 		tempError++; scope(success) tempError--;
2584 
2585 		needInstaller();
2586 		gitInstaller.require();
2587 	}
2588 
2589 	/// Disable the "<program> has stopped working"
2590 	/// standard Windows dialog.
2591 	version (Windows)
2592 	static void disableCrashDialog()
2593 	{
2594 		enum : uint { SEM_FAILCRITICALERRORS = 1, SEM_NOGPFAULTERRORBOX = 2 }
2595 		SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
2596 	}
2597 
2598 	/// Create a build environment base.
2599 	protected @property Environment baseEnvironment()
2600 	{
2601 		Environment env;
2602 
2603 		// Build a new environment from scratch, to avoid tainting the build with the current environment.
2604 		string[] newPaths;
2605 
2606 		version (Windows)
2607 		{
2608 			import std.utf;
2609 			import ae.sys.windows.imports;
2610 			mixin(importWin32!q{winbase});
2611 			mixin(importWin32!q{winnt});
2612 
2613 			TCHAR[1024] buf;
2614 			// Needed for DLLs
2615 			auto winDir = buf[0..GetWindowsDirectory(buf.ptr, buf.length)].toUTF8();
2616 			auto sysDir = buf[0..GetSystemDirectory (buf.ptr, buf.length)].toUTF8();
2617 			newPaths ~= [sysDir, winDir];
2618 
2619 			newPaths ~= gitInstaller.exePath("git").absolutePath().dirName; // For git-describe and such
2620 		}
2621 		else
2622 		{
2623 			// Needed for coreutils, make, gcc, git etc.
2624 			newPaths = ["/bin", "/usr/bin", "/usr/local/bin"];
2625 
2626 			version (linux)
2627 			{
2628 				// GCC wrappers
2629 				ensureDirExists(binDir);
2630 				newPaths = binDir ~ newPaths;
2631 			}
2632 		}
2633 
2634 		env.vars["PATH"] = newPaths.join(pathSeparator);
2635 
2636 		ensureDirExists(tmpDir);
2637 		env.vars["TMPDIR"] = env.vars["TEMP"] = env.vars["TMP"] = tmpDir;
2638 
2639 		version (Windows)
2640 		{
2641 			env.vars["SystemDrive"] = winDir.driveName;
2642 			env.vars["SystemRoot"] = winDir;
2643 		}
2644 
2645 		ensureDirExists(homeDir);
2646 		env.vars["HOME"] = homeDir;
2647 
2648 		return env;
2649 	}
2650 
2651 	/// Apply user modifications onto an environment.
2652 	/// Supports Windows-style %VAR% expansions.
2653 	static string[string] applyEnv(in string[string] target, in string[string] source)
2654 	{
2655 		// The source of variable expansions is variables in the target environment,
2656 		// if they exist, and the host environment otherwise, so e.g.
2657 		// `PATH=C:\...;%PATH%` and `MAKE=%MAKE%` work as expected.
2658 		auto oldEnv = std.process.environment.toAA();
2659 		foreach (name, value; target)
2660 			oldEnv[name] = value;
2661 
2662 		string[string] result;
2663 		foreach (name, value; target)
2664 			result[name] = value;
2665 		foreach (name, value; source)
2666 		{
2667 			string newValue = value;
2668 			foreach (oldName, oldValue; oldEnv)
2669 				newValue = newValue.replace("%" ~ oldName ~ "%", oldValue);
2670 			result[name] = oldEnv[name] = newValue;
2671 		}
2672 		return result;
2673 	}
2674 
2675 	// ******************************** Cache ********************************
2676 
2677 	enum unbuildableMarker = "unbuildable";
2678 
2679 	DCache cacheEngine;
2680 
2681 	DCache needCacheEngine()
2682 	{
2683 		if (!cacheEngine)
2684 		{
2685 			if (cacheEngineName == "git")
2686 				needGit();
2687 			cacheEngine = createCache(cacheEngineName, cacheEngineDir(cacheEngineName), this);
2688 		}
2689 		return cacheEngine;
2690 	}
2691 
2692 	void cp(string src, string dst)
2693 	{
2694 		needCacheEngine().cp(src, dst);
2695 	}
2696 
2697 	private string[] getComponentKeyOrder(string componentName)
2698 	{
2699 		auto submodule = getComponent(componentName).submodule;
2700 		return submodule
2701 			.git.query("log", "--pretty=format:%H", "--all", "--topo-order")
2702 			.splitLines()
2703 			.map!(commit => componentName ~ "-" ~ commit ~ "-")
2704 			.array
2705 		;
2706 	}
2707 
2708 	string componentNameFromKey(string key)
2709 	{
2710 		auto parts = key.split("-");
2711 		return parts[0..$-2].join("-");
2712 	}
2713 
2714 	string[][] getKeyOrder(string key)
2715 	{
2716 		if (key !is null)
2717 			return [getComponentKeyOrder(componentNameFromKey(key))];
2718 		else
2719 			return allComponents.map!(componentName => getComponentKeyOrder(componentName)).array;
2720 	}
2721 
2722 	/// Optimize entire cache.
2723 	void optimizeCache()
2724 	{
2725 		needCacheEngine().optimize();
2726 	}
2727 
2728 	bool shouldPurge(string key)
2729 	{
2730 		auto files = cacheEngine.listFiles(key);
2731 		if (files.canFind(unbuildableMarker))
2732 			return true;
2733 
2734 		if (componentNameFromKey(key) == "druntime")
2735 		{
2736 			if (!files.canFind("import/core/memory.d")
2737 			 && !files.canFind("import/core/memory.di"))
2738 				return true;
2739 		}
2740 
2741 		return false;
2742 	}
2743 
2744 	/// Delete cached "unbuildable" build results.
2745 	void purgeUnbuildable()
2746 	{
2747 		needCacheEngine()
2748 			.getEntries
2749 			.filter!(key => shouldPurge(key))
2750 			.each!((key)
2751 			{
2752 				log("Deleting: " ~ key);
2753 				cacheEngine.remove(key);
2754 			})
2755 		;
2756 	}
2757 
2758 	/// Move cached files from one cache engine to another.
2759 	void migrateCache(string sourceEngineName, string targetEngineName)
2760 	{
2761 		auto sourceEngine = createCache(sourceEngineName, cacheEngineDir(sourceEngineName), this);
2762 		auto targetEngine = createCache(targetEngineName, cacheEngineDir(targetEngineName), this);
2763 		auto tempDir = buildPath(config.local.workDir, "temp");
2764 		if (tempDir.exists)
2765 			tempDir.removeRecurse();
2766 		log("Enumerating source entries...");
2767 		auto sourceEntries = sourceEngine.getEntries();
2768 		log("Enumerating target entries...");
2769 		auto targetEntries = targetEngine.getEntries().sort();
2770 		foreach (key; sourceEntries)
2771 			if (!targetEntries.canFind(key))
2772 			{
2773 				log(key);
2774 				sourceEngine.extract(key, tempDir, fn => true);
2775 				targetEngine.add(key, tempDir);
2776 				if (tempDir.exists)
2777 					tempDir.removeRecurse();
2778 			}
2779 		targetEngine.optimize();
2780 	}
2781 
2782 	// **************************** Miscellaneous ****************************
2783 
2784 	struct LogEntry
2785 	{
2786 		string hash;
2787 		string[] message;
2788 		SysTime time;
2789 	}
2790 
2791 	/// Gets the D merge log (newest first).
2792 	LogEntry[] getLog(string refName = "refs/remotes/origin/master")
2793 	{
2794 		auto history = getMetaRepo().git.getHistory();
2795 		LogEntry[] logs;
2796 		auto master = history.commits[history.refs[refName]];
2797 		for (auto c = master; c; c = c.parents.length ? c.parents[0] : null)
2798 		{
2799 			auto time = SysTime(c.time.unixTimeToStdTime);
2800 			logs ~= LogEntry(c.hash.toString(), c.message, time);
2801 		}
2802 		return logs;
2803 	}
2804 
2805 	// ***************************** Integration *****************************
2806 
2807 	/// Override to add logging.
2808 	void log(string line)
2809 	{
2810 	}
2811 
2812 	/// Bootstrap description resolution.
2813 	/// See DMD.Config.Bootstrap.spec.
2814 	/// This is essentially a hack to allow the entire
2815 	/// Config structure to be parsed from an .ini file.
2816 	SubmoduleState parseSpec(string spec)
2817 	{
2818 		auto rev = getMetaRepo().getRef("refs/tags/" ~ spec);
2819 		log("Resolved " ~ spec ~ " to " ~ rev);
2820 		return begin(rev);
2821 	}
2822 
2823 	/// Override this method with one which returns a command,
2824 	/// which will invoke the unmergeRebaseEdit function below,
2825 	/// passing to it any additional parameters.
2826 	/// Note: Currently unused. Was previously used
2827 	/// for unmerging things using interactive rebase.
2828 	abstract string getCallbackCommand();
2829 
2830 	void callback(string[] args) { assert(false); }
2831 }