cmLocalVisualStudio7Generator.cxx 49 KB

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