cmLocalVisualStudio7Generator.cxx 50 KB

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