cmLocalVisualStudio7Generator.cxx 49 KB

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