cmLocalVisualStudio7Generator.cxx 49 KB

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