cmLocalVisualStudio7Generator.cxx 47 KB

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