cmVisualStudio10TargetGenerator.cxx 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmVisualStudio10TargetGenerator.h"
  11. #include "cmGlobalVisualStudio10Generator.h"
  12. #include "cmTarget.h"
  13. #include "cmComputeLinkInformation.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmMakefile.h"
  16. #include "cmSourceFile.h"
  17. #include "cmVisualStudioGeneratorOptions.h"
  18. #include "cmLocalVisualStudio7Generator.h"
  19. #include "cmVS10CLFlagTable.h"
  20. #include "cmVS10LinkFlagTable.h"
  21. #include "cmVS10LibFlagTable.h"
  22. #include <cmsys/auto_ptr.hxx>
  23. static std::string cmVS10EscapeXML(std::string arg)
  24. {
  25. cmSystemTools::ReplaceString(arg, "&", "&amp;");
  26. cmSystemTools::ReplaceString(arg, "<", "&lt;");
  27. cmSystemTools::ReplaceString(arg, ">", "&gt;");
  28. return arg;
  29. }
  30. static std::string cmVS10EscapeComment(std::string comment)
  31. {
  32. // MSBuild takes the CDATA of a <Message></Message> element and just
  33. // does "echo $CDATA" with no escapes. We must encode the string.
  34. // http://technet.microsoft.com/en-us/library/cc772462%28WS.10%29.aspx
  35. std::string echoable;
  36. for(std::string::iterator c = comment.begin(); c != comment.end(); ++c)
  37. {
  38. switch (*c)
  39. {
  40. case '\r': break;
  41. case '\n': echoable += '\t'; break;
  42. case '"': /* no break */
  43. case '|': /* no break */
  44. case '&': /* no break */
  45. case '<': /* no break */
  46. case '>': /* no break */
  47. case '^': echoable += '^'; /* no break */
  48. default: echoable += *c; break;
  49. }
  50. }
  51. return echoable;
  52. }
  53. cmVisualStudio10TargetGenerator::
  54. cmVisualStudio10TargetGenerator(cmTarget* target,
  55. cmGlobalVisualStudio10Generator* gg)
  56. {
  57. this->GlobalGenerator = gg;
  58. this->Target = target;
  59. this->Makefile = target->GetMakefile();
  60. this->LocalGenerator =
  61. (cmLocalVisualStudio7Generator*)
  62. this->Makefile->GetLocalGenerator();
  63. this->Name = this->Target->GetName();
  64. this->GlobalGenerator->CreateGUID(this->Name.c_str());
  65. this->GUID = this->GlobalGenerator->GetGUID(this->Name.c_str());
  66. this->Platform = gg->GetPlatformName();
  67. this->ComputeObjectNames();
  68. this->BuildFileStream = 0;
  69. }
  70. cmVisualStudio10TargetGenerator::~cmVisualStudio10TargetGenerator()
  71. {
  72. for(OptionsMap::iterator i = this->ClOptions.begin();
  73. i != this->ClOptions.end(); ++i)
  74. {
  75. delete i->second;
  76. }
  77. if(!this->BuildFileStream)
  78. {
  79. return;
  80. }
  81. if (this->BuildFileStream->Close())
  82. {
  83. this->GlobalGenerator
  84. ->FileReplacedDuringGenerate(this->PathToVcxproj);
  85. }
  86. delete this->BuildFileStream;
  87. }
  88. void cmVisualStudio10TargetGenerator::WritePlatformConfigTag(
  89. const char* tag,
  90. const char* config,
  91. int indentLevel,
  92. const char* attribute,
  93. const char* end,
  94. std::ostream* stream)
  95. {
  96. if(!stream)
  97. {
  98. stream = this->BuildFileStream;
  99. }
  100. stream->fill(' ');
  101. stream->width(indentLevel*2 );
  102. (*stream ) << "";
  103. (*stream ) << "<" << tag
  104. << " Condition=\"'$(Configuration)|$(Platform)'=='";
  105. (*stream ) << config << "|" << this->Platform << "'\"";
  106. if(attribute)
  107. {
  108. (*stream ) << attribute;
  109. }
  110. // close the tag
  111. (*stream ) << ">";
  112. if(end)
  113. {
  114. (*stream ) << end;
  115. }
  116. }
  117. void cmVisualStudio10TargetGenerator::WriteString(const char* line,
  118. int indentLevel)
  119. {
  120. this->BuildFileStream->fill(' ');
  121. this->BuildFileStream->width(indentLevel*2 );
  122. // write an empty string to get the fill level indent to print
  123. (*this->BuildFileStream ) << "";
  124. (*this->BuildFileStream ) << line;
  125. }
  126. #define VS10_USER_PROPS "$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props"
  127. void cmVisualStudio10TargetGenerator::Generate()
  128. {
  129. // do not generate external ms projects
  130. if(this->Target->GetProperty("EXTERNAL_MSPROJECT"))
  131. {
  132. return;
  133. }
  134. // Tell the global generator the name of the project file
  135. this->Target->SetProperty("GENERATOR_FILE_NAME",this->Name.c_str());
  136. this->Target->SetProperty("GENERATOR_FILE_NAME_EXT",
  137. ".vcxproj");
  138. if(this->Target->GetType() <= cmTarget::MODULE_LIBRARY)
  139. {
  140. if(!this->ComputeClOptions())
  141. {
  142. return;
  143. }
  144. }
  145. cmMakefile* mf = this->Target->GetMakefile();
  146. std::string path = mf->GetStartOutputDirectory();
  147. path += "/";
  148. path += this->Name;
  149. path += ".vcxproj";
  150. this->BuildFileStream =
  151. new cmGeneratedFileStream(path.c_str());
  152. this->PathToVcxproj = path;
  153. this->BuildFileStream->SetCopyIfDifferent(true);
  154. // Write the encoding header into the file
  155. char magic[] = {0xEF,0xBB, 0xBF};
  156. this->BuildFileStream->write(magic, 3);
  157. this->WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",0);
  158. this->WriteString("<Project DefaultTargets=\"Build\" "
  159. "ToolsVersion=\"4.0\" "
  160. "xmlns=\"http://schemas.microsoft.com/"
  161. "developer/msbuild/2003\">\n",
  162. 0);
  163. this->WriteProjectConfigurations();
  164. this->WriteString("<PropertyGroup Label=\"Globals\">\n", 1);
  165. this->WriteString("<ProjectGUID>", 2);
  166. (*this->BuildFileStream) << "{" << this->GUID << "}</ProjectGUID>\n";
  167. const char* vsProjectName = this->Target->GetProperty("VS_SCC_PROJECTNAME");
  168. const char* vsLocalPath = this->Target->GetProperty("VS_SCC_LOCALPATH");
  169. const char* vsProvider = this->Target->GetProperty("VS_SCC_PROVIDER");
  170. if( vsProjectName && vsLocalPath && vsProvider )
  171. {
  172. this->WriteString("<SccProjectName>", 2);
  173. (*this->BuildFileStream) << cmVS10EscapeXML(vsProjectName) <<
  174. "</SccProjectName>\n";
  175. this->WriteString("<SccLocalPath>", 2);
  176. (*this->BuildFileStream) << cmVS10EscapeXML(vsLocalPath) <<
  177. "</SccLocalPath>\n";
  178. this->WriteString("<SccProvider>", 2);
  179. (*this->BuildFileStream) << cmVS10EscapeXML(vsProvider) <<
  180. "</SccProvider>\n";
  181. const char* vsAuxPath = this->Target->GetProperty("VS_SCC_AUXPATH");
  182. if( vsAuxPath )
  183. {
  184. this->WriteString("<SccAuxPath>", 2);
  185. (*this->BuildFileStream) << cmVS10EscapeXML(vsAuxPath) <<
  186. "</SccAuxPath>\n";
  187. }
  188. }
  189. this->WriteString("<Keyword>Win32Proj</Keyword>\n", 2);
  190. this->WriteString("<Platform>", 2);
  191. (*this->BuildFileStream) << this->Platform << "</Platform>\n";
  192. const char* projLabel = this->Target->GetProperty("PROJECT_LABEL");
  193. if(!projLabel)
  194. {
  195. projLabel = this->Name.c_str();
  196. }
  197. this->WriteString("<ProjectName>", 2);
  198. (*this->BuildFileStream) << projLabel << "</ProjectName>\n";
  199. this->WriteString("</PropertyGroup>\n", 1);
  200. this->WriteString("<Import Project="
  201. "\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n",
  202. 1);
  203. this->WriteProjectConfigurationValues();
  204. this->WriteString(
  205. "<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n", 1);
  206. this->WriteString("<ImportGroup Label=\"ExtensionSettings\">\n", 1);
  207. this->WriteString("</ImportGroup>\n", 1);
  208. this->WriteString("<ImportGroup Label=\"PropertySheets\">\n", 1);
  209. this->WriteString("<Import Project=\"" VS10_USER_PROPS "\""
  210. " Condition=\"exists('" VS10_USER_PROPS "')\""
  211. " Label=\"LocalAppDataPlatform\" />", 2);
  212. this->WriteString("</ImportGroup>\n", 1);
  213. this->WriteString("<PropertyGroup Label=\"UserMacros\" />\n", 1);
  214. this->WritePathAndIncrementalLinkOptions();
  215. this->WriteItemDefinitionGroups();
  216. this->WriteCustomCommands();
  217. this->WriteObjSources();
  218. this->WriteCLSources();
  219. this->WriteProjectReferences();
  220. this->WriteString(
  221. "<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\""
  222. " />\n", 1);
  223. this->WriteString("<ImportGroup Label=\"ExtensionTargets\">\n", 1);
  224. this->WriteString("</ImportGroup>\n", 1);
  225. this->WriteString("</Project>", 0);
  226. // The groups are stored in a separate file for VS 10
  227. this->WriteGroups();
  228. }
  229. // ConfigurationType Application, Utility StaticLibrary DynamicLibrary
  230. void cmVisualStudio10TargetGenerator::WriteProjectConfigurations()
  231. {
  232. this->WriteString("<ItemGroup Label=\"ProjectConfigurations\">\n", 1);
  233. std::vector<std::string> *configs =
  234. static_cast<cmGlobalVisualStudio7Generator *>
  235. (this->GlobalGenerator)->GetConfigurations();
  236. for(std::vector<std::string>::iterator i = configs->begin();
  237. i != configs->end(); ++i)
  238. {
  239. this->WriteString("<ProjectConfiguration Include=\"", 2);
  240. (*this->BuildFileStream ) << *i << "|" << this->Platform << "\">\n";
  241. this->WriteString("<Configuration>", 3);
  242. (*this->BuildFileStream ) << *i << "</Configuration>\n";
  243. this->WriteString("<Platform>", 3);
  244. (*this->BuildFileStream) << this->Platform << "</Platform>\n";
  245. this->WriteString("</ProjectConfiguration>\n", 2);
  246. }
  247. this->WriteString("</ItemGroup>\n", 1);
  248. }
  249. void cmVisualStudio10TargetGenerator::WriteProjectConfigurationValues()
  250. {
  251. cmGlobalVisualStudio10Generator* gg =
  252. static_cast<cmGlobalVisualStudio10Generator*>(this->GlobalGenerator);
  253. std::vector<std::string> *configs =
  254. static_cast<cmGlobalVisualStudio7Generator *>
  255. (this->GlobalGenerator)->GetConfigurations();
  256. for(std::vector<std::string>::iterator i = configs->begin();
  257. i != configs->end(); ++i)
  258. {
  259. this->WritePlatformConfigTag("PropertyGroup",
  260. i->c_str(),
  261. 1, " Label=\"Configuration\"", "\n");
  262. std::string configType = "<ConfigurationType>";
  263. switch(this->Target->GetType())
  264. {
  265. case cmTarget::SHARED_LIBRARY:
  266. case cmTarget::MODULE_LIBRARY:
  267. configType += "DynamicLibrary";
  268. break;
  269. case cmTarget::STATIC_LIBRARY:
  270. configType += "StaticLibrary";
  271. break;
  272. case cmTarget::EXECUTABLE:
  273. configType += "Application";
  274. break;
  275. case cmTarget::UTILITY:
  276. configType += "Utility";
  277. break;
  278. }
  279. configType += "</ConfigurationType>\n";
  280. this->WriteString(configType.c_str(), 2);
  281. const char* mfcFlag =
  282. this->Target->GetMakefile()->GetDefinition("CMAKE_MFC_FLAG");
  283. std::string mfcFlagValue = mfcFlag ? mfcFlag : "0";
  284. std::string useOfMfcValue = "false";
  285. if(mfcFlagValue == "1")
  286. {
  287. useOfMfcValue = "Static";
  288. }
  289. else if(mfcFlagValue == "2")
  290. {
  291. useOfMfcValue = "Dynamic";
  292. }
  293. std::string mfcLine = "<UseOfMfc>";
  294. mfcLine += useOfMfcValue + "</UseOfMfc>\n";
  295. this->WriteString(mfcLine.c_str(), 2);
  296. if(this->Target->GetType() <= cmTarget::MODULE_LIBRARY &&
  297. this->ClOptions[*i]->UsingUnicode())
  298. {
  299. this->WriteString("<CharacterSet>Unicode</CharacterSet>\n", 2);
  300. }
  301. else
  302. {
  303. this->WriteString("<CharacterSet>MultiByte</CharacterSet>\n", 2);
  304. }
  305. if(const char* toolset = gg->GetPlatformToolset())
  306. {
  307. std::string pts = "<PlatformToolset>";
  308. pts += toolset;
  309. pts += "</PlatformToolset>\n";
  310. this->WriteString(pts.c_str(), 2);
  311. }
  312. this->WriteString("</PropertyGroup>\n", 1);
  313. }
  314. }
  315. void cmVisualStudio10TargetGenerator::WriteCustomCommands()
  316. {
  317. this->SourcesVisited.clear();
  318. std::vector<cmSourceFile*> const& sources = this->Target->GetSourceFiles();
  319. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  320. source != sources.end(); ++source)
  321. {
  322. cmSourceFile* sf = *source;
  323. this->WriteCustomCommand(sf);
  324. }
  325. }
  326. //----------------------------------------------------------------------------
  327. void cmVisualStudio10TargetGenerator::WriteCustomCommand(cmSourceFile* sf)
  328. {
  329. if(this->SourcesVisited.insert(sf).second)
  330. {
  331. if(std::vector<cmSourceFile*> const* depends =
  332. this->Target->GetSourceDepends(sf))
  333. {
  334. for(std::vector<cmSourceFile*>::const_iterator di = depends->begin();
  335. di != depends->end(); ++di)
  336. {
  337. this->WriteCustomCommand(*di);
  338. }
  339. }
  340. if(cmCustomCommand const* command = sf->GetCustomCommand())
  341. {
  342. this->WriteString("<ItemGroup>\n", 1);
  343. this->WriteCustomRule(sf, *command);
  344. this->WriteString("</ItemGroup>\n", 1);
  345. }
  346. }
  347. }
  348. void
  349. cmVisualStudio10TargetGenerator::WriteCustomRule(cmSourceFile* source,
  350. cmCustomCommand const &
  351. command)
  352. {
  353. std::string sourcePath = source->GetFullPath();
  354. // the rule file seems to need to exist for vs10
  355. if (source->GetExtension() == "rule")
  356. {
  357. if(!cmSystemTools::FileExists(sourcePath.c_str()))
  358. {
  359. // Make sure the path exists for the file
  360. std::string path = cmSystemTools::GetFilenamePath(sourcePath);
  361. cmSystemTools::MakeDirectory(path.c_str());
  362. std::ofstream fout(sourcePath.c_str());
  363. if(fout)
  364. {
  365. fout << "# generated from CMake\n";
  366. fout.flush();
  367. fout.close();
  368. }
  369. else
  370. {
  371. std::string error = "Could not create file: [";
  372. error += sourcePath;
  373. error += "] ";
  374. cmSystemTools::Error
  375. (error.c_str(), cmSystemTools::GetLastSystemError().c_str());
  376. }
  377. }
  378. }
  379. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  380. std::string comment = lg->ConstructComment(command);
  381. comment = cmVS10EscapeComment(comment);
  382. std::vector<std::string> *configs =
  383. static_cast<cmGlobalVisualStudio7Generator *>
  384. (this->GlobalGenerator)->GetConfigurations();
  385. this->WriteString("<CustomBuild Include=\"", 2);
  386. // custom command have to use relative paths or they do not
  387. // show up in the GUI
  388. std::string path = cmSystemTools::RelativePath(
  389. this->Makefile->GetCurrentOutputDirectory(),
  390. sourcePath.c_str());
  391. this->ConvertToWindowsSlash(path);
  392. (*this->BuildFileStream ) << path << "\">\n";
  393. for(std::vector<std::string>::iterator i = configs->begin();
  394. i != configs->end(); ++i)
  395. {
  396. std::string script =
  397. cmVS10EscapeXML(lg->ConstructScript(command, i->c_str()));
  398. this->WritePlatformConfigTag("Message",i->c_str(), 3);
  399. (*this->BuildFileStream ) << cmVS10EscapeXML(comment) << "</Message>\n";
  400. this->WritePlatformConfigTag("Command", i->c_str(), 3);
  401. (*this->BuildFileStream ) << script << "</Command>\n";
  402. this->WritePlatformConfigTag("AdditionalInputs", i->c_str(), 3);
  403. (*this->BuildFileStream ) << source->GetFullPath();
  404. for(std::vector<std::string>::const_iterator d =
  405. command.GetDepends().begin();
  406. d != command.GetDepends().end();
  407. ++d)
  408. {
  409. std::string dep;
  410. if(this->LocalGenerator->GetRealDependency(d->c_str(), i->c_str(), dep))
  411. {
  412. this->ConvertToWindowsSlash(dep);
  413. (*this->BuildFileStream ) << ";" << dep;
  414. }
  415. }
  416. (*this->BuildFileStream ) << ";%(AdditionalInputs)</AdditionalInputs>\n";
  417. this->WritePlatformConfigTag("Outputs", i->c_str(), 3);
  418. const char* sep = "";
  419. for(std::vector<std::string>::const_iterator o =
  420. command.GetOutputs().begin();
  421. o != command.GetOutputs().end();
  422. ++o)
  423. {
  424. std::string out = *o;
  425. this->ConvertToWindowsSlash(out);
  426. (*this->BuildFileStream ) << sep << out;
  427. sep = ";";
  428. }
  429. (*this->BuildFileStream ) << "</Outputs>\n";
  430. }
  431. this->WriteString("</CustomBuild>\n", 2);
  432. }
  433. void cmVisualStudio10TargetGenerator::ConvertToWindowsSlash(std::string& s)
  434. {
  435. // first convert all of the slashes
  436. std::string::size_type pos = 0;
  437. while((pos = s.find('/', pos)) != std::string::npos)
  438. {
  439. s[pos] = '\\';
  440. pos++;
  441. }
  442. }
  443. void cmVisualStudio10TargetGenerator::WriteGroups()
  444. {
  445. // collect up group information
  446. std::vector<cmSourceGroup> sourceGroups =
  447. this->Makefile->GetSourceGroups();
  448. std::vector<cmSourceFile*> classes = this->Target->GetSourceFiles();
  449. std::set<cmSourceGroup*> groupsUsed;
  450. std::vector<cmSourceFile*> clCompile;
  451. std::vector<cmSourceFile*> customBuild;
  452. std::vector<cmSourceFile*> none;
  453. std::vector<cmSourceFile*> headers;
  454. std::vector<cmSourceFile*> idls;
  455. std::vector<cmSourceFile*> resource;
  456. for(std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  457. s != classes.end(); s++)
  458. {
  459. cmSourceFile* sf = *s;
  460. std::string const& source = sf->GetFullPath();
  461. cmSourceGroup& sourceGroup =
  462. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  463. groupsUsed.insert(&sourceGroup);
  464. const char* lang = sf->GetLanguage();
  465. bool header = (*s)->GetPropertyAsBool("HEADER_FILE_ONLY")
  466. || this->GlobalGenerator->IgnoreFile
  467. ((*s)->GetExtension().c_str());
  468. std::string ext =
  469. cmSystemTools::LowerCase((*s)->GetExtension());
  470. if(!lang)
  471. {
  472. lang = "None";
  473. }
  474. if(header)
  475. {
  476. headers.push_back(sf);
  477. }
  478. else if(lang[0] == 'C')
  479. {
  480. clCompile.push_back(sf);
  481. }
  482. else if(strcmp(lang, "RC") == 0)
  483. {
  484. resource.push_back(sf);
  485. }
  486. else if(sf->GetCustomCommand())
  487. {
  488. customBuild.push_back(sf);
  489. }
  490. else if(ext == "idl")
  491. {
  492. idls.push_back(sf);
  493. }
  494. else
  495. {
  496. none.push_back(sf);
  497. }
  498. }
  499. this->AddMissingSourceGroups(groupsUsed, sourceGroups);
  500. // Write out group file
  501. std::string path = this->Makefile->GetStartOutputDirectory();
  502. path += "/";
  503. path += this->Name;
  504. path += ".vcxproj.filters";
  505. cmGeneratedFileStream fout(path.c_str());
  506. fout.SetCopyIfDifferent(true);
  507. char magic[] = {0xEF,0xBB, 0xBF};
  508. fout.write(magic, 3);
  509. cmGeneratedFileStream* save = this->BuildFileStream;
  510. this->BuildFileStream = & fout;
  511. this->WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  512. "<Project "
  513. "ToolsVersion=\"4.0\" "
  514. "xmlns=\"http://schemas.microsoft.com/"
  515. "developer/msbuild/2003\">\n",
  516. 0);
  517. this->WriteGroupSources("ClCompile", clCompile, sourceGroups);
  518. this->WriteGroupSources("ClInclude", headers, sourceGroups);
  519. this->WriteGroupSources("ResourceCompile", resource, sourceGroups);
  520. this->WriteGroupSources("Midl", idls, sourceGroups);
  521. this->WriteGroupSources("CustomBuild", customBuild, sourceGroups);
  522. this->WriteString("<ItemGroup>\n", 1);
  523. for(std::set<cmSourceGroup*>::iterator g = groupsUsed.begin();
  524. g != groupsUsed.end(); ++g)
  525. {
  526. cmSourceGroup* sg = *g;
  527. const char* name = sg->GetFullName();
  528. if(strlen(name) != 0)
  529. {
  530. this->WriteString("<Filter Include=\"", 2);
  531. (*this->BuildFileStream) << name << "\">\n";
  532. std::string guidName = "SG_Filter_";
  533. guidName += name;
  534. this->GlobalGenerator->CreateGUID(guidName.c_str());
  535. this->WriteString("<UniqueIdentifier>", 3);
  536. std::string guid
  537. = this->GlobalGenerator->GetGUID(guidName.c_str());
  538. (*this->BuildFileStream)
  539. << "{"
  540. << guid << "}"
  541. << "</UniqueIdentifier>\n";
  542. this->WriteString("</Filter>\n", 2);
  543. }
  544. }
  545. this->WriteString("</ItemGroup>\n", 1);
  546. this->WriteGroupSources("None", none, sourceGroups);
  547. this->WriteString("</Project>\n", 0);
  548. // restore stream pointer
  549. this->BuildFileStream = save;
  550. if (fout.Close())
  551. {
  552. this->GlobalGenerator->FileReplacedDuringGenerate(path);
  553. }
  554. }
  555. // Add to groupsUsed empty source groups that have non-empty children.
  556. void
  557. cmVisualStudio10TargetGenerator::AddMissingSourceGroups(
  558. std::set<cmSourceGroup*>& groupsUsed,
  559. const std::vector<cmSourceGroup>& allGroups
  560. )
  561. {
  562. for(std::vector<cmSourceGroup>::const_iterator current = allGroups.begin();
  563. current != allGroups.end(); ++current)
  564. {
  565. std::vector<cmSourceGroup> const& children = current->GetGroupChildren();
  566. if(children.empty())
  567. {
  568. continue; // the group is really empty
  569. }
  570. this->AddMissingSourceGroups(groupsUsed, children);
  571. cmSourceGroup* current_ptr = const_cast<cmSourceGroup*>(&(*current));
  572. if(groupsUsed.find(current_ptr) != groupsUsed.end())
  573. {
  574. continue; // group has already been added to set
  575. }
  576. // check if it least one of the group's descendants is not empty
  577. // (at least one child must already have been added)
  578. std::vector<cmSourceGroup>::const_iterator child_it = children.begin();
  579. while(child_it != children.end())
  580. {
  581. cmSourceGroup* child_ptr = const_cast<cmSourceGroup*>(&(*child_it));
  582. if(groupsUsed.find(child_ptr) != groupsUsed.end())
  583. {
  584. break; // found a child that was already added => add current group too
  585. }
  586. child_it++;
  587. }
  588. if(child_it == children.end())
  589. {
  590. continue; // no descendants have source files => ignore this group
  591. }
  592. groupsUsed.insert(current_ptr);
  593. }
  594. }
  595. void
  596. cmVisualStudio10TargetGenerator::
  597. WriteGroupSources(const char* name,
  598. std::vector<cmSourceFile*> const& sources,
  599. std::vector<cmSourceGroup>& sourceGroups)
  600. {
  601. this->WriteString("<ItemGroup>\n", 1);
  602. for(std::vector<cmSourceFile*>::const_iterator s = sources.begin();
  603. s != sources.end(); ++s)
  604. {
  605. cmSourceFile* sf = *s;
  606. if(sf->GetExtension() == "obj")
  607. {
  608. continue;
  609. }
  610. std::string const& source = sf->GetFullPath();
  611. cmSourceGroup& sourceGroup =
  612. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  613. const char* filter = sourceGroup.GetFullName();
  614. this->WriteString("<", 2);
  615. std::string path = source;
  616. // custom command sources must use relative paths or they will
  617. // not show up in the GUI.
  618. if(sf->GetCustomCommand())
  619. {
  620. path = cmSystemTools::RelativePath(
  621. this->Makefile->GetCurrentOutputDirectory(),
  622. source.c_str());
  623. }
  624. this->ConvertToWindowsSlash(path);
  625. (*this->BuildFileStream) << name << " Include=\""
  626. << path;
  627. if(strlen(filter))
  628. {
  629. (*this->BuildFileStream) << "\">\n";
  630. this->WriteString("<Filter>", 3);
  631. (*this->BuildFileStream) << filter << "</Filter>\n";
  632. this->WriteString("</", 2);
  633. (*this->BuildFileStream) << name << ">\n";
  634. }
  635. else
  636. {
  637. (*this->BuildFileStream) << "\" />\n";
  638. }
  639. }
  640. this->WriteString("</ItemGroup>\n", 1);
  641. }
  642. void cmVisualStudio10TargetGenerator::WriteObjSources()
  643. {
  644. if(this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  645. {
  646. return;
  647. }
  648. bool first = true;
  649. std::vector<cmSourceFile*>const & sources = this->Target->GetSourceFiles();
  650. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  651. source != sources.end(); ++source)
  652. {
  653. std::string ext =
  654. cmSystemTools::LowerCase((*source)->GetExtension());
  655. if(ext == "obj" || ext == "o")
  656. {
  657. if(first)
  658. {
  659. this->WriteString("<ItemGroup>\n", 1);
  660. first = false;
  661. }
  662. // If an object file is generated, then vs10
  663. // will use it in the build, and we have to list
  664. // it as None instead of Object
  665. if((*source)->GetPropertyAsBool("GENERATED"))
  666. {
  667. this->WriteString("<None Include=\"", 2);
  668. }
  669. // If it is not a generated object then we have
  670. // to use the Object type
  671. else
  672. {
  673. this->WriteString("<Object Include=\"", 2);
  674. }
  675. (*this->BuildFileStream ) << (*source)->GetFullPath() << "\" />\n";
  676. }
  677. }
  678. if(!first)
  679. {
  680. this->WriteString("</ItemGroup>\n", 1);
  681. }
  682. }
  683. void cmVisualStudio10TargetGenerator::WriteCLSources()
  684. {
  685. if(this->Target->GetType() > cmTarget::UTILITY)
  686. {
  687. return;
  688. }
  689. this->WriteString("<ItemGroup>\n", 1);
  690. std::vector<cmSourceFile*>const& sources = this->Target->GetSourceFiles();
  691. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  692. source != sources.end(); ++source)
  693. {
  694. std::string ext = cmSystemTools::LowerCase((*source)->GetExtension());
  695. if((*source)->GetCustomCommand() || ext == "o" || ext == "obj")
  696. {
  697. continue;
  698. }
  699. // If it is not a custom command and it is not a pre-built obj file,
  700. // then add it as a source (c/c++/header/rc/idl) file
  701. bool header = (*source)->GetPropertyAsBool("HEADER_FILE_ONLY")
  702. || this->GlobalGenerator->IgnoreFile(ext.c_str());
  703. const char* lang = (*source)->GetLanguage();
  704. bool cl = lang && (strcmp(lang, "C") == 0 || strcmp(lang, "CXX") ==0);
  705. bool rc = lang && (strcmp(lang, "RC") == 0);
  706. bool idl = ext == "idl";
  707. std::string sourceFile = (*source)->GetFullPath();
  708. // do not use a relative path here because it means that you
  709. // can not use as long a path to the file.
  710. this->ConvertToWindowsSlash(sourceFile);
  711. // output the source file
  712. if(header)
  713. {
  714. this->WriteString("<ClInclude Include=\"", 2);
  715. }
  716. else if(cl)
  717. {
  718. this->WriteString("<ClCompile Include=\"", 2);
  719. }
  720. else if(rc)
  721. {
  722. this->WriteString("<ResourceCompile Include=\"", 2);
  723. }
  724. else if(idl)
  725. {
  726. this->WriteString("<Midl Include=\"", 2);
  727. }
  728. else
  729. {
  730. this->WriteString("<None Include=\"", 2);
  731. }
  732. (*this->BuildFileStream ) << sourceFile << "\"";
  733. // ouput any flags specific to this source file
  734. if(!header && cl && this->OutputSourceSpecificFlags(*source))
  735. {
  736. // if the source file has specific flags the tag
  737. // is ended on a new line
  738. this->WriteString("</ClCompile>\n", 2);
  739. }
  740. else if(!header && rc && this->OutputSourceSpecificFlags(*source))
  741. {
  742. this->WriteString("</ResourceCompile>\n", 2);
  743. }
  744. else
  745. {
  746. (*this->BuildFileStream ) << " />\n";
  747. }
  748. }
  749. this->WriteString("</ItemGroup>\n", 1);
  750. }
  751. void cmVisualStudio10TargetGenerator::ComputeObjectNames()
  752. {
  753. // We may be modifying the source groups temporarily, so make a copy.
  754. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  755. // get the classes from the source lists then add them to the groups
  756. std::vector<cmSourceFile*>const & classes = this->Target->GetSourceFiles();
  757. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  758. i != classes.end(); i++)
  759. {
  760. // Add the file to the list of sources.
  761. std::string source = (*i)->GetFullPath();
  762. if(cmSystemTools::UpperCase((*i)->GetExtension()) == "DEF")
  763. {
  764. this->ModuleDefinitionFile = (*i)->GetFullPath();
  765. }
  766. cmSourceGroup& sourceGroup =
  767. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  768. sourceGroup.AssignSource(*i);
  769. }
  770. // Compute which sources need unique object computation.
  771. this->LocalGenerator->ComputeObjectNameRequirements(sourceGroups);
  772. }
  773. bool cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
  774. cmSourceFile* source)
  775. {
  776. cmSourceFile& sf = *source;
  777. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  778. // Compute the maximum length full path to the intermediate
  779. // files directory for any configuration. This is used to construct
  780. // object file names that do not produce paths that are too long.
  781. std::string dir_max;
  782. lg->ComputeMaxDirectoryLength(dir_max, *this->Target);
  783. std::string objectName;
  784. if(lg->NeedObjectName.find(&sf) != lg->NeedObjectName.end())
  785. {
  786. objectName = lg->GetObjectFileNameWithoutTarget(sf, dir_max);
  787. }
  788. std::string flags;
  789. std::string defines;
  790. if(const char* cflags = sf.GetProperty("COMPILE_FLAGS"))
  791. {
  792. flags += cflags;
  793. }
  794. if(const char* cdefs = sf.GetProperty("COMPILE_DEFINITIONS"))
  795. {
  796. defines += cdefs;
  797. }
  798. const char* lang =
  799. this->GlobalGenerator->GetLanguageFromExtension
  800. (sf.GetExtension().c_str());
  801. const char* sourceLang = this->LocalGenerator->GetSourceFileLanguage(sf);
  802. const char* linkLanguage = this->Target->GetLinkerLanguage();
  803. bool needForceLang = false;
  804. // source file does not match its extension language
  805. if(lang && sourceLang && strcmp(lang, sourceLang) != 0)
  806. {
  807. needForceLang = true;
  808. lang = sourceLang;
  809. }
  810. // if the source file does not match the linker language
  811. // then force c or c++
  812. if(needForceLang || (linkLanguage && lang
  813. && strcmp(lang, linkLanguage) != 0))
  814. {
  815. if(strcmp(lang, "CXX") == 0)
  816. {
  817. // force a C++ file type
  818. flags += " /TP ";
  819. }
  820. else if(strcmp(lang, "C") == 0)
  821. {
  822. // force to c
  823. flags += " /TC ";
  824. }
  825. }
  826. bool hasFlags = false;
  827. // for the first time we need a new line if there is something
  828. // produced here.
  829. const char* firstString = ">\n";
  830. if(objectName.size())
  831. {
  832. (*this->BuildFileStream ) << firstString;
  833. firstString = "";
  834. hasFlags = true;
  835. this->WriteString("<ObjectFileName>", 3);
  836. (*this->BuildFileStream )
  837. << "$(IntDir)/" << objectName << "</ObjectFileName>\n";
  838. }
  839. std::vector<std::string> *configs =
  840. static_cast<cmGlobalVisualStudio7Generator *>
  841. (this->GlobalGenerator)->GetConfigurations();
  842. for( std::vector<std::string>::iterator config = configs->begin();
  843. config != configs->end(); ++config)
  844. {
  845. std::string configUpper = cmSystemTools::UpperCase(*config);
  846. std::string configDefines = defines;
  847. std::string defPropName = "COMPILE_DEFINITIONS_";
  848. defPropName += configUpper;
  849. if(const char* ccdefs = sf.GetProperty(defPropName.c_str()))
  850. {
  851. if(configDefines.size())
  852. {
  853. configDefines += ";";
  854. }
  855. configDefines += ccdefs;
  856. }
  857. // if we have flags or defines for this config then
  858. // use them
  859. if(flags.size() || configDefines.size())
  860. {
  861. (*this->BuildFileStream ) << firstString;
  862. firstString = ""; // only do firstString once
  863. hasFlags = true;
  864. cmVisualStudioGeneratorOptions
  865. clOptions(this->LocalGenerator,
  866. 10, cmVisualStudioGeneratorOptions::Compiler,
  867. cmVS10CLFlagTable, 0, this);
  868. clOptions.Parse(flags.c_str());
  869. clOptions.AddDefines(configDefines.c_str());
  870. clOptions.SetConfiguration((*config).c_str());
  871. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  872. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  873. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream,
  874. " ", "\n", lang);
  875. }
  876. }
  877. return hasFlags;
  878. }
  879. void cmVisualStudio10TargetGenerator::WritePathAndIncrementalLinkOptions()
  880. {
  881. cmTarget::TargetType ttype = this->Target->GetType();
  882. if(ttype > cmTarget::GLOBAL_TARGET)
  883. {
  884. return;
  885. }
  886. this->WriteString("<PropertyGroup>\n", 2);
  887. this->WriteString("<_ProjectFileVersion>10.0.20506.1"
  888. "</_ProjectFileVersion>\n", 3);
  889. std::vector<std::string> *configs =
  890. static_cast<cmGlobalVisualStudio7Generator *>
  891. (this->GlobalGenerator)->GetConfigurations();
  892. for(std::vector<std::string>::iterator config = configs->begin();
  893. config != configs->end(); ++config)
  894. {
  895. if(ttype >= cmTarget::UTILITY)
  896. {
  897. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  898. *this->BuildFileStream
  899. << "$(Platform)\\$(Configuration)\\$(ProjectName)\\"
  900. << "</IntDir>\n";
  901. }
  902. else
  903. {
  904. std::string targetNameFull =
  905. this->Target->GetFullName(config->c_str());
  906. std::string intermediateDir = this->LocalGenerator->
  907. GetTargetDirectory(*this->Target);
  908. intermediateDir += "/";
  909. intermediateDir += *config;
  910. intermediateDir += "/";
  911. this->ConvertToWindowsSlash(intermediateDir);
  912. std::string outDir = this->Target->GetDirectory(config->c_str());
  913. this->ConvertToWindowsSlash(outDir);
  914. this->WritePlatformConfigTag("OutDir", config->c_str(), 3);
  915. *this->BuildFileStream << outDir
  916. << "\\"
  917. << "</OutDir>\n";
  918. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  919. *this->BuildFileStream << intermediateDir
  920. << "</IntDir>\n";
  921. this->WritePlatformConfigTag("TargetName", config->c_str(), 3);
  922. *this->BuildFileStream
  923. << cmSystemTools::GetFilenameWithoutLastExtension(
  924. targetNameFull.c_str())
  925. << "</TargetName>\n";
  926. this->WritePlatformConfigTag("TargetExt", config->c_str(), 3);
  927. *this->BuildFileStream
  928. << cmSystemTools::GetFilenameLastExtension(targetNameFull.c_str())
  929. << "</TargetExt>\n";
  930. this->OutputLinkIncremental(*config);
  931. }
  932. }
  933. this->WriteString("</PropertyGroup>\n", 2);
  934. }
  935. void
  936. cmVisualStudio10TargetGenerator::
  937. OutputLinkIncremental(std::string const& configName)
  938. {
  939. std::string CONFIG = cmSystemTools::UpperCase(configName);
  940. // static libraries and things greater than modules do not need
  941. // to set this option
  942. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  943. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  944. {
  945. return;
  946. }
  947. const char* linkType = "SHARED";
  948. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  949. {
  950. linkType = "EXE";
  951. }
  952. // assume incremental linking
  953. const char* incremental = "true";
  954. const char* linkLanguage =
  955. this->Target->GetLinkerLanguage(configName.c_str());
  956. if(!linkLanguage)
  957. {
  958. cmSystemTools::Error
  959. ("CMake can not determine linker language for target:",
  960. this->Name.c_str());
  961. return;
  962. }
  963. std::string linkFlagVarBase = "CMAKE_";
  964. linkFlagVarBase += linkType;
  965. linkFlagVarBase += "_LINKER_FLAGS";
  966. std::string flags = this->
  967. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  968. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  969. flags += this->
  970. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  971. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0
  972. || strcmp(linkLanguage, "Fortran") == 0)
  973. {
  974. std::string baseFlagVar = "CMAKE_";
  975. baseFlagVar += linkLanguage;
  976. baseFlagVar += "_FLAGS";
  977. flags += this->
  978. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  979. std::string flagVar = baseFlagVar + std::string("_") + CONFIG;
  980. flags +=
  981. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  982. }
  983. const char* targetLinkFlags = this->Target->GetProperty("LINK_FLAGS");
  984. if(targetLinkFlags)
  985. {
  986. flags += " ";
  987. flags += targetLinkFlags;
  988. }
  989. std::string flagsProp = "LINK_FLAGS_";
  990. flagsProp += CONFIG;
  991. if(const char* flagsConfig = this->Target->GetProperty(flagsProp.c_str()))
  992. {
  993. flags += " ";
  994. flags += flagsConfig;
  995. }
  996. if(flags.find("INCREMENTAL:NO") != flags.npos)
  997. {
  998. incremental = "false";
  999. }
  1000. this->WritePlatformConfigTag("LinkIncremental", configName.c_str(), 3);
  1001. *this->BuildFileStream << incremental
  1002. << "</LinkIncremental>\n";
  1003. const char* manifest = "true";
  1004. if(flags.find("MANIFEST:NO") != flags.npos)
  1005. {
  1006. manifest = "false";
  1007. }
  1008. this->WritePlatformConfigTag("GenerateManifest", configName.c_str(), 3);
  1009. *this->BuildFileStream << manifest
  1010. << "</GenerateManifest>\n";
  1011. }
  1012. //----------------------------------------------------------------------------
  1013. bool cmVisualStudio10TargetGenerator::ComputeClOptions()
  1014. {
  1015. std::vector<std::string> const* configs =
  1016. this->GlobalGenerator->GetConfigurations();
  1017. for(std::vector<std::string>::const_iterator i = configs->begin();
  1018. i != configs->end(); ++i)
  1019. {
  1020. if(!this->ComputeClOptions(*i))
  1021. {
  1022. return false;
  1023. }
  1024. }
  1025. return true;
  1026. }
  1027. //----------------------------------------------------------------------------
  1028. bool cmVisualStudio10TargetGenerator::ComputeClOptions(
  1029. std::string const& configName)
  1030. {
  1031. // much of this was copied from here:
  1032. // copied from cmLocalVisualStudio7Generator.cxx 805
  1033. // TODO: Integrate code below with cmLocalVisualStudio7Generator.
  1034. cmsys::auto_ptr<Options> pOptions(
  1035. new Options(this->LocalGenerator, 10, Options::Compiler,
  1036. cmVS10CLFlagTable));
  1037. Options& clOptions = *pOptions;
  1038. std::string flags;
  1039. // collect up flags for
  1040. if(this->Target->GetType() < cmTarget::UTILITY)
  1041. {
  1042. const char* linkLanguage =
  1043. this->Target->GetLinkerLanguage(configName.c_str());
  1044. if(!linkLanguage)
  1045. {
  1046. cmSystemTools::Error
  1047. ("CMake can not determine linker language for target:",
  1048. this->Name.c_str());
  1049. return false;
  1050. }
  1051. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0
  1052. || strcmp(linkLanguage, "Fortran") == 0)
  1053. {
  1054. std::string baseFlagVar = "CMAKE_";
  1055. baseFlagVar += linkLanguage;
  1056. baseFlagVar += "_FLAGS";
  1057. flags = this->
  1058. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  1059. std::string flagVar = baseFlagVar + std::string("_") +
  1060. cmSystemTools::UpperCase(configName);
  1061. flags += " ";
  1062. flags += this->
  1063. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  1064. }
  1065. // set the correct language
  1066. if(strcmp(linkLanguage, "C") == 0)
  1067. {
  1068. flags += " /TC ";
  1069. }
  1070. if(strcmp(linkLanguage, "CXX") == 0)
  1071. {
  1072. flags += " /TP ";
  1073. }
  1074. }
  1075. // Add the target-specific flags.
  1076. if(const char* targetFlags = this->Target->GetProperty("COMPILE_FLAGS"))
  1077. {
  1078. flags += " ";
  1079. flags += targetFlags;
  1080. }
  1081. std::string configUpper = cmSystemTools::UpperCase(configName);
  1082. std::string defPropName = "COMPILE_DEFINITIONS_";
  1083. defPropName += configUpper;
  1084. // Get preprocessor definitions for this directory.
  1085. std::string defineFlags = this->Target->GetMakefile()->GetDefineFlags();
  1086. clOptions.FixExceptionHandlingDefault();
  1087. clOptions.Parse(flags.c_str());
  1088. clOptions.Parse(defineFlags.c_str());
  1089. clOptions.AddDefines
  1090. (this->Makefile->GetProperty("COMPILE_DEFINITIONS"));
  1091. clOptions.AddDefines(this->Target->GetProperty("COMPILE_DEFINITIONS"));
  1092. clOptions.AddDefines(this->Makefile->GetProperty(defPropName.c_str()));
  1093. clOptions.AddDefines(this->Target->GetProperty(defPropName.c_str()));
  1094. clOptions.SetVerboseMakefile(
  1095. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  1096. // Add a definition for the configuration name.
  1097. std::string configDefine = "CMAKE_INTDIR=\"";
  1098. configDefine += configName;
  1099. configDefine += "\"";
  1100. clOptions.AddDefine(configDefine);
  1101. if(const char* exportMacro = this->Target->GetExportMacro())
  1102. {
  1103. clOptions.AddDefine(exportMacro);
  1104. }
  1105. this->ClOptions[configName] = pOptions.release();
  1106. return true;
  1107. }
  1108. //----------------------------------------------------------------------------
  1109. void cmVisualStudio10TargetGenerator::WriteClOptions(
  1110. std::string const& configName,
  1111. std::vector<std::string> const& includes)
  1112. {
  1113. Options& clOptions = *(this->ClOptions[configName]);
  1114. this->WriteString("<ClCompile>\n", 2);
  1115. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1116. this->OutputIncludes(includes);
  1117. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1118. // If not in debug mode, write the DebugInformationFormat field
  1119. // without value so PDBs don't get generated uselessly.
  1120. if(!clOptions.IsDebug())
  1121. {
  1122. this->WriteString("<DebugInformationFormat>"
  1123. "</DebugInformationFormat>\n", 3);
  1124. }
  1125. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  1126. "\n", "CXX");
  1127. this->WriteString("<AssemblerListingLocation>", 3);
  1128. *this->BuildFileStream << configName
  1129. << "</AssemblerListingLocation>\n";
  1130. this->WriteString("<ObjectFileName>$(IntDir)</ObjectFileName>\n", 3);
  1131. this->WriteString("<ProgramDataBaseFileName>", 3);
  1132. *this->BuildFileStream << this->Target->GetDirectory(configName.c_str())
  1133. << "/"
  1134. << this->Target->GetPDBName(configName.c_str())
  1135. << "</ProgramDataBaseFileName>\n";
  1136. this->WriteString("</ClCompile>\n", 2);
  1137. }
  1138. void cmVisualStudio10TargetGenerator::
  1139. OutputIncludes(std::vector<std::string> const & includes)
  1140. {
  1141. this->WriteString("<AdditionalIncludeDirectories>", 3);
  1142. for(std::vector<std::string>::const_iterator i = includes.begin();
  1143. i != includes.end(); ++i)
  1144. {
  1145. *this->BuildFileStream << *i << ";";
  1146. }
  1147. this->WriteString("%(AdditionalIncludeDirectories)"
  1148. "</AdditionalIncludeDirectories>\n", 0);
  1149. }
  1150. void cmVisualStudio10TargetGenerator::
  1151. WriteRCOptions(std::string const& configName,
  1152. std::vector<std::string> const & includes)
  1153. {
  1154. this->WriteString("<ResourceCompile>\n", 2);
  1155. Options& clOptions = *(this->ClOptions[configName]);
  1156. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  1157. "\n", "RC");
  1158. this->OutputIncludes(includes);
  1159. this->WriteString("</ResourceCompile>\n", 2);
  1160. }
  1161. void
  1162. cmVisualStudio10TargetGenerator::WriteLibOptions(std::string const& config)
  1163. {
  1164. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  1165. {
  1166. return;
  1167. }
  1168. const char* libflags = this->Target->GetProperty("STATIC_LIBRARY_FLAGS");
  1169. std::string flagsConfigVar = "STATIC_LIBRARY_FLAGS_";
  1170. flagsConfigVar += cmSystemTools::UpperCase(config);
  1171. const char* libflagsConfig =
  1172. this->Target->GetProperty(flagsConfigVar.c_str());
  1173. if(libflags || libflagsConfig)
  1174. {
  1175. this->WriteString("<Lib>\n", 2);
  1176. cmVisualStudioGeneratorOptions
  1177. libOptions(this->LocalGenerator, 10,
  1178. cmVisualStudioGeneratorOptions::Linker,
  1179. cmVS10LibFlagTable, 0, this);
  1180. libOptions.Parse(libflags?libflags:"");
  1181. libOptions.Parse(libflagsConfig?libflagsConfig:"");
  1182. libOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1183. libOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1184. this->WriteString("</Lib>\n", 2);
  1185. }
  1186. }
  1187. void cmVisualStudio10TargetGenerator::WriteLinkOptions(std::string const&
  1188. config)
  1189. {
  1190. // static libraries and things greater than modules do not need
  1191. // to set this option
  1192. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  1193. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  1194. {
  1195. return;
  1196. }
  1197. const char* linkLanguage =
  1198. this->Target->GetLinkerLanguage(config.c_str());
  1199. if(!linkLanguage)
  1200. {
  1201. cmSystemTools::Error
  1202. ("CMake can not determine linker language for target:",
  1203. this->Name.c_str());
  1204. return;
  1205. }
  1206. this->WriteString("<Link>\n", 2);
  1207. std::string CONFIG = cmSystemTools::UpperCase(config);
  1208. const char* linkType = "SHARED";
  1209. if(this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  1210. {
  1211. linkType = "MODULE";
  1212. }
  1213. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1214. {
  1215. linkType = "EXE";
  1216. }
  1217. std::string stackVar = "CMAKE_";
  1218. stackVar += linkLanguage;
  1219. stackVar += "_STACK_SIZE";
  1220. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  1221. std::string flags;
  1222. if(stackVal)
  1223. {
  1224. flags += " ";
  1225. flags += stackVal;
  1226. }
  1227. // assume incremental linking
  1228. std::string linkFlagVarBase = "CMAKE_";
  1229. linkFlagVarBase += linkType;
  1230. linkFlagVarBase += "_LINKER_FLAGS";
  1231. flags += " ";
  1232. flags += this->
  1233. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  1234. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  1235. flags += " ";
  1236. flags += this->
  1237. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  1238. const char* targetLinkFlags = this->Target->GetProperty("LINK_FLAGS");
  1239. if(targetLinkFlags)
  1240. {
  1241. flags += " ";
  1242. flags += targetLinkFlags;
  1243. }
  1244. std::string flagsProp = "LINK_FLAGS_";
  1245. flagsProp += CONFIG;
  1246. if(const char* flagsConfig = this->Target->GetProperty(flagsProp.c_str()))
  1247. {
  1248. flags += " ";
  1249. flags += flagsConfig;
  1250. }
  1251. cmVisualStudioGeneratorOptions
  1252. linkOptions(this->LocalGenerator, 10,
  1253. cmVisualStudioGeneratorOptions::Linker,
  1254. cmVS10LinkFlagTable, 0, this);
  1255. if ( this->Target->GetPropertyAsBool("WIN32_EXECUTABLE") )
  1256. {
  1257. flags += " /SUBSYSTEM:WINDOWS";
  1258. }
  1259. else
  1260. {
  1261. flags += " /SUBSYSTEM:CONSOLE";
  1262. }
  1263. cmSystemTools::ReplaceString(flags, "/INCREMENTAL:YES", "");
  1264. cmSystemTools::ReplaceString(flags, "/INCREMENTAL:NO", "");
  1265. std::string standardLibsVar = "CMAKE_";
  1266. standardLibsVar += linkLanguage;
  1267. standardLibsVar += "_STANDARD_LIBRARIES";
  1268. std::string
  1269. libs = this->Makefile->GetSafeDefinition(standardLibsVar.c_str());
  1270. // Remove trailing spaces from libs
  1271. std::string::size_type pos = libs.size()-1;
  1272. if(libs.size() != 0)
  1273. {
  1274. while(libs[pos] == ' ')
  1275. {
  1276. pos--;
  1277. }
  1278. }
  1279. if(pos != libs.size()-1)
  1280. {
  1281. libs = libs.substr(0, pos+1);
  1282. }
  1283. // Replace spaces in libs with ;
  1284. cmSystemTools::ReplaceString(libs, " ", ";");
  1285. cmComputeLinkInformation* pcli =
  1286. this->Target->GetLinkInformation(config.c_str());
  1287. if(!pcli)
  1288. {
  1289. cmSystemTools::Error
  1290. ("CMake can not compute cmComputeLinkInformation for target:",
  1291. this->Name.c_str());
  1292. return;
  1293. }
  1294. // add the libraries for the target to libs string
  1295. cmComputeLinkInformation& cli = *pcli;
  1296. this->AddLibraries(cli, libs);
  1297. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  1298. std::vector<std::string> const& ldirs = cli.GetDirectories();
  1299. const char* sep = "";
  1300. std::string linkDirs;
  1301. for(std::vector<std::string>::const_iterator d = ldirs.begin();
  1302. d != ldirs.end(); ++d)
  1303. {
  1304. // first just full path
  1305. linkDirs += sep;
  1306. linkDirs += *d;
  1307. sep = ";";
  1308. linkDirs += sep;
  1309. // next path with configuration type Debug, Release, etc
  1310. linkDirs += *d;
  1311. linkDirs += "/$(Configuration)";
  1312. linkDirs += sep;
  1313. }
  1314. linkDirs += "%(AdditionalLibraryDirectories)";
  1315. linkOptions.AddFlag("AdditionalLibraryDirectories", linkDirs.c_str());
  1316. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  1317. linkOptions.AddFlag("Version", "");
  1318. if(linkOptions.IsDebug() || flags.find("/debug") != flags.npos)
  1319. {
  1320. linkOptions.AddFlag("GenerateDebugInformation", "true");
  1321. }
  1322. else
  1323. {
  1324. linkOptions.AddFlag("GenerateDebugInformation", "false");
  1325. }
  1326. std::string targetName;
  1327. std::string targetNameSO;
  1328. std::string targetNameFull;
  1329. std::string targetNameImport;
  1330. std::string targetNamePDB;
  1331. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1332. {
  1333. this->Target->GetExecutableNames(targetName, targetNameFull,
  1334. targetNameImport, targetNamePDB,
  1335. config.c_str());
  1336. }
  1337. else
  1338. {
  1339. this->Target->GetLibraryNames(targetName, targetNameSO, targetNameFull,
  1340. targetNameImport, targetNamePDB,
  1341. config.c_str());
  1342. }
  1343. std::string dir = this->Target->GetDirectory(config.c_str());
  1344. dir += "/";
  1345. std::string pdb = dir;
  1346. pdb += targetNamePDB;
  1347. std::string imLib = this->Target->GetDirectory(config.c_str(), true);
  1348. imLib += "/";
  1349. imLib += targetNameImport;
  1350. linkOptions.AddFlag("ImportLibrary", imLib.c_str());
  1351. linkOptions.AddFlag("ProgramDataBaseFileName", pdb.c_str());
  1352. linkOptions.Parse(flags.c_str());
  1353. if(!this->ModuleDefinitionFile.empty())
  1354. {
  1355. linkOptions.AddFlag("ModuleDefinitionFile",
  1356. this->ModuleDefinitionFile.c_str());
  1357. }
  1358. linkOptions.RemoveFlag("GenerateManifest");
  1359. linkOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1360. linkOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1361. this->WriteString("</Link>\n", 2);
  1362. if(!this->GlobalGenerator->NeedLinkLibraryDependencies(*this->Target))
  1363. {
  1364. this->WriteString("<ProjectReference>\n", 2);
  1365. this->WriteString(
  1366. " <LinkLibraryDependencies>false</LinkLibraryDependencies>\n", 2);
  1367. this->WriteString("</ProjectReference>\n", 2);
  1368. }
  1369. }
  1370. void cmVisualStudio10TargetGenerator::AddLibraries(
  1371. cmComputeLinkInformation& cli,
  1372. std::string& libstring)
  1373. {
  1374. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1375. ItemVector libs = cli.GetItems();
  1376. const char* sep = ";";
  1377. for(ItemVector::const_iterator l = libs.begin(); l != libs.end(); ++l)
  1378. {
  1379. if(l->IsPath)
  1380. {
  1381. std::string path = this->LocalGenerator->
  1382. Convert(l->Value.c_str(),
  1383. cmLocalGenerator::START_OUTPUT,
  1384. cmLocalGenerator::UNCHANGED);
  1385. this->ConvertToWindowsSlash(path);
  1386. libstring += sep;
  1387. libstring += path;
  1388. }
  1389. else
  1390. {
  1391. libstring += sep;
  1392. libstring += l->Value;
  1393. }
  1394. }
  1395. }
  1396. void cmVisualStudio10TargetGenerator::
  1397. WriteMidlOptions(std::string const& /*config*/,
  1398. std::vector<std::string> const & includes)
  1399. {
  1400. this->WriteString("<Midl>\n", 2);
  1401. this->OutputIncludes(includes);
  1402. this->WriteString("<OutputDirectory>$(IntDir)</OutputDirectory>\n", 3);
  1403. this->WriteString("<HeaderFileName>%(Filename).h</HeaderFileName>\n", 3);
  1404. this->WriteString(
  1405. "<TypeLibraryName>%(Filename).tlb</TypeLibraryName>\n", 3);
  1406. this->WriteString(
  1407. "<InterfaceIdentifierFileName>"
  1408. "%(Filename)_i.c</InterfaceIdentifierFileName>\n", 3);
  1409. this->WriteString("<ProxyFileName>%(Filename)_p.c</ProxyFileName>\n",3);
  1410. this->WriteString("</Midl>\n", 2);
  1411. }
  1412. void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups()
  1413. {
  1414. std::vector<std::string> *configs =
  1415. static_cast<cmGlobalVisualStudio7Generator *>
  1416. (this->GlobalGenerator)->GetConfigurations();
  1417. std::vector<std::string> includes;
  1418. this->LocalGenerator->GetIncludeDirectories(includes);
  1419. for(std::vector<std::string>::iterator i = configs->begin();
  1420. i != configs->end(); ++i)
  1421. {
  1422. this->WritePlatformConfigTag("ItemDefinitionGroup", i->c_str(), 1);
  1423. *this->BuildFileStream << "\n";
  1424. // output cl compile flags <ClCompile></ClCompile>
  1425. if(this->Target->GetType() <= cmTarget::MODULE_LIBRARY)
  1426. {
  1427. this->WriteClOptions(*i, includes);
  1428. // output rc compile flags <ResourceCompile></ResourceCompile>
  1429. this->WriteRCOptions(*i, includes);
  1430. }
  1431. // output midl flags <Midl></Midl>
  1432. this->WriteMidlOptions(*i, includes);
  1433. // write events
  1434. this->WriteEvents(*i);
  1435. // output link flags <Link></Link>
  1436. this->WriteLinkOptions(*i);
  1437. // output lib flags <Lib></Lib>
  1438. this->WriteLibOptions(*i);
  1439. this->WriteString("</ItemDefinitionGroup>\n", 1);
  1440. }
  1441. }
  1442. void
  1443. cmVisualStudio10TargetGenerator::WriteEvents(std::string const& configName)
  1444. {
  1445. this->WriteEvent("PreLinkEvent",
  1446. this->Target->GetPreLinkCommands(), configName);
  1447. this->WriteEvent("PreBuildEvent",
  1448. this->Target->GetPreBuildCommands(), configName);
  1449. this->WriteEvent("PostBuildEvent",
  1450. this->Target->GetPostBuildCommands(), configName);
  1451. }
  1452. void cmVisualStudio10TargetGenerator::WriteEvent(
  1453. const char* name,
  1454. std::vector<cmCustomCommand> & commands,
  1455. std::string const& configName)
  1456. {
  1457. if(commands.size() == 0)
  1458. {
  1459. return;
  1460. }
  1461. this->WriteString("<", 2);
  1462. (*this->BuildFileStream ) << name << ">\n";
  1463. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  1464. std::string script;
  1465. const char* pre = "";
  1466. std::string comment;
  1467. for(std::vector<cmCustomCommand>::iterator i = commands.begin();
  1468. i != commands.end(); ++i)
  1469. {
  1470. cmCustomCommand& command = *i;
  1471. comment += pre;
  1472. comment += lg->ConstructComment(command);
  1473. script += pre;
  1474. pre = "\n";
  1475. script +=
  1476. cmVS10EscapeXML(lg->ConstructScript(command, configName.c_str()));
  1477. }
  1478. comment = cmVS10EscapeComment(comment);
  1479. this->WriteString("<Message>",3);
  1480. (*this->BuildFileStream ) << cmVS10EscapeXML(comment) << "</Message>\n";
  1481. this->WriteString("<Command>", 3);
  1482. (*this->BuildFileStream ) << script;
  1483. (*this->BuildFileStream ) << "</Command>" << "\n";
  1484. this->WriteString("</", 2);
  1485. (*this->BuildFileStream ) << name << ">\n";
  1486. }
  1487. void cmVisualStudio10TargetGenerator::WriteProjectReferences()
  1488. {
  1489. cmGlobalGenerator::TargetDependSet const& unordered
  1490. = this->GlobalGenerator->GetTargetDirectDepends(*this->Target);
  1491. typedef cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  1492. OrderedTargetDependSet;
  1493. OrderedTargetDependSet depends(unordered);
  1494. this->WriteString("<ItemGroup>\n", 1);
  1495. for( OrderedTargetDependSet::const_iterator i = depends.begin();
  1496. i != depends.end(); ++i)
  1497. {
  1498. cmTarget* dt = *i;
  1499. // skip fortran targets as they can not be processed by MSBuild
  1500. // the only reference will be in the .sln file
  1501. if(static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator)
  1502. ->TargetIsFortranOnly(*dt))
  1503. {
  1504. continue;
  1505. }
  1506. this->WriteString("<ProjectReference Include=\"", 2);
  1507. cmMakefile* mf = dt->GetMakefile();
  1508. std::string name = dt->GetName();
  1509. std::string path;
  1510. const char* p = dt->GetProperty("EXTERNAL_MSPROJECT");
  1511. if(p)
  1512. {
  1513. path = p;
  1514. }
  1515. else
  1516. {
  1517. path = mf->GetStartOutputDirectory();
  1518. path += "/";
  1519. path += dt->GetName();
  1520. path += ".vcxproj";
  1521. }
  1522. (*this->BuildFileStream) << path << "\">\n";
  1523. this->WriteString("<Project>", 3);
  1524. (*this->BuildFileStream)
  1525. << this->GlobalGenerator->GetGUID(name.c_str())
  1526. << "</Project>\n";
  1527. this->WriteString("</ProjectReference>\n", 2);
  1528. }
  1529. this->WriteString("</ItemGroup>\n", 1);
  1530. }