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. this->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(this->Makefile->GetStartOutputDirectory(),
  45. this->Makefile->GetHomeDirectory()) != 0)
  46. {
  47. if(!cmSystemTools::MakeDirectory(this->Makefile->GetStartOutputDirectory()))
  48. {
  49. cmSystemTools::Error("Error creating directory ",
  50. this->Makefile->GetStartOutputDirectory());
  51. }
  52. }
  53. this->LibraryOutputPath = "";
  54. if (this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  55. {
  56. this->LibraryOutputPath =
  57. this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  58. }
  59. if(this->LibraryOutputPath.size())
  60. {
  61. // make sure there is a trailing slash
  62. if(this->LibraryOutputPath[this->LibraryOutputPath.size()-1] != '/')
  63. {
  64. this->LibraryOutputPath += "/";
  65. }
  66. }
  67. this->ExecutableOutputPath = "";
  68. if (this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  69. {
  70. this->ExecutableOutputPath = this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  71. }
  72. if(this->ExecutableOutputPath.size())
  73. {
  74. // make sure there is a trailing slash
  75. if(this->ExecutableOutputPath[this->ExecutableOutputPath.size()-1] != '/')
  76. {
  77. this->ExecutableOutputPath += "/";
  78. }
  79. }
  80. // Create the VCProj or set of VCProj's for libraries and executables
  81. // clear project names
  82. this->CreatedProjectNames.clear();
  83. #if 1
  84. // TODO: This block should be moved to a central location for all
  85. // generators. It is duplicated in every generator.
  86. // Call TraceVSDependencies on all targets
  87. cmTargets &tgts = this->Makefile->GetTargets();
  88. for(cmTargets::iterator l = tgts.begin();
  89. l != tgts.end(); l++)
  90. {
  91. // Add a rule to regenerate the build system when the target
  92. // specification source changes.
  93. const char* suppRegenRule =
  94. this->Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  95. if (!cmSystemTools::IsOn(suppRegenRule) &&
  96. (strcmp(l->first.c_str(), CMAKE_CHECK_BUILD_SYSTEM_TARGET) != 0))
  97. {
  98. this->AddVCProjBuildRule(l->second);
  99. }
  100. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  101. // so don't build a projectfile for it
  102. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  103. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  104. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  105. {
  106. cmTarget& target = l->second;
  107. target.TraceVSDependencies(target.GetName(), this->Makefile);
  108. }
  109. }
  110. // now for all custom commands that are not used directly in a
  111. // target, add them to all targets in the current directory or
  112. // makefile
  113. std::vector<cmSourceFile*> & classesmf = this->Makefile->GetSourceFiles();
  114. for(std::vector<cmSourceFile*>::const_iterator i = classesmf.begin();
  115. i != classesmf.end(); i++)
  116. {
  117. if(cmCustomCommand* cc = (*i)->GetCustomCommand())
  118. {
  119. // while we are at it, if it is a .rule file then for visual studio 7 we
  120. // must generate it so that depend information works correctly
  121. if ((*i)->GetSourceExtension() == "rule")
  122. {
  123. std::string source = (*i)->GetFullPath();
  124. if(!cmSystemTools::FileExists(source.c_str()))
  125. {
  126. cmSystemTools::ReplaceString(source, "$(IntDir)/", "");
  127. #if defined(_WIN32) || defined(__CYGWIN__)
  128. std::ofstream fout(source.c_str(),
  129. std::ios::binary | std::ios::out | std::ios::trunc);
  130. #else
  131. std::ofstream fout(source.c_str(),
  132. std::ios::out | std::ios::trunc);
  133. #endif
  134. if(fout)
  135. {
  136. fout.write("# generated from CMake",22);
  137. fout.flush();
  138. fout.close();
  139. }
  140. }
  141. }
  142. if(!cc->IsUsed())
  143. {
  144. for(cmTargets::iterator l = tgts.begin();
  145. l != tgts.end(); l++)
  146. {
  147. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  148. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  149. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0)
  150. && (strcmp(l->first.c_str(), "ALL_BUILD") != 0)
  151. && (strcmp(l->first.c_str(), CMAKE_CHECK_BUILD_SYSTEM_TARGET) != 0))
  152. {
  153. cmTarget& target = l->second;
  154. bool sameAsTarget = false;
  155. // make sure we don't add a custom command that depends on
  156. // this target
  157. for(unsigned int k =0; k < cc->GetDepends().size(); k++)
  158. {
  159. if(cmSystemTools::GetFilenameName(cc->GetDepends()[k]) == target.GetFullName())
  160. {
  161. sameAsTarget = true;
  162. }
  163. }
  164. if(!sameAsTarget)
  165. {
  166. target.GetSourceFiles().push_back(*i);
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. #endif
  174. for(cmTargets::iterator l = tgts.begin();
  175. l != tgts.end(); l++)
  176. {
  177. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  178. // so don't build a projectfile for it
  179. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  180. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  181. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  182. {
  183. this->CreateSingleVCProj(l->first.c_str(),l->second);
  184. }
  185. }
  186. }
  187. void cmLocalVisualStudio7Generator::CreateSingleVCProj(const char *lname, cmTarget &target)
  188. {
  189. // add to the list of projects
  190. std::string pname = lname;
  191. this->CreatedProjectNames.push_back(pname);
  192. // create the dsp.cmake file
  193. std::string fname;
  194. fname = this->Makefile->GetStartOutputDirectory();
  195. fname += "/";
  196. fname += lname;
  197. fname += ".vcproj";
  198. // save the name of the real dsp file
  199. std::string realVCProj = fname;
  200. fname += ".cmake";
  201. std::ofstream fout(fname.c_str());
  202. if(!fout)
  203. {
  204. cmSystemTools::Error("Error Writing ", fname.c_str());
  205. }
  206. this->WriteVCProjFile(fout,lname,target);
  207. fout.close();
  208. // if the dsp file has changed, then write it.
  209. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realVCProj.c_str());
  210. }
  211. void cmLocalVisualStudio7Generator::AddVCProjBuildRule(cmTarget& tgt)
  212. {
  213. std::string dspname = tgt.GetName();
  214. dspname += ".vcproj.cmake";
  215. const char* dsprule = this->Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  216. cmCustomCommandLine commandLine;
  217. commandLine.push_back(dsprule);
  218. std::string makefileIn = this->Makefile->GetStartDirectory();
  219. makefileIn += "/";
  220. makefileIn += "CMakeLists.txt";
  221. std::string args;
  222. args = "-H";
  223. args +=
  224. this->Convert(this->Makefile->GetHomeDirectory(), START_OUTPUT, SHELL, true);
  225. commandLine.push_back(args);
  226. args = "-B";
  227. args +=
  228. this->Convert(this->Makefile->GetHomeOutputDirectory(),
  229. START_OUTPUT, SHELL, true);
  230. commandLine.push_back(args);
  231. std::string configFile =
  232. this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  233. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  234. std::vector<std::string> listFiles = this->Makefile->GetListFiles();
  235. bool found = false;
  236. for(std::vector<std::string>::iterator i = listFiles.begin();
  237. i != listFiles.end(); ++i)
  238. {
  239. if(*i == configFile)
  240. {
  241. found = true;
  242. }
  243. }
  244. if(!found)
  245. {
  246. listFiles.push_back(configFile);
  247. }
  248. cmCustomCommandLines commandLines;
  249. commandLines.push_back(commandLine);
  250. const char* no_working_directory = 0;
  251. const char* no_comment = 0;
  252. this->Makefile->AddCustomCommandToOutput(dspname.c_str(), listFiles, makefileIn.c_str(),
  253. commandLines, no_comment, no_working_directory, true);
  254. if(cmSourceFile* file = this->Makefile->GetSource(makefileIn.c_str()))
  255. {
  256. tgt.GetSourceFiles().push_back(file);
  257. }
  258. else
  259. {
  260. cmSystemTools::Error("Error adding rule for ", makefileIn.c_str());
  261. }
  262. }
  263. void cmLocalVisualStudio7Generator::WriteConfigurations(std::ostream& fout,
  264. const char *libName,
  265. cmTarget &target)
  266. {
  267. std::vector<std::string> *configs =
  268. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator)->GetConfigurations();
  269. fout << "\t<Configurations>\n";
  270. for( std::vector<std::string>::iterator i = configs->begin();
  271. i != configs->end(); ++i)
  272. {
  273. this->WriteConfiguration(fout, i->c_str(), libName, target);
  274. }
  275. fout << "\t</Configurations>\n";
  276. }
  277. // This is a table mapping XML tag IDE names to command line options
  278. struct cmVS7FlagTable
  279. {
  280. const char* IDEName; // name used in the IDE xml file
  281. const char* commandFlag; // command line flag
  282. const char* comment; // comment
  283. const char* value; // string value
  284. };
  285. // fill the table here
  286. // currently the comment field is not used for anything other than documentation
  287. // NOTE: Make sure the longer commandFlag comes FIRST!
  288. cmVS7FlagTable cmLocalVisualStudio7GeneratorFlagTable[] =
  289. {
  290. // option flags (some flags map to the same option)
  291. {"BasicRuntimeChecks", "GZ", "Stack frame checks", "1"},
  292. {"BasicRuntimeChecks", "RTCsu", "Both stack and uninitialized checks ", "3"},
  293. {"BasicRuntimeChecks", "RTCs", "Stack frame checks", "1"},
  294. {"BasicRuntimeChecks", "RTCu", "Uninitialized Variables ", "2"},
  295. {"BasicRuntimeChecks", "RTC1", "Both stack and uninitialized checks ", "3"},
  296. {"DebugInformationFormat", "Z7", "debug format", "1"},
  297. {"DebugInformationFormat", "Zd", "debug format", "2"},
  298. {"DebugInformationFormat", "Zi", "debug format", "3"},
  299. {"DebugInformationFormat", "ZI", "debug format", "4"},
  300. {"EnableEnhancedInstructionSet", "arch:SSE2", "Use sse2 instructions", "2"},
  301. {"EnableEnhancedInstructionSet", "arch:SSE", "Use sse instructions", "1"},
  302. {"FavorSizeOrSpeed", "Ot", "Favor fast code", "1"},
  303. {"FavorSizeOrSpeed", "Os", "Favor small code", "2"},
  304. {"Optimization", "Od", "Non Debug", "0"},
  305. {"Optimization", "O1", "Min Size", "1"},
  306. {"Optimization", "O2", "Max Speed", "2"},
  307. {"Optimization", "Ox", "Max Optimization", "3"},
  308. {"OptimizeForProcessor", "GB", "Blended processor mode", "0"},
  309. {"OptimizeForProcessor", "G5", "Pentium", "1"},
  310. {"OptimizeForProcessor", "G6", "PPro PII PIII", "2"},
  311. {"OptimizeForProcessor", "G7", "Pentium 4 or Athlon", "3"},
  312. {"InlineFunctionExpansion", "Ob0", "no inlines", "0"},
  313. {"InlineFunctionExpansion", "Ob1", "when inline keyword", "1"},
  314. {"InlineFunctionExpansion", "Ob2", "any time you can inline", "2"},
  315. {"RuntimeLibrary", "MTd", "Multithreded debug", "1"},
  316. {"RuntimeLibrary", "MT", "Multithreded", "0"},
  317. {"RuntimeLibrary", "MDd", "Multithreded dll debug", "3"},
  318. {"RuntimeLibrary", "MD", "Multithreded dll", "2"},
  319. {"RuntimeLibrary", "MLd", "Sinble Thread debug", "5"},
  320. {"RuntimeLibrary", "ML", "Sinble Thread", "4"},
  321. {"StructMemberAlignment", "Zp16", "struct align 16 byte ", "5"},
  322. {"StructMemberAlignment", "Zp1", "struct align 1 byte ", "1"},
  323. {"StructMemberAlignment", "Zp2", "struct align 2 byte ", "2"},
  324. {"StructMemberAlignment", "Zp4", "struct align 4 byte ", "3"},
  325. {"StructMemberAlignment", "Zp8", "struct align 8 byte ", "4"},
  326. {"WarningLevel", "W1", "Warning level", "1"},
  327. {"WarningLevel", "W2", "Warning level", "2"},
  328. {"WarningLevel", "W3", "Warning level", "3"},
  329. {"WarningLevel", "W4", "Warning level", "4"},
  330. // boolean flags
  331. {"BufferSecurityCheck", "GS", "Buffer security check", "TRUE"},
  332. {"EnableFibreSafeOptimization", "GT", "OmitFramePointers", "TRUE"},
  333. {"EnableFunctionLevelLinking", "Gy", "EnableFunctionLevelLinking", "TRUE"},
  334. {"EnableIntrinsicFunctions", "Oi", "EnableIntrinsicFunctions", "TRUE"},
  335. {"ExceptionHandling", "EHsc", "enable c++ exceptions", "TRUE"},
  336. {"ExceptionHandling", "GX", "enable c++ exceptions", "TRUE"},
  337. {"GlobalOptimizations", "Og", "Global Optimize", "TRUE"},
  338. {"ImproveFloatingPointConsistency", "Op", "ImproveFloatingPointConsistency", "TRUE"},
  339. {"MinimalRebuild", "Gm", "minimal rebuild", "TRUE"},
  340. {"OmitFramePointers", "Oy", "OmitFramePointers", "TRUE"},
  341. {"OptimizeForWindowsApplication", "GA", "Optimize for windows", "TRUE"},
  342. {"RuntimeTypeInfo", "GR", "Turn on Run time type information for c++", "TRUE"},
  343. {"SmallerTypeCheck", "RTCc", "smaller type check", "TRUE"},
  344. {"SuppressStartupBanner", "nologo", "SuppressStartupBanner", "TRUE"},
  345. {"WarnAsError", "WX", "Treat warnings as errors", "TRUE"},
  346. {0,0,0,0 }
  347. };
  348. cmVS7FlagTable cmLocalVisualStudio7GeneratorLinkFlagTable[] =
  349. {
  350. // option flags (some flags map to the same option)
  351. {"LinkIncremental", "INCREMENTAL:NO", "link incremental", "1"},
  352. {"LinkIncremental", "INCREMENTAL:YES", "link incremental", "2"},
  353. {0,0,0,0 }
  354. };
  355. void cmLocalVisualStudio7Generator::WriteConfiguration(std::ostream& fout,
  356. const char* configName,
  357. const char *libName,
  358. cmTarget &target)
  359. {
  360. // create a map of xml tags to the values they should have in the output
  361. // for example, "BufferSecurityCheck" = "TRUE"
  362. // first fill this table with the values for the configuration
  363. // Debug, Release, etc,
  364. // Then parse the command line flags specified in CMAKE_CXX_FLAGS
  365. // and CMAKE_C_FLAGS
  366. // and overwrite or add new values to this map
  367. std::map<cmStdString, cmStdString> flagMap;
  368. // since the default is on for this, but if /EHsc is found
  369. // in the flags it will be turned on and we have /EHSC on by
  370. // default in the CXX flags, then this is the only way to turn this off
  371. flagMap["ExceptionHandling"] = "FALSE";
  372. const char* mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
  373. if(!mfcFlag)
  374. {
  375. mfcFlag = "0";
  376. }
  377. fout << "\t\t<Configuration\n"
  378. << "\t\t\tName=\"" << configName << "|Win32\"\n"
  379. << "\t\t\tOutputDirectory=\"" << configName << "\"\n";
  380. // This is an internal type to Visual Studio, it seems that:
  381. // 4 == static library
  382. // 2 == dll
  383. // 1 == executable
  384. // 10 == utility
  385. const char* configType = "10";
  386. switch(target.GetType())
  387. {
  388. case cmTarget::STATIC_LIBRARY:
  389. configType = "4";
  390. break;
  391. case cmTarget::SHARED_LIBRARY:
  392. case cmTarget::MODULE_LIBRARY:
  393. configType = "2";
  394. break;
  395. case cmTarget::EXECUTABLE:
  396. configType = "1";
  397. break;
  398. case cmTarget::UTILITY:
  399. case cmTarget::GLOBAL_TARGET:
  400. configType = "10";
  401. default:
  402. break;
  403. }
  404. std::string flags;
  405. if(strcmp(configType, "10") != 0)
  406. {
  407. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  408. if(!linkLanguage)
  409. {
  410. cmSystemTools::Error("CMake can not determine linker language for target:",
  411. target.GetName());
  412. return;
  413. }
  414. if(!(strcmp(linkLanguage, "RC") == 0 || strcmp(linkLanguage, "DEF") == 0))
  415. {
  416. std::string baseFlagVar = "CMAKE_";
  417. baseFlagVar += linkLanguage;
  418. baseFlagVar += "_FLAGS";
  419. flags = this->Makefile->GetRequiredDefinition(baseFlagVar.c_str());
  420. std::string flagVar = baseFlagVar + std::string("_") +
  421. cmSystemTools::UpperCase(configName);
  422. flags += " ";
  423. flags += this->Makefile->GetRequiredDefinition(flagVar.c_str());
  424. }
  425. }
  426. // Add the target-specific flags.
  427. if(const char* targetFlags = target.GetProperty("COMPILE_FLAGS"))
  428. {
  429. flags += " ";
  430. flags += targetFlags;
  431. }
  432. // The intermediate directory name consists of a directory for the
  433. // target and a subdirectory for the configuration name.
  434. std::string intermediateDir = this->GetTargetDirectory(target);
  435. intermediateDir += "/";
  436. intermediateDir += configName;
  437. fout << "\t\t\tIntermediateDirectory=\""
  438. << this->ConvertToXMLOutputPath(intermediateDir.c_str())
  439. << "\"\n"
  440. << "\t\t\tConfigurationType=\"" << configType << "\"\n"
  441. << "\t\t\tUseOfMFC=\"" << mfcFlag << "\"\n"
  442. << "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n";
  443. // if -D_UNICODE or /D_UNICODE is found in the flags
  444. // change the character set to unicode, if not then
  445. // default to MBCS
  446. std::string defs = this->Makefile->GetDefineFlags();
  447. if(flags.find("D_UNICODE") != flags.npos ||
  448. defs.find("D_UNICODE") != flags.npos)
  449. {
  450. fout << "\t\t\tCharacterSet=\"1\">\n";
  451. }
  452. else
  453. {
  454. fout << "\t\t\tCharacterSet=\"2\">\n";
  455. }
  456. fout << "\t\t\t<Tool\n"
  457. << "\t\t\t\tName=\"VCCLCompilerTool\"\n"
  458. << "\t\t\t\tAdditionalOptions=\"";
  459. // now fill the flagMap from the command line flags, and
  460. // if a flag is used, it will be removed from the flags string by
  461. // this function call
  462. this->FillFlagMapFromCommandFlags(flagMap,
  463. &cmLocalVisualStudio7GeneratorFlagTable[0],
  464. flags);
  465. std::string defineFlags = this->Makefile->GetDefineFlags();
  466. // now check the define flags for flags other than -D and
  467. // put them in the map, the -D flags will be left in the defineFlags
  468. // variable as -D is not in the flagMap
  469. this->FillFlagMapFromCommandFlags(flagMap,
  470. &cmLocalVisualStudio7GeneratorFlagTable[0],
  471. defineFlags);
  472. // output remaining flags that were not mapped to anything
  473. fout << this->EscapeForXML(flags.c_str()).c_str();
  474. fout << " -DCMAKE_INTDIR=\\&quot;" << configName << "\\&quot;"
  475. << "\"\n";
  476. fout << "\t\t\t\tAdditionalIncludeDirectories=\"";
  477. std::vector<std::string> includes;
  478. this->GetIncludeDirectories(includes);
  479. std::vector<std::string>::iterator i = includes.begin();
  480. for(;i != includes.end(); ++i)
  481. {
  482. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  483. fout << ipath << ";";
  484. }
  485. fout << "\"\n";
  486. // set a few cmake specific flags
  487. if(this->Makefile->IsOn("CMAKE_CXX_USE_RTTI"))
  488. {
  489. flagMap["RuntimeTypeInfo"] = "TRUE";
  490. }
  491. if ( this->Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL") )
  492. {
  493. flagMap["WarningLevel"] = this->Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL");
  494. }
  495. // Now copy the flag map into the xml for the file
  496. for(std::map<cmStdString, cmStdString>::iterator m = flagMap.begin();
  497. m != flagMap.end(); ++m)
  498. {
  499. fout << "\t\t\t\t" << m->first << "=\"" << m->second << "\"\n";
  500. }
  501. fout << "\t\t\t\tPreprocessorDefinitions=\"";
  502. if(target.GetType() == cmTarget::SHARED_LIBRARY
  503. || target.GetType() == cmTarget::MODULE_LIBRARY)
  504. {
  505. std::string exportSymbol;
  506. if (const char* custom_export_name =
  507. target.GetProperty("DEFINE_SYMBOL"))
  508. {
  509. exportSymbol = custom_export_name;
  510. }
  511. else
  512. {
  513. std::string id = libName;
  514. id += "_EXPORTS";
  515. exportSymbol = cmSystemTools::MakeCindentifier(id.c_str());
  516. }
  517. fout << "," << exportSymbol;
  518. }
  519. this->OutputDefineFlags(defineFlags.c_str(), fout);
  520. fout << "\"\n";
  521. fout << "\t\t\t\tAssemblerListingLocation=\"" << configName << "\"\n";
  522. fout << "\t\t\t\tObjectFile=\"$(IntDir)\\\"\n";
  523. std::map<cmStdString, cmStdString>::iterator mi = flagMap.find("DebugInformationFormat");
  524. if(mi != flagMap.end() && mi->second != "1")
  525. {
  526. fout << "\t\t\t\tProgramDatabaseFileName=\""
  527. << this->LibraryOutputPath
  528. << "$(OutDir)/" << libName << ".pdb\"\n";
  529. }
  530. fout << "/>\n"; // end of <Tool Name=VCCLCompilerTool
  531. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCCustomBuildTool\"/>\n";
  532. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCResourceCompilerTool\"\n"
  533. << "\t\t\t\tAdditionalIncludeDirectories=\"";
  534. for(i = includes.begin();i != includes.end(); ++i)
  535. {
  536. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  537. fout << ipath << ";";
  538. }
  539. // add the -D flags to the RC tool
  540. fout << "\"\n"
  541. << "\t\t\t\tPreprocessorDefinitions=\"";
  542. this->OutputDefineFlags(defineFlags.c_str(), fout);
  543. fout << "\" />\n";
  544. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCMIDLTool\"\n";
  545. fout << "\t\t\t\tPreprocessorDefinitions=\"";
  546. this->OutputDefineFlags(defineFlags.c_str(), fout);
  547. fout << "\"\n";
  548. fout << "\t\t\t\tMkTypLibCompatible=\"FALSE\"\n";
  549. fout << "\t\t\t\tTargetEnvironment=\"1\"\n";
  550. fout << "\t\t\t\tGenerateStublessProxies=\"TRUE\"\n";
  551. fout << "\t\t\t\tTypeLibraryName=\"$(InputName).tlb\"\n";
  552. fout << "\t\t\t\tOutputDirectory=\"$(IntDir)\"\n";
  553. fout << "\t\t\t\tHeaderFileName=\"$(InputName).h\"\n";
  554. fout << "\t\t\t\tDLLDataFileName=\"\"\n";
  555. fout << "\t\t\t\tInterfaceIdentifierFileName=\"$(InputName)_i.c\"\n";
  556. fout << "\t\t\t\tProxyFileName=\"$(InputName)_p.c\"/>\n";
  557. // end of <Tool Name=VCMIDLTool
  558. this->OutputTargetRules(fout, target, libName);
  559. this->OutputBuildTool(fout, configName, libName, target);
  560. fout << "\t\t</Configuration>\n";
  561. }
  562. void cmLocalVisualStudio7Generator::FillFlagMapFromCommandFlags(
  563. std::map<cmStdString, cmStdString>& flagMap,
  564. cmVS7FlagTable* flagTable,
  565. std::string& flags)
  566. {
  567. std::string replace;
  568. std::string option;
  569. while(flagTable->IDEName)
  570. {
  571. option.reserve(strlen(flagTable->commandFlag)+2);
  572. // first do the - version
  573. option = "-";
  574. option += flagTable->commandFlag;
  575. while(flags.find(option) != flags.npos)
  576. {
  577. // replace the flag
  578. cmSystemTools::ReplaceString(flags, option.c_str(), "");
  579. // now put value into flag map
  580. flagMap[flagTable->IDEName] = flagTable->value;
  581. }
  582. // now do the / version
  583. option[0] = '/';
  584. while(flags.find(option) != flags.npos)
  585. {
  586. // replace the flag
  587. cmSystemTools::ReplaceString(flags, option.c_str(), "");
  588. // now put value into flag map
  589. flagMap[flagTable->IDEName] = flagTable->value;
  590. }
  591. // move to next flag
  592. flagTable++;
  593. }
  594. // If verbose makefiles have been requested and the /nologo option
  595. // was not given explicitly in the flags we want to add an attribute
  596. // to the generated project to disable logo suppression. Otherwise
  597. // the GUI default is to enable suppression.
  598. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  599. {
  600. if(flagMap.find("SuppressStartupBanner") == flagMap.end())
  601. {
  602. flagMap["SuppressStartupBanner"] = "FALSE";
  603. }
  604. }
  605. }
  606. //----------------------------------------------------------------------------
  607. std::string
  608. cmLocalVisualStudio7Generator
  609. ::GetBuildTypeLinkerFlags(std::string rootLinkerFlags, const char* configName)
  610. {
  611. std::string configTypeUpper = cmSystemTools::UpperCase(configName);
  612. std::string extraLinkOptionsBuildTypeDef = rootLinkerFlags + "_" + configTypeUpper;
  613. std::string extraLinkOptionsBuildType =
  614. this->Makefile->GetRequiredDefinition(extraLinkOptionsBuildTypeDef.c_str());
  615. return extraLinkOptionsBuildType;
  616. }
  617. void cmLocalVisualStudio7Generator::OutputBuildTool(std::ostream& fout,
  618. const char* configName,
  619. const char *libName,
  620. cmTarget &target)
  621. {
  622. std::string targetFullName = target.GetFullName(configName);
  623. std::string temp;
  624. std::string extraLinkOptions;
  625. if(target.GetType() == cmTarget::EXECUTABLE)
  626. {
  627. extraLinkOptions = this->Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS") +
  628. std::string(" ") + GetBuildTypeLinkerFlags("CMAKE_EXE_LINKER_FLAGS", configName);
  629. }
  630. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  631. {
  632. extraLinkOptions = this->Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS") +
  633. std::string(" ") + GetBuildTypeLinkerFlags("CMAKE_SHARED_LINKER_FLAGS", configName);
  634. }
  635. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  636. {
  637. extraLinkOptions = this->Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS") +
  638. std::string(" ") + GetBuildTypeLinkerFlags("CMAKE_MODULE_LINKER_FLAGS", configName);
  639. }
  640. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  641. if(targetLinkFlags)
  642. {
  643. extraLinkOptions += " ";
  644. extraLinkOptions += targetLinkFlags;
  645. }
  646. std::map<cmStdString, cmStdString> flagMap;
  647. this->
  648. FillFlagMapFromCommandFlags(flagMap,
  649. &cmLocalVisualStudio7GeneratorLinkFlagTable[0],
  650. extraLinkOptions);
  651. switch(target.GetType())
  652. {
  653. case cmTarget::STATIC_LIBRARY:
  654. {
  655. std::string libpath = this->LibraryOutputPath +
  656. "$(OutDir)/" + targetFullName;
  657. fout << "\t\t\t<Tool\n"
  658. << "\t\t\t\tName=\"VCLibrarianTool\"\n";
  659. if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
  660. {
  661. fout << "\t\t\t\tAdditionalOptions=\"" << libflags << "\"\n";
  662. }
  663. fout << "\t\t\t\tOutputFile=\""
  664. << this->ConvertToXMLOutputPathSingle(libpath.c_str()) << ".\"/>\n";
  665. break;
  666. }
  667. case cmTarget::SHARED_LIBRARY:
  668. case cmTarget::MODULE_LIBRARY:
  669. {
  670. // Compute the link library and directory information.
  671. std::vector<cmStdString> linkLibs;
  672. std::vector<cmStdString> linkDirs;
  673. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  674. fout << "\t\t\t<Tool\n"
  675. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  676. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386";
  677. if(extraLinkOptions.size())
  678. {
  679. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  680. extraLinkOptions.c_str()).c_str();
  681. }
  682. // Use the NOINHERIT macro to avoid getting VS project default
  683. // libraries which may be set by the user to something bad.
  684. fout << "\"\n"
  685. << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
  686. << this->Makefile->GetRequiredDefinition("CMAKE_STANDARD_LIBRARIES")
  687. << " ";
  688. this->OutputLibraries(fout, linkLibs);
  689. fout << "\"\n";
  690. temp = this->LibraryOutputPath;
  691. temp += configName;
  692. temp += "/";
  693. temp += targetFullName;
  694. fout << "\t\t\t\tOutputFile=\""
  695. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  696. for(std::map<cmStdString, cmStdString>::iterator i = flagMap.begin();
  697. i != flagMap.end(); ++i)
  698. {
  699. fout << "\t\t\t\t" << i->first << "=\"" << i->second << "\"\n";
  700. }
  701. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  702. this->OutputLibraryDirectories(fout, linkDirs);
  703. fout << "\"\n";
  704. this->OutputModuleDefinitionFile(fout, target);
  705. temp = this->LibraryOutputPath;
  706. temp += "$(OutDir)/";
  707. temp += libName;
  708. temp += ".pdb";
  709. fout << "\t\t\t\tProgramDatabaseFile=\"" <<
  710. this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  711. if(strcmp(configName, "Debug") == 0
  712. || strcmp(configName, "RelWithDebInfo") == 0)
  713. {
  714. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  715. }
  716. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  717. if(!linkLanguage)
  718. {
  719. cmSystemTools::Error("CMake can not determine linker language for target:",
  720. target.GetName());
  721. return;
  722. }
  723. std::string stackVar = "CMAKE_";
  724. stackVar += linkLanguage;
  725. stackVar += "_STACK_SIZE";
  726. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  727. if(stackVal)
  728. {
  729. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"\n";
  730. }
  731. temp = this->LibraryOutputPath;
  732. temp += configName;
  733. temp += "/";
  734. temp += cmSystemTools::GetFilenameWithoutLastExtension(targetFullName.c_str());
  735. temp += ".lib";
  736. fout << "\t\t\t\tImportLibrary=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
  737. }
  738. break;
  739. case cmTarget::EXECUTABLE:
  740. {
  741. // Compute the link library and directory information.
  742. std::vector<cmStdString> linkLibs;
  743. std::vector<cmStdString> linkDirs;
  744. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  745. fout << "\t\t\t<Tool\n"
  746. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  747. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386";
  748. if(extraLinkOptions.size())
  749. {
  750. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  751. extraLinkOptions.c_str()).c_str();
  752. }
  753. // Use the NOINHERIT macro to avoid getting VS project default
  754. // libraries which may be set by the user to something bad.
  755. fout << "\"\n"
  756. << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
  757. << this->Makefile->GetRequiredDefinition("CMAKE_STANDARD_LIBRARIES")
  758. << " ";
  759. this->OutputLibraries(fout, linkLibs);
  760. fout << "\"\n";
  761. temp = this->ExecutableOutputPath;
  762. temp += configName;
  763. temp += "/";
  764. temp += targetFullName;
  765. fout << "\t\t\t\tOutputFile=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  766. for(std::map<cmStdString, cmStdString>::iterator i = flagMap.begin();
  767. i != flagMap.end(); ++i)
  768. {
  769. fout << "\t\t\t\t" << i->first << "=\"" << i->second << "\"\n";
  770. }
  771. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  772. this->OutputLibraryDirectories(fout, linkDirs);
  773. fout << "\"\n";
  774. fout << "\t\t\t\tProgramDatabaseFile=\"" << this->LibraryOutputPath
  775. << "$(OutDir)\\" << libName << ".pdb\"\n";
  776. if(strcmp(configName, "Debug") == 0
  777. || strcmp(configName, "RelWithDebInfo") == 0)
  778. {
  779. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  780. }
  781. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  782. {
  783. fout << "\t\t\t\tSubSystem=\"2\"\n";
  784. }
  785. else
  786. {
  787. fout << "\t\t\t\tSubSystem=\"1\"\n";
  788. }
  789. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  790. if(!linkLanguage)
  791. {
  792. cmSystemTools::Error("CMake can not determine linker language for target:",
  793. target.GetName());
  794. return;
  795. }
  796. std::string stackVar = "CMAKE_";
  797. stackVar += linkLanguage;
  798. stackVar += "_STACK_SIZE";
  799. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  800. if(stackVal)
  801. {
  802. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"";
  803. }
  804. fout << "/>\n";
  805. break;
  806. }
  807. case cmTarget::UTILITY:
  808. case cmTarget::GLOBAL_TARGET:
  809. break;
  810. }
  811. }
  812. void cmLocalVisualStudio7Generator::OutputModuleDefinitionFile(std::ostream& fout,
  813. cmTarget &target)
  814. {
  815. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  816. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  817. i != classes.end(); i++)
  818. {
  819. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  820. {
  821. fout << "\t\t\t\tModuleDefinitionFile=\""
  822. << this->ConvertToXMLOutputPath((*i)->GetFullPath().c_str())
  823. << "\"\n";
  824. return;
  825. }
  826. }
  827. }
  828. //----------------------------------------------------------------------------
  829. void
  830. cmLocalVisualStudio7Generator
  831. ::OutputLibraries(std::ostream& fout,
  832. std::vector<cmStdString> const& libs)
  833. {
  834. for(std::vector<cmStdString>::const_iterator l = libs.begin();
  835. l != libs.end(); ++l)
  836. {
  837. fout << this->ConvertToXMLOutputPath(l->c_str()) << " ";
  838. }
  839. }
  840. //----------------------------------------------------------------------------
  841. void
  842. cmLocalVisualStudio7Generator
  843. ::OutputLibraryDirectories(std::ostream& fout,
  844. std::vector<cmStdString> const& dirs)
  845. {
  846. const char* comma = "";
  847. for(std::vector<cmStdString>::const_iterator d = dirs.begin();
  848. d != dirs.end(); ++d)
  849. {
  850. std::string dir = *d;
  851. if(!dir.empty())
  852. {
  853. if(dir[dir.size()-1] != '/')
  854. {
  855. dir += "/";
  856. }
  857. dir += "$(OutDir)";
  858. fout << comma << this->ConvertToXMLOutputPath(dir.c_str())
  859. << "," << this->ConvertToXMLOutputPath(d->c_str());
  860. comma = ",";
  861. }
  862. }
  863. }
  864. //----------------------------------------------------------------------------
  865. void cmLocalVisualStudio7Generator::OutputDefineFlags(const char* flags,
  866. std::ostream& fout)
  867. {
  868. std::string defs = flags;
  869. cmSystemTools::ReplaceString(defs, "/D","-D");
  870. std::string::size_type pos = defs.find("-D");
  871. bool done = pos == std::string::npos;
  872. if(!done)
  873. {
  874. fout << ",";
  875. }
  876. while(!done)
  877. {
  878. std::string::size_type nextpos = defs.find("-D", pos+2);
  879. std::string define;
  880. if(nextpos != std::string::npos)
  881. {
  882. define = defs.substr(pos+2, nextpos - pos -3);
  883. }
  884. else
  885. {
  886. define = defs.substr(pos+2);
  887. done = true;
  888. }
  889. // Double-quotes in the value of the definition must be escaped
  890. // with a backslash. The entire definition should be quoted in
  891. // the generated xml attribute to avoid confusing the VS parser.
  892. cmSystemTools::ReplaceString(define, "\"", "\\&quot;");
  893. fout << "&quot;" << define << "&quot;,";
  894. if(!done)
  895. {
  896. pos = defs.find("-D", nextpos);
  897. }
  898. }
  899. }
  900. void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
  901. const char *libName,
  902. cmTarget &target)
  903. {
  904. // get the configurations
  905. std::vector<std::string> *configs =
  906. static_cast<cmGlobalVisualStudio7Generator *>
  907. (this->GlobalGenerator)->GetConfigurations();
  908. // trace the visual studio dependencies
  909. std::string name = libName;
  910. name += ".vcproj.cmake";
  911. // We may be modifying the source groups temporarily, so make a copy.
  912. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  913. // get the classes from the source lists then add them to the groups
  914. std::vector<cmSourceFile*> & classes = target.GetSourceFiles();
  915. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  916. i != classes.end(); i++)
  917. {
  918. // Add the file to the list of sources.
  919. std::string source = (*i)->GetFullPath();
  920. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  921. {
  922. this->ModuleDefinitionFile = (*i)->GetFullPath();
  923. }
  924. cmSourceGroup& sourceGroup =
  925. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  926. sourceGroup.AssignSource(*i);
  927. }
  928. // open the project
  929. this->WriteProjectStart(fout, libName, target, sourceGroups);
  930. // write the configuration information
  931. this->WriteConfigurations(fout, libName, target);
  932. fout << "\t<Files>\n";
  933. // Loop through every source group.
  934. for(unsigned int i = 0; i < sourceGroups.size(); ++i)
  935. {
  936. cmSourceGroup sg = sourceGroups[i];
  937. this->WriteGroup(&sg, target, fout, libName, configs);
  938. }
  939. //}
  940. fout << "\t</Files>\n";
  941. // Write the VCProj file's footer.
  942. this->WriteVCProjFooter(fout);
  943. }
  944. void cmLocalVisualStudio7Generator::WriteGroup(const cmSourceGroup *sg, cmTarget target, std::ostream &fout, const char *libName, std::vector<std::string> *configs)
  945. {
  946. const std::vector<const cmSourceFile *> &sourceFiles =
  947. sg->GetSourceFiles();
  948. // If the group is empty, don't write it at all.
  949. if(sourceFiles.empty() && sg->GetGroupChildren().empty())
  950. {
  951. return;
  952. }
  953. // If the group has a name, write the header.
  954. std::string name = sg->GetName();
  955. if(name != "")
  956. {
  957. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  958. }
  959. // Loop through each source in the source group.
  960. for(std::vector<const cmSourceFile *>::const_iterator sf =
  961. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  962. {
  963. std::string source = (*sf)->GetFullPath();
  964. const cmCustomCommand *command = (*sf)->GetCustomCommand();
  965. std::string compileFlags;
  966. std::string additionalDeps;
  967. // Add per-source flags.
  968. const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS");
  969. if(cflags)
  970. {
  971. compileFlags += " ";
  972. compileFlags += cflags;
  973. }
  974. const char* lang =
  975. this->GlobalGenerator->GetLanguageFromExtension((*sf)->GetSourceExtension().c_str());
  976. if(lang && strcmp(lang, "CXX") == 0)
  977. {
  978. // force a C++ file type
  979. compileFlags += " /TP ";
  980. }
  981. // Check for extra object-file dependencies.
  982. const char* deps = (*sf)->GetProperty("OBJECT_DEPENDS");
  983. if(deps)
  984. {
  985. std::vector<std::string> depends;
  986. cmSystemTools::ExpandListArgument(deps, depends);
  987. if(!depends.empty())
  988. {
  989. std::vector<std::string>::iterator i = depends.begin();
  990. additionalDeps = this->ConvertToXMLOutputPath(i->c_str());
  991. for(++i;i != depends.end(); ++i)
  992. {
  993. additionalDeps += ";";
  994. additionalDeps += this->ConvertToXMLOutputPath(i->c_str());
  995. }
  996. }
  997. }
  998. if (source != libName || target.GetType() == cmTarget::UTILITY ||
  999. target.GetType() == cmTarget::GLOBAL_TARGET )
  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 *>(this->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::GLOBAL_TARGET)
  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(this->Version == 71)
  1216. {
  1217. fout << "\tVersion=\"7.10\"\n";
  1218. }
  1219. else
  1220. {
  1221. if (this->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 *>(this->GlobalGenerator);
  1242. fout << "\tName=\"" << projLabel << "\"\n";
  1243. if(this->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 = this->Makefile->GetTargets();
  1292. cmGlobalVisualStudio7Generator* gg =
  1293. static_cast<cmGlobalVisualStudio7Generator *>(this->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. }