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 auto bs = targets[m_project.name].buildSettings; 49 logDebug("About to generate projects for %s, with %s direct dependencies.", m_project.rootPackage.name, m_project.rootPackage.dependencies.length); 50 generateProjectFiles(settings, targets); 51 generateSolutionFile(settings, targets); 52 logInfo("VisualD project generated."); 53 } 54 55 private { 56 void generateSolutionFile(GeneratorSettings settings, in TargetInfo[string] targets) 57 { 58 auto ret = appender!(char[])(); 59 auto configs = m_project.getPackageConfigs(settings.platform, settings.config); 60 auto some_uuid = generateUUID(); 61 62 // Solution header 63 ret.put("Microsoft Visual Studio Solution File, Format Version 11.00\n"); 64 ret.put("# Visual Studio 2010\n"); 65 66 bool[string] visited; 67 void generateSolutionEntry(string pack) { 68 if (pack in visited) return; 69 visited[pack] = true; 70 71 auto ti = targets[pack]; 72 73 auto uuid = guid(pack); 74 ret.formattedWrite("Project(\"%s\") = \"%s\", \"%s\", \"%s\"\n", 75 some_uuid, pack, projFileName(pack), uuid); 76 77 if (ti.linkDependencies.length && ti.buildSettings.targetType != TargetType.staticLibrary) { 78 ret.put("\tProjectSection(ProjectDependencies) = postProject\n"); 79 foreach (d; ti.linkDependencies) 80 if (!isHeaderOnlyPackage(d, targets)) { 81 // TODO: clarify what "uuid = uuid" should mean 82 ret.formattedWrite("\t\t%s = %s\n", guid(d), guid(d)); 83 } 84 ret.put("\tEndProjectSection\n"); 85 } 86 87 ret.put("EndProject\n"); 88 89 foreach (d; ti.dependencies) generateSolutionEntry(d); 90 } 91 92 auto mainpack = m_project.rootPackage.name; 93 94 generateSolutionEntry(mainpack); 95 96 // Global section contains configurations 97 ret.put("Global\n"); 98 ret.put("\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n"); 99 ret.formattedWrite("\t\t%s|%s = %s|%s\n", 100 settings.buildType, 101 settings.platform.architecture[0].vsArchitecture, 102 settings.buildType, 103 settings.platform.architecture[0].vsArchitecture); 104 ret.put("\tEndGlobalSection\n"); 105 ret.put("\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\n"); 106 107 const string[] sub = ["ActiveCfg", "Build.0"]; 108 const string[] conf = [settings.buildType~"|"~settings.platform.architecture[0].vsArchitecture]; 109 auto projectUuid = guid(mainpack); 110 foreach (t; targets.byKey) 111 foreach (c; conf) 112 foreach (s; sub) 113 formattedWrite(ret, "\t\t%s.%s.%s = %s\n", guid(t), c, s, c); 114 115 // TODO: for all dependencies 116 ret.put("\tEndGlobalSection\n"); 117 118 ret.put("\tGlobalSection(SolutionProperties) = preSolution\n"); 119 ret.put("\t\tHideSolutionNode = FALSE\n"); 120 ret.put("\tEndGlobalSection\n"); 121 ret.put("EndGlobal\n"); 122 123 // Writing solution file 124 logDebug("About to write to .sln file with %s bytes", to!string(ret.data.length)); 125 auto sln = openFile(solutionFileName(), FileMode.CreateTrunc); 126 scope(exit) sln.close(); 127 sln.put(ret.data); 128 sln.flush(); 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 int i = 0; 157 auto ret = appender!(char[])(); 158 159 auto project_file_dir = m_project.rootPackage.path ~ projFileName(packname).parentPath; 160 ret.put("<DProject>\n"); 161 ret.formattedWrite(" <ProjectGuid>%s</ProjectGuid>\n", guid(packname)); 162 163 // Several configurations (debug, release, unittest) 164 generateProjectConfiguration(ret, packname, settings.buildType, settings, targets); 165 //generateProjectConfiguration(ret, packname, "release", settings, targets); 166 //generateProjectConfiguration(ret, packname, "unittest", settings, targets); 167 168 // Add all files 169 auto files = targets[packname].buildSettings; 170 SourceFile[string] sourceFiles; 171 void addSourceFile(Path file_path, Path structure_path, bool build) 172 { 173 auto key = file_path.toString(); 174 auto sf = sourceFiles.get(key, SourceFile.init); 175 sf.filePath = file_path; 176 if (!sf.build) { 177 sf.build = build; 178 sf.structurePath = structure_path; 179 } 180 sourceFiles[key] = sf; 181 } 182 183 void addFile(string s, bool build) { 184 auto sp = Path(s); 185 assert(sp.absolute, format("Source path in %s expected to be absolute: %s", packname, s)); 186 //if( !sp.absolute ) sp = pack.path ~ sp; 187 addSourceFile(sp.relativeTo(project_file_dir), determineStructurePath(sp, targets[packname]), build); 188 } 189 190 foreach (p; targets[packname].packages) 191 if (!p.packageInfoFilename.empty) 192 addFile(p.packageInfoFilename.toNativeString(), false); 193 194 if (files.targetType == TargetType.staticLibrary) 195 foreach(s; files.sourceFiles.filter!(s => !isLinkerFile(s))) addFile(s, true); 196 else 197 foreach(s; files.sourceFiles.filter!(s => !s.endsWith(".lib"))) addFile(s, true); 198 199 foreach(s; files.importFiles) addFile(s, false); 200 foreach(s; files.stringImportFiles) addFile(s, false); 201 202 // Create folders and files 203 ret.formattedWrite(" <Folder name=\"%s\">", getPackageFileName(packname)); 204 Path lastFolder; 205 foreach(source; sortedSources(sourceFiles.values)) { 206 logDebug("source looking at %s", source.structurePath); 207 auto cur = source.structurePath[0 .. source.structurePath.length-1]; 208 if(lastFolder != cur) { 209 size_t same = 0; 210 foreach(idx; 0..min(lastFolder.length, cur.length)) 211 if(lastFolder[idx] != cur[idx]) break; 212 else same = idx+1; 213 214 const decrease = lastFolder.length - min(lastFolder.length, same); 215 const increase = cur.length - min(cur.length, same); 216 217 foreach(unused; 0..decrease) 218 ret.put("\n </Folder>"); 219 foreach(idx; 0..increase) 220 ret.formattedWrite("\n <Folder name=\"%s\">", cur[same + idx].toString()); 221 lastFolder = cur; 222 } 223 ret.formattedWrite("\n <File %spath=\"%s\" />", source.build ? "" : "tool=\"None\" ", source.filePath.toNativeString()); 224 } 225 // Finalize all open folders 226 foreach(unused; 0..lastFolder.length) 227 ret.put("\n </Folder>"); 228 ret.put("\n </Folder>\n</DProject>"); 229 230 logDebug("About to write to '%s.visualdproj' file %s bytes", getPackageFileName(packname), ret.data.length); 231 auto proj = openFile(projFileName(packname), FileMode.CreateTrunc); 232 scope(exit) proj.close(); 233 proj.put(ret.data); 234 proj.flush(); 235 } 236 237 void generateProjectConfiguration(Appender!(char[]) ret, string pack, string type, GeneratorSettings settings, in TargetInfo[string] targets) 238 { 239 auto project_file_dir = m_project.rootPackage.path ~ projFileName(pack).parentPath; 240 auto buildsettings = targets[pack].buildSettings.dup; 241 242 string[] getSettings(string setting)(){ return __traits(getMember, buildsettings, setting); } 243 string[] getPathSettings(string setting)() 244 { 245 auto settings = getSettings!setting(); 246 auto ret = new string[settings.length]; 247 foreach (i; 0 .. settings.length) { 248 // \" is interpreted as an escaped " by cmd.exe, so we need to avoid that 249 auto p = Path(settings[i]).relativeTo(project_file_dir); 250 p.endsWithSlash = false; 251 ret[i] = '"' ~ p.toNativeString() ~ '"'; 252 } 253 return ret; 254 } 255 256 foreach(architecture; settings.platform.architecture) { 257 auto arch = architecture.vsArchitecture; 258 ret.formattedWrite(" <Config name=\"%s\" platform=\"%s\">\n", to!string(type), arch); 259 260 // FIXME: handle compiler options in an abstract way instead of searching for DMD specific flags 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 string debugSuffix = type == "debug" ? "_d" : ""; 289 auto bin_path = pack == m_project.rootPackage.name ? Path(buildsettings.targetPath) : Path(".dub/lib/"); 290 bin_path.endsWithSlash = true; 291 ret.formattedWrite(" <lib>%s</lib>\n", output_type); 292 ret.formattedWrite(" <exefile>%s%s%s.%s</exefile>\n", bin_path.toNativeString(), buildsettings.targetName, debugSuffix, output_ext); 293 294 // include paths and string imports 295 string imports = join(getPathSettings!"importPaths"(), " "); 296 string stringImports = join(getPathSettings!"stringImportPaths"(), " "); 297 ret.formattedWrite(" <imppath>%s</imppath>\n", imports); 298 ret.formattedWrite(" <fileImppath>%s</fileImppath>\n", stringImports); 299 300 ret.formattedWrite(" <program>%s</program>\n", "$(DMDInstallDir)windows\\bin\\dmd.exe"); // FIXME: use the actually selected compiler! 301 ret.formattedWrite(" <additionalOptions>%s</additionalOptions>\n", getSettings!"dflags"().join(" ")); 302 303 // Add version identifiers 304 string versions = join(getSettings!"versions"(), " "); 305 ret.formattedWrite(" <versionids>%s</versionids>\n", versions); 306 307 // Add libraries, system libs need to be suffixed by ".lib". 308 string linkLibs = join(map!(a => a~".lib")(getSettings!"libs"()), " "); 309 string addLinkFiles = join(getSettings!"sourceFiles"().filter!(s => s.endsWith(".lib"))(), " "); 310 if (arch == "x86") addLinkFiles ~= " phobos.lib"; 311 if (output_type != StaticLib) ret.formattedWrite(" <libfiles>%s %s</libfiles>\n", linkLibs, addLinkFiles); 312 313 // Unittests 314 ret.formattedWrite(" <useUnitTests>%s</useUnitTests>\n", buildsettings.options & BuildOption.unittests ? "1" : "0"); 315 316 // compute directory for intermediate files (need dummy/ because of how -op determines the resulting path) 317 size_t ndummy = 0; 318 foreach (f; buildsettings.sourceFiles) { 319 auto rpath = Path(f).relativeTo(project_file_dir); 320 size_t nd = 0; 321 foreach (i; 0 .. rpath.length) 322 if (rpath[i] == "..") 323 nd++; 324 if (nd > ndummy) ndummy = nd; 325 } 326 string intersubdir = replicate("dummy/", ndummy) ~ getPackageFileName(pack); 327 328 ret.put(" <obj>0</obj>\n"); 329 ret.put(" <link>0</link>\n"); 330 ret.put(" <subsystem>0</subsystem>\n"); 331 ret.put(" <multiobj>0</multiobj>\n"); 332 int singlefilemode; 333 final switch (settings.buildMode) with (BuildMode) { 334 case separate: singlefilemode = 2; break; 335 case allAtOnce: singlefilemode = 0; break; 336 case singleFile: singlefilemode = 1; break; 337 //case compileOnly: singlefilemode = 3; break; 338 } 339 ret.formattedWrite(" <singleFileCompilation>%s</singleFileCompilation>\n", singlefilemode); 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(" <scheduler>0</scheduler>\n"); 353 ret.put(" <useDeprecated>0</useDeprecated>\n"); 354 ret.put(" <useAssert>0</useAssert>\n"); 355 ret.put(" <useInvariants>0</useInvariants>\n"); 356 ret.put(" <useIn>0</useIn>\n"); 357 ret.put(" <useOut>0</useOut>\n"); 358 ret.put(" <useArrayBounds>0</useArrayBounds>\n"); 359 ret.formattedWrite(" <noboundscheck>%s</noboundscheck>\n", buildsettings.options & BuildOption.noBoundsCheck ? "1" : "0"); 360 ret.put(" <useSwitchError>0</useSwitchError>\n"); 361 ret.put(" <preservePaths>1</preservePaths>\n"); 362 ret.formattedWrite(" <warnings>%s</warnings>\n", buildsettings.options & BuildOption.warningsAsErrors ? "1" : "0"); 363 ret.formattedWrite(" <infowarnings>%s</infowarnings>\n", buildsettings.options & BuildOption.warnings ? "1" : "0"); 364 ret.formattedWrite(" <checkProperty>%s</checkProperty>\n", buildsettings.options & BuildOption.property ? "1" : "0"); 365 ret.formattedWrite(" <genStackFrame>%s</genStackFrame>\n", buildsettings.options & BuildOption.alwaysStackFrame ? "1" : "0"); 366 ret.put(" <pic>0</pic>\n"); 367 ret.formattedWrite(" <cov>%s</cov>\n", buildsettings.options & BuildOption.coverage ? "1" : "0"); 368 ret.put(" <nofloat>0</nofloat>\n"); 369 ret.put(" <Dversion>2</Dversion>\n"); 370 ret.formattedWrite(" <ignoreUnsupportedPragmas>%s</ignoreUnsupportedPragmas>\n", buildsettings.options & BuildOption.ignoreUnknownPragmas ? "1" : "0"); 371 ret.formattedWrite(" <compiler>%s</compiler>\n", settings.compiler.name == "ldc" ? 2 : settings.compiler.name == "gdc" ? 1 : 0); 372 ret.formattedWrite(" <otherDMD>0</otherDMD>\n"); 373 ret.formattedWrite(" <outdir>%s</outdir>\n", bin_path.toNativeString()); 374 ret.formattedWrite(" <objdir>.dub/obj/%s/%s</objdir>\n", to!string(type), intersubdir); 375 ret.put(" <objname />\n"); 376 ret.put(" <libname />\n"); 377 ret.put(" <doDocComments>0</doDocComments>\n"); 378 ret.put(" <docdir />\n"); 379 ret.put(" <docname />\n"); 380 ret.put(" <modules_ddoc />\n"); 381 ret.put(" <ddocfiles />\n"); 382 ret.put(" <doHdrGeneration>0</doHdrGeneration>\n"); 383 ret.put(" <hdrdir />\n"); 384 ret.put(" <hdrname />\n"); 385 ret.put(" <doXGeneration>1</doXGeneration>\n"); 386 ret.put(" <xfilename>$(IntDir)\\$(TargetName).json</xfilename>\n"); 387 ret.put(" <debuglevel>0</debuglevel>\n"); 388 ret.put(" <versionlevel>0</versionlevel>\n"); 389 ret.put(" <debugids />\n"); 390 ret.put(" <dump_source>0</dump_source>\n"); 391 ret.put(" <mapverbosity>0</mapverbosity>\n"); 392 ret.put(" <createImplib>0</createImplib>\n"); 393 ret.put(" <defaultlibname />\n"); 394 ret.put(" <debuglibname />\n"); 395 ret.put(" <moduleDepsFile />\n"); 396 ret.put(" <run>0</run>\n"); 397 ret.put(" <runargs />\n"); 398 ret.put(" <runCv2pdb>1</runCv2pdb>\n"); 399 ret.put(" <pathCv2pdb>$(VisualDInstallDir)cv2pdb\\cv2pdb.exe</pathCv2pdb>\n"); 400 ret.put(" <cv2pdbPre2043>0</cv2pdbPre2043>\n"); 401 ret.put(" <cv2pdbNoDemangle>0</cv2pdbNoDemangle>\n"); 402 ret.put(" <cv2pdbEnumType>0</cv2pdbEnumType>\n"); 403 ret.put(" <cv2pdbOptions />\n"); 404 ret.put(" <objfiles />\n"); 405 ret.put(" <linkswitches />\n"); 406 ret.put(" <libpaths />\n"); 407 ret.put(" <deffile />\n"); 408 ret.put(" <resfile />\n"); 409 ret.put(" <preBuildCommand />\n"); 410 ret.put(" <postBuildCommand />\n"); 411 ret.put(" <filesToClean>*.obj;*.cmd;*.build;*.dep</filesToClean>\n"); 412 ret.put(" </Config>\n"); 413 } // foreach(architecture) 414 } 415 416 void performOnDependencies(const Package main, string[string] configs, void delegate(const Package pack) op) 417 { 418 foreach (p; m_project.getTopologicalPackageList(false, main, configs)) { 419 if (p is main) continue; 420 op(p); 421 } 422 } 423 424 string generateUUID() const { 425 import std..string; 426 return "{" ~ toUpper(randomUUID().toString()) ~ "}"; 427 } 428 429 string guid(string projectName) { 430 if(projectName !in m_projectUuids) 431 m_projectUuids[projectName] = generateUUID(); 432 return m_projectUuids[projectName]; 433 } 434 435 auto solutionFileName() const { 436 version(DUBBING) return getPackageFileName(m_project.rootPackage) ~ ".dubbed.sln"; 437 else return getPackageFileName(m_project.rootPackage.name) ~ ".sln"; 438 } 439 440 Path projFileName(string pack) const { 441 auto basepath = Path(".");//Path(".dub/"); 442 version(DUBBING) return basepath ~ (getPackageFileName(pack) ~ ".dubbed.visualdproj"); 443 else return basepath ~ (getPackageFileName(pack) ~ ".visualdproj"); 444 } 445 } 446 447 // TODO: nice folders 448 struct SourceFile { 449 Path structurePath; 450 Path filePath; 451 bool build; 452 453 hash_t toHash() const nothrow @trusted { return structurePath.toHash() ^ filePath.toHash() ^ (build * 0x1f3e7b2c); } 454 int opCmp(ref const SourceFile rhs) const { return sortOrder(this, rhs); } 455 // "a < b" for folder structures (deepest folder first, else lexical) 456 private final static int sortOrder(ref const SourceFile a, ref const SourceFile b) { 457 assert(!a.structurePath.empty); 458 assert(!b.structurePath.empty); 459 auto as = a.structurePath; 460 auto bs = b.structurePath; 461 462 // Check for different folders, compare folders only (omit last one). 463 for(uint idx=0; idx<min(as.length-1, bs.length-1); ++idx) 464 if(as[idx] != bs[idx]) 465 return as[idx].opCmp(bs[idx]); 466 467 if(as.length != bs.length) { 468 // If length differ, the longer one is "smaller", that is more 469 // specialized and will be put out first. 470 return as.length > bs.length? -1 : 1; 471 } 472 else { 473 // Both paths indicate files in the same directory, use lexical 474 // ordering for those. 475 return as.head.opCmp(bs.head); 476 } 477 } 478 } 479 480 auto sortedSources(SourceFile[] sources) { 481 return sort(sources); 482 } 483 484 unittest { 485 SourceFile[] sfs = [ 486 { Path("b/file.d"), Path("") }, 487 { Path("b/b/fileA.d"), Path("") }, 488 { Path("a/file.d"), Path("") }, 489 { Path("b/b/fileB.d"), Path("") }, 490 { Path("b/b/b/fileA.d"), Path("") }, 491 { Path("b/c/fileA.d"), Path("") }, 492 ]; 493 auto sorted = sort(sfs); 494 SourceFile[] sortedSfs; 495 foreach(sr; sorted) 496 sortedSfs ~= sr; 497 assert(sortedSfs[0].structurePath == Path("a/file.d"), "1"); 498 assert(sortedSfs[1].structurePath == Path("b/b/b/fileA.d"), "2"); 499 assert(sortedSfs[2].structurePath == Path("b/b/fileA.d"), "3"); 500 assert(sortedSfs[3].structurePath == Path("b/b/fileB.d"), "4"); 501 assert(sortedSfs[4].structurePath == Path("b/c/fileA.d"), "5"); 502 assert(sortedSfs[5].structurePath == Path("b/file.d"), "6"); 503 } 504 } 505 506 private Path determineStructurePath(Path file_path, in ProjectGenerator.TargetInfo target) 507 { 508 foreach (p; target.packages) { 509 if (file_path.startsWith(p.path)) 510 return Path(getPackageFileName(p.name)) ~ file_path[p.path.length .. $]; 511 } 512 return Path("misc/") ~ file_path.head; 513 } 514 515 private string getPackageFileName(string pack) 516 { 517 return pack.replace(":", "_"); 518 } 519 520 private @property string vsArchitecture(string architecture) 521 { 522 switch(architecture) { 523 default: logWarn("Unsupported platform('%s'), defaulting to x86", architecture); goto case; 524 case "x86": return "Win32"; 525 case "x86_64": return "x64"; 526 } 527 }