cmLocalVisualStudio7Generator.cxx 48 KB

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