cmLocalVisualStudio7Generator.cxx 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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 "cmGlobalVisualStudio7Generator.h"
  14. #include "cmLocalVisualStudio7Generator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmCacheManager.h"
  19. #include "cmake.h"
  20. cmLocalVisualStudio7Generator::cmLocalVisualStudio7Generator()
  21. {
  22. m_Version = 7;
  23. }
  24. cmLocalVisualStudio7Generator::~cmLocalVisualStudio7Generator()
  25. {
  26. }
  27. void cmLocalVisualStudio7Generator::Generate()
  28. {
  29. std::set<cmStdString> lang;
  30. lang.insert("C");
  31. lang.insert("CXX");
  32. lang.insert("RC");
  33. lang.insert("IDL");
  34. lang.insert("DEF");
  35. this->CreateCustomTargetsAndCommands(lang);
  36. this->OutputVCProjFile();
  37. }
  38. // TODO
  39. // for CommandLine= need to repleace quotes with &quot
  40. // write out configurations
  41. void cmLocalVisualStudio7Generator::OutputVCProjFile()
  42. {
  43. // If not an in source build, then create the output directory
  44. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  45. m_Makefile->GetHomeDirectory()) != 0)
  46. {
  47. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  48. {
  49. cmSystemTools::Error("Error creating directory ",
  50. m_Makefile->GetStartOutputDirectory());
  51. }
  52. }
  53. m_LibraryOutputPath = "";
  54. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  55. {
  56. m_LibraryOutputPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  57. }
  58. if(m_LibraryOutputPath.size())
  59. {
  60. // make sure there is a trailing slash
  61. if(m_LibraryOutputPath[m_LibraryOutputPath.size()-1] != '/')
  62. {
  63. m_LibraryOutputPath += "/";
  64. }
  65. }
  66. m_ExecutableOutputPath = "";
  67. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  68. {
  69. m_ExecutableOutputPath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  70. }
  71. if(m_ExecutableOutputPath.size())
  72. {
  73. // make sure there is a trailing slash
  74. if(m_ExecutableOutputPath[m_ExecutableOutputPath.size()-1] != '/')
  75. {
  76. m_ExecutableOutputPath += "/";
  77. }
  78. }
  79. // Create the VCProj or set of VCProj's for libraries and executables
  80. // clear project names
  81. m_CreatedProjectNames.clear();
  82. #if 1
  83. // TODO: This block should be moved to a central location for all
  84. // generators. It is duplicated in every generator.
  85. // Call TraceVSDependencies on all targets
  86. cmTargets &tgts = m_Makefile->GetTargets();
  87. for(cmTargets::iterator l = tgts.begin();
  88. l != tgts.end(); l++)
  89. {
  90. // Add a rule to regenerate the build system when the target
  91. // specification source changes.
  92. const char* suppRegenRule =
  93. m_Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  94. if (!cmSystemTools::IsOn(suppRegenRule) &&
  95. (strcmp(l->first.c_str(), CMAKE_CHECK_BUILD_SYSTEM_TARGET) != 0))
  96. {
  97. this->AddVCProjBuildRule(l->second);
  98. }
  99. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  100. // so don't build a projectfile for it
  101. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  102. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  103. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  104. {
  105. cmTarget& target = l->second;
  106. target.TraceVSDependencies(target.GetName(), m_Makefile);
  107. }
  108. }
  109. // now for all custom commands that are not used directly in a
  110. // target, add them to all targets in the current directory or
  111. // makefile
  112. std::vector<cmSourceFile*> & classesmf = m_Makefile->GetSourceFiles();
  113. for(std::vector<cmSourceFile*>::const_iterator i = classesmf.begin();
  114. i != classesmf.end(); i++)
  115. {
  116. if(cmCustomCommand* cc = (*i)->GetCustomCommand())
  117. {
  118. // while we are at it, if it is a .rule file then for visual studio 7 we
  119. // must generate it so that depend information works correctly
  120. if ((*i)->GetSourceExtension() == "rule")
  121. {
  122. std::string source = (*i)->GetFullPath();
  123. if(!cmSystemTools::FileExists(source.c_str()))
  124. {
  125. cmSystemTools::ReplaceString(source, "$(IntDir)/", "");
  126. #if defined(_WIN32) || defined(__CYGWIN__)
  127. std::ofstream fout(source.c_str(),
  128. std::ios::binary | std::ios::out | std::ios::trunc);
  129. #else
  130. std::ofstream fout(source.c_str(),
  131. std::ios::out | std::ios::trunc);
  132. #endif
  133. if(fout)
  134. {
  135. fout.write("# generated from CMake",22);
  136. fout.flush();
  137. fout.close();
  138. }
  139. }
  140. }
  141. if(!cc->IsUsed())
  142. {
  143. for(cmTargets::iterator l = tgts.begin();
  144. l != tgts.end(); l++)
  145. {
  146. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  147. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  148. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0)
  149. && (strcmp(l->first.c_str(), "ALL_BUILD") != 0)
  150. && (strcmp(l->first.c_str(), "RUN_TESTS") != 0)
  151. && (strcmp(l->first.c_str(), "INSTALL") != 0)
  152. && (strcmp(l->first.c_str(), CMAKE_CHECK_BUILD_SYSTEM_TARGET) != 0))
  153. {
  154. cmTarget& target = l->second;
  155. bool sameAsTarget = false;
  156. // make sure we don't add a custom command that depends on
  157. // this target
  158. for(unsigned int k =0; k < cc->GetDepends().size(); k++)
  159. {
  160. if(cmSystemTools::GetFilenameName(cc->GetDepends()[k]) == target.GetFullName())
  161. {
  162. sameAsTarget = true;
  163. }
  164. }
  165. if(!sameAsTarget)
  166. {
  167. target.GetSourceFiles().push_back(*i);
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. #endif
  175. for(cmTargets::iterator l = tgts.begin();
  176. l != tgts.end(); l++)
  177. {
  178. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  179. // so don't build a projectfile for it
  180. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  181. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  182. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  183. {
  184. this->CreateSingleVCProj(l->first.c_str(),l->second);
  185. }
  186. }
  187. }
  188. void cmLocalVisualStudio7Generator::CreateSingleVCProj(const char *lname, cmTarget &target)
  189. {
  190. // add to the list of projects
  191. std::string pname = lname;
  192. m_CreatedProjectNames.push_back(pname);
  193. // create the dsp.cmake file
  194. std::string fname;
  195. fname = m_Makefile->GetStartOutputDirectory();
  196. fname += "/";
  197. fname += lname;
  198. fname += ".vcproj";
  199. // save the name of the real dsp file
  200. std::string realVCProj = fname;
  201. fname += ".cmake";
  202. std::ofstream fout(fname.c_str());
  203. if(!fout)
  204. {
  205. cmSystemTools::Error("Error Writing ", fname.c_str());
  206. }
  207. this->WriteVCProjFile(fout,lname,target);
  208. fout.close();
  209. // if the dsp file has changed, then write it.
  210. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realVCProj.c_str());
  211. }
  212. void cmLocalVisualStudio7Generator::AddVCProjBuildRule(cmTarget& tgt)
  213. {
  214. std::string dspname = tgt.GetName();
  215. dspname += ".vcproj.cmake";
  216. const char* dsprule = m_Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  217. cmCustomCommandLine commandLine;
  218. commandLine.push_back(dsprule);
  219. std::string makefileIn = m_Makefile->GetStartDirectory();
  220. makefileIn += "/";
  221. makefileIn += "CMakeLists.txt";
  222. std::string args;
  223. args = "-H";
  224. args +=
  225. this->Convert(m_Makefile->GetHomeDirectory(), START_OUTPUT, SHELL, true);
  226. commandLine.push_back(args);
  227. args = "-B";
  228. args +=
  229. this->Convert(m_Makefile->GetHomeOutputDirectory(),
  230. START_OUTPUT, SHELL, true);
  231. commandLine.push_back(args);
  232. std::string configFile =
  233. m_Makefile->GetRequiredDefinition("CMAKE_ROOT");
  234. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  235. std::vector<std::string> listFiles = m_Makefile->GetListFiles();
  236. bool found = false;
  237. for(std::vector<std::string>::iterator i = listFiles.begin();
  238. i != listFiles.end(); ++i)
  239. {
  240. if(*i == configFile)
  241. {
  242. found = true;
  243. }
  244. }
  245. if(!found)
  246. {
  247. listFiles.push_back(configFile);
  248. }
  249. cmCustomCommandLines commandLines;
  250. commandLines.push_back(commandLine);
  251. const char* no_working_directory = 0;
  252. const char* no_comment = 0;
  253. m_Makefile->AddCustomCommandToOutput(dspname.c_str(), listFiles, makefileIn.c_str(),
  254. commandLines, no_comment, no_working_directory, true);
  255. if(cmSourceFile* file = m_Makefile->GetSource(makefileIn.c_str()))
  256. {
  257. tgt.GetSourceFiles().push_back(file);
  258. }
  259. else
  260. {
  261. cmSystemTools::Error("Error adding rule for ", makefileIn.c_str());
  262. }
  263. }
  264. void cmLocalVisualStudio7Generator::WriteConfigurations(std::ostream& fout,
  265. const char *libName,
  266. cmTarget &target)
  267. {
  268. std::vector<std::string> *configs =
  269. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator)->GetConfigurations();
  270. fout << "\t<Configurations>\n";
  271. for( std::vector<std::string>::iterator i = configs->begin();
  272. i != configs->end(); ++i)
  273. {
  274. this->WriteConfiguration(fout, i->c_str(), libName, target);
  275. }
  276. fout << "\t</Configurations>\n";
  277. }
  278. // This is a table mapping XML tag IDE names to command line options
  279. struct cmVS7FlagTable
  280. {
  281. const char* IDEName; // name used in the IDE xml file
  282. const char* commandFlag; // command line flag
  283. const char* comment; // comment
  284. const char* value; // string value
  285. };
  286. // fill the table here
  287. // currently the comment field is not used for anything other than documentation
  288. // NOTE: Make sure the longer commandFlag comes FIRST!
  289. cmVS7FlagTable cmLocalVisualStudio7GeneratorFlagTable[] =
  290. {
  291. // option flags (some flags map to the same option)
  292. {"BasicRuntimeChecks", "GZ", "Stack frame checks", "1"},
  293. {"BasicRuntimeChecks", "RTCsu", "Both stack and uninitialized checks ", "3"},
  294. {"BasicRuntimeChecks", "RTCs", "Stack frame checks", "1"},
  295. {"BasicRuntimeChecks", "RTCu", "Uninitialized Variables ", "2"},
  296. {"BasicRuntimeChecks", "RTC1", "Both stack and uninitialized checks ", "3"},
  297. {"DebugInformationFormat", "Z7", "debug format", "1"},
  298. {"DebugInformationFormat", "Zd", "debug format", "2"},
  299. {"DebugInformationFormat", "Zi", "debug format", "3"},
  300. {"DebugInformationFormat", "ZI", "debug format", "4"},
  301. {"EnableEnhancedInstructionSet", "arch:SSE2", "Use sse2 instructions", "2"},
  302. {"EnableEnhancedInstructionSet", "arch:SSE", "Use sse instructions", "1"},
  303. {"FavorSizeOrSpeed", "Ot", "Favor fast code", "1"},
  304. {"FavorSizeOrSpeed", "Os", "Favor small code", "2"},
  305. {"Optimization", "Od", "Non Debug", "0"},
  306. {"Optimization", "O1", "Min Size", "1"},
  307. {"Optimization", "O2", "Max Speed", "2"},
  308. {"Optimization", "Ox", "Max Optimization", "3"},
  309. {"OptimizeForProcessor", "GB", "Blended processor mode", "0"},
  310. {"OptimizeForProcessor", "G5", "Pentium", "1"},
  311. {"OptimizeForProcessor", "G6", "PPro PII PIII", "2"},
  312. {"OptimizeForProcessor", "G7", "Pentium 4 or Athlon", "3"},
  313. {"InlineFunctionExpansion", "Ob0", "no inlines", "0"},
  314. {"InlineFunctionExpansion", "Ob1", "when inline keyword", "1"},
  315. {"InlineFunctionExpansion", "Ob2", "any time you can inline", "2"},
  316. {"RuntimeLibrary", "MTd", "Multithreded debug", "1"},
  317. {"RuntimeLibrary", "MT", "Multithreded", "0"},
  318. {"RuntimeLibrary", "MDd", "Multithreded dll debug", "3"},
  319. {"RuntimeLibrary", "MD", "Multithreded dll", "2"},
  320. {"RuntimeLibrary", "MLd", "Sinble Thread debug", "5"},
  321. {"RuntimeLibrary", "ML", "Sinble Thread", "4"},
  322. {"StructMemberAlignment", "Zp16", "struct align 16 byte ", "5"},
  323. {"StructMemberAlignment", "Zp1", "struct align 1 byte ", "1"},
  324. {"StructMemberAlignment", "Zp2", "struct align 2 byte ", "2"},
  325. {"StructMemberAlignment", "Zp4", "struct align 4 byte ", "3"},
  326. {"StructMemberAlignment", "Zp8", "struct align 8 byte ", "4"},
  327. {"WarningLevel", "W1", "Warning level", "1"},
  328. {"WarningLevel", "W2", "Warning level", "2"},
  329. {"WarningLevel", "W3", "Warning level", "3"},
  330. {"WarningLevel", "W4", "Warning level", "4"},
  331. // boolean flags
  332. {"BufferSecurityCheck", "GS", "Buffer security check", "TRUE"},
  333. {"EnableFibreSafeOptimization", "GT", "OmitFramePointers", "TRUE"},
  334. {"EnableFunctionLevelLinking", "Gy", "EnableFunctionLevelLinking", "TRUE"},
  335. {"EnableIntrinsicFunctions", "Oi", "EnableIntrinsicFunctions", "TRUE"},
  336. {"ExceptionHandling", "EHsc", "enable c++ exceptions", "TRUE"},
  337. {"ExceptionHandling", "GX", "enable c++ exceptions", "TRUE"},
  338. {"GlobalOptimizations", "Og", "Global Optimize", "TRUE"},
  339. {"ImproveFloatingPointConsistency", "Op", "ImproveFloatingPointConsistency", "TRUE"},
  340. {"MinimalRebuild", "Gm", "minimal rebuild", "TRUE"},
  341. {"OmitFramePointers", "Oy", "OmitFramePointers", "TRUE"},
  342. {"OptimizeForWindowsApplication", "GA", "Optimize for windows", "TRUE"},
  343. {"RuntimeTypeInfo", "GR", "Turn on Run time type information for c++", "TRUE"},
  344. {"SmallerTypeCheck", "RTCc", "smaller type check", "TRUE"},
  345. {"SuppressStartupBanner", "nologo", "SuppressStartupBanner", "TRUE"},
  346. {"WarnAsError", "WX", "Treat warnings as errors", "TRUE"},
  347. {0,0,0,0 }
  348. };
  349. cmVS7FlagTable cmLocalVisualStudio7GeneratorLinkFlagTable[] =
  350. {
  351. // option flags (some flags map to the same option)
  352. {"LinkIncremental", "INCREMENTAL:NO", "link incremental", "1"},
  353. {"LinkIncremental", "INCREMENTAL:YES", "link incremental", "2"},
  354. {0,0,0,0 }
  355. };
  356. void cmLocalVisualStudio7Generator::WriteConfiguration(std::ostream& fout,
  357. const char* configName,
  358. const char *libName,
  359. cmTarget &target)
  360. {
  361. // create a map of xml tags to the values they should have in the output
  362. // for example, "BufferSecurityCheck" = "TRUE"
  363. // first fill this table with the values for the configuration
  364. // Debug, Release, etc,
  365. // Then parse the command line flags specified in CMAKE_CXX_FLAGS
  366. // and CMAKE_C_FLAGS
  367. // and overwrite or add new values to this map
  368. std::map<cmStdString, cmStdString> flagMap;
  369. // since the default is on for this, but if /EHsc is found
  370. // in the flags it will be turned on and we have /EHSC on by
  371. // default in the CXX flags, then this is the only way to turn this off
  372. flagMap["ExceptionHandling"] = "FALSE";
  373. const char* mfcFlag = m_Makefile->GetDefinition("CMAKE_MFC_FLAG");
  374. if(!mfcFlag)
  375. {
  376. mfcFlag = "0";
  377. }
  378. fout << "\t\t<Configuration\n"
  379. << "\t\t\tName=\"" << configName << "|Win32\"\n"
  380. << "\t\t\tOutputDirectory=\"" << configName << "\"\n";
  381. // This is an internal type to Visual Studio, it seems that:
  382. // 4 == static library
  383. // 2 == dll
  384. // 1 == executable
  385. // 10 == utility
  386. const char* configType = "10";
  387. switch(target.GetType())
  388. {
  389. case cmTarget::STATIC_LIBRARY:
  390. configType = "4";
  391. break;
  392. case cmTarget::SHARED_LIBRARY:
  393. case cmTarget::MODULE_LIBRARY:
  394. configType = "2";
  395. break;
  396. case cmTarget::EXECUTABLE:
  397. configType = "1";
  398. break;
  399. case cmTarget::UTILITY:
  400. configType = "10";
  401. default:
  402. break;
  403. }
  404. std::string flags;
  405. std::string flagsRelease = " ";
  406. std::string flagsMinSize = " ";
  407. std::string flagsDebug = " ";
  408. std::string flagsDebugRel = " ";
  409. if(strcmp(configType, "10") != 0)
  410. {
  411. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  412. if(!linkLanguage)
  413. {
  414. cmSystemTools::Error("CMake can not determine linker language for target:",
  415. target.GetName());
  416. return;
  417. }
  418. if(!(strcmp(linkLanguage, "RC") == 0 || strcmp(linkLanguage, "DEF") == 0))
  419. {
  420. std::string baseFlagVar = "CMAKE_";
  421. baseFlagVar += linkLanguage;
  422. baseFlagVar += "_FLAGS";
  423. flags = m_Makefile->GetRequiredDefinition(baseFlagVar.c_str());
  424. std::string flagVar = baseFlagVar + "_RELEASE";
  425. flagsRelease += m_Makefile->GetRequiredDefinition(flagVar.c_str());
  426. flagVar = baseFlagVar + "_MINSIZEREL";
  427. flagsMinSize += m_Makefile->GetRequiredDefinition(flagVar.c_str());
  428. flagVar = baseFlagVar + "_DEBUG";
  429. flagsDebug += m_Makefile->GetRequiredDefinition(flagVar.c_str());
  430. flagVar = baseFlagVar + "_RELWITHDEBINFO";
  431. flagsDebugRel += m_Makefile->GetRequiredDefinition(flagVar.c_str());
  432. }
  433. }
  434. std::string programDatabase;
  435. const char* pre = "WIN32,_DEBUG,_WINDOWS";
  436. // fill the flagMap for Debug, Release, MinSizeRel, and RelWithDebInfo
  437. // also set the flags, and pre-defined macros
  438. if(strcmp(configName, "Debug") == 0)
  439. {
  440. flagMap["InlineFunctionExpansion"] = "0";
  441. flagMap["Optimization"] = "0";
  442. flagMap["RuntimeLibrary"] = "3";
  443. flags += flagsDebug;
  444. pre = "WIN32,_DEBUG,_WINDOWS";
  445. std::string libpath = m_LibraryOutputPath +
  446. "$(OutDir)/" + libName + ".pdb";
  447. programDatabase = "\t\t\t\tProgramDatabaseFileName=\"";
  448. programDatabase += libpath;
  449. programDatabase += "\"";
  450. }
  451. else if (strcmp(configName, "Release") == 0)
  452. {
  453. flagMap["InlineFunctionExpansion"] = "1";
  454. flagMap["Optimization"] = "2";
  455. flagMap["RuntimeLibrary"] = "2";
  456. flags += flagsRelease;
  457. pre = "WIN32,_WINDOWS";
  458. flags += flagsRelease;
  459. }
  460. else if(strcmp(configName, "MinSizeRel") == 0)
  461. {
  462. flagMap["InlineFunctionExpansion"] = "1";
  463. flagMap["Optimization"] = "1";
  464. flagMap["RuntimeLibrary"] = "2";
  465. pre = "WIN32,_WINDOWS";
  466. flags += flagsMinSize;
  467. }
  468. else if(strcmp(configName, "RelWithDebInfo") == 0)
  469. {
  470. flagMap["InlineFunctionExpansion"] = "1";
  471. flagMap["Optimization"] = "2";
  472. flagMap["RuntimeLibrary"] = "2";
  473. pre = "WIN32,_WINDOWS";
  474. flags += flagsDebugRel;
  475. std::string libpath = m_LibraryOutputPath +
  476. "$(OutDir)/" + libName + ".pdb";
  477. programDatabase = "\t\t\t\tProgramDatabaseFileName=\"";
  478. programDatabase += libpath;
  479. programDatabase += "\"";
  480. }
  481. // Add the target-specific flags.
  482. if(const char* targetFlags = target.GetProperty("COMPILE_FLAGS"))
  483. {
  484. flags += " ";
  485. flags += targetFlags;
  486. }
  487. // The intermediate directory name consists of a directory for the
  488. // target and a subdirectory for the configuration name.
  489. std::string intermediateDir = this->GetTargetDirectory(target);
  490. intermediateDir += "/";
  491. intermediateDir += configName;
  492. fout << "\t\t\tIntermediateDirectory=\""
  493. << this->ConvertToXMLOutputPath(intermediateDir.c_str())
  494. << "\"\n"
  495. << "\t\t\tConfigurationType=\"" << configType << "\"\n"
  496. << "\t\t\tUseOfMFC=\"" << mfcFlag << "\"\n"
  497. << "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n";
  498. // if -D_UNICODE or /D_UNICODE is found in the flags
  499. // change the character set to unicode, if not then
  500. // default to MBCS
  501. std::string defs = m_Makefile->GetDefineFlags();
  502. if(flags.find("D_UNICODE") != flags.npos ||
  503. defs.find("D_UNICODE") != flags.npos)
  504. {
  505. fout << "\t\t\tCharacterSet=\"1\">\n";
  506. }
  507. else
  508. {
  509. fout << "\t\t\tCharacterSet=\"2\">\n";
  510. }
  511. fout << "\t\t\t<Tool\n"
  512. << "\t\t\t\tName=\"VCCLCompilerTool\"\n"
  513. << "\t\t\t\tAdditionalOptions=\"";
  514. // now fill the flagMap from the command line flags, and
  515. // if a flag is used, it will be removed from the flags string by
  516. // this function call
  517. this->FillFlagMapFromCommandFlags(flagMap,
  518. &cmLocalVisualStudio7GeneratorFlagTable[0],
  519. flags);
  520. std::string defineFlags = m_Makefile->GetDefineFlags();
  521. // now check the define flags for flags other than -D and
  522. // put them in the map, the -D flags will be left in the defineFlags
  523. // variable as -D is not in the flagMap
  524. this->FillFlagMapFromCommandFlags(flagMap,
  525. &cmLocalVisualStudio7GeneratorFlagTable[0],
  526. defineFlags);
  527. // output remaining flags that were not mapped to anything
  528. fout << this->EscapeForXML(flags.c_str()).c_str();
  529. fout << " -DCMAKE_INTDIR=\\&quot;" << configName << "\\&quot;"
  530. << "\"\n";
  531. fout << "\t\t\t\tAdditionalIncludeDirectories=\"";
  532. std::vector<std::string> includes;
  533. this->GetIncludeDirectories(includes);
  534. std::vector<std::string>::iterator i = includes.begin();
  535. for(;i != includes.end(); ++i)
  536. {
  537. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  538. fout << ipath << ";";
  539. }
  540. fout << "\"\n";
  541. // set a few cmake specific flags
  542. if(m_Makefile->IsOn("CMAKE_CXX_USE_RTTI"))
  543. {
  544. flagMap["RuntimeTypeInfo"] = "TRUE";
  545. }
  546. if ( m_Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL") )
  547. {
  548. flagMap["WarningLevel"] = m_Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL");
  549. }
  550. // Now copy the flag map into the xml for the file
  551. for(std::map<cmStdString, cmStdString>::iterator m = flagMap.begin();
  552. m != flagMap.end(); ++m)
  553. {
  554. fout << "\t\t\t\t" << m->first << "=\"" << m->second << "\"\n";
  555. }
  556. fout << "\t\t\t\tPreprocessorDefinitions=\"" << pre;
  557. if(target.GetType() == cmTarget::SHARED_LIBRARY
  558. || target.GetType() == cmTarget::MODULE_LIBRARY)
  559. {
  560. std::string exportSymbol;
  561. if (const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  562. {
  563. exportSymbol = custom_export_name;
  564. }
  565. else
  566. {
  567. std::string id = libName;
  568. id += "_EXPORTS";
  569. exportSymbol = cmSystemTools::MakeCindentifier(id.c_str());
  570. }
  571. fout << "," << exportSymbol;
  572. }
  573. this->OutputDefineFlags(defineFlags.c_str(), fout);
  574. fout << "\"\n";
  575. fout << "\t\t\t\tAssemblerListingLocation=\"" << configName << "\"\n";
  576. fout << "\t\t\t\tObjectFile=\"$(IntDir)\\\"\n";
  577. if(programDatabase.size())
  578. {
  579. fout << programDatabase << "\n";
  580. }
  581. fout << "/>\n"; // end of <Tool Name=VCCLCompilerTool
  582. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCCustomBuildTool\"/>\n";
  583. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCResourceCompilerTool\"\n"
  584. << "\t\t\t\tAdditionalIncludeDirectories=\"";
  585. for(i = includes.begin();i != includes.end(); ++i)
  586. {
  587. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  588. fout << ipath << ";";
  589. }
  590. // add the -D flags to the RC tool
  591. fout << "\"\n"
  592. << "\t\t\t\tPreprocessorDefinitions=\"" << pre;
  593. this->OutputDefineFlags(defineFlags.c_str(), fout);
  594. fout << "\" />\n";
  595. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCMIDLTool\"\n";
  596. fout << "\t\t\t\tPreprocessorDefinitions=\"" << pre;
  597. this->OutputDefineFlags(defineFlags.c_str(), fout);
  598. fout << "\"\n";
  599. fout << "\t\t\t\tMkTypLibCompatible=\"FALSE\"\n";
  600. fout << "\t\t\t\tTargetEnvironment=\"1\"\n";
  601. fout << "\t\t\t\tGenerateStublessProxies=\"TRUE\"\n";
  602. fout << "\t\t\t\tTypeLibraryName=\"$(InputName).tlb\"\n";
  603. fout << "\t\t\t\tOutputDirectory=\"$(IntDir)\"\n";
  604. fout << "\t\t\t\tHeaderFileName=\"$(InputName).h\"\n";
  605. fout << "\t\t\t\tDLLDataFileName=\"\"\n";
  606. fout << "\t\t\t\tInterfaceIdentifierFileName=\"$(InputName)_i.c\"\n";
  607. fout << "\t\t\t\tProxyFileName=\"$(InputName)_p.c\"/>\n";
  608. // end of <Tool Name=VCMIDLTool
  609. this->OutputTargetRules(fout, target, libName);
  610. this->OutputBuildTool(fout, configName, libName, target);
  611. fout << "\t\t</Configuration>\n";
  612. }
  613. void cmLocalVisualStudio7Generator::FillFlagMapFromCommandFlags(
  614. std::map<cmStdString, cmStdString>& flagMap,
  615. cmVS7FlagTable* flagTable,
  616. std::string& flags)
  617. {
  618. std::string replace;
  619. std::string option;
  620. while(flagTable->IDEName)
  621. {
  622. option.reserve(strlen(flagTable->commandFlag)+2);
  623. // first do the - version
  624. option = "-";
  625. option += flagTable->commandFlag;
  626. while(flags.find(option) != flags.npos)
  627. {
  628. // replace the flag
  629. cmSystemTools::ReplaceString(flags, option.c_str(), "");
  630. // now put value into flag map
  631. flagMap[flagTable->IDEName] = flagTable->value;
  632. }
  633. // now do the / version
  634. option[0] = '/';
  635. while(flags.find(option) != flags.npos)
  636. {
  637. // replace the flag
  638. cmSystemTools::ReplaceString(flags, option.c_str(), "");
  639. // now put value into flag map
  640. flagMap[flagTable->IDEName] = flagTable->value;
  641. }
  642. // move to next flag
  643. flagTable++;
  644. }
  645. // If verbose makefiles have been requested and the /nologo option
  646. // was not given explicitly in the flags we want to add an attribute
  647. // to the generated project to disable logo suppression. Otherwise
  648. // the GUI default is to enable suppression.
  649. if(m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  650. {
  651. if(flagMap.find("SuppressStartupBanner") == flagMap.end())
  652. {
  653. flagMap["SuppressStartupBanner"] = "FALSE";
  654. }
  655. }
  656. }
  657. //----------------------------------------------------------------------------
  658. std::string
  659. cmLocalVisualStudio7Generator
  660. ::GetBuildTypeLinkerFlags(std::string rootLinkerFlags, const char* configName)
  661. {
  662. std::string configTypeUpper = cmSystemTools::UpperCase(configName);
  663. std::string extraLinkOptionsBuildTypeDef = rootLinkerFlags + "_" + configTypeUpper;
  664. std::string extraLinkOptionsBuildType =
  665. m_Makefile->GetRequiredDefinition(extraLinkOptionsBuildTypeDef.c_str());
  666. return extraLinkOptionsBuildType;
  667. }
  668. void cmLocalVisualStudio7Generator::OutputBuildTool(std::ostream& fout,
  669. const char* configName,
  670. const char *libName,
  671. cmTarget &target)
  672. {
  673. std::string targetFullName = target.GetFullName(configName);
  674. std::string temp;
  675. std::string extraLinkOptions;
  676. if(target.GetType() == cmTarget::EXECUTABLE)
  677. {
  678. extraLinkOptions = m_Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS") +
  679. std::string(" ") + GetBuildTypeLinkerFlags("CMAKE_EXE_LINKER_FLAGS", configName);
  680. }
  681. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  682. {
  683. extraLinkOptions = m_Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS") +
  684. std::string(" ") + GetBuildTypeLinkerFlags("CMAKE_SHARED_LINKER_FLAGS", configName);
  685. }
  686. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  687. {
  688. extraLinkOptions = m_Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS") +
  689. std::string(" ") + GetBuildTypeLinkerFlags("CMAKE_MODULE_LINKER_FLAGS", configName);
  690. }
  691. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  692. if(targetLinkFlags)
  693. {
  694. extraLinkOptions += " ";
  695. extraLinkOptions += targetLinkFlags;
  696. }
  697. std::map<cmStdString, cmStdString> flagMap;
  698. this->
  699. FillFlagMapFromCommandFlags(flagMap,
  700. &cmLocalVisualStudio7GeneratorLinkFlagTable[0],
  701. extraLinkOptions);
  702. switch(target.GetType())
  703. {
  704. case cmTarget::STATIC_LIBRARY:
  705. {
  706. std::string libpath = m_LibraryOutputPath +
  707. "$(OutDir)/" + targetFullName;
  708. fout << "\t\t\t<Tool\n"
  709. << "\t\t\t\tName=\"VCLibrarianTool\"\n";
  710. if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
  711. {
  712. fout << "\t\t\t\tAdditionalOptions=\"" << libflags << "\"\n";
  713. }
  714. fout << "\t\t\t\tOutputFile=\""
  715. << this->ConvertToXMLOutputPathSingle(libpath.c_str()) << ".\"/>\n";
  716. break;
  717. }
  718. case cmTarget::SHARED_LIBRARY:
  719. case cmTarget::MODULE_LIBRARY:
  720. {
  721. // Compute the link library and directory information.
  722. std::vector<cmStdString> linkLibs;
  723. std::vector<cmStdString> linkDirs;
  724. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  725. fout << "\t\t\t<Tool\n"
  726. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  727. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386";
  728. if(extraLinkOptions.size())
  729. {
  730. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  731. extraLinkOptions.c_str()).c_str();
  732. }
  733. // Use the NOINHERIT macro to avoid getting VS project default
  734. // libraries which may be set by the user to something bad.
  735. fout << "\"\n"
  736. << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
  737. << m_Makefile->GetRequiredDefinition("CMAKE_STANDARD_LIBRARIES")
  738. << " ";
  739. this->OutputLibraries(fout, linkLibs);
  740. fout << "\"\n";
  741. temp = m_LibraryOutputPath;
  742. temp += configName;
  743. temp += "/";
  744. temp += targetFullName;
  745. fout << "\t\t\t\tOutputFile=\""
  746. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  747. for(std::map<cmStdString, cmStdString>::iterator i = flagMap.begin();
  748. i != flagMap.end(); ++i)
  749. {
  750. fout << "\t\t\t\t" << i->first << "=\"" << i->second << "\"\n";
  751. }
  752. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  753. this->OutputLibraryDirectories(fout, linkDirs);
  754. fout << "\"\n";
  755. this->OutputModuleDefinitionFile(fout, target);
  756. temp = m_LibraryOutputPath;
  757. temp += "$(OutDir)/";
  758. temp += libName;
  759. temp += ".pdb";
  760. fout << "\t\t\t\tProgramDatabaseFile=\"" <<
  761. this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  762. if(strcmp(configName, "Debug") == 0
  763. || strcmp(configName, "RelWithDebInfo") == 0)
  764. {
  765. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  766. }
  767. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  768. if(!linkLanguage)
  769. {
  770. cmSystemTools::Error("CMake can not determine linker language for target:",
  771. target.GetName());
  772. return;
  773. }
  774. std::string stackVar = "CMAKE_";
  775. stackVar += linkLanguage;
  776. stackVar += "_STACK_SIZE";
  777. const char* stackVal = m_Makefile->GetDefinition(stackVar.c_str());
  778. if(stackVal)
  779. {
  780. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"\n";
  781. }
  782. temp = m_LibraryOutputPath;
  783. temp += configName;
  784. temp += "/";
  785. temp += cmSystemTools::GetFilenameWithoutLastExtension(targetFullName.c_str());
  786. temp += ".lib";
  787. fout << "\t\t\t\tImportLibrary=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
  788. }
  789. break;
  790. case cmTarget::EXECUTABLE:
  791. {
  792. // Compute the link library and directory information.
  793. std::vector<cmStdString> linkLibs;
  794. std::vector<cmStdString> linkDirs;
  795. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  796. fout << "\t\t\t<Tool\n"
  797. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  798. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386";
  799. if(extraLinkOptions.size())
  800. {
  801. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  802. extraLinkOptions.c_str()).c_str();
  803. }
  804. // Use the NOINHERIT macro to avoid getting VS project default
  805. // libraries which may be set by the user to something bad.
  806. fout << "\"\n"
  807. << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
  808. << m_Makefile->GetRequiredDefinition("CMAKE_STANDARD_LIBRARIES")
  809. << " ";
  810. this->OutputLibraries(fout, linkLibs);
  811. fout << "\"\n";
  812. temp = m_ExecutableOutputPath;
  813. temp += configName;
  814. temp += "/";
  815. temp += targetFullName;
  816. fout << "\t\t\t\tOutputFile=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  817. for(std::map<cmStdString, cmStdString>::iterator i = flagMap.begin();
  818. i != flagMap.end(); ++i)
  819. {
  820. fout << "\t\t\t\t" << i->first << "=\"" << i->second << "\"\n";
  821. }
  822. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  823. this->OutputLibraryDirectories(fout, linkDirs);
  824. fout << "\"\n";
  825. fout << "\t\t\t\tProgramDatabaseFile=\"" << m_LibraryOutputPath
  826. << "$(OutDir)\\" << libName << ".pdb\"\n";
  827. if(strcmp(configName, "Debug") == 0
  828. || strcmp(configName, "RelWithDebInfo") == 0)
  829. {
  830. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  831. }
  832. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  833. {
  834. fout << "\t\t\t\tSubSystem=\"2\"\n";
  835. }
  836. else
  837. {
  838. fout << "\t\t\t\tSubSystem=\"1\"\n";
  839. }
  840. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  841. if(!linkLanguage)
  842. {
  843. cmSystemTools::Error("CMake can not determine linker language for target:",
  844. target.GetName());
  845. return;
  846. }
  847. std::string stackVar = "CMAKE_";
  848. stackVar += linkLanguage;
  849. stackVar += "_STACK_SIZE";
  850. const char* stackVal = m_Makefile->GetDefinition(stackVar.c_str());
  851. if(stackVal)
  852. {
  853. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"";
  854. }
  855. fout << "/>\n";
  856. break;
  857. }
  858. case cmTarget::UTILITY:
  859. break;
  860. }
  861. }
  862. void cmLocalVisualStudio7Generator::OutputModuleDefinitionFile(std::ostream& fout,
  863. cmTarget &target)
  864. {
  865. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  866. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  867. i != classes.end(); i++)
  868. {
  869. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  870. {
  871. fout << "\t\t\t\tModuleDefinitionFile=\""
  872. << this->ConvertToXMLOutputPath((*i)->GetFullPath().c_str())
  873. << "\"\n";
  874. return;
  875. }
  876. }
  877. }
  878. //----------------------------------------------------------------------------
  879. void
  880. cmLocalVisualStudio7Generator
  881. ::OutputLibraries(std::ostream& fout,
  882. std::vector<cmStdString> const& libs)
  883. {
  884. for(std::vector<cmStdString>::const_iterator l = libs.begin();
  885. l != libs.end(); ++l)
  886. {
  887. fout << this->ConvertToXMLOutputPath(l->c_str()) << " ";
  888. }
  889. }
  890. //----------------------------------------------------------------------------
  891. void
  892. cmLocalVisualStudio7Generator
  893. ::OutputLibraryDirectories(std::ostream& fout,
  894. std::vector<cmStdString> const& dirs)
  895. {
  896. const char* comma = "";
  897. for(std::vector<cmStdString>::const_iterator d = dirs.begin();
  898. d != dirs.end(); ++d)
  899. {
  900. std::string dir = *d;
  901. if(!dir.empty())
  902. {
  903. if(dir[dir.size()-1] != '/')
  904. {
  905. dir += "/";
  906. }
  907. dir += "$(OutDir)";
  908. fout << comma << this->ConvertToXMLOutputPath(dir.c_str())
  909. << "," << this->ConvertToXMLOutputPath(d->c_str());
  910. comma = ",";
  911. }
  912. }
  913. }
  914. //----------------------------------------------------------------------------
  915. void cmLocalVisualStudio7Generator::OutputDefineFlags(const char* flags,
  916. std::ostream& fout)
  917. {
  918. std::string defs = flags;
  919. cmSystemTools::ReplaceString(defs, "/D","-D");
  920. std::string::size_type pos = defs.find("-D");
  921. bool done = pos == std::string::npos;
  922. if(!done)
  923. {
  924. fout << ",";
  925. }
  926. while(!done)
  927. {
  928. std::string::size_type nextpos = defs.find("-D", pos+2);
  929. std::string define;
  930. if(nextpos != std::string::npos)
  931. {
  932. define = defs.substr(pos+2, nextpos - pos -3);
  933. }
  934. else
  935. {
  936. define = defs.substr(pos+2);
  937. done = true;
  938. }
  939. // Double-quotes in the value of the definition must be escaped
  940. // with a backslash. The entire definition should be quoted in
  941. // the generated xml attribute to avoid confusing the VS parser.
  942. cmSystemTools::ReplaceString(define, "\"", "\\&quot;");
  943. fout << "&quot;" << define << "&quot;,";
  944. if(!done)
  945. {
  946. pos = defs.find("-D", nextpos);
  947. }
  948. }
  949. }
  950. void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
  951. const char *libName,
  952. cmTarget &target)
  953. {
  954. // get the configurations
  955. std::vector<std::string> *configs =
  956. static_cast<cmGlobalVisualStudio7Generator *>
  957. (m_GlobalGenerator)->GetConfigurations();
  958. // trace the visual studio dependencies
  959. std::string name = libName;
  960. name += ".vcproj.cmake";
  961. // We may be modifying the source groups temporarily, so make a copy.
  962. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  963. // get the classes from the source lists then add them to the groups
  964. std::vector<cmSourceFile*> & classes = target.GetSourceFiles();
  965. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  966. i != classes.end(); i++)
  967. {
  968. // Add the file to the list of sources.
  969. std::string source = (*i)->GetFullPath();
  970. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  971. {
  972. m_ModuleDefinitionFile = (*i)->GetFullPath();
  973. }
  974. cmSourceGroup& sourceGroup =
  975. m_Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  976. sourceGroup.AssignSource(*i);
  977. }
  978. // open the project
  979. this->WriteProjectStart(fout, libName, target, sourceGroups);
  980. // write the configuration information
  981. this->WriteConfigurations(fout, libName, target);
  982. fout << "\t<Files>\n";
  983. // Loop through every source group.
  984. for(unsigned int i = 0; i < sourceGroups.size(); ++i)
  985. {
  986. cmSourceGroup sg = sourceGroups[i];
  987. this->WriteGroup(&sg, target, fout, libName, configs);
  988. }
  989. //}
  990. fout << "\t</Files>\n";
  991. // Write the VCProj file's footer.
  992. this->WriteVCProjFooter(fout);
  993. }
  994. void cmLocalVisualStudio7Generator::WriteGroup(const cmSourceGroup *sg, cmTarget target, std::ostream &fout, const char *libName, std::vector<std::string> *configs)
  995. {
  996. const std::vector<const cmSourceFile *> &sourceFiles =
  997. sg->GetSourceFiles();
  998. // If the group is empty, don't write it at all.
  999. if(sourceFiles.empty() && sg->GetGroupChildren().empty())
  1000. {
  1001. return;
  1002. }
  1003. // If the group has a name, write the header.
  1004. std::string name = sg->GetName();
  1005. if(name != "")
  1006. {
  1007. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  1008. }
  1009. // Loop through each source in the source group.
  1010. for(std::vector<const cmSourceFile *>::const_iterator sf =
  1011. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  1012. {
  1013. std::string source = (*sf)->GetFullPath();
  1014. const cmCustomCommand *command = (*sf)->GetCustomCommand();
  1015. std::string compileFlags;
  1016. std::string additionalDeps;
  1017. // Add per-source flags.
  1018. const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS");
  1019. if(cflags)
  1020. {
  1021. compileFlags += " ";
  1022. compileFlags += cflags;
  1023. }
  1024. const char* lang =
  1025. m_GlobalGenerator->GetLanguageFromExtension((*sf)->GetSourceExtension().c_str());
  1026. if(lang && strcmp(lang, "CXX") == 0)
  1027. {
  1028. // force a C++ file type
  1029. compileFlags += " /TP ";
  1030. }
  1031. // Check for extra object-file dependencies.
  1032. const char* deps = (*sf)->GetProperty("OBJECT_DEPENDS");
  1033. if(deps)
  1034. {
  1035. std::vector<std::string> depends;
  1036. cmSystemTools::ExpandListArgument(deps, depends);
  1037. if(!depends.empty())
  1038. {
  1039. std::vector<std::string>::iterator i = depends.begin();
  1040. additionalDeps = this->ConvertToXMLOutputPath(i->c_str());
  1041. for(++i;i != depends.end(); ++i)
  1042. {
  1043. additionalDeps += ";";
  1044. additionalDeps += this->ConvertToXMLOutputPath(i->c_str());
  1045. }
  1046. }
  1047. }
  1048. if (source != libName || target.GetType() == cmTarget::UTILITY)
  1049. {
  1050. fout << "\t\t\t<File\n";
  1051. std::string d = this->ConvertToXMLOutputPathSingle(source.c_str());
  1052. // Tell MS-Dev what the source is. If the compiler knows how to
  1053. // build it, then it will.
  1054. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  1055. if (command)
  1056. {
  1057. // Construct the entire set of commands in one string.
  1058. std::string script = this->ConstructScript(command->GetCommandLines(),
  1059. command->GetWorkingDirectory());
  1060. const char* comment = command->GetComment();
  1061. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  1062. this->WriteCustomRule(fout, source.c_str(), script.c_str(),
  1063. (*comment?comment:"Custom Rule"),
  1064. command->GetDepends(),
  1065. command->GetOutput(), flags);
  1066. }
  1067. else if(compileFlags.size() || additionalDeps.length())
  1068. {
  1069. const char* aCompilerTool = "VCCLCompilerTool";
  1070. std::string ext = (*sf)->GetSourceExtension();
  1071. ext = cmSystemTools::LowerCase(ext);
  1072. if(ext == "idl")
  1073. {
  1074. aCompilerTool = "VCMIDLTool";
  1075. }
  1076. if(ext == "rc")
  1077. {
  1078. aCompilerTool = "VCResourceCompilerTool";
  1079. }
  1080. if(ext == "def")
  1081. {
  1082. aCompilerTool = "VCCustomBuildTool";
  1083. }
  1084. for(std::vector<std::string>::iterator i = configs->begin();
  1085. i != configs->end(); ++i)
  1086. {
  1087. fout << "\t\t\t\t<FileConfiguration\n"
  1088. << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n"
  1089. << "\t\t\t\t\t<Tool\n"
  1090. << "\t\t\t\t\tName=\"" << aCompilerTool << "\"\n";
  1091. if(compileFlags.size())
  1092. {
  1093. fout << "\t\t\t\t\tAdditionalOptions=\""
  1094. << this->EscapeForXML(compileFlags.c_str()) << "\"\n";
  1095. }
  1096. if(additionalDeps.length())
  1097. {
  1098. fout << "\t\t\t\t\tAdditionalDependencies=\""
  1099. << additionalDeps.c_str() << "\"\n";
  1100. }
  1101. fout << "\t\t\t\t\t/>\n"
  1102. << "\t\t\t\t</FileConfiguration>\n";
  1103. }
  1104. }
  1105. fout << "\t\t\t</File>\n";
  1106. }
  1107. }
  1108. std::vector<cmSourceGroup> children = sg->GetGroupChildren();
  1109. for(unsigned int i=0;i<children.size();++i)
  1110. {
  1111. this->WriteGroup(&children[i], target, fout, libName, configs);
  1112. }
  1113. // If the group has a name, write the footer.
  1114. if(name != "")
  1115. {
  1116. this->WriteVCProjEndGroup(fout);
  1117. }
  1118. }
  1119. void cmLocalVisualStudio7Generator::
  1120. WriteCustomRule(std::ostream& fout,
  1121. const char* source,
  1122. const char* command,
  1123. const char* comment,
  1124. const std::vector<std::string>& depends,
  1125. const char *output,
  1126. const char* compileFlags)
  1127. {
  1128. std::vector<std::string>::iterator i;
  1129. std::vector<std::string> *configs =
  1130. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator)->GetConfigurations();
  1131. for(i = configs->begin(); i != configs->end(); ++i)
  1132. {
  1133. fout << "\t\t\t\t<FileConfiguration\n";
  1134. fout << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n";
  1135. if(compileFlags)
  1136. {
  1137. fout << "\t\t\t\t\t<Tool\n"
  1138. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  1139. << "\t\t\t\t\tAdditionalOptions=\""
  1140. << this->EscapeForXML(compileFlags) << "\"/>\n";
  1141. }
  1142. fout << "\t\t\t\t\t<Tool\n"
  1143. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  1144. << "\t\t\t\t\tDescription=\"Building " << comment;
  1145. fout << " " << output;
  1146. fout << "\"\n"
  1147. << "\t\t\t\t\tCommandLine=\""
  1148. << this->EscapeForXML(command) << "\"\n"
  1149. << "\t\t\t\t\tAdditionalDependencies=\"";
  1150. // Write out the dependencies for the rule.
  1151. std::string temp;
  1152. for(std::vector<std::string>::const_iterator d = depends.begin();
  1153. d != depends.end(); ++d)
  1154. {
  1155. // Lookup the real name of the dependency in case it is a CMake target.
  1156. std::string dep = this->GetRealDependency(d->c_str(), i->c_str());
  1157. fout << this->ConvertToXMLOutputPath(dep.c_str())
  1158. << ";";
  1159. }
  1160. fout << "\"\n";
  1161. fout << "\t\t\t\t\tOutputs=\"";
  1162. if(output == 0)
  1163. {
  1164. fout << source << "_force";
  1165. }
  1166. // Write a rule for the output generated by this command.
  1167. fout << this->ConvertToXMLOutputPathSingle(output);
  1168. fout << "\"/>\n";
  1169. fout << "\t\t\t\t</FileConfiguration>\n";
  1170. }
  1171. }
  1172. void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
  1173. const char* group,
  1174. const char* )
  1175. {
  1176. fout << "\t\t<Filter\n"
  1177. << "\t\t\tName=\"" << group << "\"\n"
  1178. << "\t\t\tFilter=\"\">\n";
  1179. }
  1180. void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
  1181. {
  1182. fout << "\t\t</Filter>\n";
  1183. }
  1184. // look for custom rules on a target and collect them together
  1185. void cmLocalVisualStudio7Generator::OutputTargetRules(std::ostream& fout,
  1186. cmTarget &target,
  1187. const char * /*libName*/)
  1188. {
  1189. if (target.GetType() > cmTarget::UTILITY)
  1190. {
  1191. return;
  1192. }
  1193. // add the pre build rules
  1194. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"";
  1195. bool init = false;
  1196. for (std::vector<cmCustomCommand>::const_iterator cr =
  1197. target.GetPreBuildCommands().begin();
  1198. cr != target.GetPreBuildCommands().end(); ++cr)
  1199. {
  1200. if(!init)
  1201. {
  1202. fout << "\nCommandLine=\"";
  1203. init = true;
  1204. }
  1205. std::string script = this->ConstructScript(cr->GetCommandLines(), cr->GetWorkingDirectory());
  1206. fout << this->EscapeForXML(script.c_str()).c_str();
  1207. }
  1208. if (init)
  1209. {
  1210. fout << "\"";
  1211. }
  1212. fout << "/>\n";
  1213. // add the pre Link rules
  1214. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreLinkEventTool\"";
  1215. init = false;
  1216. for (std::vector<cmCustomCommand>::const_iterator cr =
  1217. target.GetPreLinkCommands().begin();
  1218. cr != target.GetPreLinkCommands().end(); ++cr)
  1219. {
  1220. if(!init)
  1221. {
  1222. fout << "\nCommandLine=\"";
  1223. init = true;
  1224. }
  1225. std::string script = this->ConstructScript(cr->GetCommandLines(),
  1226. cr->GetWorkingDirectory());
  1227. fout << this->EscapeForXML(script.c_str()).c_str();
  1228. }
  1229. if (init)
  1230. {
  1231. fout << "\"";
  1232. }
  1233. fout << "/>\n";
  1234. // add the PostBuild rules
  1235. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  1236. init = false;
  1237. for (std::vector<cmCustomCommand>::const_iterator cr =
  1238. target.GetPostBuildCommands().begin();
  1239. cr != target.GetPostBuildCommands().end(); ++cr)
  1240. {
  1241. if(!init)
  1242. {
  1243. fout << "\nCommandLine=\"";
  1244. init = true;
  1245. }
  1246. std::string script = this->ConstructScript(cr->GetCommandLines(), cr->GetWorkingDirectory());
  1247. fout << this->EscapeForXML(script.c_str()).c_str();
  1248. }
  1249. if (init)
  1250. {
  1251. fout << "\"";
  1252. }
  1253. fout << "/>\n";
  1254. }
  1255. void
  1256. cmLocalVisualStudio7Generator::WriteProjectStart(std::ostream& fout,
  1257. const char *libName,
  1258. cmTarget & target,
  1259. std::vector<cmSourceGroup> &)
  1260. {
  1261. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  1262. << "<VisualStudioProject\n"
  1263. << "\tProjectType=\"Visual C++\"\n";
  1264. if(m_Version == 71)
  1265. {
  1266. fout << "\tVersion=\"7.10\"\n";
  1267. }
  1268. else
  1269. {
  1270. if (m_Version == 8)
  1271. {
  1272. fout << "\tVersion=\"8.00\"\n";
  1273. }
  1274. else
  1275. {
  1276. fout << "\tVersion=\"7.00\"\n";
  1277. }
  1278. }
  1279. const char* projLabel = target.GetProperty("PROJECT_LABEL");
  1280. if(!projLabel)
  1281. {
  1282. projLabel = libName;
  1283. }
  1284. const char* keyword = target.GetProperty("VS_KEYWORD");
  1285. if(!keyword)
  1286. {
  1287. keyword = "Win32Proj";
  1288. }
  1289. cmGlobalVisualStudio7Generator* gg =
  1290. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator);
  1291. fout << "\tName=\"" << projLabel << "\"\n";
  1292. if(m_Version == 8)
  1293. {
  1294. fout << "\tProjectGUID=\"{" << gg->GetGUID(libName) << "}\"\n";
  1295. }
  1296. fout << "\tSccProjectName=\"\"\n"
  1297. << "\tSccLocalPath=\"\"\n"
  1298. << "\tKeyword=\"" << keyword << "\">\n"
  1299. << "\t<Platforms>\n"
  1300. << "\t\t<Platform\n\t\t\tName=\"Win32\"/>\n"
  1301. << "\t</Platforms>\n";
  1302. }
  1303. void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout)
  1304. {
  1305. fout << "\t<Globals>\n"
  1306. << "\t</Globals>\n"
  1307. << "</VisualStudioProject>\n";
  1308. }
  1309. std::string cmLocalVisualStudio7Generator::EscapeForXML(const char* s)
  1310. {
  1311. std::string ret = s;
  1312. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1313. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1314. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1315. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1316. cmSystemTools::ReplaceString(ret, "\n", "&#x0D;&#x0A;");
  1317. return ret;
  1318. }
  1319. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPath(const char* path)
  1320. {
  1321. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1322. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1323. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1324. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1325. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1326. return ret;
  1327. }
  1328. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPathSingle(const char* path)
  1329. {
  1330. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1331. cmSystemTools::ReplaceString(ret, "\"", "");
  1332. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1333. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1334. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1335. return ret;
  1336. }
  1337. void cmLocalVisualStudio7Generator::ConfigureFinalPass()
  1338. {
  1339. cmLocalGenerator::ConfigureFinalPass();
  1340. cmTargets &tgts = m_Makefile->GetTargets();
  1341. cmGlobalVisualStudio7Generator* gg =
  1342. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator);
  1343. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  1344. {
  1345. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  1346. {
  1347. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  1348. const cmCustomCommandLines& cmds = cc.GetCommandLines();
  1349. std::string project_name = cmds[0][0];
  1350. gg->CreateGUID(project_name.c_str());
  1351. }
  1352. else
  1353. {
  1354. gg->CreateGUID(l->first.c_str());
  1355. }
  1356. }
  1357. }
  1358. //----------------------------------------------------------------------------
  1359. std::string cmLocalVisualStudio7Generator::GetTargetDirectory(cmTarget& target)
  1360. {
  1361. std::string dir;
  1362. dir += target.GetName();
  1363. dir += ".dir";
  1364. return dir;
  1365. }