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.vibecompat.core.log;
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 			auto projectUuid = guid(mainpack);
108 			foreach (t; targets.byKey)
109 				foreach (c; conf)
110 					foreach (s; sub)
111 						formattedWrite(ret, "\t\t%s.%s.%s = %s\n", guid(t), c, s, c);
112 
113 			// TODO: for all dependencies
114 			ret.put("\tEndGlobalSection\n");
115 
116 			ret.put("\tGlobalSection(SolutionProperties) = preSolution\n");
117 			ret.put("\t\tHideSolutionNode = FALSE\n");
118 			ret.put("\tEndGlobalSection\n");
119 			ret.put("EndGlobal\n");
120 
121 			// Writing solution file
122 			logDebug("About to write to .sln file with %s bytes", to!string(ret.data.length));
123 			auto sln = openFile(solutionFileName(), FileMode.createTrunc);
124 			scope(exit) sln.close();
125 			sln.put(ret.data);
126 			sln.flush();
127 
128 			logInfo("Solution '%s' generated.", solutionFileName());
129 		}
130 
131 
132 		void generateProjectFiles(GeneratorSettings settings, in TargetInfo[string] targets)
133 		{
134 			bool[string] visited;
135 			void performRec(string name) {
136 				if (name in visited) return;
137 				visited[name] = true;
138 				generateProjectFile(name, settings, targets);
139 				foreach (d; targets[name].dependencies)
140 					performRec(d);
141 			}
142 
143 			performRec(m_project.rootPackage.name);
144 		}
145 
146 		bool isHeaderOnlyPackage(string pack, in TargetInfo[string] targets)
147 		const {
148 			auto buildsettings = targets[pack].buildSettings;
149 			if (!buildsettings.sourceFiles.any!(f => f.endsWith(".d"))())
150 				return true;
151 			return false;
152 		}
153 
154 		void generateProjectFile(string packname, GeneratorSettings settings, in TargetInfo[string] targets)
155 		{
156 			import dub.compilers.utils : isLinkerFile;
157 
158 			int i = 0;
159 			auto ret = appender!(char[])();
160 
161 			auto project_file_dir = m_project.rootPackage.path ~ projFileName(packname).parentPath;
162 			ret.put("<DProject>\n");
163 			ret.formattedWrite("  <ProjectGuid>%s</ProjectGuid>\n", guid(packname));
164 
165 			// Several configurations (debug, release, unittest)
166 			generateProjectConfiguration(ret, packname, settings.buildType, settings, targets);
167 			//generateProjectConfiguration(ret, packname, "release", settings, targets);
168 			//generateProjectConfiguration(ret, packname, "unittest", settings, targets);
169 
170 			// Add all files
171 			auto files = targets[packname].buildSettings;
172 			SourceFile[string] sourceFiles;
173 			void addSourceFile(NativePath file_path, NativePath structure_path, bool build)
174 			{
175 				auto key = file_path.toString();
176 				auto sf = sourceFiles.get(key, SourceFile.init);
177 				sf.filePath = file_path;
178 				if (!sf.build) {
179 					sf.build = build;
180 					sf.structurePath = structure_path;
181 				}
182 				sourceFiles[key] = sf;
183 			}
184 
185 			void addFile(string s, bool build) {
186 				auto sp = NativePath(s);
187 				assert(sp.absolute, format("Source path in %s expected to be absolute: %s", packname, s));
188 				//if( !sp.absolute ) sp = pack.path ~ sp;
189 				addSourceFile(sp.relativeTo(project_file_dir), determineStructurePath(sp, targets[packname]), build);
190 			}
191 
192 			foreach (p; targets[packname].packages)
193 				if (!p.recipePath.empty)
194 					addFile(p.recipePath.toNativeString(), false);
195 
196 			if (files.targetType == TargetType.staticLibrary)
197 				foreach(s; files.sourceFiles.filter!(s => !isLinkerFile(s))) addFile(s, true);
198 			else
199 				foreach(s; files.sourceFiles.filter!(s => !s.endsWith(".lib"))) addFile(s, true);
200 
201 			foreach(s; files.importFiles) addFile(s, false);
202 			foreach(s; files.stringImportFiles) addFile(s, false);
203 
204 			// Create folders and files
205 			ret.formattedWrite("  <Folder name=\"%s\">", getPackageFileName(packname));
206 			typeof(NativePath.init.head)[] lastFolder;
207 			foreach(source; sortedSources(sourceFiles.values)) {
208 				logDebug("source looking at %s", source.structurePath);
209 				auto cur = source.structurePath.parentPath.bySegment.array;
210 				if(lastFolder != cur) {
211 					size_t same = 0;
212 					foreach(idx; 0..min(lastFolder.length, cur.length))
213 						if(lastFolder[idx] != cur[idx]) break;
214 						else same = idx+1;
215 
216 					const decrease = lastFolder.length - min(lastFolder.length, same);
217 					const increase = cur.length - min(cur.length, same);
218 
219 					foreach(unused; 0..decrease)
220 						ret.put("\n    </Folder>");
221 					foreach(idx; 0..increase)
222 						ret.formattedWrite("\n    <Folder name=\"%s\">", cur[same + idx].toString());
223 					lastFolder = cur;
224 				}
225 				ret.formattedWrite("\n      <File %spath=\"%s\" />", source.build ? "" : "tool=\"None\" ", source.filePath.toNativeString());
226 			}
227 			// Finalize all open folders
228 			foreach(unused; 0..lastFolder.length)
229 				ret.put("\n    </Folder>");
230 			ret.put("\n  </Folder>\n</DProject>");
231 
232 			logDebug("About to write to '%s.visualdproj' file %s bytes", getPackageFileName(packname), ret.data.length);
233 			auto proj = openFile(projFileName(packname), FileMode.createTrunc);
234 			scope(exit) proj.close();
235 			proj.put(ret.data);
236 			proj.flush();
237 		}
238 
239 		void generateProjectConfiguration(Appender!(char[]) ret, string pack, string type, GeneratorSettings settings, in TargetInfo[string] targets)
240 		{
241 			auto project_file_dir = m_project.rootPackage.path ~ projFileName(pack).parentPath;
242 			auto buildsettings = targets[pack].buildSettings.dup;
243 
244 			string[] getSettings(string setting)(){ return __traits(getMember, buildsettings, setting); }
245 			string[] getPathSettings(string setting)()
246 			{
247 				auto settings = getSettings!setting();
248 				auto ret = new string[settings.length];
249 				foreach (i; 0 .. settings.length) {
250 					// \" is interpreted as an escaped " by cmd.exe, so we need to avoid that
251 					auto p = NativePath(settings[i]).relativeTo(project_file_dir);
252 					p.endsWithSlash = false;
253 					ret[i] = '"' ~ p.toNativeString() ~ '"';
254 				}
255 				return ret;
256 			}
257 
258 			foreach(architecture; settings.platform.architecture) {
259 				auto arch = architecture.vsArchitecture;
260 				ret.formattedWrite("  <Config name=\"%s\" platform=\"%s\">\n", to!string(type), arch);
261 
262 				// debug and optimize setting
263 				ret.formattedWrite("    <symdebug>%s</symdebug>\n", buildsettings.options & BuildOption.debugInfo ? "1" : "0");
264 				ret.formattedWrite("    <optimize>%s</optimize>\n", buildsettings.options & BuildOption.optimize ? "1" : "0");
265 				ret.formattedWrite("    <useInline>%s</useInline>\n", buildsettings.options & BuildOption.inline ? "1" : "0");
266 				ret.formattedWrite("    <release>%s</release>\n", buildsettings.options & BuildOption.releaseMode ? "1" : "0");
267 
268 				// Lib or exe?
269 				enum
270 				{
271 					Executable = 0,
272 					StaticLib = 1,
273 					DynamicLib = 2
274 				}
275 
276 				int output_type = StaticLib; // library
277 				string output_ext = "lib";
278 				if (buildsettings.targetType == TargetType.executable)
279 				{
280 					output_type = Executable;
281 					output_ext = "exe";
282 				}
283 				else if (buildsettings.targetType == TargetType.dynamicLibrary)
284 				{
285 					output_type = DynamicLib;
286 					output_ext = "dll";
287 				}
288 				auto bin_path = pack == m_project.rootPackage.name ? NativePath(buildsettings.targetPath) : NativePath("lib/");
289 				bin_path.endsWithSlash = true;
290 				ret.formattedWrite("    <lib>%s</lib>\n", output_type);
291 				ret.formattedWrite("    <exefile>%s%s.%s</exefile>\n", bin_path.toNativeString(), buildsettings.targetName, output_ext);
292 
293 				// include paths and string imports
294 				string imports = join(getPathSettings!"importPaths"(), " ");
295 				string stringImports = join(getPathSettings!"stringImportPaths"(), " ");
296 				ret.formattedWrite("    <imppath>%s</imppath>\n", imports);
297 				ret.formattedWrite("    <fileImppath>%s</fileImppath>\n", stringImports);
298 
299 				ret.formattedWrite("    <program>%s</program>\n", "$(DMDInstallDir)windows\\bin\\dmd.exe"); // FIXME: use the actually selected compiler!
300 				ret.formattedWrite("    <additionalOptions>%s</additionalOptions>\n", getSettings!"dflags"().join(" "));
301 
302 				// Add version identifiers
303 				string versions = join(getSettings!"versions"(), " ");
304 				ret.formattedWrite("    <versionids>%s</versionids>\n", versions);
305 
306 				// Add libraries, system libs need to be suffixed by ".lib".
307 				string linkLibs = join(map!(a => a~".lib")(getSettings!"libs"()), " ");
308 				string addLinkFiles = join(getSettings!"sourceFiles"().filter!(s => s.endsWith(".lib"))(), " ");
309 				if (arch == "x86") addLinkFiles ~= " phobos.lib";
310 				if (output_type != StaticLib) ret.formattedWrite("    <libfiles>%s %s</libfiles>\n", linkLibs, addLinkFiles);
311 
312 				// Unittests
313 				ret.formattedWrite("    <useUnitTests>%s</useUnitTests>\n", buildsettings.options & BuildOption.unittests ? "1" : "0");
314 
315 				// compute directory for intermediate files (need dummy/ because of how -op determines the resulting path)
316 				size_t ndummy = 0;
317 				foreach (f; buildsettings.sourceFiles) {
318 					auto rpath = NativePath(f).relativeTo(project_file_dir);
319 					size_t nd = 0;
320 					foreach (s; rpath.bySegment)
321 						if (s == "..")
322 							nd++;
323 					if (nd > ndummy) ndummy = nd;
324 				}
325 				string intersubdir = replicate("dummy/", ndummy) ~ getPackageFileName(pack);
326 
327 				ret.put("    <obj>0</obj>\n");
328 				ret.put("    <link>0</link>\n");
329 				ret.put("    <subsystem>0</subsystem>\n");
330 				ret.put("    <multiobj>0</multiobj>\n");
331 				int singlefilemode;
332 				final switch (settings.buildMode) with (BuildMode) {
333 					case separate: singlefilemode = 2; break;
334 					case allAtOnce: singlefilemode = 0; break;
335 					case singleFile: singlefilemode = 1; break;
336 					//case compileOnly: singlefilemode = 3; break;
337 				}
338 				ret.formattedWrite("    <singleFileCompilation>%s</singleFileCompilation>\n", singlefilemode);
339 				ret.formattedWrite("    <mscoff>%s</mscoff>", buildsettings.dflags.canFind("-m32mscoff") ? "1" : "0");
340 				ret.put("    <oneobj>0</oneobj>\n");
341 				ret.put("    <trace>0</trace>\n");
342 				ret.put("    <quiet>0</quiet>\n");
343 				ret.formattedWrite("    <verbose>%s</verbose>\n", buildsettings.options & BuildOption.verbose ? "1" : "0");
344 				ret.put("    <vtls>0</vtls>\n");
345 				ret.put("    <cpu>0</cpu>\n");
346 				ret.formattedWrite("    <isX86_64>%s</isX86_64>\n", arch == "x64" ? 1 : 0);
347 				ret.put("    <isLinux>0</isLinux>\n");
348 				ret.put("    <isOSX>0</isOSX>\n");
349 				ret.put("    <isWindows>0</isWindows>\n");
350 				ret.put("    <isFreeBSD>0</isFreeBSD>\n");
351 				ret.put("    <isSolaris>0</isSolaris>\n");
352 				ret.put("    <isDragonFlyBSD>0</isDragonFlyBSD>\n");
353 				ret.put("    <scheduler>0</scheduler>\n");
354 				ret.put("    <useDeprecated>0</useDeprecated>\n");
355 				ret.put("    <useAssert>0</useAssert>\n");
356 				ret.put("    <useInvariants>0</useInvariants>\n");
357 				ret.put("    <useIn>0</useIn>\n");
358 				ret.put("    <useOut>0</useOut>\n");
359 				ret.put("    <useArrayBounds>0</useArrayBounds>\n");
360 				ret.formattedWrite("    <noboundscheck>%s</noboundscheck>\n", buildsettings.options & BuildOption.noBoundsCheck ? "1" : "0");
361 				ret.put("    <useSwitchError>0</useSwitchError>\n");
362 				ret.put("    <preservePaths>1</preservePaths>\n");
363 				ret.formattedWrite("    <warnings>%s</warnings>\n", buildsettings.options & BuildOption.warningsAsErrors ? "1" : "0");
364 				ret.formattedWrite("    <infowarnings>%s</infowarnings>\n", buildsettings.options & BuildOption.warnings ? "1" : "0");
365 				ret.formattedWrite("    <checkProperty>%s</checkProperty>\n", buildsettings.options & BuildOption.property ? "1" : "0");
366 				ret.formattedWrite("    <genStackFrame>%s</genStackFrame>\n", buildsettings.options & BuildOption.alwaysStackFrame ? "1" : "0");
367 				ret.put("    <pic>0</pic>\n");
368 				ret.formattedWrite("    <cov>%s</cov>\n", buildsettings.options & BuildOption.coverage ? "1" : "0");
369 				ret.put("    <nofloat>0</nofloat>\n");
370 				ret.put("    <Dversion>2</Dversion>\n");
371 				ret.formattedWrite("    <ignoreUnsupportedPragmas>%s</ignoreUnsupportedPragmas>\n", buildsettings.options & BuildOption.ignoreUnknownPragmas ? "1" : "0");
372 				ret.formattedWrite("    <compiler>%s</compiler>\n", settings.compiler.name == "ldc" ? 2 : settings.compiler.name == "gdc" ? 1 : 0);
373 				ret.formattedWrite("    <otherDMD>0</otherDMD>\n");
374 				ret.formattedWrite("    <outdir>%s</outdir>\n", bin_path.toNativeString());
375 				ret.formattedWrite("    <objdir>obj/%s/%s</objdir>\n", to!string(type), intersubdir);
376 				ret.put("    <objname />\n");
377 				ret.put("    <libname />\n");
378 				ret.put("    <doDocComments>0</doDocComments>\n");
379 				ret.put("    <docdir />\n");
380 				ret.put("    <docname />\n");
381 				ret.put("    <modules_ddoc />\n");
382 				ret.put("    <ddocfiles />\n");
383 				ret.put("    <doHdrGeneration>0</doHdrGeneration>\n");
384 				ret.put("    <hdrdir />\n");
385 				ret.put("    <hdrname />\n");
386 				ret.put("    <doXGeneration>1</doXGeneration>\n");
387 				ret.put("    <xfilename>$(IntDir)\\$(TargetName).json</xfilename>\n");
388 				ret.put("    <debuglevel>0</debuglevel>\n");
389 				ret.put("    <versionlevel>0</versionlevel>\n");
390 				ret.put("    <debugids />\n");
391 				ret.put("    <dump_source>0</dump_source>\n");
392 				ret.put("    <mapverbosity>0</mapverbosity>\n");
393 				ret.put("    <createImplib>0</createImplib>\n");
394 				ret.put("    <defaultlibname />\n");
395 				ret.put("    <debuglibname />\n");
396 				ret.put("    <moduleDepsFile />\n");
397 				ret.put("    <run>0</run>\n");
398 				ret.put("    <runargs />\n");
399 				ret.put("    <runCv2pdb>1</runCv2pdb>\n");
400 				ret.put("    <pathCv2pdb>$(VisualDInstallDir)cv2pdb\\cv2pdb.exe</pathCv2pdb>\n");
401 				ret.put("    <cv2pdbPre2043>0</cv2pdbPre2043>\n");
402 				ret.put("    <cv2pdbNoDemangle>0</cv2pdbNoDemangle>\n");
403 				ret.put("    <cv2pdbEnumType>0</cv2pdbEnumType>\n");
404 				ret.put("    <cv2pdbOptions />\n");
405 				ret.put("    <objfiles />\n");
406 				ret.put("    <linkswitches />\n");
407 				ret.put("    <libpaths />\n");
408 				ret.put("    <deffile />\n");
409 				ret.put("    <resfile />\n");
410 				auto wdir = NativePath(buildsettings.workingDirectory);
411 				if (!wdir.absolute) wdir = m_project.rootPackage.path ~ wdir;
412 				ret.formattedWrite("    <debugworkingdir>%s</debugworkingdir>\n",
413 					wdir.relativeTo(project_file_dir).toNativeString());
414 				ret.put("    <preBuildCommand />\n");
415 				ret.put("    <postBuildCommand />\n");
416 				ret.put("    <filesToClean>*.obj;*.cmd;*.build;*.dep</filesToClean>\n");
417 				ret.put("  </Config>\n");
418 			} // foreach(architecture)
419 		}
420 
421 		void performOnDependencies(const Package main, string[string] configs, void delegate(const Package pack) op)
422 		{
423 			foreach (p; m_project.getTopologicalPackageList(false, main, configs)) {
424 				if (p is main) continue;
425 				op(p);
426 			}
427 		}
428 
429 		string generateUUID() const {
430 			import std.string;
431 			return "{" ~ toUpper(randomUUID().toString()) ~ "}";
432 		}
433 
434 		string guid(string projectName) {
435 			if(projectName !in m_projectUuids)
436 				m_projectUuids[projectName] = generateUUID();
437 			return m_projectUuids[projectName];
438 		}
439 
440 		auto solutionFileName() const {
441 			version(DUBBING) return getPackageFileName(m_project.rootPackage) ~ ".dubbed.sln";
442 			else return getPackageFileName(m_project.rootPackage.name) ~ ".sln";
443 		}
444 
445 		NativePath projFileName(string pack) const {
446 			auto basepath = NativePath(".dub/");
447 			version(DUBBING) return basepath ~ (getPackageFileName(pack) ~ ".dubbed.visualdproj");
448 			else return basepath ~ (getPackageFileName(pack) ~ ".visualdproj");
449 		}
450 	}
451 
452 	// TODO: nice folders
453 	private struct SourceFile {
454 		NativePath structurePath;
455 		NativePath filePath;
456 		bool build;
457 
458 		hash_t toHash() const nothrow @trusted { return structurePath.toHash() ^ filePath.toHash() ^ (build * 0x1f3e7b2c); }
459 		int opCmp(ref const SourceFile rhs) const { return sortOrder(this, rhs); }
460 		// "a < b" for folder structures (deepest folder first, else lexical)
461 		private final static int sortOrder(ref const SourceFile a, ref const SourceFile b) {
462 			assert(!a.structurePath.empty);
463 			assert(!b.structurePath.empty);
464 			static if (is(typeof(a.structurePath.nodes))) { // vibe.d < 0.8.2
465 				auto as = a.structurePath.nodes;
466 				auto bs = b.structurePath.nodes;
467 			} else {
468 				auto as = a.structurePath.bySegment.array;
469 				auto bs = b.structurePath.bySegment.array;
470 			}
471 
472 			// Check for different folders, compare folders only (omit last one).
473 			for(uint idx=0; idx<min(as.length-1, bs.length-1); ++idx)
474 				if(as[idx] != bs[idx])
475 					return as[idx].name.cmp(bs[idx].name);
476 
477 			if(as.length != bs.length) {
478 				// If length differ, the longer one is "smaller", that is more
479 				// specialized and will be put out first.
480 				return as.length > bs.length? -1 : 1;
481 			}
482 			else {
483 				// Both paths indicate files in the same directory, use lexical
484 				// ordering for those.
485 				return as[$-1].name.cmp(bs[$-1].name);
486 			}
487 		}
488 	}
489 
490 	private auto sortedSources(SourceFile[] sources) {
491 		return sort(sources);
492 	}
493 
494 	unittest {
495 		SourceFile[] sfs = [
496 			{ NativePath("b/file.d"), NativePath("") },
497 			{ NativePath("b/b/fileA.d"), NativePath("") },
498 			{ NativePath("a/file.d"), NativePath("") },
499 			{ NativePath("b/b/fileB.d"), NativePath("") },
500 			{ NativePath("b/b/b/fileA.d"), NativePath("") },
501 			{ NativePath("b/c/fileA.d"), NativePath("") },
502 		];
503 		auto sorted = sort(sfs);
504 		SourceFile[] sortedSfs;
505 		foreach(sr; sorted)
506 			sortedSfs ~= sr;
507 		assert(sortedSfs[0].structurePath == NativePath("a/file.d"), "1");
508 		assert(sortedSfs[1].structurePath == NativePath("b/b/b/fileA.d"), "2");
509 		assert(sortedSfs[2].structurePath == NativePath("b/b/fileA.d"), "3");
510 		assert(sortedSfs[3].structurePath == NativePath("b/b/fileB.d"), "4");
511 		assert(sortedSfs[4].structurePath == NativePath("b/c/fileA.d"), "5");
512 		assert(sortedSfs[5].structurePath == NativePath("b/file.d"), "6");
513 	}
514 }
515 
516 private NativePath determineStructurePath(NativePath file_path, in ProjectGenerator.TargetInfo target)
517 {
518 	foreach (p; target.packages) {
519 		if (file_path.startsWith(p.path))
520 			return NativePath(getPackageFileName(p.name)) ~ file_path.relativeTo(p.path);
521 	}
522 	return NativePath("misc/") ~ file_path.head;
523 }
524 
525 private string getPackageFileName(string pack)
526 {
527 	return pack.replace(":", "_");
528 }
529 
530 private @property string vsArchitecture(string architecture)
531 {
532 	switch(architecture) {
533 		default: logWarn("Unsupported platform('%s'), defaulting to x86", architecture); goto case;
534 		case "x86", "x86_mscoff": return "Win32";
535 		case "x86_64": return "x64";
536 	}
537 }