cmLocalVisualStudio7Generator.cxx 48 KB

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