cmLocalVisualStudio7Generator.cxx 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  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. this->OutputTargetRules(fout, target, libName);
  578. this->OutputBuildTool(fout, configName, libName, target);
  579. fout << "\t\t</Configuration>\n";
  580. }
  581. void cmLocalVisualStudio7Generator::FillFlagMapFromCommandFlags(
  582. std::map<cmStdString, cmStdString>& flagMap,
  583. cmVS7FlagTable* flagTable,
  584. std::string& flags)
  585. {
  586. std::string replace;
  587. std::string option;
  588. while(flagTable->IDEName)
  589. {
  590. option.reserve(strlen(flagTable->commandFlag)+2);
  591. // first do the - version
  592. option = "-";
  593. option += flagTable->commandFlag;
  594. while(flags.find(option) != flags.npos)
  595. {
  596. // replace the flag
  597. cmSystemTools::ReplaceString(flags, option.c_str(), "");
  598. // now put value into flag map
  599. flagMap[flagTable->IDEName] = flagTable->value;
  600. }
  601. // now do the / version
  602. option[0] = '/';
  603. while(flags.find(option) != flags.npos)
  604. {
  605. // replace the flag
  606. cmSystemTools::ReplaceString(flags, option.c_str(), "");
  607. // now put value into flag map
  608. flagMap[flagTable->IDEName] = flagTable->value;
  609. }
  610. // move to next flag
  611. flagTable++;
  612. }
  613. // If verbose makefiles have been requested and the /nologo option
  614. // was not given explicitly in the flags we want to add an attribute
  615. // to the generated project to disable logo suppression. Otherwise
  616. // the GUI default is to enable suppression.
  617. if(m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  618. {
  619. if(flagMap.find("SuppressStartupBanner") == flagMap.end())
  620. {
  621. flagMap["SuppressStartupBanner"] = "FALSE";
  622. }
  623. }
  624. }
  625. //----------------------------------------------------------------------------
  626. std::string
  627. cmLocalVisualStudio7Generator
  628. ::GetBuildTypeLinkerFlags(std::string rootLinkerFlags, const char* configName)
  629. {
  630. std::string configTypeUpper = cmSystemTools::UpperCase(configName);
  631. std::string extraLinkOptionsBuildTypeDef = rootLinkerFlags + "_" + configTypeUpper;
  632. std::string extraLinkOptionsBuildType =
  633. m_Makefile->GetRequiredDefinition(extraLinkOptionsBuildTypeDef.c_str());
  634. return extraLinkOptionsBuildType;
  635. }
  636. void cmLocalVisualStudio7Generator::OutputBuildTool(std::ostream& fout,
  637. const char* configName,
  638. const char *libName,
  639. cmTarget &target)
  640. {
  641. std::string targetFullName = target.GetFullName(configName);
  642. std::string temp;
  643. std::string extraLinkOptions;
  644. if(target.GetType() == cmTarget::EXECUTABLE)
  645. {
  646. extraLinkOptions = m_Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS") +
  647. std::string(" ") + GetBuildTypeLinkerFlags("CMAKE_EXE_LINKER_FLAGS", configName);
  648. }
  649. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  650. {
  651. extraLinkOptions = m_Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS") +
  652. std::string(" ") + GetBuildTypeLinkerFlags("CMAKE_SHARED_LINKER_FLAGS", configName);
  653. }
  654. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  655. {
  656. extraLinkOptions = m_Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS") +
  657. std::string(" ") + GetBuildTypeLinkerFlags("CMAKE_MODULE_LINKER_FLAGS", configName);
  658. }
  659. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  660. if(targetLinkFlags)
  661. {
  662. extraLinkOptions += " ";
  663. extraLinkOptions += targetLinkFlags;
  664. }
  665. std::map<cmStdString, cmStdString> flagMap;
  666. this->
  667. FillFlagMapFromCommandFlags(flagMap,
  668. &cmLocalVisualStudio7GeneratorLinkFlagTable[0],
  669. extraLinkOptions);
  670. switch(target.GetType())
  671. {
  672. case cmTarget::STATIC_LIBRARY:
  673. {
  674. std::string libpath = m_LibraryOutputPath +
  675. "$(OutDir)/" + targetFullName;
  676. fout << "\t\t\t<Tool\n"
  677. << "\t\t\t\tName=\"VCLibrarianTool\"\n";
  678. if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
  679. {
  680. fout << "\t\t\t\tAdditionalOptions=\"" << libflags << "\"\n";
  681. }
  682. fout << "\t\t\t\tOutputFile=\""
  683. << this->ConvertToXMLOutputPathSingle(libpath.c_str()) << ".\"/>\n";
  684. break;
  685. }
  686. case cmTarget::SHARED_LIBRARY:
  687. case cmTarget::MODULE_LIBRARY:
  688. {
  689. // Compute the link library and directory information.
  690. std::vector<cmStdString> linkLibs;
  691. std::vector<cmStdString> linkDirs;
  692. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  693. fout << "\t\t\t<Tool\n"
  694. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  695. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386";
  696. if(extraLinkOptions.size())
  697. {
  698. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  699. extraLinkOptions.c_str()).c_str();
  700. }
  701. fout << "\"\n"
  702. << "\t\t\t\tAdditionalDependencies=\" odbc32.lib odbccp32.lib ";
  703. this->OutputLibraries(fout, linkLibs);
  704. fout << "\"\n";
  705. temp = m_LibraryOutputPath;
  706. temp += configName;
  707. temp += "/";
  708. temp += targetFullName;
  709. fout << "\t\t\t\tOutputFile=\""
  710. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  711. for(std::map<cmStdString, cmStdString>::iterator i = flagMap.begin();
  712. i != flagMap.end(); ++i)
  713. {
  714. fout << "\t\t\t\t" << i->first << "=\"" << i->second << "\"\n";
  715. }
  716. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  717. this->OutputLibraryDirectories(fout, linkDirs);
  718. fout << "\"\n";
  719. this->OutputModuleDefinitionFile(fout, target);
  720. temp = m_LibraryOutputPath;
  721. temp += "$(OutDir)/";
  722. temp += libName;
  723. temp += ".pdb";
  724. fout << "\t\t\t\tProgramDatabaseFile=\"" <<
  725. this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  726. if(strcmp(configName, "Debug") == 0
  727. || strcmp(configName, "RelWithDebInfo") == 0)
  728. {
  729. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  730. }
  731. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  732. if(!linkLanguage)
  733. {
  734. cmSystemTools::Error("CMake can not determine linker language for target:",
  735. target.GetName());
  736. return;
  737. }
  738. std::string stackVar = "CMAKE_";
  739. stackVar += linkLanguage;
  740. stackVar += "_STACK_SIZE";
  741. const char* stackVal = m_Makefile->GetDefinition(stackVar.c_str());
  742. if(stackVal)
  743. {
  744. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"\n";
  745. }
  746. temp = m_LibraryOutputPath;
  747. temp += configName;
  748. temp += "/";
  749. temp += cmSystemTools::GetFilenameWithoutLastExtension(targetFullName.c_str());
  750. temp += ".lib";
  751. fout << "\t\t\t\tImportLibrary=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
  752. }
  753. break;
  754. case cmTarget::EXECUTABLE:
  755. {
  756. // Compute the link library and directory information.
  757. std::vector<cmStdString> linkLibs;
  758. std::vector<cmStdString> linkDirs;
  759. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  760. fout << "\t\t\t<Tool\n"
  761. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  762. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386";
  763. if(extraLinkOptions.size())
  764. {
  765. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  766. extraLinkOptions.c_str()).c_str();
  767. }
  768. fout << "\"\n"
  769. << "\t\t\t\tAdditionalDependencies=\""
  770. << m_Makefile->GetRequiredDefinition("CMAKE_STANDARD_LIBRARIES")
  771. << " ";
  772. this->OutputLibraries(fout, linkLibs);
  773. fout << "\"\n";
  774. temp = m_ExecutableOutputPath;
  775. temp += configName;
  776. temp += "/";
  777. temp += targetFullName;
  778. fout << "\t\t\t\tOutputFile=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  779. for(std::map<cmStdString, cmStdString>::iterator i = flagMap.begin();
  780. i != flagMap.end(); ++i)
  781. {
  782. fout << "\t\t\t\t" << i->first << "=\"" << i->second << "\"\n";
  783. }
  784. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  785. this->OutputLibraryDirectories(fout, linkDirs);
  786. fout << "\"\n";
  787. fout << "\t\t\t\tProgramDatabaseFile=\"" << m_LibraryOutputPath
  788. << "$(OutDir)\\" << libName << ".pdb\"\n";
  789. if(strcmp(configName, "Debug") == 0
  790. || strcmp(configName, "RelWithDebInfo") == 0)
  791. {
  792. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  793. }
  794. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  795. {
  796. fout << "\t\t\t\tSubSystem=\"2\"\n";
  797. }
  798. else
  799. {
  800. fout << "\t\t\t\tSubSystem=\"1\"\n";
  801. }
  802. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  803. if(!linkLanguage)
  804. {
  805. cmSystemTools::Error("CMake can not determine linker language for target:",
  806. target.GetName());
  807. return;
  808. }
  809. std::string stackVar = "CMAKE_";
  810. stackVar += linkLanguage;
  811. stackVar += "_STACK_SIZE";
  812. const char* stackVal = m_Makefile->GetDefinition(stackVar.c_str());
  813. if(stackVal)
  814. {
  815. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"";
  816. }
  817. fout << "/>\n";
  818. break;
  819. }
  820. case cmTarget::UTILITY:
  821. break;
  822. }
  823. }
  824. void cmLocalVisualStudio7Generator::OutputModuleDefinitionFile(std::ostream& fout,
  825. cmTarget &target)
  826. {
  827. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  828. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  829. i != classes.end(); i++)
  830. {
  831. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  832. {
  833. fout << "\t\t\t\tModuleDefinitionFile=\""
  834. << this->ConvertToXMLOutputPath((*i)->GetFullPath().c_str())
  835. << "\"\n";
  836. return;
  837. }
  838. }
  839. }
  840. //----------------------------------------------------------------------------
  841. void
  842. cmLocalVisualStudio7Generator
  843. ::OutputLibraries(std::ostream& fout,
  844. std::vector<cmStdString> const& libs)
  845. {
  846. for(std::vector<cmStdString>::const_iterator l = libs.begin();
  847. l != libs.end(); ++l)
  848. {
  849. fout << this->ConvertToXMLOutputPath(l->c_str()) << " ";
  850. }
  851. }
  852. //----------------------------------------------------------------------------
  853. void
  854. cmLocalVisualStudio7Generator
  855. ::OutputLibraryDirectories(std::ostream& fout,
  856. std::vector<cmStdString> const& dirs)
  857. {
  858. const char* comma = "";
  859. for(std::vector<cmStdString>::const_iterator d = dirs.begin();
  860. d != dirs.end(); ++d)
  861. {
  862. std::string dir = *d;
  863. if(!dir.empty())
  864. {
  865. if(dir[dir.size()-1] != '/')
  866. {
  867. dir += "/";
  868. }
  869. dir += "$(OutDir)";
  870. fout << comma << this->ConvertToXMLOutputPath(dir.c_str())
  871. << "," << this->ConvertToXMLOutputPath(d->c_str());
  872. comma = ",";
  873. }
  874. }
  875. }
  876. //----------------------------------------------------------------------------
  877. void cmLocalVisualStudio7Generator::OutputDefineFlags(const char* flags,
  878. std::ostream& fout)
  879. {
  880. std::string defs = flags;
  881. cmSystemTools::ReplaceString(defs, "/D","-D");
  882. std::string::size_type pos = defs.find("-D");
  883. bool done = pos == std::string::npos;
  884. if(!done)
  885. {
  886. fout << ",";
  887. }
  888. while(!done)
  889. {
  890. std::string::size_type nextpos = defs.find("-D", pos+2);
  891. std::string define;
  892. if(nextpos != std::string::npos)
  893. {
  894. define = defs.substr(pos+2, nextpos - pos -3);
  895. }
  896. else
  897. {
  898. define = defs.substr(pos+2);
  899. done = true;
  900. }
  901. // Double-quotes in the value of the definition must be escaped
  902. // with a backslash. The entire definition should be quoted in
  903. // the generated xml attribute to avoid confusing the VS parser.
  904. cmSystemTools::ReplaceString(define, "\"", "\\&quot;");
  905. fout << "&quot;" << define << "&quot;,";
  906. if(!done)
  907. {
  908. pos = defs.find("-D", nextpos);
  909. }
  910. }
  911. }
  912. void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
  913. const char *libName,
  914. cmTarget &target)
  915. {
  916. // get the configurations
  917. std::vector<std::string> *configs =
  918. static_cast<cmGlobalVisualStudio7Generator *>
  919. (m_GlobalGenerator)->GetConfigurations();
  920. // trace the visual studio dependencies
  921. std::string name = libName;
  922. name += ".vcproj.cmake";
  923. // We may be modifying the source groups temporarily, so make a copy.
  924. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  925. // get the classes from the source lists then add them to the groups
  926. std::vector<cmSourceFile*> & classes = target.GetSourceFiles();
  927. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  928. i != classes.end(); i++)
  929. {
  930. // Add the file to the list of sources.
  931. std::string source = (*i)->GetFullPath();
  932. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  933. {
  934. m_ModuleDefinitionFile = (*i)->GetFullPath();
  935. }
  936. cmSourceGroup& sourceGroup =
  937. m_Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  938. sourceGroup.AssignSource(*i);
  939. }
  940. // open the project
  941. this->WriteProjectStart(fout, libName, target, sourceGroups);
  942. // write the configuration information
  943. this->WriteConfigurations(fout, libName, target);
  944. fout << "\t<Files>\n";
  945. // Loop through every source group.
  946. for(unsigned int i = 0; i < sourceGroups.size(); ++i)
  947. {
  948. cmSourceGroup sg = sourceGroups[i];
  949. this->WriteGroup(&sg, target, fout, libName, configs);
  950. }
  951. //}
  952. fout << "\t</Files>\n";
  953. // Write the VCProj file's footer.
  954. this->WriteVCProjFooter(fout);
  955. }
  956. void cmLocalVisualStudio7Generator::WriteGroup(const cmSourceGroup *sg, cmTarget target, std::ostream &fout, const char *libName, std::vector<std::string> *configs)
  957. {
  958. const std::vector<const cmSourceFile *> &sourceFiles =
  959. sg->GetSourceFiles();
  960. // If the group is empty, don't write it at all.
  961. if(sourceFiles.empty() && sg->GetGroupChildren().empty())
  962. {
  963. return;
  964. }
  965. // If the group has a name, write the header.
  966. std::string name = sg->GetName();
  967. if(name != "")
  968. {
  969. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  970. }
  971. // Loop through each source in the source group.
  972. for(std::vector<const cmSourceFile *>::const_iterator sf =
  973. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  974. {
  975. std::string source = (*sf)->GetFullPath();
  976. const cmCustomCommand *command = (*sf)->GetCustomCommand();
  977. std::string compileFlags;
  978. std::string additionalDeps;
  979. // Check for extra compiler flags.
  980. if(target.GetProperty("COMPILE_FLAGS"))
  981. {
  982. compileFlags += " ";
  983. compileFlags += target.GetProperty("COMPILE_FLAGS");
  984. }
  985. const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS");
  986. if(cflags)
  987. {
  988. compileFlags += " ";
  989. compileFlags += cflags;
  990. }
  991. const char* lang =
  992. m_GlobalGenerator->GetLanguageFromExtension((*sf)->GetSourceExtension().c_str());
  993. if(lang && strcmp(lang, "CXX") == 0)
  994. {
  995. // force a C++ file type
  996. compileFlags += " /TP ";
  997. }
  998. // Check for extra object-file dependencies.
  999. const char* deps = (*sf)->GetProperty("OBJECT_DEPENDS");
  1000. if(deps)
  1001. {
  1002. std::vector<std::string> depends;
  1003. cmSystemTools::ExpandListArgument(deps, depends);
  1004. if(!depends.empty())
  1005. {
  1006. std::vector<std::string>::iterator i = depends.begin();
  1007. additionalDeps = this->ConvertToXMLOutputPath(i->c_str());
  1008. for(++i;i != depends.end(); ++i)
  1009. {
  1010. additionalDeps += ";";
  1011. additionalDeps += this->ConvertToXMLOutputPath(i->c_str());
  1012. }
  1013. }
  1014. }
  1015. if (source != libName || target.GetType() == cmTarget::UTILITY)
  1016. {
  1017. fout << "\t\t\t<File\n";
  1018. std::string d = this->ConvertToXMLOutputPathSingle(source.c_str());
  1019. // Tell MS-Dev what the source is. If the compiler knows how to
  1020. // build it, then it will.
  1021. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  1022. if (command)
  1023. {
  1024. // Construct the entire set of commands in one string.
  1025. std::string script = this->ConstructScript(command->GetCommandLines(),
  1026. command->GetWorkingDirectory());
  1027. const char* comment = command->GetComment();
  1028. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  1029. this->WriteCustomRule(fout, source.c_str(), script.c_str(),
  1030. (*comment?comment:"Custom Rule"),
  1031. command->GetDepends(),
  1032. command->GetOutput(), flags);
  1033. }
  1034. else if(compileFlags.size() || additionalDeps.length())
  1035. {
  1036. const char* aCompilerTool = "VCCLCompilerTool";
  1037. std::string ext = (*sf)->GetSourceExtension();
  1038. ext = cmSystemTools::LowerCase(ext);
  1039. if(ext == "idl")
  1040. {
  1041. aCompilerTool = "VCMIDLTool";
  1042. }
  1043. if(ext == "rc")
  1044. {
  1045. aCompilerTool = "VCResourceCompilerTool";
  1046. }
  1047. if(ext == "def")
  1048. {
  1049. aCompilerTool = "VCCustomBuildTool";
  1050. }
  1051. for(std::vector<std::string>::iterator i = configs->begin();
  1052. i != configs->end(); ++i)
  1053. {
  1054. fout << "\t\t\t\t<FileConfiguration\n"
  1055. << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n"
  1056. << "\t\t\t\t\t<Tool\n"
  1057. << "\t\t\t\t\tName=\"" << aCompilerTool << "\"\n";
  1058. if(compileFlags.size())
  1059. {
  1060. fout << "\t\t\t\t\tAdditionalOptions=\""
  1061. << this->EscapeForXML(compileFlags.c_str()) << "\"\n";
  1062. }
  1063. if(additionalDeps.length())
  1064. {
  1065. fout << "\t\t\t\t\tAdditionalDependencies=\""
  1066. << additionalDeps.c_str() << "\"\n";
  1067. }
  1068. fout << "\t\t\t\t\t/>\n"
  1069. << "\t\t\t\t</FileConfiguration>\n";
  1070. }
  1071. }
  1072. fout << "\t\t\t</File>\n";
  1073. }
  1074. }
  1075. std::vector<cmSourceGroup> children = sg->GetGroupChildren();
  1076. for(unsigned int i=0;i<children.size();++i)
  1077. {
  1078. this->WriteGroup(&children[i], target, fout, libName, configs);
  1079. }
  1080. // If the group has a name, write the footer.
  1081. if(name != "")
  1082. {
  1083. this->WriteVCProjEndGroup(fout);
  1084. }
  1085. }
  1086. void cmLocalVisualStudio7Generator::
  1087. WriteCustomRule(std::ostream& fout,
  1088. const char* source,
  1089. const char* command,
  1090. const char* comment,
  1091. const std::vector<std::string>& depends,
  1092. const char *output,
  1093. const char* compileFlags)
  1094. {
  1095. std::vector<std::string>::iterator i;
  1096. std::vector<std::string> *configs =
  1097. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator)->GetConfigurations();
  1098. for(i = configs->begin(); i != configs->end(); ++i)
  1099. {
  1100. fout << "\t\t\t\t<FileConfiguration\n";
  1101. fout << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n";
  1102. if(compileFlags)
  1103. {
  1104. fout << "\t\t\t\t\t<Tool\n"
  1105. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  1106. << "\t\t\t\t\tAdditionalOptions=\""
  1107. << this->EscapeForXML(compileFlags) << "\"/>\n";
  1108. }
  1109. fout << "\t\t\t\t\t<Tool\n"
  1110. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  1111. << "\t\t\t\t\tDescription=\"Building " << comment;
  1112. fout << " " << output;
  1113. fout << "\"\n"
  1114. << "\t\t\t\t\tCommandLine=\""
  1115. << this->EscapeForXML(command) << "\"\n"
  1116. << "\t\t\t\t\tAdditionalDependencies=\"";
  1117. // Write out the dependencies for the rule.
  1118. std::string temp;
  1119. for(std::vector<std::string>::const_iterator d = depends.begin();
  1120. d != depends.end(); ++d)
  1121. {
  1122. // Lookup the real name of the dependency in case it is a CMake target.
  1123. std::string dep = this->GetRealDependency(d->c_str(), i->c_str());
  1124. fout << this->ConvertToXMLOutputPath(dep.c_str())
  1125. << ";";
  1126. }
  1127. fout << "\"\n";
  1128. fout << "\t\t\t\t\tOutputs=\"";
  1129. if(output == 0)
  1130. {
  1131. fout << source << "_force";
  1132. }
  1133. // Write a rule for the output generated by this command.
  1134. fout << this->ConvertToXMLOutputPathSingle(output);
  1135. fout << "\"/>\n";
  1136. fout << "\t\t\t\t</FileConfiguration>\n";
  1137. }
  1138. }
  1139. void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
  1140. const char* group,
  1141. const char* )
  1142. {
  1143. fout << "\t\t<Filter\n"
  1144. << "\t\t\tName=\"" << group << "\"\n"
  1145. << "\t\t\tFilter=\"\">\n";
  1146. }
  1147. void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
  1148. {
  1149. fout << "\t\t</Filter>\n";
  1150. }
  1151. // look for custom rules on a target and collect them together
  1152. void cmLocalVisualStudio7Generator::OutputTargetRules(std::ostream& fout,
  1153. cmTarget &target,
  1154. const char * /*libName*/)
  1155. {
  1156. if (target.GetType() > cmTarget::UTILITY)
  1157. {
  1158. return;
  1159. }
  1160. // add the pre build rules
  1161. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"";
  1162. bool init = false;
  1163. for (std::vector<cmCustomCommand>::const_iterator cr =
  1164. target.GetPreBuildCommands().begin();
  1165. cr != target.GetPreBuildCommands().end(); ++cr)
  1166. {
  1167. if(!init)
  1168. {
  1169. fout << "\nCommandLine=\"";
  1170. init = true;
  1171. }
  1172. std::string script = this->ConstructScript(cr->GetCommandLines(), cr->GetWorkingDirectory());
  1173. fout << this->EscapeForXML(script.c_str()).c_str();
  1174. }
  1175. if (init)
  1176. {
  1177. fout << "\"";
  1178. }
  1179. fout << "/>\n";
  1180. // add the pre Link rules
  1181. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreLinkEventTool\"";
  1182. init = false;
  1183. for (std::vector<cmCustomCommand>::const_iterator cr =
  1184. target.GetPreLinkCommands().begin();
  1185. cr != target.GetPreLinkCommands().end(); ++cr)
  1186. {
  1187. if(!init)
  1188. {
  1189. fout << "\nCommandLine=\"";
  1190. init = true;
  1191. }
  1192. std::string script = this->ConstructScript(cr->GetCommandLines(),
  1193. cr->GetWorkingDirectory());
  1194. fout << this->EscapeForXML(script.c_str()).c_str();
  1195. }
  1196. if (init)
  1197. {
  1198. fout << "\"";
  1199. }
  1200. fout << "/>\n";
  1201. // add the PostBuild rules
  1202. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  1203. init = false;
  1204. for (std::vector<cmCustomCommand>::const_iterator cr =
  1205. target.GetPostBuildCommands().begin();
  1206. cr != target.GetPostBuildCommands().end(); ++cr)
  1207. {
  1208. if(!init)
  1209. {
  1210. fout << "\nCommandLine=\"";
  1211. init = true;
  1212. }
  1213. std::string script = this->ConstructScript(cr->GetCommandLines(), cr->GetWorkingDirectory());
  1214. fout << this->EscapeForXML(script.c_str()).c_str();
  1215. }
  1216. if (init)
  1217. {
  1218. fout << "\"";
  1219. }
  1220. fout << "/>\n";
  1221. }
  1222. void
  1223. cmLocalVisualStudio7Generator::WriteProjectStart(std::ostream& fout,
  1224. const char *libName,
  1225. cmTarget & target,
  1226. std::vector<cmSourceGroup> &)
  1227. {
  1228. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  1229. << "<VisualStudioProject\n"
  1230. << "\tProjectType=\"Visual C++\"\n";
  1231. if(m_Version == 71)
  1232. {
  1233. fout << "\tVersion=\"7.10\"\n";
  1234. }
  1235. else
  1236. {
  1237. if (m_Version == 8)
  1238. {
  1239. fout << "\tVersion=\"8.00\"\n";
  1240. }
  1241. else
  1242. {
  1243. fout << "\tVersion=\"7.00\"\n";
  1244. }
  1245. }
  1246. const char* projLabel = target.GetProperty("PROJECT_LABEL");
  1247. if(!projLabel)
  1248. {
  1249. projLabel = libName;
  1250. }
  1251. const char* keyword = target.GetProperty("VS_KEYWORD");
  1252. if(!keyword)
  1253. {
  1254. keyword = "Win32Proj";
  1255. }
  1256. cmGlobalVisualStudio7Generator* gg =
  1257. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator);
  1258. fout << "\tName=\"" << projLabel << "\"\n";
  1259. if(m_Version == 8)
  1260. {
  1261. fout << "\tProjectGUID=\"{" << gg->GetGUID(libName) << "}\"\n";
  1262. }
  1263. fout << "\tSccProjectName=\"\"\n"
  1264. << "\tSccLocalPath=\"\"\n"
  1265. << "\tKeyword=\"" << keyword << "\">\n"
  1266. << "\t<Platforms>\n"
  1267. << "\t\t<Platform\n\t\t\tName=\"Win32\"/>\n"
  1268. << "\t</Platforms>\n";
  1269. }
  1270. void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout)
  1271. {
  1272. fout << "\t<Globals>\n"
  1273. << "\t</Globals>\n"
  1274. << "</VisualStudioProject>\n";
  1275. }
  1276. std::string cmLocalVisualStudio7Generator::EscapeForXML(const char* s)
  1277. {
  1278. std::string ret = s;
  1279. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1280. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1281. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1282. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1283. cmSystemTools::ReplaceString(ret, "\n", "&#x0D;&#x0A;");
  1284. return ret;
  1285. }
  1286. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPath(const char* path)
  1287. {
  1288. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1289. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1290. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1291. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1292. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1293. return ret;
  1294. }
  1295. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPathSingle(const char* path)
  1296. {
  1297. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1298. cmSystemTools::ReplaceString(ret, "\"", "");
  1299. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1300. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1301. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1302. return ret;
  1303. }
  1304. void cmLocalVisualStudio7Generator::ConfigureFinalPass()
  1305. {
  1306. cmLocalGenerator::ConfigureFinalPass();
  1307. cmTargets &tgts = m_Makefile->GetTargets();
  1308. cmGlobalVisualStudio7Generator* gg =
  1309. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator);
  1310. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  1311. {
  1312. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  1313. {
  1314. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  1315. const cmCustomCommandLines& cmds = cc.GetCommandLines();
  1316. std::string project_name = cmds[0][0];
  1317. gg->CreateGUID(project_name.c_str());
  1318. }
  1319. else
  1320. {
  1321. gg->CreateGUID(l->first.c_str());
  1322. }
  1323. }
  1324. }
  1325. //----------------------------------------------------------------------------
  1326. std::string cmLocalVisualStudio7Generator::GetTargetDirectory(cmTarget& target)
  1327. {
  1328. std::string dir;
  1329. dir += target.GetName();
  1330. dir += ".dir";
  1331. return dir;
  1332. }