cmLocalVisualStudio7Generator.cxx 46 KB

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