cmVisualStudio10TargetGenerator.cxx 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. (this->LocalGenerator->GetGlobalGenerator());
  423. bool needForceLang = false;
  424. // source file does not match its extension language
  425. if(lang && sourceLang && strcmp(lang, sourceLang) != 0)
  426. {
  427. needForceLang = true;
  428. lang = sourceLang;
  429. }
  430. // if the source file does not match the linker language
  431. // then force c or c++
  432. if(needForceLang || (linkLanguage && lang
  433. && strcmp(lang, linkLanguage) != 0))
  434. {
  435. if(strcmp(lang, "CXX") == 0)
  436. {
  437. // force a C++ file type
  438. flags += " /TP ";
  439. }
  440. else if(strcmp(lang, "C") == 0)
  441. {
  442. // force to c
  443. flags += " /TC ";
  444. }
  445. }
  446. // for the first time we need a new line if there is something
  447. // produced here.
  448. const char* firstString = ">\n";
  449. std::vector<std::string> *configs =
  450. static_cast<cmGlobalVisualStudio7Generator *>
  451. (this->GlobalGenerator)->GetConfigurations();
  452. bool hasFlags = false;
  453. for( std::vector<std::string>::iterator config = configs->begin();
  454. config != configs->end(); ++config)
  455. {
  456. std::string configUpper = cmSystemTools::UpperCase(*config);
  457. std::string configDefines = defines;
  458. std::string defPropName = "COMPILE_DEFINITIONS_";
  459. defPropName += configUpper;
  460. if(const char* ccdefs = sf.GetProperty(defPropName.c_str()))
  461. {
  462. if(configDefines.size())
  463. {
  464. configDefines += ",";
  465. }
  466. configDefines += ccdefs;
  467. }
  468. // if we have flags or defines for this config then
  469. // use them
  470. if(flags.size() || configDefines.size())
  471. {
  472. (*this->BuildFileStream ) << firstString;
  473. firstString = ""; // only do firstString once
  474. hasFlags = true;
  475. cmVisualStudioGeneratorOptions
  476. clOptions(this->LocalGenerator,
  477. 10, cmVisualStudioGeneratorOptions::Compiler,
  478. cmVS10CLFlagTable, 0, this);
  479. clOptions.Parse(flags.c_str());
  480. clOptions.AddDefines(configDefines.c_str());
  481. clOptions.SetConfiguration((*config).c_str());
  482. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  483. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  484. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream,
  485. " ", "\n");
  486. }
  487. }
  488. return hasFlags;
  489. }
  490. void cmVisualStudio10TargetGenerator::WritePathAndIncrementalLinkOptions()
  491. {
  492. this->WriteString("<PropertyGroup>\n", 2);
  493. this->WriteString("<_ProjectFileVersion>10.0.20506.1"
  494. "</_ProjectFileVersion>\n", 3);
  495. std::vector<std::string> *configs =
  496. static_cast<cmGlobalVisualStudio7Generator *>
  497. (this->GlobalGenerator)->GetConfigurations();
  498. for(std::vector<std::string>::iterator config = configs->begin();
  499. config != configs->end(); ++config)
  500. {
  501. std::string targetNameFull =
  502. this->Target->GetFullName(config->c_str());
  503. std::string intermediateDir = this->LocalGenerator->
  504. GetTargetDirectory(*this->Target);
  505. intermediateDir += "/";
  506. intermediateDir += *config;
  507. intermediateDir += "/";
  508. this->ConvertToWindowsSlash(intermediateDir);
  509. std::string outDir = this->Target->GetDirectory(config->c_str());
  510. this->ConvertToWindowsSlash(outDir);
  511. this->WritePlatformConfigTag("OutDir", config->c_str(), 3);
  512. *this->BuildFileStream << outDir
  513. << "\\"
  514. << "</OutDir>\n";
  515. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  516. *this->BuildFileStream << intermediateDir
  517. << "</IntDir>\n";
  518. this->WritePlatformConfigTag("TargetName", config->c_str(), 3);
  519. *this->BuildFileStream << cmSystemTools::GetFilenameWithoutExtension(
  520. targetNameFull.c_str())
  521. << "</TargetName>\n";
  522. this->WritePlatformConfigTag("TargetExt", config->c_str(), 3);
  523. *this->BuildFileStream << cmSystemTools::GetFilenameLastExtension(
  524. targetNameFull.c_str())
  525. << "</TargetExt>\n";
  526. this->OutputLinkIncremental(*config);
  527. }
  528. this->WriteString("</PropertyGroup>\n", 2);
  529. }
  530. void
  531. cmVisualStudio10TargetGenerator::
  532. OutputLinkIncremental(std::string const& configName)
  533. {
  534. std::string CONFIG = cmSystemTools::UpperCase(configName);
  535. // static libraries and things greater than modules do not need
  536. // to set this option
  537. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  538. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  539. {
  540. return;
  541. }
  542. const char* linkType = "SHARED";
  543. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  544. {
  545. linkType = "EXE";
  546. }
  547. // assume incremental linking
  548. const char* incremental = "true";
  549. const char* linkLanguage =
  550. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  551. if(!linkLanguage)
  552. {
  553. cmSystemTools::Error
  554. ("CMake can not determine linker language for target:",
  555. this->Target->GetName());
  556. return;
  557. }
  558. std::string linkFlagVarBase = "CMAKE_";
  559. linkFlagVarBase += linkType;
  560. linkFlagVarBase += "_LINKER_FLAGS";
  561. std::string flags = this->
  562. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  563. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  564. flags += this->
  565. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  566. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0
  567. || strcmp(linkLanguage, "Fortran") == 0)
  568. {
  569. std::string baseFlagVar = "CMAKE_";
  570. baseFlagVar += linkLanguage;
  571. baseFlagVar += "_FLAGS";
  572. flags += this->
  573. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  574. std::string flagVar = baseFlagVar + std::string("_") + CONFIG;
  575. flags +=
  576. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  577. }
  578. if(flags.find("INCREMENTAL:NO") != flags.npos)
  579. {
  580. incremental = "false";
  581. }
  582. this->WritePlatformConfigTag("LinkIncremental", configName.c_str(), 3);
  583. *this->BuildFileStream << incremental
  584. << "</LinkIncremental>\n";
  585. }
  586. void
  587. cmVisualStudio10TargetGenerator::
  588. WriteClOptions(std::string const& configName,
  589. std::vector<std::string> const & includes)
  590. {
  591. // much of this was copied from here:
  592. // copied from cmLocalVisualStudio7Generator.cxx 805
  593. this->WriteString("<ClCompile>\n", 2);
  594. cmVisualStudioGeneratorOptions
  595. clOptions(this->LocalGenerator,
  596. 10, cmVisualStudioGeneratorOptions::Compiler,
  597. cmVS10CLFlagTable);
  598. std::string flags;
  599. // collect up flags for
  600. if(this->Target->GetType() < cmTarget::UTILITY)
  601. {
  602. const char* linkLanguage =
  603. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  604. if(!linkLanguage)
  605. {
  606. cmSystemTools::Error
  607. ("CMake can not determine linker language for target:",
  608. this->Target->GetName());
  609. return;
  610. }
  611. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0
  612. || strcmp(linkLanguage, "Fortran") == 0)
  613. {
  614. std::string baseFlagVar = "CMAKE_";
  615. baseFlagVar += linkLanguage;
  616. baseFlagVar += "_FLAGS";
  617. flags = this->
  618. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  619. std::string flagVar = baseFlagVar + std::string("_") +
  620. cmSystemTools::UpperCase(configName);
  621. flags += " ";
  622. flags += this->
  623. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  624. }
  625. // set the correct language
  626. if(strcmp(linkLanguage, "C") == 0)
  627. {
  628. flags += " /TC ";
  629. }
  630. if(strcmp(linkLanguage, "CXX") == 0)
  631. {
  632. flags += " /TP ";
  633. }
  634. }
  635. std::string configUpper = cmSystemTools::UpperCase(configName);
  636. std::string defPropName = "COMPILE_DEFINITIONS_";
  637. defPropName += configUpper;
  638. // Get preprocessor definitions for this directory.
  639. std::string defineFlags = this->Target->GetMakefile()->GetDefineFlags();
  640. clOptions.FixExceptionHandlingDefault();
  641. clOptions.Parse(flags.c_str());
  642. clOptions.Parse(defineFlags.c_str());
  643. clOptions.AddDefines
  644. (this->Makefile->GetProperty("COMPILE_DEFINITIONS"));
  645. clOptions.AddDefines(this->Target->GetProperty("COMPILE_DEFINITIONS"));
  646. clOptions.AddDefines(this->Makefile->GetProperty(defPropName.c_str()));
  647. clOptions.AddDefines(this->Target->GetProperty(defPropName.c_str()));
  648. clOptions.SetVerboseMakefile(
  649. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  650. // Add a definition for the configuration name.
  651. std::string configDefine = "CMAKE_INTDIR=\"";
  652. configDefine += configName;
  653. configDefine += "\"";
  654. clOptions.AddDefine(configDefine);
  655. if(const char* exportMacro = this->Target->GetExportMacro())
  656. {
  657. clOptions.AddDefine(exportMacro);
  658. }
  659. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  660. this->OutputIncludes(includes);
  661. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  662. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  663. "\n");
  664. this->WriteString("<AssemblerListingLocation>", 3);
  665. *this->BuildFileStream << configName
  666. << "</AssemblerListingLocation>\n";
  667. this->WriteString("<ObjectFileName>$(IntDir)</ObjectFileName>\n", 3);
  668. this->WriteString("<ProgramDataBaseFileName>", 3);
  669. *this->BuildFileStream << this->Target->GetDirectory(configName.c_str())
  670. << "/"
  671. << this->Target->GetPDBName(configName.c_str())
  672. << "</ProgramDataBaseFileName>\n";
  673. this->WriteString("</ClCompile>\n", 2);
  674. }
  675. void cmVisualStudio10TargetGenerator::
  676. OutputIncludes(std::vector<std::string> const & includes)
  677. {
  678. this->WriteString("<AdditionalIncludeDirectories>", 3);
  679. for(std::vector<std::string>::const_iterator i = includes.begin();
  680. i != includes.end(); ++i)
  681. {
  682. *this->BuildFileStream << *i << ";";
  683. }
  684. this->WriteString("%(AdditionalIncludeDirectories)"
  685. "</AdditionalIncludeDirectories>\n", 0);
  686. }
  687. void cmVisualStudio10TargetGenerator::
  688. WriteRCOptions(std::string const& ,
  689. std::vector<std::string> const & includes)
  690. {
  691. this->WriteString("<ResourceCompile>\n", 2);
  692. this->OutputIncludes(includes);
  693. this->WriteString("</ResourceCompile>\n", 2);
  694. }
  695. void cmVisualStudio10TargetGenerator::WriteLibOptions(std::string const&
  696. )
  697. {
  698. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  699. {
  700. return;
  701. }
  702. if(const char* libflags = this->Target
  703. ->GetProperty("STATIC_LIBRARY_FLAGS"))
  704. {
  705. this->WriteString("<Lib>\n", 2);
  706. cmVisualStudioGeneratorOptions
  707. libOptions(this->LocalGenerator,
  708. 10, cmVisualStudioGeneratorOptions::Compiler,
  709. cmVS10LibFlagTable, 0, this);
  710. libOptions.Parse(libflags);
  711. libOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  712. libOptions.OutputFlagMap(*this->BuildFileStream, " ");
  713. this->WriteString("</Lib>\n", 2);
  714. }
  715. }
  716. void cmVisualStudio10TargetGenerator::WriteLinkOptions(std::string const&
  717. config)
  718. {
  719. // static libraries and things greater than modules do not need
  720. // to set this option
  721. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  722. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  723. {
  724. return;
  725. }
  726. const char* linkLanguage =
  727. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  728. if(!linkLanguage)
  729. {
  730. cmSystemTools::Error
  731. ("CMake can not determine linker language for target:",
  732. this->Target->GetName());
  733. return;
  734. }
  735. this->WriteString("<Link>\n", 2);
  736. std::string CONFIG = cmSystemTools::UpperCase(config);
  737. const char* linkType = "SHARED";
  738. if(this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  739. {
  740. linkType = "MODULE";
  741. }
  742. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  743. {
  744. linkType = "EXE";
  745. }
  746. std::string stackVar = "CMAKE_";
  747. stackVar += linkLanguage;
  748. stackVar += "_STACK_SIZE";
  749. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  750. std::string flags;
  751. if(stackVal)
  752. {
  753. flags += " ";
  754. flags += stackVal;
  755. }
  756. // assume incremental linking
  757. std::string linkFlagVarBase = "CMAKE_";
  758. linkFlagVarBase += linkType;
  759. linkFlagVarBase += "_LINKER_FLAGS";
  760. flags += " ";
  761. flags += this->
  762. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  763. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  764. flags += " ";
  765. flags += this->
  766. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  767. const char* targetLinkFlags = this->Target->GetProperty("LINK_FLAGS");
  768. if(targetLinkFlags)
  769. {
  770. flags += " ";
  771. flags += targetLinkFlags;
  772. }
  773. cmVisualStudioGeneratorOptions
  774. linkOptions(this->LocalGenerator,
  775. 10, cmVisualStudioGeneratorOptions::Compiler,
  776. cmVS10LinkFlagTable);
  777. if ( this->Target->GetPropertyAsBool("WIN32_EXECUTABLE") )
  778. {
  779. flags += " /SUBSYSTEM:WINDOWS";
  780. }
  781. else
  782. {
  783. flags += " /SUBSYSTEM:CONSOLE";
  784. }
  785. cmSystemTools::ReplaceString(flags, "/INCREMENTAL:YES", "");
  786. cmSystemTools::ReplaceString(flags, "/INCREMENTAL:NO", "");
  787. std::string standardLibsVar = "CMAKE_";
  788. standardLibsVar += linkLanguage;
  789. standardLibsVar += "_STANDARD_LIBRARIES";
  790. std::string
  791. libs = this->Makefile->GetSafeDefinition(standardLibsVar.c_str());
  792. // Remove trailing spaces from libs
  793. std::string::size_type pos = libs.size()-1;
  794. if(libs.size() != 0)
  795. {
  796. while(libs[pos] == ' ')
  797. {
  798. pos--;
  799. }
  800. }
  801. if(pos != libs.size()-1)
  802. {
  803. libs = libs.substr(0, pos+1);
  804. }
  805. // Replace spaces in libs with ;
  806. cmSystemTools::ReplaceString(libs, " ", ";");
  807. cmComputeLinkInformation* pcli =
  808. this->Target->GetLinkInformation(config.c_str());
  809. if(!pcli)
  810. {
  811. cmSystemTools::Error
  812. ("CMake can not compute cmComputeLinkInformation for target:",
  813. this->Target->GetName());
  814. return;
  815. }
  816. // add the libraries for the target to libs string
  817. cmComputeLinkInformation& cli = *pcli;
  818. this->AddLibraries(cli, libs);
  819. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  820. std::vector<std::string> const& ldirs = cli.GetDirectories();
  821. const char* sep = "";
  822. std::string linkDirs;
  823. for(std::vector<std::string>::const_iterator d = ldirs.begin();
  824. d != ldirs.end(); ++d)
  825. {
  826. linkDirs += sep;
  827. linkDirs += *d;
  828. sep = ";";
  829. }
  830. linkDirs += "%(AdditionalLibraryDirectories)";
  831. linkOptions.AddFlag("AdditionalLibraryDirectories", linkDirs.c_str());
  832. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  833. linkOptions.AddFlag("Version", "0.0");
  834. if(linkOptions.IsDebug() || flags.find("/debug") != flags.npos)
  835. {
  836. linkOptions.AddFlag("GenerateDebugInformation", "true");
  837. }
  838. else
  839. {
  840. linkOptions.AddFlag("GenerateDebugInformation", "false");
  841. }
  842. std::string targetName;
  843. std::string targetNameSO;
  844. std::string targetNameFull;
  845. std::string targetNameImport;
  846. std::string targetNamePDB;
  847. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  848. {
  849. this->Target->GetExecutableNames(targetName, targetNameFull,
  850. targetNameImport, targetNamePDB,
  851. config.c_str());
  852. }
  853. else
  854. {
  855. this->Target->GetLibraryNames(targetName, targetNameSO, targetNameFull,
  856. targetNameImport, targetNamePDB,
  857. config.c_str());
  858. }
  859. std::string dir = this->Target->GetDirectory(config.c_str());
  860. dir += "/";
  861. std::string imLib = dir;
  862. imLib += targetNameImport;
  863. std::string pdb = dir;
  864. pdb += targetNamePDB;
  865. linkOptions.AddFlag("ImportLibrary", imLib.c_str());
  866. linkOptions.AddFlag("ProgramDataBaseFileName", pdb.c_str());
  867. linkOptions.Parse(flags.c_str());
  868. linkOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  869. linkOptions.OutputFlagMap(*this->BuildFileStream, " ");
  870. this->WriteString("</Link>\n", 2);
  871. }
  872. void cmVisualStudio10TargetGenerator::AddLibraries(
  873. cmComputeLinkInformation& cli,
  874. std::string& libstring)
  875. {
  876. typedef cmComputeLinkInformation::ItemVector ItemVector;
  877. ItemVector libs = cli.GetItems();
  878. const char* sep = ";";
  879. for(ItemVector::const_iterator l = libs.begin(); l != libs.end(); ++l)
  880. {
  881. if(l->IsPath)
  882. {
  883. std::string path = this->LocalGenerator->
  884. Convert(l->Value.c_str(),
  885. cmLocalGenerator::START_OUTPUT,
  886. cmLocalGenerator::UNCHANGED);
  887. libstring += sep;
  888. libstring += path;
  889. }
  890. else
  891. {
  892. libstring += sep;
  893. libstring += l->Value;
  894. }
  895. }
  896. }
  897. void cmVisualStudio10TargetGenerator::
  898. WriteMidlOptions(std::string const& /*config*/,
  899. std::vector<std::string> const & includes)
  900. {
  901. this->WriteString("<Midl>\n", 2);
  902. this->OutputIncludes(includes);
  903. // Need this stuff, but there is an midl.xml file...
  904. // should we look for .idl language?, and flags?
  905. /*
  906. <MkTypLibCompatible>false</MkTypLibCompatible>
  907. <TargetEnvironment>Win32</TargetEnvironment>
  908. <GenerateStublessProxies>true</GenerateStublessProxies>
  909. <TypeLibraryName>%(FileName).tlb</TypeLibraryName>
  910. <OutputDirectory>$(IntDir)\</OutputDirectory>
  911. <HeaderFileName>%(FileName).h</HeaderFileName>
  912. <DllDataFileName>
  913. </DllDataFileName>
  914. <InterfaceIdentifierFileName>%(FileName)_i.c
  915. </InterfaceIdentifierFileName>
  916. <ProxyFileName>%(FileName)_p.c</ProxyFileName>
  917. */
  918. this->WriteString("</Midl>\n", 2);
  919. }
  920. void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups()
  921. {
  922. std::vector<std::string> *configs =
  923. static_cast<cmGlobalVisualStudio7Generator *>
  924. (this->GlobalGenerator)->GetConfigurations();
  925. std::vector<std::string> includes;
  926. this->LocalGenerator->GetIncludeDirectories(includes);
  927. for(std::vector<std::string>::iterator i = configs->begin();
  928. i != configs->end(); ++i)
  929. {
  930. this->WritePlatformConfigTag("ItemDefinitionGroup", i->c_str(), 1);
  931. *this->BuildFileStream << "\n";
  932. // output cl compile flags <ClCompile></ClCompile>
  933. if(this->Target->GetType() <= cmTarget::MODULE_LIBRARY)
  934. {
  935. this->WriteClOptions(*i, includes);
  936. // output rc compile flags <ResourceCompile></ResourceCompile>
  937. this->WriteRCOptions(*i, includes);
  938. }
  939. // output midl flags <Midl></Midl>
  940. this->WriteMidlOptions(*i, includes);
  941. // output link flags <Link></Link>
  942. this->WriteLinkOptions(*i);
  943. // output lib flags <Lib></Lib>
  944. this->WriteLibOptions(*i);
  945. this->WriteString("</ItemDefinitionGroup>\n", 1);
  946. }
  947. }
  948. void cmVisualStudio10TargetGenerator::WriteProjectReferences()
  949. {
  950. // TODO
  951. // This should have dependent targets listed like this:
  952. /*
  953. <ItemGroup>
  954. <ProjectReference Include="ZERO_CHECK.vcxproj">
  955. <Project>{2f1e4f3c-0a51-46c3-aaf9-e486599604f2}</Project>
  956. </ProjectReference>
  957. </ItemGroup>
  958. */
  959. }