cmVisualStudio10TargetGenerator.cxx 54 KB

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