1 /**
2 	Generator for VisualD project files
3 
4 	Copyright: © 2012-2013 Matthias Dondorff
5 	License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
6 	Authors: Matthias Dondorff
7 */
8 module dub.generators.visuald;
9 
10 import dub.compilers.compiler;
11 import dub.generators.generator;
12 import dub.internal.utils;
13 import dub.internal.vibecompat.core.file;
14 import dub.internal.logging;
15 import dub.package_;
16 import dub.packagemanager;
17 import dub.project;
18 
19 import std.algorithm;
20 import std.array;
21 import std.conv;
22 import std.exception;
23 import std.format;
24 import std.string : format;
25 import std.uuid;
26 
27 
28 // Dubbing is developing dub...
29 //version = DUBBING;
30 
31 // TODO: handle pre/post build commands
32 
33 
34 class VisualDGenerator : ProjectGenerator {
35 	private {
36 		PackageManager m_pkgMgr;
37 		string[string] m_projectUuids;
38 	}
39 
40 	this(Project project)
41 	{
42 		super(project);
43 		m_pkgMgr = project.packageManager;
44 	}
45 
46 	override void generateTargets(GeneratorSettings settings, in TargetInfo[string] targets)
47 	{
48 		logDebug("About to generate projects for %s, with %s direct dependencies.", m_project.rootPackage.name, m_project.rootPackage.getAllDependencies().length);
49 		generateProjectFiles(settings, targets);
50 		generateSolutionFile(settings, targets);
51 	}
52 
53 	private {
54 		void generateSolutionFile(GeneratorSettings settings, in TargetInfo[string] targets)
55 		{
56 			auto ret = appender!(char[])();
57 			auto configs = m_project.getPackageConfigs(settings.platform, settings.config);
58 			auto some_uuid = generateUUID();
59 
60 			// Solution header
61 			ret.put("Microsoft Visual Studio Solution File, Format Version 11.00\n");
62 			ret.put("# Visual Studio 2010\n");
63 
64 			bool[string] visited;
65 			void generateSolutionEntry(string pack) {
66 				if (pack in visited) return;
67 				visited[pack] = true;
68 
69 				auto ti = targets[pack];
70 
71 				auto uuid = guid(pack);
72 				ret.formattedWrite("Project(\"%s\") = \"%s\", \"%s\", \"%s\"\n",
73 					some_uuid, pack, projFileName(pack), uuid);
74 
75 				if (ti.linkDependencies.length && ti.buildSettings.targetType != TargetType.staticLibrary) {
76 					ret.put("\tProjectSection(ProjectDependencies) = postProject\n");
77 					foreach (d; ti.linkDependencies)
78 						if (!isHeaderOnlyPackage(d, targets)) {
79 							// TODO: clarify what "uuid = uuid" should mean
80 							ret.formattedWrite("\t\t%s = %s\n", guid(d), guid(d));
81 						}
82 					ret.put("\tEndProjectSection\n");
83 				}
84 
85 				ret.put("EndProject\n");
86 
87 				foreach (d; ti.dependencies) generateSolutionEntry(d);
88 			}
89 
90 			auto mainpack = m_project.rootPackage.name;
91 
92 			generateSolutionEntry(mainpack);
93 
94 			// Global section contains configurations
95 			ret.put("Global\n");
96 			ret.put("\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n");
97 			ret.formattedWrite("\t\t%s|%s = %s|%s\n",
98 				settings.buildType,
99 				settings.platform.architecture[0].vsArchitecture,
100 				settings.buildType,
101 				settings.platform.architecture[0].vsArchitecture);
102 			ret.put("\tEndGlobalSection\n");
103 			ret.put("\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\n");
104 
105 			const string[] sub = ["ActiveCfg", "Build.0"];
106 			const string[] conf = [settings.buildType~"|"~settings.platform.architecture[0].vsArchitecture];
107 			foreach (t; targets.byKey)
108 				foreach (c; conf)
109 					foreach (s; sub)
110 						formattedWrite(ret, "\t\t%s.%s.%s = %s\n", guid(t), c, s, c);
111 
112 			// TODO: for all dependencies
113 			ret.put("\tEndGlobalSection\n");
114 
115 			ret.put("\tGlobalSection(SolutionProperties) = preSolution\n");
116 			ret.put("\t\tHideSolutionNode = FALSE\n");
117 			ret.put("\tEndGlobalSection\n");
118 			ret.put("EndGlobal\n");
119 
120 			// Writing solution file
121 			logDebug("About to write to .sln file with %s bytes", to!string(ret.data.length));
122 			NativePath(solutionFileName()).writeFile(ret.data);
123 			logInfo("Generated", Color.green, "%s (solution)", solutionFileName());
124 		}
125 
126 
127 		void generateProjectFiles(GeneratorSettings settings, in TargetInfo[string] targets)
128 		{
129 			bool[string] visited;
130 			void performRec(string name) {
131 				if (name in visited) return;
132 				visited[name] = true;
133 				generateProjectFile(name, settings, targets);
134 				foreach (d; targets[name].dependencies)
135 					performRec(d);
136 			}
137 
138 			performRec(m_project.rootPackage.name);
139 		}
140 
141 		bool isHeaderOnlyPackage(string pack, in TargetInfo[string] targets)
142 		const {
143 			auto buildsettings = targets[pack].buildSettings;
144 			if (!buildsettings.sourceFiles.any!(f => f.endsWith(".d"))())
145 				return true;
146 			return false;
147 		}
148 
149 		void generateProjectFile(string packname, GeneratorSettings settings, in TargetInfo[string] targets)
150 		{
151 			import dub.compilers.utils : isLinkerFile;
152 
153 			auto ret = appender!(char[])();
154 
155 			auto root_package_path = m_project.rootPackage.path;
156 			auto basepath = NativePath(".dub/");
157 			if (!isWritableDir(basepath, true))
158 				throw new Exception(".dub is not writeable");
159 			ret.put("<DProject>\n");
160 			ret.formattedWrite("  <ProjectGuid>%s</ProjectGuid>\n", guid(packname));
161 
162 			// Several configurations (debug, release, unittest)
163 			generateProjectConfiguration(ret, packname, settings.buildType, settings, targets);
164 			//generateProjectConfiguration(ret, packname, "release", settings, targets);
165 			//generateProjectConfiguration(ret, packname, "unittest", settings, targets);
166 
167 			// Add all files
168 			auto files = targets[packname].buildSettings;
169 			SourceFile[string] sourceFiles;
170 			void addSourceFile(NativePath file_path, NativePath structure_path, SourceFile.Action action)
171 			{
172 				auto key = file_path.toString();
173 				auto sf = sourceFiles.get(key, SourceFile.init);
174 				sf.filePath = file_path;
175 				if (sf.action == SourceFile.Action.none) {
176 					sf.action = action;
177 					sf.structurePath = structure_path;
178 				}
179 				sourceFiles[key] = sf;
180 			}
181 
182 			void addFile(string s, SourceFile.Action action) {
183 				auto sp = NativePath(s);
184 				assert(sp.absolute, format("Source path in %s expected to be absolute: %s", packname, s));
185 				//if( !sp.absolute ) sp = pack.path ~ sp;
186 				addSourceFile(sp.relativeTo(getWorkingDirectory() ~ basepath), determineStructurePath(sp, targets[packname]), action);
187 			}
188 
189 			foreach (p; targets[packname].packages)
190 				if (!p.recipePath.empty)
191 					addFile(p.recipePath.toNativeString(), SourceFile.Action.none);
192 
193 			if (files.targetType == TargetType.staticLibrary)
194 				foreach(s; files.sourceFiles.filter!(s => !isLinkerFile(settings.platform, s))) addFile(s, SourceFile.Action.build);
195 			else
196 				foreach(s; files.sourceFiles.filter!(s => !s.endsWith(".lib"))) addFile(s, SourceFile.Action.build);
197 
198 			foreach(s; files.importFiles) addFile(s, SourceFile.Action.none);
199 			foreach(s; files.stringImportFiles) addFile(s, SourceFile.Action.none);
200 			findFilesMatchingGlobs(root_package_path, files.copyFiles, s => addFile(s, SourceFile.Action.copy));
201 			findFilesMatchingGlobs(root_package_path, files.extraDependencyFiles, s => addFile(s, SourceFile.Action.none));
202 
203 			// Create folders and files
204 			ret.formattedWrite("  <Folder name=\"%s\">", getPackageFileName(packname));
205 			typeof(NativePath.init.head)[] lastFolder;
206 			foreach(source; sortedSources(sourceFiles.values)) {
207 				logDebug("source looking at %s", source.structurePath);
208 				auto cur = source.structurePath.parentPath.bySegment.array;
209 				if(lastFolder != cur) {
210 					size_t same = 0;
211 					foreach(idx; 0..min(lastFolder.length, cur.length))
212 						if(lastFolder[idx] != cur[idx]) break;
213 						else same = idx+1;
214 
215 					const decrease = lastFolder.length - min(lastFolder.length, same);
216 					const increase = cur.length - min(cur.length, same);
217 
218 					foreach(unused; 0..decrease)
219 						ret.put("\n    </Folder>");
220 					foreach(idx; 0..increase)
221 						ret.formattedWrite("\n    <Folder name=\"%s\">", cur[same + idx].name);
222 					lastFolder = cur;
223 				}
224 				final switch (source.action) with (SourceFile.Action)
225 				{
226 					case none:
227 						ret.formattedWrite("\n      <File path=\"%s\" tool=\"None\" />", source.filePath.toNativeString());
228 						break;
229 					case build:
230 						ret.formattedWrite("\n      <File path=\"%s\" />", source.filePath.toNativeString());
231 						break;
232 					case copy:
233 						ret.formattedWrite("\n      <File customcmd=\"copy /Y $(InputPath) $(TargetDir)\" path=\"%s\" tool=\"Custom\" />", source.filePath.toNativeString());
234 						break;
235 				}
236 			}
237 			// Finalize all open folders
238 			foreach(unused; 0..lastFolder.length)
239 				ret.put("\n    </Folder>");
240 			ret.put("\n  </Folder>\n</DProject>");
241 
242 			logDebug("About to write to '%s.visualdproj' file %s bytes", getPackageFileName(packname), ret.data.length);
243 			projFileName(packname).writeFile(ret.data);
244 		}
245 
246 		void generateProjectConfiguration(Appender!(char[]) ret, string pack, string type, GeneratorSettings settings, in TargetInfo[string] targets)
247 		{
248 			auto buildsettings = targets[pack].buildSettings.dup;
249 			auto basepath = NativePath(".dub/");
250 
251 			string[] getSettings(string setting)(){ return __traits(getMember, buildsettings, setting); }
252 			string[] getPathSettings(string setting)()
253 			{
254 				auto settings = getSettings!setting();
255 				auto ret = new string[settings.length];
256 				foreach (i; 0 .. settings.length) {
257 					// \" is interpreted as an escaped " by cmd.exe, so we need to avoid that
258 					auto p = NativePath(settings[i]).relativeTo(getWorkingDirectory() ~ basepath);
259 					p.endsWithSlash = false;
260 					ret[i] = '"' ~ p.toNativeString() ~ '"';
261 				}
262 				return ret;
263 			}
264 
265 			if (buildsettings.targetType == TargetType.none)
266 				return;
267 
268 			foreach(architecture; settings.platform.architecture) {
269 				auto arch = architecture.vsArchitecture;
270 				ret.formattedWrite("  <Config name=\"%s\" platform=\"%s\">\n", to!string(type), arch);
271 
272 				// debug and optimize setting
273 				ret.formattedWrite("    <symdebug>%s</symdebug>\n", buildsettings.options & BuildOption.debugInfo ? "1" : "0");
274 				ret.formattedWrite("    <optimize>%s</optimize>\n", buildsettings.options & BuildOption.optimize ? "1" : "0");
275 				ret.formattedWrite("    <useInline>%s</useInline>\n", buildsettings.options & BuildOption.inline ? "1" : "0");
276 				ret.formattedWrite("    <release>%s</release>\n", buildsettings.options & BuildOption.releaseMode ? "1" : "0");
277 
278 				// Lib or exe?
279 				enum
280 				{
281 					Executable = 0,
282 					StaticLib = 1,
283 					DynamicLib = 2
284 				}
285 
286 				int output_type = StaticLib; // library
287 				string output_ext = "lib";
288 				if (buildsettings.targetType == TargetType.executable)
289 				{
290 					output_type = Executable;
291 					output_ext = "exe";
292 				}
293 				else if (buildsettings.targetType == TargetType.dynamicLibrary)
294 				{
295 					output_type = DynamicLib;
296 					output_ext = "dll";
297 				}
298 				auto bin_path = pack == m_project.rootPackage.name ? NativePath(buildsettings.targetPath) : NativePath("lib/");
299 				bin_path.endsWithSlash = true;
300 				ret.formattedWrite("    <lib>%s</lib>\n", output_type);
301 				ret.formattedWrite("    <exefile>%s%s.%s</exefile>\n", bin_path.toNativeString(), buildsettings.targetName, output_ext);
302 
303 				// include paths and string imports
304 				string imports = join(getPathSettings!"importPaths"(), " ");
305 				string stringImports = join(getPathSettings!"stringImportPaths"(), " ");
306 				ret.formattedWrite("    <imppath>%s</imppath>\n", imports);
307 				ret.formattedWrite("    <fileImppath>%s</fileImppath>\n", stringImports);
308 
309 				ret.formattedWrite("    <program>%s</program>\n", "$(DMDInstallDir)windows\\bin\\dmd.exe"); // FIXME: use the actually selected compiler!
310 				ret.formattedWrite("    <additionalOptions>%s</additionalOptions>\n", getSettings!"dflags"().join(" "));
311 
312 				// Add version identifiers
313 				string versions = join(getSettings!"versions"(), " ");
314 				ret.formattedWrite("    <versionids>%s</versionids>\n", versions);
315 
316 				// Add libraries, system libs need to be suffixed by ".lib".
317 				string linkLibs = join(map!(a => a~".lib")(getSettings!"libs"()), " ");
318 				string addLinkFiles = join(getSettings!"sourceFiles"().filter!(s => s.endsWith(".lib"))(), " ");
319 				if (arch == "x86") addLinkFiles ~= " phobos.lib";
320 				if (output_type != StaticLib) ret.formattedWrite("    <libfiles>%s %s</libfiles>\n", linkLibs, addLinkFiles);
321 
322 				// Unittests
323 				ret.formattedWrite("    <useUnitTests>%s</useUnitTests>\n", buildsettings.options & BuildOption.unittests ? "1" : "0");
324 
325 				// Better C
326 				ret.formattedWrite("    <betterC>%s</betterC>\n", buildsettings.options & BuildOption.betterC ? "1" : "0");
327 
328 				// compute directory for intermediate files (need dummy/ because of how -op determines the resulting path)
329 				size_t ndummy = 0;
330 				foreach (f; buildsettings.sourceFiles) {
331 					auto rpath = NativePath(f).relativeTo(getWorkingDirectory() ~ basepath);
332 					size_t nd = 0;
333 					foreach (s; rpath.bySegment)
334 						if (s == "..")
335 							nd++;
336 					if (nd > ndummy) ndummy = nd;
337 				}
338 				string intersubdir = replicate("dummy/", ndummy) ~ getPackageFileName(pack);
339 
340 				ret.put("    <obj>0</obj>\n");
341 				ret.put("    <link>0</link>\n");
342 				ret.put("    <subsystem>0</subsystem>\n");
343 				ret.put("    <multiobj>0</multiobj>\n");
344 				int singlefilemode;
345 				final switch (settings.buildMode) with (BuildMode) {
346 					case separate: singlefilemode = 2; break;
347 					case allAtOnce: singlefilemode = 0; break;
348 					case singleFile: singlefilemode = 1; break;
349 					//case compileOnly: singlefilemode = 3; break;
350 				}
351 				ret.formattedWrite("    <singleFileCompilation>%s</singleFileCompilation>\n", singlefilemode);
352 				ret.formattedWrite("    <mscoff>%s</mscoff>", buildsettings.dflags.canFind("-m32mscoff") ? "1" : "0");
353 				ret.put("    <oneobj>0</oneobj>\n");
354 				ret.put("    <trace>0</trace>\n");
355 				ret.put("    <quiet>0</quiet>\n");
356 				ret.formattedWrite("    <verbose>%s</verbose>\n", buildsettings.options & BuildOption.verbose ? "1" : "0");
357 				ret.put("    <vtls>0</vtls>\n");
358 				ret.put("    <cpu>0</cpu>\n");
359 				ret.formattedWrite("    <isX86_64>%s</isX86_64>\n", arch == "x64" ? 1 : 0);
360 				ret.put("    <isLinux>0</isLinux>\n");
361 				ret.put("    <isOSX>0</isOSX>\n");
362 				ret.put("    <isWindows>0</isWindows>\n");
363 				ret.put("    <isFreeBSD>0</isFreeBSD>\n");
364 				ret.put("    <isSolaris>0</isSolaris>\n");
365 				ret.put("    <isDragonFlyBSD>0</isDragonFlyBSD>\n");
366 				ret.put("    <scheduler>0</scheduler>\n");
367 				ret.put("    <useDeprecated>0</useDeprecated>\n");
368 				ret.put("    <useAssert>0</useAssert>\n");
369 				ret.put("    <useInvariants>0</useInvariants>\n");
370 				ret.put("    <useIn>0</useIn>\n");
371 				ret.put("    <useOut>0</useOut>\n");
372 				ret.put("    <useArrayBounds>0</useArrayBounds>\n");
373 				ret.formattedWrite("    <noboundscheck>%s</noboundscheck>\n", buildsettings.options & BuildOption.noBoundsCheck ? "1" : "0");
374 				ret.put("    <useSwitchError>0</useSwitchError>\n");
375 				ret.put("    <preservePaths>1</preservePaths>\n");
376 				ret.formattedWrite("    <warnings>%s</warnings>\n", buildsettings.options & BuildOption.warningsAsErrors ? "1" : "0");
377 				ret.formattedWrite("    <infowarnings>%s</infowarnings>\n", buildsettings.options & BuildOption.warnings ? "1" : "0");
378 				ret.formattedWrite("    <checkProperty>%s</checkProperty>\n", buildsettings.options & BuildOption.property ? "1" : "0");
379 				ret.formattedWrite("    <genStackFrame>%s</genStackFrame>\n", buildsettings.options & BuildOption.alwaysStackFrame ? "1" : "0");
380 				ret.put("    <pic>0</pic>\n");
381 				ret.formattedWrite("    <cov>%s</cov>\n", buildsettings.options & BuildOption.coverage ? "1" : "0");
382 				ret.put("    <nofloat>0</nofloat>\n");
383 				ret.put("    <Dversion>2</Dversion>\n");
384 				ret.formattedWrite("    <ignoreUnsupportedPragmas>%s</ignoreUnsupportedPragmas>\n", buildsettings.options & BuildOption.ignoreUnknownPragmas ? "1" : "0");
385 				ret.formattedWrite("    <compiler>%s</compiler>\n", settings.compiler.name == "ldc" ? 2 : settings.compiler.name == "gdc" ? 1 : 0);
386 				ret.formattedWrite("    <otherDMD>0</otherDMD>\n");
387 				ret.formattedWrite("    <outdir>%s</outdir>\n", bin_path.toNativeString());
388 				ret.formattedWrite("    <objdir>obj/%s/%s</objdir>\n", to!string(type), intersubdir);
389 				ret.put("    <objname />\n");
390 				ret.put("    <libname />\n");
391 				ret.put("    <doDocComments>0</doDocComments>\n");
392 				ret.put("    <docdir />\n");
393 				ret.put("    <docname />\n");
394 				ret.put("    <modules_ddoc />\n");
395 				ret.put("    <ddocfiles />\n");
396 				ret.put("    <doHdrGeneration>0</doHdrGeneration>\n");
397 				ret.put("    <hdrdir />\n");
398 				ret.put("    <hdrname />\n");
399 				ret.put("    <doXGeneration>1</doXGeneration>\n");
400 				ret.put("    <xfilename>$(IntDir)\\$(TargetName).json</xfilename>\n");
401 				ret.put("    <debuglevel>0</debuglevel>\n");
402 				ret.put("    <versionlevel>0</versionlevel>\n");
403 				ret.put("    <debugids />\n");
404 				ret.put("    <dump_source>0</dump_source>\n");
405 				ret.put("    <mapverbosity>0</mapverbosity>\n");
406 				ret.put("    <createImplib>0</createImplib>\n");
407 				ret.put("    <defaultlibname />\n");
408 				ret.put("    <debuglibname />\n");
409 				ret.put("    <moduleDepsFile />\n");
410 				ret.put("    <run>0</run>\n");
411 				ret.put("    <runargs />\n");
412 				ret.put("    <runCv2pdb>1</runCv2pdb>\n");
413 				ret.put("    <pathCv2pdb>$(VisualDInstallDir)cv2pdb\\cv2pdb.exe</pathCv2pdb>\n");
414 				ret.put("    <cv2pdbPre2043>0</cv2pdbPre2043>\n");
415 				ret.put("    <cv2pdbNoDemangle>0</cv2pdbNoDemangle>\n");
416 				ret.put("    <cv2pdbEnumType>0</cv2pdbEnumType>\n");
417 				ret.put("    <cv2pdbOptions />\n");
418 				ret.put("    <objfiles />\n");
419 				ret.put("    <linkswitches />\n");
420 				ret.put("    <libpaths />\n");
421 				ret.put("    <deffile />\n");
422 				ret.put("    <resfile />\n");
423 				auto wdir = NativePath(buildsettings.workingDirectory);
424 				if (!wdir.absolute) wdir = m_project.rootPackage.path ~ wdir;
425 				ret.formattedWrite("    <debugworkingdir>%s</debugworkingdir>\n",
426 					wdir.relativeTo(getWorkingDirectory() ~ basepath).toNativeString());
427 				ret.put("    <preBuildCommand />\n");
428 				ret.put("    <postBuildCommand />\n");
429 				ret.put("    <filesToClean>*.obj;*.cmd;*.build;*.dep</filesToClean>\n");
430 				ret.put("  </Config>\n");
431 			} // foreach(architecture)
432 		}
433 
434 		void performOnDependencies(const Package main, string[string] configs, void delegate(const Package pack) op)
435 		{
436 			foreach (p; m_project.getTopologicalPackageList(false, main, configs)) {
437 				if (p is main) continue;
438 				op(p);
439 			}
440 		}
441 
442 		string generateUUID() const {
443 			import std.string;
444 			return "{" ~ toUpper(randomUUID().toString()) ~ "}";
445 		}
446 
447 		string guid(string projectName) {
448 			if(projectName !in m_projectUuids)
449 				m_projectUuids[projectName] = generateUUID();
450 			return m_projectUuids[projectName];
451 		}
452 
453 		auto solutionFileName() const {
454 			version(DUBBING) return getPackageFileName(m_project.rootPackage) ~ ".dubbed.sln";
455 			else return getPackageFileName(m_project.rootPackage.name) ~ ".sln";
456 		}
457 
458 		NativePath projFileName(string pack) const {
459 			auto basepath = NativePath(".dub/");
460 			version(DUBBING) return basepath ~ (getPackageFileName(pack) ~ ".dubbed.visualdproj");
461 			else return basepath ~ (getPackageFileName(pack) ~ ".visualdproj");
462 		}
463 	}
464 
465 	// TODO: nice folders
466 	private struct SourceFile {
467 		NativePath structurePath;
468 		NativePath filePath;
469 		enum Action { none, build, copy };
470 		Action action = Action.none;
471 
472 		size_t toHash() const nothrow @trusted { return structurePath.toHash() ^ filePath.toHash() ^ (action * 0x1f3e7b2c); }
473 		int opCmp(ref const SourceFile rhs) const { return sortOrder(this, rhs); }
474 		// "a < b" for folder structures (deepest folder first, else lexical)
475 		private final static int sortOrder(ref const SourceFile a, ref const SourceFile b) {
476 			assert(!a.structurePath.empty);
477 			assert(!b.structurePath.empty);
478 			static if (is(typeof(a.structurePath.nodes))) { // vibe.d < 0.8.2
479 				auto as = a.structurePath.nodes;
480 				auto bs = b.structurePath.nodes;
481 			} else {
482 				auto as = a.structurePath.bySegment.array;
483 				auto bs = b.structurePath.bySegment.array;
484 			}
485 
486 			// Check for different folders, compare folders only (omit last one).
487 			for(uint idx=0; idx<min(as.length-1, bs.length-1); ++idx)
488 				if(as[idx] != bs[idx])
489 					return as[idx].name.cmp(bs[idx].name);
490 
491 			if(as.length != bs.length) {
492 				// If length differ, the longer one is "smaller", that is more
493 				// specialized and will be put out first.
494 				return as.length > bs.length? -1 : 1;
495 			}
496 			else {
497 				// Both paths indicate files in the same directory, use lexical
498 				// ordering for those.
499 				return as[$-1].name.cmp(bs[$-1].name);
500 			}
501 		}
502 	}
503 
504 	private auto sortedSources(SourceFile[] sources) {
505 		return sort(sources);
506 	}
507 
508 	unittest {
509 		SourceFile[] sfs = [
510 			{ NativePath("b/file.d"), NativePath("") },
511 			{ NativePath("b/b/fileA.d"), NativePath("") },
512 			{ NativePath("a/file.d"), NativePath("") },
513 			{ NativePath("b/b/fileB.d"), NativePath("") },
514 			{ NativePath("b/b/b/fileA.d"), NativePath("") },
515 			{ NativePath("b/c/fileA.d"), NativePath("") },
516 		];
517 		auto sorted = sort(sfs);
518 		SourceFile[] sortedSfs;
519 		foreach(sr; sorted)
520 			sortedSfs ~= sr;
521 		assert(sortedSfs[0].structurePath == NativePath("a/file.d"), "1");
522 		assert(sortedSfs[1].structurePath == NativePath("b/b/b/fileA.d"), "2");
523 		assert(sortedSfs[2].structurePath == NativePath("b/b/fileA.d"), "3");
524 		assert(sortedSfs[3].structurePath == NativePath("b/b/fileB.d"), "4");
525 		assert(sortedSfs[4].structurePath == NativePath("b/c/fileA.d"), "5");
526 		assert(sortedSfs[5].structurePath == NativePath("b/file.d"), "6");
527 	}
528 }
529 
530 private NativePath determineStructurePath(NativePath file_path, in ProjectGenerator.TargetInfo target)
531 {
532 	foreach (p; target.packages) {
533 		if (file_path.startsWith(p.path))
534 			return NativePath(getPackageFileName(p.name)) ~ file_path.relativeTo(p.path);
535 	}
536 	return NativePath("misc/") ~ file_path.head;
537 }
538 
539 private string getPackageFileName(string pack)
540 {
541 	return pack.replace(":", "_");
542 }
543 
544 private @property string vsArchitecture(string architecture)
545 {
546 	switch(architecture) {
547 		default: logWarn("Unsupported platform('%s'), defaulting to x86", architecture); goto case;
548 		case "x86", "x86_mscoff": return "Win32";
549 		case "x86_64": return "x64";
550 	}
551 }