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