cmVisualStudio10TargetGenerator.cxx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmVisualStudio10TargetGenerator.h"
  14. #include "cmGlobalVisualStudio7Generator.h"
  15. #include "cmTarget.h"
  16. #include "cmComputeLinkInformation.h"
  17. #include "cmGeneratedFileStream.h"
  18. #include "cmMakefile.h"
  19. #include "cmSourceFile.h"
  20. #include "cmVisualStudioGeneratorOptions.h"
  21. #include "cmLocalVisualStudio7Generator.h"
  22. #include "cmVS10CLFlagTable.h"
  23. #include "cmVS10LinkFlagTable.h"
  24. #include "cmVS10LibFlagTable.h"
  25. cmVisualStudio10TargetGenerator::
  26. cmVisualStudio10TargetGenerator(cmTarget* target,
  27. cmGlobalVisualStudio7Generator* gg)
  28. {
  29. this->GlobalGenerator = gg;
  30. this->GlobalGenerator->CreateGUID(target->GetName());
  31. this->GUID = this->GlobalGenerator->GetGUID(target->GetName());
  32. this->Target = target;
  33. this->Makefile = target->GetMakefile();
  34. this->LocalGenerator =
  35. (cmLocalVisualStudio7Generator*)
  36. this->Makefile->GetLocalGenerator();
  37. this->Platform = "|Win32";
  38. }
  39. cmVisualStudio10TargetGenerator::~cmVisualStudio10TargetGenerator()
  40. {
  41. delete this->BuildFileStream;
  42. }
  43. void cmVisualStudio10TargetGenerator::WritePlatformConfigTag(
  44. const char* tag,
  45. const char* config,
  46. int indentLevel,
  47. const char* attribute,
  48. const char* end,
  49. std::ostream* stream)
  50. {
  51. if(!stream)
  52. {
  53. stream = this->BuildFileStream;
  54. }
  55. stream->fill(' ');
  56. stream->width(indentLevel*2 );
  57. (*stream ) << "";
  58. (*stream ) << "<" << tag
  59. << " Condition=\"'$(Configuration)|$(Platform)'=='";
  60. (*stream ) << config << this->Platform << "'\"";
  61. if(attribute)
  62. {
  63. (*stream ) << attribute;
  64. }
  65. // close the tag
  66. (*stream ) << ">";
  67. if(end)
  68. {
  69. (*stream ) << end;
  70. }
  71. }
  72. void cmVisualStudio10TargetGenerator::WriteString(const char* line,
  73. int indentLevel)
  74. {
  75. this->BuildFileStream->fill(' ');
  76. this->BuildFileStream->width(indentLevel*2 );
  77. // write an empty string to get the fill level indent to print
  78. (*this->BuildFileStream ) << "";
  79. (*this->BuildFileStream ) << line;
  80. }
  81. void cmVisualStudio10TargetGenerator::Generate()
  82. {
  83. // Tell the global generator the name of the project file
  84. this->Target->SetProperty("GENERATOR_FILE_NAME",this->Target->GetName());
  85. this->Target->SetProperty("GENERATOR_FILE_NAME_EXT",
  86. ".vcxproj");
  87. cmMakefile* mf = this->Target->GetMakefile();
  88. std::string path = mf->GetStartOutputDirectory();
  89. path += "/";
  90. path += this->Target->GetName();
  91. path += ".vcxproj";
  92. this->BuildFileStream =
  93. new cmGeneratedFileStream(path.c_str());
  94. this->BuildFileStream->SetCopyIfDifferent(true);
  95. // Write the encoding header into the file
  96. char magic[] = {0xEF,0xBB, 0xBF};
  97. this->BuildFileStream->write(magic, 3);
  98. this->WriteString("<Project DefaultTargets=\"Build\" "
  99. "ToolsVersion=\"4.0\" "
  100. "xmlns=\"http://schemas.microsoft.com/"
  101. "developer/msbuild/2003\">\n",
  102. 0);
  103. this->WriteProjectConfigurations();
  104. this->WriteString("<PropertyGroup Label=\"Globals\">\n", 1);
  105. this->WriteString("<ProjectGUID>", 2);
  106. (*this->BuildFileStream) << "{" << this->GUID << "}</ProjectGUID>\n";
  107. this->WriteString("<SccProjectName />\n", 2);
  108. this->WriteString("<SccLocalPath />\n", 2);
  109. this->WriteString("<Keyword>Win32Proj</Keyword>\n", 2);
  110. this->WriteString("</PropertyGroup>\n", 1);
  111. this->WriteString("<Import Project="
  112. "\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n",
  113. 1);
  114. this->WriteProjectConfigurationValues();
  115. this->WriteString(
  116. "<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n", 1);
  117. this->WriteString("<ImportGroup Label=\"ExtensionSettings\">\n", 1);
  118. this->WriteString("</ImportGroup>\n", 1);
  119. this->WriteString("<ImportGroup Label=\"PropertySheets\">\n", 1);
  120. this->WriteString("<Import Project="
  121. "\"$(LocalAppData)\\Microsoft\\VisualStudio\\10.0\\"
  122. "Microsoft.Cpp.$(Platform).user.props\" "
  123. "Condition=\"exists('$(LocalAppData)\\Microsoft"
  124. "\\VisualStudio\\10.0\\"
  125. "Microsoft.Cpp.$(Platform).user.props')\" />\n", 2);
  126. this->WriteString("</ImportGroup>\n", 1);
  127. this->WriteString("<PropertyGroup Label=\"UserMacros\" />\n", 1);
  128. this->WritePathAndIncrementalLinkOptions();
  129. this->WriteItemDefinitionGroups();
  130. this->WriteCustomCommands();
  131. this->WriteObjSources();
  132. this->WriteCLSources();
  133. this->WriteProjectReferences();
  134. this->WriteString(
  135. "<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\""
  136. " />\n", 1);
  137. this->WriteString("<ImportGroup Label=\"ExtensionTargets\">\n", 1);
  138. this->WriteString("</ImportGroup>\n", 1);
  139. this->WriteString("</Project>", 0);
  140. // The groups are stored in a separate file for VS 10
  141. this->WriteGroups();
  142. }
  143. // ConfigurationType Application, Utility StaticLibrary DynamicLibrary
  144. void cmVisualStudio10TargetGenerator::WriteProjectConfigurations()
  145. {
  146. this->WriteString("<ItemGroup Label=\"ProjectConfigurations\">\n", 1);
  147. std::vector<std::string> *configs =
  148. static_cast<cmGlobalVisualStudio7Generator *>
  149. (this->GlobalGenerator)->GetConfigurations();
  150. for(std::vector<std::string>::iterator i = configs->begin();
  151. i != configs->end(); ++i)
  152. {
  153. this->WriteString("<ProjectConfiguration Include=\"", 2);
  154. (*this->BuildFileStream ) << *i << this->Platform << "\">\n";
  155. this->WriteString("<Configuration>", 3);
  156. (*this->BuildFileStream ) << *i << "</Configuration>\n";
  157. this->WriteString("<Platform>Win32</Platform>\n", 3);
  158. this->WriteString("</ProjectConfiguration>\n", 2);
  159. }
  160. this->WriteString("</ItemGroup>\n", 1);
  161. }
  162. void cmVisualStudio10TargetGenerator::WriteProjectConfigurationValues()
  163. {
  164. std::vector<std::string> *configs =
  165. static_cast<cmGlobalVisualStudio7Generator *>
  166. (this->GlobalGenerator)->GetConfigurations();
  167. for(std::vector<std::string>::iterator i = configs->begin();
  168. i != configs->end(); ++i)
  169. {
  170. this->WritePlatformConfigTag("PropertyGroup",
  171. i->c_str(),
  172. 1, " Label=\"Configuration\"", "\n");
  173. std::string configType = "<ConfigurationType>";
  174. switch(this->Target->GetType())
  175. {
  176. case cmTarget::SHARED_LIBRARY:
  177. case cmTarget::MODULE_LIBRARY:
  178. configType += "DynamicLibrary";
  179. break;
  180. case cmTarget::STATIC_LIBRARY:
  181. configType += "StaticLibrary";
  182. break;
  183. case cmTarget::EXECUTABLE:
  184. configType += "Application";
  185. break;
  186. case cmTarget::UTILITY:
  187. configType += "Utility";
  188. break;
  189. }
  190. configType += "</ConfigurationType>\n";
  191. this->WriteString(configType.c_str(), 2);
  192. const char* mfcFlag =
  193. this->Target->GetMakefile()->GetDefinition("CMAKE_MFC_FLAG");
  194. if(mfcFlag)
  195. {
  196. this->WriteString("<UseOfMfc>true</UseOfMfc>\n", 2);
  197. }
  198. else
  199. {
  200. this->WriteString("<UseOfMfc>false</UseOfMfc>\n", 2);
  201. }
  202. this->WriteString("<CharacterSet>MultiByte</CharacterSet>\n", 2);
  203. this->WriteString("</PropertyGroup>\n", 1);
  204. }
  205. }
  206. void cmVisualStudio10TargetGenerator::WriteCustomCommands()
  207. {
  208. this->WriteString("<ItemGroup>\n", 1);
  209. std::vector<cmSourceFile*>const & sources = this->Target->GetSourceFiles();
  210. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  211. source != sources.end(); ++source)
  212. {
  213. if(cmCustomCommand const* command = (*source)->GetCustomCommand())
  214. {
  215. this->WriteCustomRule(*source, *command);
  216. }
  217. }
  218. this->WriteString("</ItemGroup>\n", 1);
  219. }
  220. void
  221. cmVisualStudio10TargetGenerator::WriteCustomRule(cmSourceFile* source,
  222. cmCustomCommand const &
  223. command)
  224. {
  225. std::string sourcePath = source->GetFullPath();
  226. // the rule file seems to need to exist for vs10
  227. if (source->GetExtension() == "rule")
  228. {
  229. if(!cmSystemTools::FileExists(sourcePath.c_str()))
  230. {
  231. std::ofstream fout(sourcePath.c_str());
  232. if(fout)
  233. {
  234. fout << "# generated from CMake\n";
  235. fout.flush();
  236. fout.close();
  237. }
  238. }
  239. }
  240. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  241. std::string comment = lg->ConstructComment(command);
  242. std::vector<std::string> *configs =
  243. static_cast<cmGlobalVisualStudio7Generator *>
  244. (this->GlobalGenerator)->GetConfigurations();
  245. this->WriteString("<CustomBuild Include=\"", 2);
  246. (*this->BuildFileStream ) <<
  247. cmSystemTools::RelativePath(this->Makefile->GetCurrentOutputDirectory(),
  248. sourcePath.c_str()) << "\">\n";
  249. for(std::vector<std::string>::iterator i = configs->begin();
  250. i != configs->end(); ++i)
  251. {
  252. std::string script =
  253. lg->ConstructScript(command.GetCommandLines(),
  254. command.GetWorkingDirectory(),
  255. i->c_str(),
  256. command.GetEscapeOldStyle(),
  257. command.GetEscapeAllowMakeVars());
  258. this->WritePlatformConfigTag("Message",i->c_str(), 3);
  259. (*this->BuildFileStream ) << comment << "</Message>\n";
  260. this->WritePlatformConfigTag("Command", i->c_str(), 3);
  261. (*this->BuildFileStream ) << script << "</Command>\n";
  262. this->WritePlatformConfigTag("AdditionalInputs", i->c_str(), 3);
  263. (*this->BuildFileStream ) << source->GetFullPath();
  264. for(std::vector<std::string>::const_iterator d =
  265. command.GetDepends().begin();
  266. d != command.GetDepends().end();
  267. ++d)
  268. {
  269. std::string dep = this->LocalGenerator->
  270. GetRealDependency(d->c_str(), i->c_str());
  271. this->ConvertToWindowsSlash(dep);
  272. (*this->BuildFileStream ) << ";" << dep;
  273. }
  274. (*this->BuildFileStream ) << ";%(AdditionalInputs)</AdditionalInputs>\n";
  275. this->WritePlatformConfigTag("Outputs", i->c_str(), 3);
  276. const char* sep = "";
  277. for(std::vector<std::string>::const_iterator o =
  278. command.GetOutputs().begin();
  279. o != command.GetOutputs().end();
  280. ++o)
  281. {
  282. std::string out = *o;
  283. this->ConvertToWindowsSlash(out);
  284. (*this->BuildFileStream ) << sep << out;
  285. sep = ";";
  286. }
  287. (*this->BuildFileStream ) << ";%(Outputs)</Outputs>\n";
  288. }
  289. this->WriteString("</CustomBuild>\n", 2);
  290. }
  291. void cmVisualStudio10TargetGenerator::ConvertToWindowsSlash(std::string& s)
  292. {
  293. // first convert all of the slashes
  294. std::string::size_type pos = 0;
  295. while((pos = s.find('/', pos)) != std::string::npos)
  296. {
  297. s[pos] = '\\';
  298. pos++;
  299. }
  300. }
  301. void cmVisualStudio10TargetGenerator::WriteGroups()
  302. {
  303. // This should create a target.vcxproj.filters file
  304. // something like this:
  305. /*
  306. <?xml version="1.0" encoding="utf-8"?>
  307. <Project ToolsVersion="4.0"
  308. xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  309. <ItemGroup>
  310. <CustomBuild Include="..\CMakeLists.txt" />
  311. </ItemGroup>
  312. <ItemGroup>
  313. <Filter Include="Source Files">
  314. <UniqueIdentifier>{05072589-c7be-439a-8fd7-5db6ee5008a9}
  315. </UniqueIdentifier>
  316. </Filter>
  317. </ItemGroup>
  318. <ItemGroup>
  319. <ClCompile Include="..\foo.c">
  320. <Filter>Source Files</Filter>
  321. </ClCompile>
  322. <ClCompile Include="..\testCCompiler.c">
  323. <Filter>Source Files</Filter>
  324. </ClCompile>
  325. </ItemGroup>
  326. </Project>
  327. */
  328. }
  329. void cmVisualStudio10TargetGenerator::WriteObjSources()
  330. {
  331. if(this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  332. {
  333. return;
  334. }
  335. bool first = true;
  336. std::vector<cmSourceFile*>const & sources = this->Target->GetSourceFiles();
  337. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  338. source != sources.end(); ++source)
  339. {
  340. if((*source)->GetExtension() == "obj")
  341. {
  342. if(first)
  343. {
  344. this->WriteString("<ItemGroup>\n", 1);
  345. first = false;
  346. }
  347. this->WriteString("<None Include=\"", 2);
  348. (*this->BuildFileStream ) << (*source)->GetFullPath() << "\" />\n";
  349. }
  350. }
  351. if(!first)
  352. {
  353. this->WriteString("</ItemGroup>\n", 1);
  354. }
  355. }
  356. void cmVisualStudio10TargetGenerator::WriteCLSources()
  357. {
  358. this->WriteString("<ItemGroup>\n", 1);
  359. if(this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  360. {
  361. this->WriteString("<None Include=\"", 2);
  362. (*this->BuildFileStream ) << this->Target->GetDirectory()
  363. << "\\" << this->Target->GetName()
  364. << "\" />\n";
  365. }
  366. else
  367. {
  368. std::vector<cmSourceFile*>const& sources = this->Target->GetSourceFiles();
  369. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  370. source != sources.end(); ++source)
  371. {
  372. // if it is not a custom command then add it as a c/c++ file,
  373. // TODO: need to check for idl or rc
  374. if(!(*source)->GetCustomCommand()
  375. && !(*source)->GetPropertyAsBool("HEADER_FILE_ONLY")
  376. && !this->GlobalGenerator->IgnoreFile
  377. ((*source)->GetExtension().c_str()))
  378. {
  379. const char* lang = (*source)->GetLanguage();
  380. if(lang && (strcmp(lang, "C") == 0 || strcmp(lang, "CXX") ==0))
  381. {
  382. std::string sourceFile = (*source)->GetFullPath();
  383. // output the source file
  384. this->WriteString("<ClCompile Include=\"", 2);
  385. (*this->BuildFileStream ) << sourceFile << "\"";
  386. // ouput any flags specific to this source file
  387. if(this->OutputSourceSpecificFlags(*source))
  388. {
  389. // if the source file has specific flags the tag
  390. // is ended on a new line
  391. this->WriteString("</ClCompile>\n", 2);
  392. }
  393. else
  394. {
  395. (*this->BuildFileStream ) << " />\n";
  396. }
  397. }
  398. }
  399. }
  400. }
  401. this->WriteString("</ItemGroup>\n", 1);
  402. }
  403. bool cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
  404. cmSourceFile* source)
  405. {
  406. cmSourceFile& sf = *source;
  407. std::string flags;
  408. std::string defines;
  409. if(const char* cflags = sf.GetProperty("COMPILE_FLAGS"))
  410. {
  411. flags += cflags;
  412. }
  413. if(const char* cdefs = sf.GetProperty("COMPILE_DEFINITIONS"))
  414. {
  415. defines += cdefs;
  416. }
  417. const char* lang =
  418. this->GlobalGenerator->GetLanguageFromExtension
  419. (sf.GetExtension().c_str());
  420. const char* sourceLang = this->LocalGenerator->GetSourceFileLanguage(sf);
  421. const char* linkLanguage = this->Target->GetLinkerLanguage();
  422. bool needForceLang = false;
  423. // source file does not match its extension language
  424. if(lang && sourceLang && strcmp(lang, sourceLang) != 0)
  425. {
  426. needForceLang = true;
  427. lang = sourceLang;
  428. }
  429. // if the source file does not match the linker language
  430. // then force c or c++
  431. if(needForceLang || (linkLanguage && lang
  432. && strcmp(lang, linkLanguage) != 0))
  433. {
  434. if(strcmp(lang, "CXX") == 0)
  435. {
  436. // force a C++ file type
  437. flags += " /TP ";
  438. }
  439. else if(strcmp(lang, "C") == 0)
  440. {
  441. // force to c
  442. flags += " /TC ";
  443. }
  444. }
  445. // for the first time we need a new line if there is something
  446. // produced here.
  447. const char* firstString = ">\n";
  448. std::vector<std::string> *configs =
  449. static_cast<cmGlobalVisualStudio7Generator *>
  450. (this->GlobalGenerator)->GetConfigurations();
  451. bool hasFlags = false;
  452. for( std::vector<std::string>::iterator config = configs->begin();
  453. config != configs->end(); ++config)
  454. {
  455. std::string configUpper = cmSystemTools::UpperCase(*config);
  456. std::string configDefines = defines;
  457. std::string defPropName = "COMPILE_DEFINITIONS_";
  458. defPropName += configUpper;
  459. if(const char* ccdefs = sf.GetProperty(defPropName.c_str()))
  460. {
  461. if(configDefines.size())
  462. {
  463. configDefines += ",";
  464. }
  465. configDefines += ccdefs;
  466. }
  467. // if we have flags or defines for this config then
  468. // use them
  469. if(flags.size() || configDefines.size())
  470. {
  471. (*this->BuildFileStream ) << firstString;
  472. firstString = ""; // only do firstString once
  473. hasFlags = true;
  474. cmVisualStudioGeneratorOptions
  475. clOptions(this->LocalGenerator,
  476. 10, cmVisualStudioGeneratorOptions::Compiler,
  477. cmVS10CLFlagTable, 0, this);
  478. clOptions.Parse(flags.c_str());
  479. clOptions.AddDefines(configDefines.c_str());
  480. clOptions.SetConfiguration((*config).c_str());
  481. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  482. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  483. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream,
  484. " ", "\n");
  485. }
  486. }
  487. return hasFlags;
  488. }
  489. void cmVisualStudio10TargetGenerator::WritePathAndIncrementalLinkOptions()
  490. {
  491. this->WriteString("<PropertyGroup>\n", 2);
  492. this->WriteString("<_ProjectFileVersion>10.0.20506.1"
  493. "</_ProjectFileVersion>\n", 3);
  494. std::vector<std::string> *configs =
  495. static_cast<cmGlobalVisualStudio7Generator *>
  496. (this->GlobalGenerator)->GetConfigurations();
  497. for(std::vector<std::string>::iterator config = configs->begin();
  498. config != configs->end(); ++config)
  499. {
  500. std::string targetNameFull =
  501. this->Target->GetFullName(config->c_str());
  502. std::string intermediateDir = this->LocalGenerator->
  503. GetTargetDirectory(*this->Target);
  504. intermediateDir += "/";
  505. intermediateDir += *config;
  506. intermediateDir += "/";
  507. this->ConvertToWindowsSlash(intermediateDir);
  508. std::string outDir = this->Target->GetDirectory(config->c_str());
  509. this->ConvertToWindowsSlash(outDir);
  510. this->WritePlatformConfigTag("OutDir", config->c_str(), 3);
  511. *this->BuildFileStream << outDir
  512. << "\\"
  513. << "</OutDir>\n";
  514. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  515. *this->BuildFileStream << intermediateDir
  516. << "</IntDir>\n";
  517. this->WritePlatformConfigTag("TargetName", config->c_str(), 3);
  518. *this->BuildFileStream << cmSystemTools::GetFilenameWithoutExtension(
  519. targetNameFull.c_str())
  520. << "</TargetName>\n";
  521. this->WritePlatformConfigTag("TargetExt", config->c_str(), 3);
  522. *this->BuildFileStream << cmSystemTools::GetFilenameLastExtension(
  523. targetNameFull.c_str())
  524. << "</TargetExt>\n";
  525. this->OutputLinkIncremental(*config);
  526. }
  527. this->WriteString("</PropertyGroup>\n", 2);
  528. }
  529. void
  530. cmVisualStudio10TargetGenerator::
  531. OutputLinkIncremental(std::string const& configName)
  532. {
  533. std::string CONFIG = cmSystemTools::UpperCase(configName);
  534. // static libraries and things greater than modules do not need
  535. // to set this option
  536. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  537. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  538. {
  539. return;
  540. }
  541. const char* linkType = "SHARED";
  542. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  543. {
  544. linkType = "EXE";
  545. }
  546. // assume incremental linking
  547. const char* incremental = "true";
  548. const char* linkLanguage =
  549. this->Target->GetLinkerLanguage(configName.c_str());
  550. if(!linkLanguage)
  551. {
  552. cmSystemTools::Error
  553. ("CMake can not determine linker language for target:",
  554. this->Target->GetName());
  555. return;
  556. }
  557. std::string linkFlagVarBase = "CMAKE_";
  558. linkFlagVarBase += linkType;
  559. linkFlagVarBase += "_LINKER_FLAGS";
  560. std::string flags = this->
  561. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  562. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  563. flags += this->
  564. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  565. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0
  566. || strcmp(linkLanguage, "Fortran") == 0)
  567. {
  568. std::string baseFlagVar = "CMAKE_";
  569. baseFlagVar += linkLanguage;
  570. baseFlagVar += "_FLAGS";
  571. flags += this->
  572. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  573. std::string flagVar = baseFlagVar + std::string("_") + CONFIG;
  574. flags +=
  575. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  576. }
  577. if(flags.find("INCREMENTAL:NO") != flags.npos)
  578. {
  579. incremental = "false";
  580. }
  581. this->WritePlatformConfigTag("LinkIncremental", configName.c_str(), 3);
  582. *this->BuildFileStream << incremental
  583. << "</LinkIncremental>\n";
  584. }
  585. void
  586. cmVisualStudio10TargetGenerator::
  587. WriteClOptions(std::string const& configName,
  588. std::vector<std::string> const & includes)
  589. {
  590. // much of this was copied from here:
  591. // copied from cmLocalVisualStudio7Generator.cxx 805
  592. this->WriteString("<ClCompile>\n", 2);
  593. cmVisualStudioGeneratorOptions
  594. clOptions(this->LocalGenerator,
  595. 10, cmVisualStudioGeneratorOptions::Compiler,
  596. cmVS10CLFlagTable);
  597. std::string flags;
  598. // collect up flags for
  599. if(this->Target->GetType() < cmTarget::UTILITY)
  600. {
  601. const char* linkLanguage =
  602. this->Target->GetLinkerLanguage(configName.c_str());
  603. if(!linkLanguage)
  604. {
  605. cmSystemTools::Error
  606. ("CMake can not determine linker language for target:",
  607. this->Target->GetName());
  608. return;
  609. }
  610. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0
  611. || strcmp(linkLanguage, "Fortran") == 0)
  612. {
  613. std::string baseFlagVar = "CMAKE_";
  614. baseFlagVar += linkLanguage;
  615. baseFlagVar += "_FLAGS";
  616. flags = this->
  617. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  618. std::string flagVar = baseFlagVar + std::string("_") +
  619. cmSystemTools::UpperCase(configName);
  620. flags += " ";
  621. flags += this->
  622. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  623. }
  624. // set the correct language
  625. if(strcmp(linkLanguage, "C") == 0)
  626. {
  627. flags += " /TC ";
  628. }
  629. if(strcmp(linkLanguage, "CXX") == 0)
  630. {
  631. flags += " /TP ";
  632. }
  633. }
  634. std::string configUpper = cmSystemTools::UpperCase(configName);
  635. std::string defPropName = "COMPILE_DEFINITIONS_";
  636. defPropName += configUpper;
  637. // Get preprocessor definitions for this directory.
  638. std::string defineFlags = this->Target->GetMakefile()->GetDefineFlags();
  639. clOptions.FixExceptionHandlingDefault();
  640. clOptions.Parse(flags.c_str());
  641. clOptions.Parse(defineFlags.c_str());
  642. clOptions.AddDefines
  643. (this->Makefile->GetProperty("COMPILE_DEFINITIONS"));
  644. clOptions.AddDefines(this->Target->GetProperty("COMPILE_DEFINITIONS"));
  645. clOptions.AddDefines(this->Makefile->GetProperty(defPropName.c_str()));
  646. clOptions.AddDefines(this->Target->GetProperty(defPropName.c_str()));
  647. clOptions.SetVerboseMakefile(
  648. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  649. // Add a definition for the configuration name.
  650. std::string configDefine = "CMAKE_INTDIR=\"";
  651. configDefine += configName;
  652. configDefine += "\"";
  653. clOptions.AddDefine(configDefine);
  654. if(const char* exportMacro = this->Target->GetExportMacro())
  655. {
  656. clOptions.AddDefine(exportMacro);
  657. }
  658. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  659. this->OutputIncludes(includes);
  660. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  661. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  662. "\n");
  663. this->WriteString("<AssemblerListingLocation>", 3);
  664. *this->BuildFileStream << configName
  665. << "</AssemblerListingLocation>\n";
  666. this->WriteString("<ObjectFileName>$(IntDir)</ObjectFileName>\n", 3);
  667. this->WriteString("<ProgramDataBaseFileName>", 3);
  668. *this->BuildFileStream << this->Target->GetDirectory(configName.c_str())
  669. << "/"
  670. << this->Target->GetPDBName(configName.c_str())
  671. << "</ProgramDataBaseFileName>\n";
  672. this->WriteString("</ClCompile>\n", 2);
  673. }
  674. void cmVisualStudio10TargetGenerator::
  675. OutputIncludes(std::vector<std::string> const & includes)
  676. {
  677. this->WriteString("<AdditionalIncludeDirectories>", 3);
  678. for(std::vector<std::string>::const_iterator i = includes.begin();
  679. i != includes.end(); ++i)
  680. {
  681. *this->BuildFileStream << *i << ";";
  682. }
  683. this->WriteString("%(AdditionalIncludeDirectories)"
  684. "</AdditionalIncludeDirectories>\n", 0);
  685. }
  686. void cmVisualStudio10TargetGenerator::
  687. WriteRCOptions(std::string const& ,
  688. std::vector<std::string> const & includes)
  689. {
  690. this->WriteString("<ResourceCompile>\n", 2);
  691. this->OutputIncludes(includes);
  692. this->WriteString("</ResourceCompile>\n", 2);
  693. }
  694. void cmVisualStudio10TargetGenerator::WriteLibOptions(std::string const&
  695. )
  696. {
  697. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  698. {
  699. return;
  700. }
  701. if(const char* libflags = this->Target
  702. ->GetProperty("STATIC_LIBRARY_FLAGS"))
  703. {
  704. this->WriteString("<Lib>\n", 2);
  705. cmVisualStudioGeneratorOptions
  706. libOptions(this->LocalGenerator,
  707. 10, cmVisualStudioGeneratorOptions::Compiler,
  708. cmVS10LibFlagTable, 0, this);
  709. libOptions.Parse(libflags);
  710. libOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  711. libOptions.OutputFlagMap(*this->BuildFileStream, " ");
  712. this->WriteString("</Lib>\n", 2);
  713. }
  714. }
  715. void cmVisualStudio10TargetGenerator::WriteLinkOptions(std::string const&
  716. config)
  717. {
  718. // static libraries and things greater than modules do not need
  719. // to set this option
  720. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  721. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  722. {
  723. return;
  724. }
  725. const char* linkLanguage =
  726. this->Target->GetLinkerLanguage(config.c_str());
  727. if(!linkLanguage)
  728. {
  729. cmSystemTools::Error
  730. ("CMake can not determine linker language for target:",
  731. this->Target->GetName());
  732. return;
  733. }
  734. this->WriteString("<Link>\n", 2);
  735. std::string CONFIG = cmSystemTools::UpperCase(config);
  736. const char* linkType = "SHARED";
  737. if(this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  738. {
  739. linkType = "MODULE";
  740. }
  741. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  742. {
  743. linkType = "EXE";
  744. }
  745. std::string stackVar = "CMAKE_";
  746. stackVar += linkLanguage;
  747. stackVar += "_STACK_SIZE";
  748. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  749. std::string flags;
  750. if(stackVal)
  751. {
  752. flags += " ";
  753. flags += stackVal;
  754. }
  755. // assume incremental linking
  756. std::string linkFlagVarBase = "CMAKE_";
  757. linkFlagVarBase += linkType;
  758. linkFlagVarBase += "_LINKER_FLAGS";
  759. flags += " ";
  760. flags += this->
  761. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  762. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  763. flags += " ";
  764. flags += this->
  765. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  766. const char* targetLinkFlags = this->Target->GetProperty("LINK_FLAGS");
  767. if(targetLinkFlags)
  768. {
  769. flags += " ";
  770. flags += targetLinkFlags;
  771. }
  772. cmVisualStudioGeneratorOptions
  773. linkOptions(this->LocalGenerator,
  774. 10, cmVisualStudioGeneratorOptions::Compiler,
  775. cmVS10LinkFlagTable);
  776. if ( this->Target->GetPropertyAsBool("WIN32_EXECUTABLE") )
  777. {
  778. flags += " /SUBSYSTEM:WINDOWS";
  779. }
  780. else
  781. {
  782. flags += " /SUBSYSTEM:CONSOLE";
  783. }
  784. cmSystemTools::ReplaceString(flags, "/INCREMENTAL:YES", "");
  785. cmSystemTools::ReplaceString(flags, "/INCREMENTAL:NO", "");
  786. std::string standardLibsVar = "CMAKE_";
  787. standardLibsVar += linkLanguage;
  788. standardLibsVar += "_STANDARD_LIBRARIES";
  789. std::string
  790. libs = this->Makefile->GetSafeDefinition(standardLibsVar.c_str());
  791. // Remove trailing spaces from libs
  792. std::string::size_type pos = libs.size()-1;
  793. if(libs.size() != 0)
  794. {
  795. while(libs[pos] == ' ')
  796. {
  797. pos--;
  798. }
  799. }
  800. if(pos != libs.size()-1)
  801. {
  802. libs = libs.substr(0, pos+1);
  803. }
  804. // Replace spaces in libs with ;
  805. cmSystemTools::ReplaceString(libs, " ", ";");
  806. cmComputeLinkInformation* pcli =
  807. this->Target->GetLinkInformation(config.c_str());
  808. if(!pcli)
  809. {
  810. cmSystemTools::Error
  811. ("CMake can not compute cmComputeLinkInformation for target:",
  812. this->Target->GetName());
  813. return;
  814. }
  815. // add the libraries for the target to libs string
  816. cmComputeLinkInformation& cli = *pcli;
  817. this->AddLibraries(cli, libs);
  818. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  819. std::vector<std::string> const& ldirs = cli.GetDirectories();
  820. const char* sep = "";
  821. std::string linkDirs;
  822. for(std::vector<std::string>::const_iterator d = ldirs.begin();
  823. d != ldirs.end(); ++d)
  824. {
  825. linkDirs += sep;
  826. linkDirs += *d;
  827. sep = ";";
  828. }
  829. linkDirs += "%(AdditionalLibraryDirectories)";
  830. linkOptions.AddFlag("AdditionalLibraryDirectories", linkDirs.c_str());
  831. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  832. linkOptions.AddFlag("Version", "0.0");
  833. if(linkOptions.IsDebug() || flags.find("/debug") != flags.npos)
  834. {
  835. linkOptions.AddFlag("GenerateDebugInformation", "true");
  836. }
  837. else
  838. {
  839. linkOptions.AddFlag("GenerateDebugInformation", "false");
  840. }
  841. std::string targetName;
  842. std::string targetNameSO;
  843. std::string targetNameFull;
  844. std::string targetNameImport;
  845. std::string targetNamePDB;
  846. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  847. {
  848. this->Target->GetExecutableNames(targetName, targetNameFull,
  849. targetNameImport, targetNamePDB,
  850. config.c_str());
  851. }
  852. else
  853. {
  854. this->Target->GetLibraryNames(targetName, targetNameSO, targetNameFull,
  855. targetNameImport, targetNamePDB,
  856. config.c_str());
  857. }
  858. std::string dir = this->Target->GetDirectory(config.c_str());
  859. dir += "/";
  860. std::string imLib = dir;
  861. imLib += targetNameImport;
  862. std::string pdb = dir;
  863. pdb += targetNamePDB;
  864. linkOptions.AddFlag("ImportLibrary", imLib.c_str());
  865. linkOptions.AddFlag("ProgramDataBaseFileName", pdb.c_str());
  866. linkOptions.Parse(flags.c_str());
  867. linkOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  868. linkOptions.OutputFlagMap(*this->BuildFileStream, " ");
  869. this->WriteString("</Link>\n", 2);
  870. }
  871. void cmVisualStudio10TargetGenerator::AddLibraries(
  872. cmComputeLinkInformation& cli,
  873. std::string& libstring)
  874. {
  875. typedef cmComputeLinkInformation::ItemVector ItemVector;
  876. ItemVector libs = cli.GetItems();
  877. const char* sep = ";";
  878. for(ItemVector::const_iterator l = libs.begin(); l != libs.end(); ++l)
  879. {
  880. if(l->IsPath)
  881. {
  882. std::string path = this->LocalGenerator->
  883. Convert(l->Value.c_str(),
  884. cmLocalGenerator::START_OUTPUT,
  885. cmLocalGenerator::UNCHANGED);
  886. libstring += sep;
  887. libstring += path;
  888. }
  889. else
  890. {
  891. libstring += sep;
  892. libstring += l->Value;
  893. }
  894. }
  895. }
  896. void cmVisualStudio10TargetGenerator::
  897. WriteMidlOptions(std::string const& /*config*/,
  898. std::vector<std::string> const & includes)
  899. {
  900. this->WriteString("<Midl>\n", 2);
  901. this->OutputIncludes(includes);
  902. // Need this stuff, but there is an midl.xml file...
  903. // should we look for .idl language?, and flags?
  904. /*
  905. <MkTypLibCompatible>false</MkTypLibCompatible>
  906. <TargetEnvironment>Win32</TargetEnvironment>
  907. <GenerateStublessProxies>true</GenerateStublessProxies>
  908. <TypeLibraryName>%(FileName).tlb</TypeLibraryName>
  909. <OutputDirectory>$(IntDir)\</OutputDirectory>
  910. <HeaderFileName>%(FileName).h</HeaderFileName>
  911. <DllDataFileName>
  912. </DllDataFileName>
  913. <InterfaceIdentifierFileName>%(FileName)_i.c
  914. </InterfaceIdentifierFileName>
  915. <ProxyFileName>%(FileName)_p.c</ProxyFileName>
  916. */
  917. this->WriteString("</Midl>\n", 2);
  918. }
  919. void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups()
  920. {
  921. std::vector<std::string> *configs =
  922. static_cast<cmGlobalVisualStudio7Generator *>
  923. (this->GlobalGenerator)->GetConfigurations();
  924. std::vector<std::string> includes;
  925. this->LocalGenerator->GetIncludeDirectories(includes);
  926. for(std::vector<std::string>::iterator i = configs->begin();
  927. i != configs->end(); ++i)
  928. {
  929. this->WritePlatformConfigTag("ItemDefinitionGroup", i->c_str(), 1);
  930. *this->BuildFileStream << "\n";
  931. // output cl compile flags <ClCompile></ClCompile>
  932. if(this->Target->GetType() <= cmTarget::MODULE_LIBRARY)
  933. {
  934. this->WriteClOptions(*i, includes);
  935. // output rc compile flags <ResourceCompile></ResourceCompile>
  936. this->WriteRCOptions(*i, includes);
  937. }
  938. // output midl flags <Midl></Midl>
  939. this->WriteMidlOptions(*i, includes);
  940. // output link flags <Link></Link>
  941. this->WriteLinkOptions(*i);
  942. // output lib flags <Lib></Lib>
  943. this->WriteLibOptions(*i);
  944. this->WriteString("</ItemDefinitionGroup>\n", 1);
  945. }
  946. }
  947. void cmVisualStudio10TargetGenerator::WriteProjectReferences()
  948. {
  949. // TODO
  950. // This should have dependent targets listed like this:
  951. /*
  952. <ItemGroup>
  953. <ProjectReference Include="ZERO_CHECK.vcxproj">
  954. <Project>{2f1e4f3c-0a51-46c3-aaf9-e486599604f2}</Project>
  955. </ProjectReference>
  956. </ItemGroup>
  957. */
  958. }