cmVisualStudio10TargetGenerator.cxx 52 KB

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