cmLocalVisualStudio7Generator.cxx 52 KB

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