cmLocalVisualStudio7Generator.cxx 48 KB

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