cmVisualStudio10TargetGenerator.cxx 52 KB

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