The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1540 lines
67KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_PROJECTEXPORT_MSVC_JUCEHEADER__
  19. #define __JUCER_PROJECTEXPORT_MSVC_JUCEHEADER__
  20. #include "jucer_ProjectExporter.h"
  21. #include "jucer_ProjectSaver.h"
  22. //==============================================================================
  23. class MSVCProjectExporterBase : public ProjectExporter
  24. {
  25. public:
  26. MSVCProjectExporterBase (Project& p, const ValueTree& t, const char* const folderName)
  27. : ProjectExporter (p, t)
  28. {
  29. if (getTargetLocationString().isEmpty())
  30. getTargetLocationValue() = getDefaultBuildsRootFolder() + folderName;
  31. if ((int) getLibraryType().getValue() <= 0)
  32. getLibraryType() = 1;
  33. projectGUID = createGUID (project.getProjectUID());
  34. updateOldSettings();
  35. }
  36. //==============================================================================
  37. bool usesMMFiles() const { return false; }
  38. bool isVisualStudio() const { return true; }
  39. bool canCopeWithDuplicateFiles() { return false; }
  40. bool launchProject()
  41. {
  42. #if JUCE_WINDOWS
  43. return getSLNFile().startAsProcess();
  44. #else
  45. return false;
  46. #endif
  47. }
  48. void createExporterProperties (PropertyListBuilder& props)
  49. {
  50. if (projectType.isLibrary())
  51. {
  52. const char* const libTypes[] = { "Static Library (.lib)", "Dynamic Library (.dll)", nullptr };
  53. const int libTypeValues[] = { 1, 2 };
  54. props.add (new ChoicePropertyComponent (getLibraryType(), "Library Type",
  55. StringArray (libTypes),
  56. Array<var> (libTypeValues, numElementsInArray (libTypeValues))));
  57. }
  58. }
  59. protected:
  60. String projectGUID;
  61. mutable File rcFile, iconFile;
  62. File getProjectFile (const String& extension) const { return getTargetFolder().getChildFile (project.getProjectFilenameRoot()).withFileExtension (extension); }
  63. File getSLNFile() const { return getProjectFile (".sln"); }
  64. Value getLibraryType() { return getSetting (Ids::libraryType); }
  65. String getLibraryString() const { return getSettingString (Ids::libraryType); }
  66. bool isLibraryDLL() const { return msvcIsDLL || (projectType.isLibrary() && (int) settings [Ids::libraryType] == 2); }
  67. static String prependIfNotAbsolute (const String& file, const char* prefix)
  68. {
  69. if (File::isAbsolutePath (file) || file.startsWithChar ('$'))
  70. prefix = "";
  71. return prefix + FileHelpers::windowsStylePath (file);
  72. }
  73. static String getIntDirFile (const String& file) { return prependIfNotAbsolute (file, "$(IntDir)\\"); }
  74. static String getOutDirFile (const String& file) { return prependIfNotAbsolute (file, "$(OutDir)\\"); }
  75. void updateOldSettings()
  76. {
  77. {
  78. const String oldStylePrebuildCommand (getSettingString (Ids::prebuildCommand));
  79. settings.removeProperty (Ids::prebuildCommand, nullptr);
  80. if (oldStylePrebuildCommand.isNotEmpty())
  81. for (ConfigIterator config (*this); config.next();)
  82. dynamic_cast <MSVCBuildConfiguration&> (*config).getPrebuildCommand() = oldStylePrebuildCommand;
  83. }
  84. {
  85. const String oldStyleLibName (getSettingString ("libraryName_Debug"));
  86. settings.removeProperty ("libraryName_Debug", nullptr);
  87. if (oldStyleLibName.isNotEmpty())
  88. for (ConfigIterator config (*this); config.next();)
  89. if (config->isDebug())
  90. config->getTargetBinaryName() = oldStyleLibName;
  91. }
  92. {
  93. const String oldStyleLibName (getSettingString ("libraryName_Release"));
  94. settings.removeProperty ("libraryName_Release", nullptr);
  95. if (oldStyleLibName.isNotEmpty())
  96. for (ConfigIterator config (*this); config.next();)
  97. if (! config->isDebug())
  98. config->getTargetBinaryName() = oldStyleLibName;
  99. }
  100. }
  101. //==============================================================================
  102. class MSVCBuildConfiguration : public BuildConfiguration
  103. {
  104. public:
  105. MSVCBuildConfiguration (Project& project, const ValueTree& settings)
  106. : BuildConfiguration (project, settings)
  107. {
  108. if (getWarningLevel() == 0)
  109. getWarningLevelValue() = 4;
  110. setValueIfVoid (shouldGenerateManifestValue(), true);
  111. }
  112. Value getWarningLevelValue() { return getValue (Ids::winWarningLevel); }
  113. int getWarningLevel() const { return config [Ids::winWarningLevel]; }
  114. Value getPrebuildCommand() { return getValue (Ids::prebuildCommand); }
  115. String getPrebuildCommandString() const { return config [Ids::prebuildCommand]; }
  116. Value getPostbuildCommand() { return getValue (Ids::postbuildCommand); }
  117. String getPostbuildCommandString() const { return config [Ids::postbuildCommand]; }
  118. Value shouldGenerateDebugSymbolsValue() { return getValue (Ids::alwaysGenerateDebugSymbols); }
  119. bool shouldGenerateDebugSymbols() const { return config [Ids::alwaysGenerateDebugSymbols]; }
  120. Value shouldGenerateManifestValue() { return getValue (Ids::generateManifest); }
  121. bool shouldGenerateManifest() const { return config [Ids::generateManifest]; }
  122. Value getWholeProgramOptValue() { return getValue (Ids::wholeProgramOptimisation); }
  123. bool shouldDisableWholeProgramOpt() const { return static_cast<int> (config [Ids::wholeProgramOptimisation]) > 0; }
  124. Value getUsingRuntimeLibDLL() { return getValue (Ids::useRuntimeLibDLL); }
  125. bool isUsingRuntimeLibDLL() const { return config [Ids::useRuntimeLibDLL]; }
  126. String getIntermediatesPath() const { return config [Ids::intermediatesPath].toString(); }
  127. Value getIntermediatesPathValue() { return getValue (Ids::intermediatesPath); }
  128. String getOutputFilename (const String& suffix, bool forceSuffix) const
  129. {
  130. const String target (File::createLegalFileName (getTargetBinaryNameString().trim()));
  131. if (forceSuffix || ! target.containsChar ('.'))
  132. return target.upToLastOccurrenceOf (".", false, false) + suffix;
  133. return target;
  134. }
  135. void createConfigProperties (PropertyListBuilder& props)
  136. {
  137. props.add (new TextPropertyComponent (getIntermediatesPathValue(), "Intermediates path", 2048, false),
  138. "An optional path to a folder to use for the intermediate build files. Note that Visual Studio allows "
  139. "you to use macros in this path, e.g. \"$(TEMP)\\MyAppBuildFiles\\$(Configuration)\", which is a handy way to "
  140. "send them to the user's temp folder.");
  141. const char* const warningLevelNames[] = { "Low", "Medium", "High", nullptr };
  142. const int warningLevels[] = { 2, 3, 4 };
  143. props.add (new ChoicePropertyComponent (getWarningLevelValue(), "Warning Level",
  144. StringArray (warningLevelNames), Array<var> (warningLevels, numElementsInArray (warningLevels))));
  145. {
  146. const char* const runtimeNames[] = { "(Default)", "Use static runtime", "Use DLL runtime", nullptr };
  147. const var runtimeValues[] = { var(), var (false), var (true) };
  148. props.add (new ChoicePropertyComponent (getUsingRuntimeLibDLL(), "Runtime Library",
  149. StringArray (runtimeNames), Array<var> (runtimeValues, numElementsInArray (runtimeValues))));
  150. }
  151. {
  152. const char* const wpoNames[] = { "Enable link-time code generation when possible",
  153. "Always disable link-time code generation", nullptr };
  154. const var wpoValues[] = { var(), var (1) };
  155. props.add (new ChoicePropertyComponent (getWholeProgramOptValue(), "Whole Program Optimisation",
  156. StringArray (wpoNames), Array<var> (wpoValues, numElementsInArray (wpoValues))));
  157. }
  158. if (! isDebug())
  159. props.add (new BooleanPropertyComponent (shouldGenerateDebugSymbolsValue(), "Debug Symbols", "Force generation of debug symbols"));
  160. props.add (new TextPropertyComponent (getPrebuildCommand(), "Pre-build Command", 2048, false));
  161. props.add (new TextPropertyComponent (getPostbuildCommand(), "Post-build Command", 2048, false));
  162. props.add (new BooleanPropertyComponent (shouldGenerateManifestValue(), "Manifest", "Generate Manifest"));
  163. }
  164. };
  165. BuildConfiguration::Ptr createBuildConfig (const ValueTree& settings) const
  166. {
  167. return new MSVCBuildConfiguration (project, settings);
  168. }
  169. static int getWarningLevel (const BuildConfiguration& config)
  170. {
  171. return dynamic_cast <const MSVCBuildConfiguration&> (config).getWarningLevel();
  172. }
  173. //==============================================================================
  174. String getConfigTargetPath (const BuildConfiguration& config) const
  175. {
  176. const String binaryPath (config.getTargetBinaryRelativePathString().trim());
  177. if (binaryPath.isEmpty())
  178. return prependDot (File::createLegalFileName (config.getName().trim()));
  179. RelativePath binaryRelPath (binaryPath, RelativePath::projectFolder);
  180. if (binaryRelPath.isAbsolute())
  181. return binaryRelPath.toWindowsStyle();
  182. return prependDot (binaryRelPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder)
  183. .toWindowsStyle());
  184. }
  185. String getPreprocessorDefs (const BuildConfiguration& config, const String& joinString) const
  186. {
  187. StringPairArray defines (msvcExtraPreprocessorDefs);
  188. defines.set ("WIN32", "");
  189. defines.set ("_WINDOWS", "");
  190. if (config.isDebug())
  191. {
  192. defines.set ("DEBUG", "");
  193. defines.set ("_DEBUG", "");
  194. }
  195. else
  196. {
  197. defines.set ("NDEBUG", "");
  198. }
  199. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  200. StringArray result;
  201. for (int i = 0; i < defines.size(); ++i)
  202. {
  203. String def (defines.getAllKeys()[i]);
  204. const String value (defines.getAllValues()[i]);
  205. if (value.isNotEmpty())
  206. def << "=" << value;
  207. result.add (def);
  208. }
  209. return result.joinIntoString (joinString);
  210. }
  211. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  212. {
  213. StringArray searchPaths (extraSearchPaths);
  214. searchPaths.addArray (config.getHeaderSearchPaths());
  215. searchPaths.removeDuplicates (false);
  216. return searchPaths;
  217. }
  218. virtual String createConfigName (const BuildConfiguration& config) const
  219. {
  220. return config.getName() + "|Win32";
  221. }
  222. //==============================================================================
  223. void writeSolutionFile (OutputStream& out, const String& versionString, String commentString, const File& vcProject) const
  224. {
  225. if (commentString.isNotEmpty())
  226. commentString += newLine;
  227. out << "Microsoft Visual Studio Solution File, Format Version " << versionString << newLine
  228. << commentString
  229. << "Project(\"" << createGUID (projectName + "sln_guid") << "\") = \"" << projectName << "\", \""
  230. << vcProject.getFileName() << "\", \"" << projectGUID << '"' << newLine
  231. << "EndProject" << newLine
  232. << "Global" << newLine
  233. << "\tGlobalSection(SolutionConfigurationPlatforms) = preSolution" << newLine;
  234. for (ConstConfigIterator i (*this); i.next();)
  235. {
  236. const String configName (createConfigName (*i));
  237. out << "\t\t" << configName << " = " << configName << newLine;
  238. }
  239. out << "\tEndGlobalSection" << newLine
  240. << "\tGlobalSection(ProjectConfigurationPlatforms) = postSolution" << newLine;
  241. for (ConstConfigIterator i (*this); i.next();)
  242. {
  243. const String configName (createConfigName (*i));
  244. out << "\t\t" << projectGUID << "." << configName << ".ActiveCfg = " << configName << newLine;
  245. out << "\t\t" << projectGUID << "." << configName << ".Build.0 = " << configName << newLine;
  246. }
  247. out << "\tEndGlobalSection" << newLine
  248. << "\tGlobalSection(SolutionProperties) = preSolution" << newLine
  249. << "\t\tHideSolutionNode = FALSE" << newLine
  250. << "\tEndGlobalSection" << newLine
  251. << "EndGlobal" << newLine;
  252. }
  253. //==============================================================================
  254. static void writeBMPImage (const Image& image, const int w, const int h, MemoryOutputStream& out)
  255. {
  256. const int maskStride = (w / 8 + 3) & ~3;
  257. out.writeInt (40); // bitmapinfoheader size
  258. out.writeInt (w);
  259. out.writeInt (h * 2);
  260. out.writeShort (1); // planes
  261. out.writeShort (32); // bits
  262. out.writeInt (0); // compression
  263. out.writeInt ((h * w * 4) + (h * maskStride)); // size image
  264. out.writeInt (0); // x pixels per meter
  265. out.writeInt (0); // y pixels per meter
  266. out.writeInt (0); // clr used
  267. out.writeInt (0); // clr important
  268. const Image::BitmapData bitmap (image, Image::BitmapData::readOnly);
  269. const int alphaThreshold = 5;
  270. int y;
  271. for (y = h; --y >= 0;)
  272. {
  273. for (int x = 0; x < w; ++x)
  274. {
  275. const Colour pixel (bitmap.getPixelColour (x, y));
  276. if (pixel.getAlpha() <= alphaThreshold)
  277. {
  278. out.writeInt (0);
  279. }
  280. else
  281. {
  282. out.writeByte ((char) pixel.getBlue());
  283. out.writeByte ((char) pixel.getGreen());
  284. out.writeByte ((char) pixel.getRed());
  285. out.writeByte ((char) pixel.getAlpha());
  286. }
  287. }
  288. }
  289. for (y = h; --y >= 0;)
  290. {
  291. int mask = 0, count = 0;
  292. for (int x = 0; x < w; ++x)
  293. {
  294. const Colour pixel (bitmap.getPixelColour (x, y));
  295. mask <<= 1;
  296. if (pixel.getAlpha() <= alphaThreshold)
  297. mask |= 1;
  298. if (++count == 8)
  299. {
  300. out.writeByte ((char) mask);
  301. count = 0;
  302. mask = 0;
  303. }
  304. }
  305. if (mask != 0)
  306. out.writeByte ((char) mask);
  307. for (int i = maskStride - w / 8; --i >= 0;)
  308. out.writeByte (0);
  309. }
  310. }
  311. static void writeIconFile (const Array<Image>& images, MemoryOutputStream& out)
  312. {
  313. out.writeShort (0); // reserved
  314. out.writeShort (1); // .ico tag
  315. out.writeShort ((short) images.size());
  316. MemoryOutputStream dataBlock;
  317. const int imageDirEntrySize = 16;
  318. const int dataBlockStart = 6 + images.size() * imageDirEntrySize;
  319. for (int i = 0; i < images.size(); ++i)
  320. {
  321. const size_t oldDataSize = dataBlock.getDataSize();
  322. const Image& image = images.getReference (i);
  323. const int w = image.getWidth();
  324. const int h = image.getHeight();
  325. if (w >= 256 || h >= 256)
  326. {
  327. PNGImageFormat pngFormat;
  328. pngFormat.writeImageToStream (image, dataBlock);
  329. }
  330. else
  331. {
  332. writeBMPImage (image, w, h, dataBlock);
  333. }
  334. out.writeByte ((char) w);
  335. out.writeByte ((char) h);
  336. out.writeByte (0);
  337. out.writeByte (0);
  338. out.writeShort (1); // colour planes
  339. out.writeShort (32); // bits per pixel
  340. out.writeInt ((int) (dataBlock.getDataSize() - oldDataSize));
  341. out.writeInt (dataBlockStart + oldDataSize);
  342. }
  343. jassert (out.getPosition() == dataBlockStart);
  344. out << dataBlock;
  345. }
  346. bool hasResourceFile() const
  347. {
  348. return ! projectType.isLibrary();
  349. }
  350. void createResourcesAndIcon() const
  351. {
  352. if (hasResourceFile())
  353. {
  354. Array<Image> images;
  355. const int sizes[] = { 16, 32, 48, 256 };
  356. for (int i = 0; i < numElementsInArray (sizes); ++i)
  357. {
  358. Image im (getBestIconForSize (sizes[i], true));
  359. if (im.isValid())
  360. images.add (im);
  361. }
  362. if (images.size() > 0)
  363. {
  364. iconFile = getTargetFolder().getChildFile ("icon.ico");
  365. MemoryOutputStream mo;
  366. writeIconFile (images, mo);
  367. overwriteFileIfDifferentOrThrow (iconFile, mo);
  368. }
  369. createRCFile();
  370. }
  371. }
  372. void createRCFile() const
  373. {
  374. rcFile = getTargetFolder().getChildFile ("resources.rc");
  375. const String version (project.getVersionString());
  376. MemoryOutputStream mo;
  377. mo << "#ifdef JUCE_USER_DEFINED_RC_FILE" << newLine
  378. << " #include JUCE_USER_DEFINED_RC_FILE" << newLine
  379. << "#else" << newLine
  380. << newLine
  381. << "#undef WIN32_LEAN_AND_MEAN" << newLine
  382. << "#define WIN32_LEAN_AND_MEAN" << newLine
  383. << "#include <windows.h>" << newLine
  384. << newLine
  385. << "VS_VERSION_INFO VERSIONINFO" << newLine
  386. << "FILEVERSION " << getCommaSeparatedVersionNumber (version) << newLine
  387. << "BEGIN" << newLine
  388. << " BLOCK \"StringFileInfo\"" << newLine
  389. << " BEGIN" << newLine
  390. << " BLOCK \"040904E4\"" << newLine
  391. << " BEGIN" << newLine;
  392. writeRCValue (mo, "CompanyName", project.getCompanyName().toString());
  393. writeRCValue (mo, "FileDescription", project.getTitle());
  394. writeRCValue (mo, "FileVersion", version);
  395. writeRCValue (mo, "ProductName", project.getTitle());
  396. writeRCValue (mo, "ProductVersion", version);
  397. mo << " END" << newLine
  398. << " END" << newLine
  399. << newLine
  400. << " BLOCK \"VarFileInfo\"" << newLine
  401. << " BEGIN" << newLine
  402. << " VALUE \"Translation\", 0x409, 65001" << newLine
  403. << " END" << newLine
  404. << "END" << newLine
  405. << newLine
  406. << "#endif" << newLine;
  407. if (iconFile != File::nonexistent)
  408. mo << newLine
  409. << "IDI_ICON1 ICON DISCARDABLE " << iconFile.getFileName().quoted()
  410. << newLine
  411. << "IDI_ICON2 ICON DISCARDABLE " << iconFile.getFileName().quoted();
  412. overwriteFileIfDifferentOrThrow (rcFile, mo);
  413. }
  414. static void writeRCValue (MemoryOutputStream& mo, const String& name, const String& value)
  415. {
  416. if (value.isNotEmpty())
  417. mo << " VALUE \"" << name << "\", \""
  418. << CodeHelpers::addEscapeChars (value) << "\\0\"" << newLine;
  419. }
  420. static String getCommaSeparatedVersionNumber (const String& version)
  421. {
  422. StringArray versionParts;
  423. versionParts.addTokens (version, ",.", "");
  424. versionParts.trim();
  425. versionParts.removeEmptyStrings();
  426. while (versionParts.size() < 4)
  427. versionParts.add ("0");
  428. return versionParts.joinIntoString (",");
  429. }
  430. static String prependDot (const String& filename)
  431. {
  432. return FileHelpers::isAbsolutePath (filename) ? filename
  433. : (".\\" + filename);
  434. }
  435. JUCE_DECLARE_NON_COPYABLE (MSVCProjectExporterBase)
  436. };
  437. //==============================================================================
  438. class MSVCProjectExporterVC2008 : public MSVCProjectExporterBase
  439. {
  440. public:
  441. //==============================================================================
  442. MSVCProjectExporterVC2008 (Project& project_, const ValueTree& settings_,
  443. const char* folderName = "VisualStudio2008")
  444. : MSVCProjectExporterBase (project_, settings_, folderName)
  445. {
  446. name = getName();
  447. }
  448. static const char* getName() { return "Visual Studio 2008"; }
  449. static const char* getValueTreeTypeName() { return "VS2008"; }
  450. int getVisualStudioVersion() const { return 9; }
  451. static MSVCProjectExporterVC2008* createForSettings (Project& project, const ValueTree& settings)
  452. {
  453. if (settings.hasType (getValueTreeTypeName()))
  454. return new MSVCProjectExporterVC2008 (project, settings);
  455. return nullptr;
  456. }
  457. //==============================================================================
  458. void create (const OwnedArray<LibraryModule>&) const
  459. {
  460. createResourcesAndIcon();
  461. if (hasResourceFile())
  462. {
  463. for (int i = 0; i < getAllGroups().size(); ++i)
  464. {
  465. Project::Item& group = getAllGroups().getReference(i);
  466. if (group.getID() == ProjectSaver::getGeneratedGroupID())
  467. {
  468. if (iconFile != File::nonexistent)
  469. {
  470. group.addFile (iconFile, -1, true);
  471. group.findItemForFile (iconFile).getShouldAddToResourceValue() = false;
  472. }
  473. group.addFile (rcFile, -1, true);
  474. group.findItemForFile (rcFile).getShouldAddToResourceValue() = false;
  475. break;
  476. }
  477. }
  478. }
  479. {
  480. XmlElement projectXml ("VisualStudioProject");
  481. fillInProjectXml (projectXml);
  482. writeXmlOrThrow (projectXml, getVCProjFile(), "UTF-8", 10);
  483. }
  484. {
  485. MemoryOutputStream mo;
  486. writeSolutionFile (mo, getSolutionVersionString(), String::empty, getVCProjFile());
  487. overwriteFileIfDifferentOrThrow (getSLNFile(), mo);
  488. }
  489. }
  490. protected:
  491. virtual String getProjectVersionString() const { return "9.00"; }
  492. virtual String getSolutionVersionString() const { return "10.00" + newLine + "# Visual C++ Express 2008"; }
  493. File getVCProjFile() const { return getProjectFile (".vcproj"); }
  494. //==============================================================================
  495. void fillInProjectXml (XmlElement& projectXml) const
  496. {
  497. projectXml.setAttribute ("ProjectType", "Visual C++");
  498. projectXml.setAttribute ("Version", getProjectVersionString());
  499. projectXml.setAttribute ("Name", projectName);
  500. projectXml.setAttribute ("ProjectGUID", projectGUID);
  501. projectXml.setAttribute ("TargetFrameworkVersion", "131072");
  502. {
  503. XmlElement* platforms = projectXml.createNewChildElement ("Platforms");
  504. XmlElement* platform = platforms->createNewChildElement ("Platform");
  505. platform->setAttribute ("Name", "Win32");
  506. }
  507. projectXml.createNewChildElement ("ToolFiles");
  508. createConfigs (*projectXml.createNewChildElement ("Configurations"));
  509. projectXml.createNewChildElement ("References");
  510. createFiles (*projectXml.createNewChildElement ("Files"));
  511. projectXml.createNewChildElement ("Globals");
  512. }
  513. //==============================================================================
  514. void addFile (const RelativePath& file, XmlElement& parent, const bool excludeFromBuild, const bool useStdcall) const
  515. {
  516. jassert (file.getRoot() == RelativePath::buildTargetFolder);
  517. XmlElement* fileXml = parent.createNewChildElement ("File");
  518. fileXml->setAttribute ("RelativePath", file.toWindowsStyle());
  519. if (excludeFromBuild || useStdcall)
  520. {
  521. for (ConstConfigIterator i (*this); i.next();)
  522. {
  523. XmlElement* fileConfig = fileXml->createNewChildElement ("FileConfiguration");
  524. fileConfig->setAttribute ("Name", createConfigName (*i));
  525. if (excludeFromBuild)
  526. fileConfig->setAttribute ("ExcludedFromBuild", "true");
  527. XmlElement* tool = createToolElement (*fileConfig, "VCCLCompilerTool");
  528. if (useStdcall)
  529. tool->setAttribute ("CallingConvention", "2");
  530. }
  531. }
  532. }
  533. XmlElement* createGroup (const String& groupName, XmlElement& parent) const
  534. {
  535. XmlElement* filter = parent.createNewChildElement ("Filter");
  536. filter->setAttribute ("Name", groupName);
  537. return filter;
  538. }
  539. void addFiles (const Project::Item& projectItem, XmlElement& parent) const
  540. {
  541. if (projectItem.isGroup())
  542. {
  543. XmlElement* filter = createGroup (projectItem.getName(), parent);
  544. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  545. addFiles (projectItem.getChild(i), *filter);
  546. }
  547. else if (projectItem.shouldBeAddedToTargetProject())
  548. {
  549. const RelativePath path (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  550. addFile (path, parent,
  551. projectItem.shouldBeAddedToBinaryResources() || (shouldFileBeCompiledByDefault (path) && ! projectItem.shouldBeCompiled()),
  552. shouldFileBeCompiledByDefault (path) && (bool) projectItem.shouldUseStdCall());
  553. }
  554. }
  555. void createFiles (XmlElement& files) const
  556. {
  557. for (int i = 0; i < getAllGroups().size(); ++i)
  558. {
  559. const Project::Item& group = getAllGroups().getReference(i);
  560. if (group.getNumChildren() > 0)
  561. addFiles (group, files);
  562. }
  563. }
  564. //==============================================================================
  565. XmlElement* createToolElement (XmlElement& parent, const String& toolName) const
  566. {
  567. XmlElement* const e = parent.createNewChildElement ("Tool");
  568. e->setAttribute ("Name", toolName);
  569. return e;
  570. }
  571. void createConfig (XmlElement& xml, const MSVCBuildConfiguration& config) const
  572. {
  573. const bool isDebug = config.isDebug();
  574. xml.setAttribute ("Name", createConfigName (config));
  575. xml.setAttribute ("OutputDirectory", FileHelpers::windowsStylePath (getConfigTargetPath (config)));
  576. if (config.getIntermediatesPath().isNotEmpty())
  577. xml.setAttribute ("IntermediateDirectory", FileHelpers::windowsStylePath (config.getIntermediatesPath()));
  578. xml.setAttribute ("ConfigurationType", isLibraryDLL() ? "2" : (projectType.isLibrary() ? "4" : "1"));
  579. xml.setAttribute ("UseOfMFC", "0");
  580. xml.setAttribute ("ATLMinimizesCRunTimeLibraryUsage", "false");
  581. xml.setAttribute ("CharacterSet", "2");
  582. if (! (isDebug || config.shouldDisableWholeProgramOpt()))
  583. xml.setAttribute ("WholeProgramOptimization", "1");
  584. XmlElement* preBuildEvent = createToolElement (xml, "VCPreBuildEventTool");
  585. if (config.getPrebuildCommandString().isNotEmpty())
  586. {
  587. preBuildEvent->setAttribute ("Description", "Pre-build");
  588. preBuildEvent->setAttribute ("CommandLine", config.getPrebuildCommandString());
  589. }
  590. createToolElement (xml, "VCCustomBuildTool");
  591. createToolElement (xml, "VCXMLDataGeneratorTool");
  592. createToolElement (xml, "VCWebServiceProxyGeneratorTool");
  593. if (! projectType.isLibrary())
  594. {
  595. XmlElement* midl = createToolElement (xml, "VCMIDLTool");
  596. midl->setAttribute ("PreprocessorDefinitions", isDebug ? "_DEBUG" : "NDEBUG");
  597. midl->setAttribute ("MkTypLibCompatible", "true");
  598. midl->setAttribute ("SuppressStartupBanner", "true");
  599. midl->setAttribute ("TargetEnvironment", "1");
  600. midl->setAttribute ("TypeLibraryName", getIntDirFile (config.getOutputFilename (".tlb", true)));
  601. midl->setAttribute ("HeaderFileName", "");
  602. }
  603. {
  604. XmlElement* compiler = createToolElement (xml, "VCCLCompilerTool");
  605. compiler->setAttribute ("Optimization", getOptimisationLevelString (config.getOptimisationLevelInt()));
  606. if (isDebug)
  607. {
  608. compiler->setAttribute ("BufferSecurityCheck", "");
  609. compiler->setAttribute ("DebugInformationFormat", projectType.isLibrary() ? "3" : "4");
  610. }
  611. else
  612. {
  613. compiler->setAttribute ("InlineFunctionExpansion", "1");
  614. compiler->setAttribute ("StringPooling", "true");
  615. }
  616. compiler->setAttribute ("AdditionalIncludeDirectories", replacePreprocessorTokens (config, getHeaderSearchPaths (config).joinIntoString (";")));
  617. compiler->setAttribute ("PreprocessorDefinitions", getPreprocessorDefs (config, ";"));
  618. compiler->setAttribute ("RuntimeLibrary", config.isUsingRuntimeLibDLL() ? (isDebug ? 3 : 2) // MT DLL
  619. : (isDebug ? 1 : 0)); // MT static
  620. compiler->setAttribute ("RuntimeTypeInfo", "true");
  621. compiler->setAttribute ("UsePrecompiledHeader", "0");
  622. compiler->setAttribute ("PrecompiledHeaderFile", getIntDirFile (config.getOutputFilename (".pch", true)));
  623. compiler->setAttribute ("AssemblerListingLocation", "$(IntDir)\\");
  624. compiler->setAttribute ("ObjectFile", "$(IntDir)\\");
  625. compiler->setAttribute ("ProgramDataBaseFileName", "$(IntDir)\\");
  626. compiler->setAttribute ("WarningLevel", String (getWarningLevel (config)));
  627. compiler->setAttribute ("SuppressStartupBanner", "true");
  628. const String extraFlags (replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim());
  629. if (extraFlags.isNotEmpty())
  630. compiler->setAttribute ("AdditionalOptions", extraFlags);
  631. }
  632. createToolElement (xml, "VCManagedResourceCompilerTool");
  633. {
  634. XmlElement* resCompiler = createToolElement (xml, "VCResourceCompilerTool");
  635. resCompiler->setAttribute ("PreprocessorDefinitions", isDebug ? "_DEBUG" : "NDEBUG");
  636. }
  637. createToolElement (xml, "VCPreLinkEventTool");
  638. if (! projectType.isLibrary())
  639. {
  640. XmlElement* linker = createToolElement (xml, "VCLinkerTool");
  641. linker->setAttribute ("OutputFile", getOutDirFile (config.getOutputFilename (msvcTargetSuffix, false)));
  642. linker->setAttribute ("SuppressStartupBanner", "true");
  643. linker->setAttribute ("IgnoreDefaultLibraryNames", isDebug ? "libcmt.lib, msvcrt.lib" : "");
  644. linker->setAttribute ("GenerateDebugInformation", (isDebug || config.shouldGenerateDebugSymbols()) ? "true" : "false");
  645. linker->setAttribute ("ProgramDatabaseFile", getIntDirFile (config.getOutputFilename (".pdb", true)));
  646. linker->setAttribute ("SubSystem", msvcIsWindowsSubsystem ? "2" : "1");
  647. const StringArray librarySearchPaths (config.getLibrarySearchPaths());
  648. if (librarySearchPaths.size() > 0)
  649. linker->setAttribute ("AdditionalLibraryDirectories", librarySearchPaths.joinIntoString (";"));
  650. linker->setAttribute ("GenerateManifest", config.shouldGenerateManifest() ? "true" : "false");
  651. if (! isDebug)
  652. {
  653. linker->setAttribute ("OptimizeReferences", "2");
  654. linker->setAttribute ("EnableCOMDATFolding", "2");
  655. }
  656. linker->setAttribute ("TargetMachine", "1"); // (64-bit build = 5)
  657. if (msvcDelayLoadedDLLs.isNotEmpty())
  658. linker->setAttribute ("DelayLoadDLLs", msvcDelayLoadedDLLs);
  659. if (config.config [Ids::msvcModuleDefinitionFile].toString().isNotEmpty())
  660. linker->setAttribute ("ModuleDefinitionFile", config.config [Ids::msvcModuleDefinitionFile].toString());
  661. String externalLibraries (getExternalLibrariesString());
  662. if (externalLibraries.isNotEmpty())
  663. linker->setAttribute ("AdditionalDependencies", replacePreprocessorTokens (config, externalLibraries).trim());
  664. String extraLinkerOptions (getExtraLinkerFlagsString());
  665. if (extraLinkerOptions.isNotEmpty())
  666. linker->setAttribute ("AdditionalOptions", replacePreprocessorTokens (config, extraLinkerOptions).trim());
  667. }
  668. else
  669. {
  670. if (isLibraryDLL())
  671. {
  672. XmlElement* linker = createToolElement (xml, "VCLinkerTool");
  673. String extraLinkerOptions (getExtraLinkerFlagsString());
  674. extraLinkerOptions << " /IMPLIB:" << getOutDirFile (config.getOutputFilename (".lib", true));
  675. linker->setAttribute ("AdditionalOptions", replacePreprocessorTokens (config, extraLinkerOptions).trim());
  676. String externalLibraries (getExternalLibrariesString());
  677. if (externalLibraries.isNotEmpty())
  678. linker->setAttribute ("AdditionalDependencies", replacePreprocessorTokens (config, externalLibraries).trim());
  679. linker->setAttribute ("OutputFile", getOutDirFile (config.getOutputFilename (msvcTargetSuffix, false)));
  680. linker->setAttribute ("IgnoreDefaultLibraryNames", isDebug ? "libcmt.lib, msvcrt.lib" : "");
  681. }
  682. else
  683. {
  684. XmlElement* librarian = createToolElement (xml, "VCLibrarianTool");
  685. librarian->setAttribute ("OutputFile", getOutDirFile (config.getOutputFilename (msvcTargetSuffix, false)));
  686. librarian->setAttribute ("IgnoreDefaultLibraryNames", isDebug ? "libcmt.lib, msvcrt.lib" : "");
  687. }
  688. }
  689. createToolElement (xml, "VCALinkTool");
  690. createToolElement (xml, "VCManifestTool");
  691. createToolElement (xml, "VCXDCMakeTool");
  692. {
  693. XmlElement* bscMake = createToolElement (xml, "VCBscMakeTool");
  694. bscMake->setAttribute ("SuppressStartupBanner", "true");
  695. bscMake->setAttribute ("OutputFile", getIntDirFile (config.getOutputFilename (".bsc", true)));
  696. }
  697. createToolElement (xml, "VCFxCopTool");
  698. if (! projectType.isLibrary())
  699. createToolElement (xml, "VCAppVerifierTool");
  700. XmlElement* postBuildEvent = createToolElement (xml, "VCPostBuildEventTool");
  701. if (config.getPostbuildCommandString().isNotEmpty())
  702. {
  703. postBuildEvent->setAttribute ("Description", "Post-build");
  704. postBuildEvent->setAttribute ("CommandLine", config.getPostbuildCommandString());
  705. }
  706. }
  707. void createConfigs (XmlElement& xml) const
  708. {
  709. for (ConstConfigIterator config (*this); config.next();)
  710. createConfig (*xml.createNewChildElement ("Configuration"),
  711. dynamic_cast <const MSVCBuildConfiguration&> (*config));
  712. }
  713. static const char* getOptimisationLevelString (int level)
  714. {
  715. switch (level)
  716. {
  717. case optimiseMaxSpeed: return "3";
  718. case optimiseMinSize: return "1";
  719. default: return "0";
  720. }
  721. }
  722. //==============================================================================
  723. JUCE_DECLARE_NON_COPYABLE (MSVCProjectExporterVC2008)
  724. };
  725. //==============================================================================
  726. class MSVCProjectExporterVC2005 : public MSVCProjectExporterVC2008
  727. {
  728. public:
  729. MSVCProjectExporterVC2005 (Project& p, const ValueTree& t)
  730. : MSVCProjectExporterVC2008 (p, t, "VisualStudio2005")
  731. {
  732. name = getName();
  733. }
  734. static const char* getName() { return "Visual Studio 2005"; }
  735. static const char* getValueTreeTypeName() { return "VS2005"; }
  736. int getVisualStudioVersion() const { return 8; }
  737. static MSVCProjectExporterVC2005* createForSettings (Project& project, const ValueTree& settings)
  738. {
  739. if (settings.hasType (getValueTreeTypeName()))
  740. return new MSVCProjectExporterVC2005 (project, settings);
  741. return nullptr;
  742. }
  743. protected:
  744. String getProjectVersionString() const { return "8.00"; }
  745. String getSolutionVersionString() const { return "9.00" + newLine + "# Visual C++ Express 2005"; }
  746. JUCE_DECLARE_NON_COPYABLE (MSVCProjectExporterVC2005)
  747. };
  748. //==============================================================================
  749. class MSVCProjectExporterVC2010 : public MSVCProjectExporterBase
  750. {
  751. public:
  752. MSVCProjectExporterVC2010 (Project& p, const ValueTree& t, const char* folderName = "VisualStudio2010")
  753. : MSVCProjectExporterBase (p, t, folderName)
  754. {
  755. name = getName();
  756. }
  757. static const char* getName() { return "Visual Studio 2010"; }
  758. static const char* getValueTreeTypeName() { return "VS2010"; }
  759. int getVisualStudioVersion() const { return 10; }
  760. virtual String getPlatformToolset() const { return "Windows7.1SDK"; }
  761. virtual String getSolutionComment() const { return "# Visual Studio 2010"; }
  762. static MSVCProjectExporterVC2010* createForSettings (Project& project, const ValueTree& settings)
  763. {
  764. if (settings.hasType (getValueTreeTypeName()))
  765. return new MSVCProjectExporterVC2010 (project, settings);
  766. return nullptr;
  767. }
  768. //==============================================================================
  769. void create (const OwnedArray<LibraryModule>&) const
  770. {
  771. createResourcesAndIcon();
  772. {
  773. XmlElement projectXml ("Project");
  774. fillInProjectXml (projectXml);
  775. addPlatformToolsetToPropertyGroup (projectXml);
  776. writeXmlOrThrow (projectXml, getVCProjFile(), "utf-8", 100);
  777. }
  778. {
  779. XmlElement filtersXml ("Project");
  780. fillInFiltersXml (filtersXml);
  781. writeXmlOrThrow (filtersXml, getVCProjFiltersFile(), "utf-8", 100);
  782. }
  783. {
  784. MemoryOutputStream mo;
  785. writeSolutionFile (mo, "11.00", getSolutionComment(), getVCProjFile());
  786. overwriteFileIfDifferentOrThrow (getSLNFile(), mo);
  787. }
  788. }
  789. protected:
  790. //==============================================================================
  791. class VC2010BuildConfiguration : public MSVCBuildConfiguration
  792. {
  793. public:
  794. VC2010BuildConfiguration (Project& project, const ValueTree& settings)
  795. : MSVCBuildConfiguration (project, settings)
  796. {
  797. if (getArchitectureType().toString().isEmpty())
  798. getArchitectureType() = get32BitArchName();
  799. }
  800. //==============================================================================
  801. static const char* get32BitArchName() { return "32-bit"; }
  802. static const char* get64BitArchName() { return "x64"; }
  803. Value getArchitectureType() { return getValue (Ids::winArchitecture); }
  804. bool is64Bit() const { return config [Ids::winArchitecture].toString() == get64BitArchName(); }
  805. //==============================================================================
  806. void createConfigProperties (PropertyListBuilder& props)
  807. {
  808. MSVCBuildConfiguration::createConfigProperties (props);
  809. const char* const archTypes[] = { get32BitArchName(), get64BitArchName() };
  810. props.add (new ChoicePropertyComponent (getArchitectureType(), "Architecture",
  811. StringArray (archTypes, numElementsInArray (archTypes)),
  812. Array<var> (archTypes, numElementsInArray (archTypes))));
  813. }
  814. };
  815. virtual void addPlatformToolsetToPropertyGroup (XmlElement&) const {}
  816. BuildConfiguration::Ptr createBuildConfig (const ValueTree& settings) const
  817. {
  818. return new VC2010BuildConfiguration (project, settings);
  819. }
  820. static bool is64Bit (const BuildConfiguration& config)
  821. {
  822. return dynamic_cast <const VC2010BuildConfiguration&> (config).is64Bit();
  823. }
  824. //==============================================================================
  825. File getVCProjFile() const { return getProjectFile (".vcxproj"); }
  826. File getVCProjFiltersFile() const { return getProjectFile (".vcxproj.filters"); }
  827. String createConfigName (const BuildConfiguration& config) const
  828. {
  829. return config.getName() + (is64Bit (config) ? "|x64"
  830. : "|Win32");
  831. }
  832. void setConditionAttribute (XmlElement& xml, const BuildConfiguration& config) const
  833. {
  834. xml.setAttribute ("Condition", "'$(Configuration)|$(Platform)'=='" + createConfigName (config) + "'");
  835. }
  836. //==============================================================================
  837. void fillInProjectXml (XmlElement& projectXml) const
  838. {
  839. projectXml.setAttribute ("DefaultTargets", "Build");
  840. projectXml.setAttribute ("ToolsVersion", "4.0");
  841. projectXml.setAttribute ("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  842. {
  843. XmlElement* configsGroup = projectXml.createNewChildElement ("ItemGroup");
  844. configsGroup->setAttribute ("Label", "ProjectConfigurations");
  845. for (ConstConfigIterator config (*this); config.next();)
  846. {
  847. XmlElement* e = configsGroup->createNewChildElement ("ProjectConfiguration");
  848. e->setAttribute ("Include", createConfigName (*config));
  849. e->createNewChildElement ("Configuration")->addTextElement (config->getName());
  850. e->createNewChildElement ("Platform")->addTextElement (is64Bit (*config) ? "x64" : "Win32");
  851. }
  852. }
  853. {
  854. XmlElement* globals = projectXml.createNewChildElement ("PropertyGroup");
  855. globals->setAttribute ("Label", "Globals");
  856. globals->createNewChildElement ("ProjectGuid")->addTextElement (projectGUID);
  857. }
  858. {
  859. XmlElement* imports = projectXml.createNewChildElement ("Import");
  860. imports->setAttribute ("Project", "$(VCTargetsPath)\\Microsoft.Cpp.Default.props");
  861. }
  862. for (ConstConfigIterator i (*this); i.next();)
  863. {
  864. const MSVCBuildConfiguration& config = dynamic_cast <const MSVCBuildConfiguration&> (*i);
  865. XmlElement* e = projectXml.createNewChildElement ("PropertyGroup");
  866. setConditionAttribute (*e, config);
  867. e->setAttribute ("Label", "Configuration");
  868. e->createNewChildElement ("ConfigurationType")->addTextElement (getProjectType());
  869. e->createNewChildElement ("UseOfMfc")->addTextElement ("false");
  870. e->createNewChildElement ("CharacterSet")->addTextElement ("MultiByte");
  871. if (! (config.isDebug() || config.shouldDisableWholeProgramOpt()))
  872. e->createNewChildElement ("WholeProgramOptimization")->addTextElement ("true");
  873. if (is64Bit (config))
  874. e->createNewChildElement ("PlatformToolset")->addTextElement (getPlatformToolset());
  875. }
  876. {
  877. XmlElement* e = projectXml.createNewChildElement ("Import");
  878. e->setAttribute ("Project", "$(VCTargetsPath)\\Microsoft.Cpp.props");
  879. }
  880. {
  881. XmlElement* e = projectXml.createNewChildElement ("ImportGroup");
  882. e->setAttribute ("Label", "ExtensionSettings");
  883. }
  884. {
  885. XmlElement* e = projectXml.createNewChildElement ("ImportGroup");
  886. e->setAttribute ("Label", "PropertySheets");
  887. XmlElement* p = e->createNewChildElement ("Import");
  888. p->setAttribute ("Project", "$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props");
  889. p->setAttribute ("Condition", "exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')");
  890. p->setAttribute ("Label", "LocalAppDataPlatform");
  891. }
  892. {
  893. XmlElement* e = projectXml.createNewChildElement ("PropertyGroup");
  894. e->setAttribute ("Label", "UserMacros");
  895. }
  896. {
  897. XmlElement* props = projectXml.createNewChildElement ("PropertyGroup");
  898. props->createNewChildElement ("_ProjectFileVersion")->addTextElement ("10.0.30319.1");
  899. for (ConstConfigIterator i (*this); i.next();)
  900. {
  901. const MSVCBuildConfiguration& config = dynamic_cast <const MSVCBuildConfiguration&> (*i);
  902. {
  903. XmlElement* outdir = props->createNewChildElement ("OutDir");
  904. setConditionAttribute (*outdir, config);
  905. outdir->addTextElement (FileHelpers::windowsStylePath (getConfigTargetPath (config)) + "\\");
  906. }
  907. if (config.getIntermediatesPath().isNotEmpty())
  908. {
  909. XmlElement* intdir = props->createNewChildElement ("IntDir");
  910. setConditionAttribute (*intdir, config);
  911. intdir->addTextElement (FileHelpers::windowsStylePath (config.getIntermediatesPath()) + "\\");
  912. }
  913. {
  914. XmlElement* name = props->createNewChildElement ("TargetName");
  915. setConditionAttribute (*name, config);
  916. name->addTextElement (config.getOutputFilename (String::empty, true));
  917. }
  918. {
  919. XmlElement* manifest = props->createNewChildElement ("GenerateManifest");
  920. setConditionAttribute (*manifest, config);
  921. manifest->addTextElement (config.shouldGenerateManifest() ? "true" : "false");
  922. }
  923. const StringArray librarySearchPaths (config.getLibrarySearchPaths());
  924. if (librarySearchPaths.size() > 0)
  925. {
  926. XmlElement* libPath = props->createNewChildElement ("LibraryPath");
  927. setConditionAttribute (*libPath, config);
  928. libPath->addTextElement ("$(LibraryPath);" + librarySearchPaths.joinIntoString (";"));
  929. }
  930. }
  931. }
  932. for (ConstConfigIterator i (*this); i.next();)
  933. {
  934. const MSVCBuildConfiguration& config = dynamic_cast <const MSVCBuildConfiguration&> (*i);
  935. const bool isDebug = config.isDebug();
  936. XmlElement* group = projectXml.createNewChildElement ("ItemDefinitionGroup");
  937. setConditionAttribute (*group, config);
  938. {
  939. XmlElement* midl = group->createNewChildElement ("Midl");
  940. midl->createNewChildElement ("PreprocessorDefinitions")->addTextElement (isDebug ? "_DEBUG;%(PreprocessorDefinitions)"
  941. : "NDEBUG;%(PreprocessorDefinitions)");
  942. midl->createNewChildElement ("MkTypLibCompatible")->addTextElement ("true");
  943. midl->createNewChildElement ("SuppressStartupBanner")->addTextElement ("true");
  944. midl->createNewChildElement ("TargetEnvironment")->addTextElement ("Win32");
  945. midl->createNewChildElement ("HeaderFileName");
  946. }
  947. bool isUsingEditAndContinue = false;
  948. {
  949. XmlElement* cl = group->createNewChildElement ("ClCompile");
  950. cl->createNewChildElement ("Optimization")->addTextElement (getOptimisationLevelString (config.getOptimisationLevelInt()));
  951. if (isDebug && config.getOptimisationLevelInt() <= optimisationOff)
  952. {
  953. isUsingEditAndContinue = ! is64Bit (config);
  954. cl->createNewChildElement ("DebugInformationFormat")
  955. ->addTextElement (isUsingEditAndContinue ? "EditAndContinue"
  956. : "ProgramDatabase");
  957. }
  958. StringArray includePaths (getHeaderSearchPaths (config));
  959. includePaths.add ("%(AdditionalIncludeDirectories)");
  960. cl->createNewChildElement ("AdditionalIncludeDirectories")->addTextElement (includePaths.joinIntoString (";"));
  961. cl->createNewChildElement ("PreprocessorDefinitions")->addTextElement (getPreprocessorDefs (config, ";") + ";%(PreprocessorDefinitions)");
  962. cl->createNewChildElement ("RuntimeLibrary")->addTextElement (config.isUsingRuntimeLibDLL() ? (isDebug ? "MultiThreadedDebugDLL" : "MultiThreadedDLL")
  963. : (isDebug ? "MultiThreadedDebug" : "MultiThreaded"));
  964. cl->createNewChildElement ("RuntimeTypeInfo")->addTextElement ("true");
  965. cl->createNewChildElement ("PrecompiledHeader");
  966. cl->createNewChildElement ("AssemblerListingLocation")->addTextElement ("$(IntDir)\\");
  967. cl->createNewChildElement ("ObjectFileName")->addTextElement ("$(IntDir)\\");
  968. cl->createNewChildElement ("ProgramDataBaseFileName")->addTextElement ("$(IntDir)\\");
  969. cl->createNewChildElement ("WarningLevel")->addTextElement ("Level" + String (getWarningLevel (config)));
  970. cl->createNewChildElement ("SuppressStartupBanner")->addTextElement ("true");
  971. cl->createNewChildElement ("MultiProcessorCompilation")->addTextElement ("true");
  972. const String extraFlags (replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim());
  973. if (extraFlags.isNotEmpty())
  974. cl->createNewChildElement ("AdditionalOptions")->addTextElement (extraFlags + " %(AdditionalOptions)");
  975. }
  976. {
  977. XmlElement* res = group->createNewChildElement ("ResourceCompile");
  978. res->createNewChildElement ("PreprocessorDefinitions")->addTextElement (isDebug ? "_DEBUG;%(PreprocessorDefinitions)"
  979. : "NDEBUG;%(PreprocessorDefinitions)");
  980. }
  981. {
  982. XmlElement* link = group->createNewChildElement ("Link");
  983. link->createNewChildElement ("OutputFile")->addTextElement (getOutDirFile (config.getOutputFilename (msvcTargetSuffix, false)));
  984. link->createNewChildElement ("SuppressStartupBanner")->addTextElement ("true");
  985. link->createNewChildElement ("IgnoreSpecificDefaultLibraries")->addTextElement (isDebug ? "libcmt.lib; msvcrt.lib;;%(IgnoreSpecificDefaultLibraries)"
  986. : "%(IgnoreSpecificDefaultLibraries)");
  987. link->createNewChildElement ("GenerateDebugInformation")->addTextElement ((isDebug || config.shouldGenerateDebugSymbols()) ? "true" : "false");
  988. link->createNewChildElement ("ProgramDatabaseFile")->addTextElement (getIntDirFile (config.getOutputFilename (".pdb", true)));
  989. link->createNewChildElement ("SubSystem")->addTextElement (msvcIsWindowsSubsystem ? "Windows" : "Console");
  990. if (! is64Bit (config))
  991. link->createNewChildElement ("TargetMachine")->addTextElement ("MachineX86");
  992. if (isUsingEditAndContinue)
  993. link->createNewChildElement ("ImageHasSafeExceptionHandlers")->addTextElement ("false");
  994. if (! isDebug)
  995. {
  996. link->createNewChildElement ("OptimizeReferences")->addTextElement ("true");
  997. link->createNewChildElement ("EnableCOMDATFolding")->addTextElement ("true");
  998. }
  999. const StringArray librarySearchPaths (config.getLibrarySearchPaths());
  1000. if (librarySearchPaths.size() > 0)
  1001. link->createNewChildElement ("AdditionalLibraryDirectories")->addTextElement (replacePreprocessorTokens (config, librarySearchPaths.joinIntoString (";"))
  1002. + ";%(AdditionalLibraryDirectories)");
  1003. String externalLibraries (getExternalLibrariesString());
  1004. if (externalLibraries.isNotEmpty())
  1005. link->createNewChildElement ("AdditionalDependencies")->addTextElement (replacePreprocessorTokens (config, externalLibraries).trim()
  1006. + ";%(AdditionalDependencies)");
  1007. String extraLinkerOptions (getExtraLinkerFlagsString());
  1008. if (extraLinkerOptions.isNotEmpty())
  1009. link->createNewChildElement ("AdditionalOptions")->addTextElement (replacePreprocessorTokens (config, extraLinkerOptions).trim()
  1010. + " %(AdditionalOptions)");
  1011. if (msvcDelayLoadedDLLs.isNotEmpty())
  1012. link->createNewChildElement ("DelayLoadDLLs")->addTextElement (msvcDelayLoadedDLLs);
  1013. if (config.config [Ids::msvcModuleDefinitionFile].toString().isNotEmpty())
  1014. link->createNewChildElement ("ModuleDefinitionFile")
  1015. ->addTextElement (config.config [Ids::msvcModuleDefinitionFile].toString());
  1016. }
  1017. {
  1018. XmlElement* bsc = group->createNewChildElement ("Bscmake");
  1019. bsc->createNewChildElement ("SuppressStartupBanner")->addTextElement ("true");
  1020. bsc->createNewChildElement ("OutputFile")->addTextElement (getIntDirFile (config.getOutputFilename (".bsc", true)));
  1021. }
  1022. if (config.getPrebuildCommandString().isNotEmpty())
  1023. group->createNewChildElement ("PreBuildEvent")
  1024. ->createNewChildElement ("Command")
  1025. ->addTextElement (config.getPrebuildCommandString());
  1026. if (config.getPostbuildCommandString().isNotEmpty())
  1027. group->createNewChildElement ("PostBuildEvent")
  1028. ->createNewChildElement ("Command")
  1029. ->addTextElement (config.getPostbuildCommandString());
  1030. }
  1031. ScopedPointer<XmlElement> otherFilesGroup (new XmlElement ("ItemGroup"));
  1032. {
  1033. XmlElement* cppFiles = projectXml.createNewChildElement ("ItemGroup");
  1034. XmlElement* headerFiles = projectXml.createNewChildElement ("ItemGroup");
  1035. for (int i = 0; i < getAllGroups().size(); ++i)
  1036. {
  1037. const Project::Item& group = getAllGroups().getReference(i);
  1038. if (group.getNumChildren() > 0)
  1039. addFilesToCompile (group, *cppFiles, *headerFiles, *otherFilesGroup);
  1040. }
  1041. }
  1042. if (iconFile != File::nonexistent)
  1043. {
  1044. XmlElement* e = otherFilesGroup->createNewChildElement ("None");
  1045. e->setAttribute ("Include", prependDot (iconFile.getFileName()));
  1046. }
  1047. if (otherFilesGroup->getFirstChildElement() != nullptr)
  1048. projectXml.addChildElement (otherFilesGroup.release());
  1049. if (hasResourceFile())
  1050. {
  1051. XmlElement* rcGroup = projectXml.createNewChildElement ("ItemGroup");
  1052. XmlElement* e = rcGroup->createNewChildElement ("ResourceCompile");
  1053. e->setAttribute ("Include", prependDot (rcFile.getFileName()));
  1054. }
  1055. {
  1056. XmlElement* e = projectXml.createNewChildElement ("Import");
  1057. e->setAttribute ("Project", "$(VCTargetsPath)\\Microsoft.Cpp.targets");
  1058. }
  1059. {
  1060. XmlElement* e = projectXml.createNewChildElement ("ImportGroup");
  1061. e->setAttribute ("Label", "ExtensionTargets");
  1062. }
  1063. }
  1064. String getProjectType() const
  1065. {
  1066. if (projectType.isGUIApplication() || projectType.isCommandLineApp()) return "Application";
  1067. if (isLibraryDLL()) return "DynamicLibrary";
  1068. if (projectType.isLibrary()) return "StaticLibrary";
  1069. jassertfalse;
  1070. return String::empty;
  1071. }
  1072. static const char* getOptimisationLevelString (int level)
  1073. {
  1074. switch (level)
  1075. {
  1076. case optimiseMaxSpeed: return "Full";
  1077. case optimiseMinSize: return "MinSpace";
  1078. default: return "Disabled";
  1079. }
  1080. }
  1081. //==============================================================================
  1082. void addFilesToCompile (const Project::Item& projectItem, XmlElement& cpps, XmlElement& headers, XmlElement& otherFiles) const
  1083. {
  1084. if (projectItem.isGroup())
  1085. {
  1086. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  1087. addFilesToCompile (projectItem.getChild(i), cpps, headers, otherFiles);
  1088. }
  1089. else if (projectItem.shouldBeAddedToTargetProject())
  1090. {
  1091. const RelativePath path (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  1092. jassert (path.getRoot() == RelativePath::buildTargetFolder);
  1093. if (path.hasFileExtension ("cpp;cc;cxx;c"))
  1094. {
  1095. XmlElement* e = cpps.createNewChildElement ("ClCompile");
  1096. e->setAttribute ("Include", path.toWindowsStyle());
  1097. if (! projectItem.shouldBeCompiled())
  1098. e->createNewChildElement ("ExcludedFromBuild")->addTextElement ("true");
  1099. if (projectItem.shouldUseStdCall())
  1100. e->createNewChildElement ("CallingConvention")->addTextElement ("StdCall");
  1101. }
  1102. else if (path.hasFileExtension (headerFileExtensions))
  1103. {
  1104. headers.createNewChildElement ("ClInclude")->setAttribute ("Include", path.toWindowsStyle());
  1105. }
  1106. else if (! path.hasFileExtension ("mm;m"))
  1107. {
  1108. otherFiles.createNewChildElement ("None")->setAttribute ("Include", path.toWindowsStyle());
  1109. }
  1110. }
  1111. }
  1112. //==============================================================================
  1113. void addFilterGroup (XmlElement& groups, const String& path) const
  1114. {
  1115. XmlElement* e = groups.createNewChildElement ("Filter");
  1116. e->setAttribute ("Include", path);
  1117. e->createNewChildElement ("UniqueIdentifier")->addTextElement (createGUID (path + "_guidpathsaltxhsdf"));
  1118. }
  1119. void addFileToFilter (const RelativePath& file, const String& groupPath,
  1120. XmlElement& cpps, XmlElement& headers, XmlElement& otherFiles) const
  1121. {
  1122. XmlElement* e;
  1123. if (file.hasFileExtension (headerFileExtensions))
  1124. e = headers.createNewChildElement ("ClInclude");
  1125. else if (file.hasFileExtension (sourceFileExtensions))
  1126. e = cpps.createNewChildElement ("ClCompile");
  1127. else
  1128. e = otherFiles.createNewChildElement ("None");
  1129. jassert (file.getRoot() == RelativePath::buildTargetFolder);
  1130. e->setAttribute ("Include", file.toWindowsStyle());
  1131. e->createNewChildElement ("Filter")->addTextElement (groupPath);
  1132. }
  1133. void addFilesToFilter (const Project::Item& projectItem, const String& path,
  1134. XmlElement& cpps, XmlElement& headers, XmlElement& otherFiles, XmlElement& groups) const
  1135. {
  1136. if (projectItem.isGroup())
  1137. {
  1138. addFilterGroup (groups, path);
  1139. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  1140. addFilesToFilter (projectItem.getChild(i),
  1141. (path.isEmpty() ? String::empty : (path + "\\")) + projectItem.getChild(i).getName(),
  1142. cpps, headers, otherFiles, groups);
  1143. }
  1144. else if (projectItem.shouldBeAddedToTargetProject())
  1145. {
  1146. addFileToFilter (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder),
  1147. path.upToLastOccurrenceOf ("\\", false, false), cpps, headers, otherFiles);
  1148. }
  1149. }
  1150. void addFilesToFilter (const Array<RelativePath>& files, const String& path,
  1151. XmlElement& cpps, XmlElement& headers, XmlElement& otherFiles, XmlElement& groups)
  1152. {
  1153. if (files.size() > 0)
  1154. {
  1155. addFilterGroup (groups, path);
  1156. for (int i = 0; i < files.size(); ++i)
  1157. addFileToFilter (files.getReference(i), path, cpps, headers, otherFiles);
  1158. }
  1159. }
  1160. void fillInFiltersXml (XmlElement& filterXml) const
  1161. {
  1162. filterXml.setAttribute ("ToolsVersion", "4.0");
  1163. filterXml.setAttribute ("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  1164. XmlElement* groupsXml = filterXml.createNewChildElement ("ItemGroup");
  1165. XmlElement* cpps = filterXml.createNewChildElement ("ItemGroup");
  1166. XmlElement* headers = filterXml.createNewChildElement ("ItemGroup");
  1167. ScopedPointer<XmlElement> otherFilesGroup (new XmlElement ("ItemGroup"));
  1168. for (int i = 0; i < getAllGroups().size(); ++i)
  1169. {
  1170. const Project::Item& group = getAllGroups().getReference(i);
  1171. if (group.getNumChildren() > 0)
  1172. addFilesToFilter (group, group.getName(), *cpps, *headers, *otherFilesGroup, *groupsXml);
  1173. }
  1174. if (iconFile.exists())
  1175. {
  1176. XmlElement* e = otherFilesGroup->createNewChildElement ("None");
  1177. e->setAttribute ("Include", prependDot (iconFile.getFileName()));
  1178. e->createNewChildElement ("Filter")->addTextElement (ProjectSaver::getJuceCodeGroupName());
  1179. }
  1180. if (otherFilesGroup->getFirstChildElement() != nullptr)
  1181. filterXml.addChildElement (otherFilesGroup.release());
  1182. if (hasResourceFile())
  1183. {
  1184. XmlElement* rcGroup = filterXml.createNewChildElement ("ItemGroup");
  1185. XmlElement* e = rcGroup->createNewChildElement ("ResourceCompile");
  1186. e->setAttribute ("Include", prependDot (rcFile.getFileName()));
  1187. e->createNewChildElement ("Filter")->addTextElement (ProjectSaver::getJuceCodeGroupName());
  1188. }
  1189. }
  1190. JUCE_DECLARE_NON_COPYABLE (MSVCProjectExporterVC2010)
  1191. };
  1192. //==============================================================================
  1193. class MSVCProjectExporterVC2012 : public MSVCProjectExporterVC2010
  1194. {
  1195. public:
  1196. MSVCProjectExporterVC2012 (Project& p, const ValueTree& t)
  1197. : MSVCProjectExporterVC2010 (p, t, "VisualStudio2012")
  1198. {
  1199. name = getName();
  1200. }
  1201. static const char* getName() { return "Visual Studio 2012"; }
  1202. static const char* getValueTreeTypeName() { return "VS2012"; }
  1203. int getVisualStudioVersion() const { return 11; }
  1204. String getSolutionComment() const { return "# Visual Studio 2012"; }
  1205. String getPlatformToolset() const
  1206. {
  1207. const String s (settings [Ids::toolset].toString());
  1208. return s.isNotEmpty() ? s : "v110";
  1209. }
  1210. Value getPlatformToolsetValue() { return getSetting (Ids::toolset); }
  1211. static MSVCProjectExporterVC2012* createForSettings (Project& project, const ValueTree& settings)
  1212. {
  1213. if (settings.hasType (getValueTreeTypeName()))
  1214. return new MSVCProjectExporterVC2012 (project, settings);
  1215. return nullptr;
  1216. }
  1217. void createExporterProperties (PropertyListBuilder& props)
  1218. {
  1219. MSVCProjectExporterVC2010::createExporterProperties (props);
  1220. const char* const toolsetNames[] = { "(default)", "v110", "v110_xp", "Windows7.1SDK", nullptr };
  1221. const var toolsets[] = { var(), "v110", "v110_xp", "Windows7.1SDK" };
  1222. props.add (new ChoicePropertyComponent (getPlatformToolsetValue(), "Platform Toolset",
  1223. StringArray (toolsetNames),
  1224. Array<var> (toolsets, numElementsInArray (toolsets))));
  1225. }
  1226. private:
  1227. void addPlatformToolsetToPropertyGroup (XmlElement& project) const
  1228. {
  1229. forEachXmlChildElementWithTagName (project, e, "PropertyGroup")
  1230. {
  1231. XmlElement* platformToolset (new XmlElement ("PlatformToolset"));
  1232. platformToolset->addTextElement (getPlatformToolset());
  1233. e->addChildElement (platformToolset);
  1234. }
  1235. }
  1236. JUCE_DECLARE_NON_COPYABLE (MSVCProjectExporterVC2012)
  1237. };
  1238. #endif // __JUCER_PROJECTEXPORT_MSVC_JUCEHEADER__