cmLocalVisualStudio7Generator.cxx 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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_Version71 = false;
  23. }
  24. cmLocalVisualStudio7Generator::~cmLocalVisualStudio7Generator()
  25. {
  26. }
  27. void cmLocalVisualStudio7Generator::Generate(bool /* fromTheTop */)
  28. {
  29. this->OutputVCProjFile();
  30. }
  31. // TODO
  32. // for CommandLine= need to repleace quotes with &quot
  33. // write out configurations
  34. void cmLocalVisualStudio7Generator::OutputVCProjFile()
  35. {
  36. // If not an in source build, then create the output directory
  37. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  38. m_Makefile->GetHomeDirectory()) != 0)
  39. {
  40. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  41. {
  42. cmSystemTools::Error("Error creating directory ",
  43. m_Makefile->GetStartOutputDirectory());
  44. }
  45. }
  46. m_LibraryOutputPath = "";
  47. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  48. {
  49. m_LibraryOutputPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  50. }
  51. if(m_LibraryOutputPath.size())
  52. {
  53. // make sure there is a trailing slash
  54. if(m_LibraryOutputPath[m_LibraryOutputPath.size()-1] != '/')
  55. {
  56. m_LibraryOutputPath += "/";
  57. }
  58. }
  59. m_ExecutableOutputPath = "";
  60. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  61. {
  62. m_ExecutableOutputPath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  63. }
  64. if(m_ExecutableOutputPath.size())
  65. {
  66. // make sure there is a trailing slash
  67. if(m_ExecutableOutputPath[m_ExecutableOutputPath.size()-1] != '/')
  68. {
  69. m_ExecutableOutputPath += "/";
  70. }
  71. }
  72. // Create the VCProj or set of VCProj's for libraries and executables
  73. // clear project names
  74. m_CreatedProjectNames.clear();
  75. // expand vars for custom commands
  76. m_Makefile->ExpandVariablesInCustomCommands();
  77. // build any targets
  78. cmTargets &tgts = m_Makefile->GetTargets();
  79. for(cmTargets::iterator l = tgts.begin();
  80. l != tgts.end(); l++)
  81. {
  82. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  83. // so don't build a projectfile for it
  84. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  85. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  86. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  87. {
  88. this->CreateSingleVCProj(l->first.c_str(),l->second);
  89. }
  90. }
  91. }
  92. void cmLocalVisualStudio7Generator::CreateSingleVCProj(const char *lname, cmTarget &target)
  93. {
  94. // add to the list of projects
  95. std::string pname = lname;
  96. m_CreatedProjectNames.push_back(pname);
  97. // create the dsp.cmake file
  98. std::string fname;
  99. fname = m_Makefile->GetStartOutputDirectory();
  100. fname += "/";
  101. fname += lname;
  102. fname += ".vcproj";
  103. // save the name of the real dsp file
  104. std::string realVCProj = fname;
  105. fname += ".cmake";
  106. std::ofstream fout(fname.c_str());
  107. if(!fout)
  108. {
  109. cmSystemTools::Error("Error Writing ", fname.c_str());
  110. }
  111. this->WriteVCProjFile(fout,lname,target);
  112. fout.close();
  113. // if the dsp file has changed, then write it.
  114. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realVCProj.c_str());
  115. }
  116. void cmLocalVisualStudio7Generator::AddVCProjBuildRule()
  117. {
  118. std::string dspname = *(m_CreatedProjectNames.end()-1);
  119. if(dspname == "ALL_BUILD")
  120. {
  121. return;
  122. }
  123. dspname += ".vcproj.cmake";
  124. std::string makefileIn = m_Makefile->GetStartDirectory();
  125. makefileIn += "/";
  126. makefileIn += "CMakeLists.txt";
  127. makefileIn = cmSystemTools::ConvertToOutputPath(makefileIn.c_str());
  128. std::string dsprule = "${CMAKE_COMMAND}";
  129. m_Makefile->ExpandVariablesInString(dsprule);
  130. dsprule = cmSystemTools::ConvertToOutputPath(dsprule.c_str());
  131. std::vector<std::string> argv;
  132. argv.push_back(makefileIn);
  133. makefileIn = m_Makefile->GetStartDirectory();
  134. makefileIn += "/";
  135. makefileIn += "CMakeLists.txt";
  136. std::string args;
  137. args = "-H";
  138. args +=
  139. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeDirectory());
  140. argv.push_back(args);
  141. args = "-S";
  142. args +=
  143. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartDirectory());
  144. argv.push_back(args);
  145. args = "-O";
  146. args +=
  147. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartOutputDirectory());
  148. argv.push_back(args);
  149. args = "-B";
  150. args +=
  151. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory());
  152. argv.push_back(args);
  153. std::string configFile =
  154. m_Makefile->GetDefinition("CMAKE_ROOT");
  155. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  156. std::vector<std::string> listFiles = m_Makefile->GetListFiles();
  157. bool found = false;
  158. for(std::vector<std::string>::iterator i = listFiles.begin();
  159. i != listFiles.end(); ++i)
  160. {
  161. if(*i == configFile)
  162. {
  163. found = true;
  164. }
  165. }
  166. if(!found)
  167. {
  168. listFiles.push_back(configFile);
  169. }
  170. m_Makefile->AddCustomCommandToOutput(dspname.c_str(), dsprule.c_str(),
  171. argv, makefileIn.c_str(), listFiles,
  172. NULL, true);
  173. }
  174. void cmLocalVisualStudio7Generator::WriteConfigurations(std::ostream& fout,
  175. const char *libName,
  176. const cmTarget &target)
  177. {
  178. std::vector<std::string> *configs =
  179. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator)->GetConfigurations();
  180. fout << "\t<Configurations>\n";
  181. for( std::vector<std::string>::iterator i = configs->begin();
  182. i != configs->end(); ++i)
  183. {
  184. this->WriteConfiguration(fout, i->c_str(), libName, target);
  185. }
  186. fout << "\t</Configurations>\n";
  187. }
  188. void cmLocalVisualStudio7Generator::WriteConfiguration(std::ostream& fout,
  189. const char* configName,
  190. const char *libName,
  191. const cmTarget &target)
  192. {
  193. const char* mfcFlag = m_Makefile->GetDefinition("CMAKE_MFC_FLAG");
  194. if(!mfcFlag)
  195. {
  196. mfcFlag = "0";
  197. }
  198. fout << "\t\t<Configuration\n"
  199. << "\t\t\tName=\"" << configName << "|Win32\"\n"
  200. << "\t\t\tOutputDirectory=\"" << configName << "\"\n";
  201. // This is an internal type to Visual Studio, it seems that:
  202. // 4 == static library
  203. // 2 == dll
  204. // 1 == executable
  205. // 10 == utility
  206. const char* configType = "10";
  207. switch(target.GetType())
  208. {
  209. case cmTarget::STATIC_LIBRARY:
  210. configType = "4";
  211. break;
  212. case cmTarget::SHARED_LIBRARY:
  213. case cmTarget::MODULE_LIBRARY:
  214. configType = "2";
  215. break;
  216. case cmTarget::EXECUTABLE:
  217. case cmTarget::WIN32_EXECUTABLE:
  218. configType = "1";
  219. break;
  220. case cmTarget::UTILITY:
  221. configType = "10";
  222. default:
  223. break;
  224. }
  225. fout << "\t\t\tIntermediateDirectory=\".\\" << configName << "\"\n"
  226. << "\t\t\tConfigurationType=\"" << configType << "\"\n"
  227. << "\t\t\tUseOfMFC=\"" << mfcFlag << "\"\n"
  228. << "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n"
  229. << "\t\t\tCharacterSet=\"2\">\n";
  230. fout << "\t\t\t<Tool\n"
  231. << "\t\t\t\tName=\"VCCLCompilerTool\"\n"
  232. << "\t\t\t\tAdditionalOptions=\"";
  233. std::string flags;
  234. if(target.HasCxx())
  235. {
  236. flags = m_Makefile->GetDefinition("CMAKE_CXX_FLAGS");
  237. }
  238. else
  239. {
  240. if(m_Makefile->GetDefinition("CMAKE_C_FLAGS"))
  241. {
  242. flags = m_Makefile->GetDefinition("CMAKE_C_FLAGS");
  243. }
  244. }
  245. cmSystemTools::ReplaceString(flags, "\"", "&quot;");
  246. fout << flags;
  247. fout << " -DCMAKE_INTDIR=\\&quot;" << configName << "\\&quot;"
  248. << "\"\n";
  249. fout << "\t\t\t\tAdditionalIncludeDirectories=\"";
  250. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  251. std::vector<std::string>::iterator i = includes.begin();
  252. for(;i != includes.end(); ++i)
  253. {
  254. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  255. fout << ipath << ";";
  256. }
  257. fout << "\"\n";
  258. // Optimization = 0 None Debug /O0
  259. // Optimization = 1 MinSize /O1
  260. // Optimization = 2 MaxSpeed /O2
  261. // Optimization = 3 Max Optimization /O3
  262. // RuntimeLibrary = 0 /MT multithread
  263. // RuntimeLibrary = 1 /MTd multithread debug
  264. // RuntimeLibrary = 2 /MD multithread dll
  265. // RuntimeLibrary = 3 /MDd multithread dll debug
  266. // RuntimeLibrary = 4 /ML single thread
  267. // RuntimeLibrary = 5 /MLd single thread debug
  268. // InlineFunctionExpansion = 0 none
  269. // InlineFunctionExpansion = 1 when inline keyword
  270. // InlineFunctionExpansion = 2 any time you can
  271. if(strcmp(configName, "Debug") == 0)
  272. {
  273. fout << "\t\t\t\tOptimization=\"0\"\n"
  274. << "\t\t\t\tRuntimeLibrary=\"3\"\n"
  275. << "\t\t\t\tInlineFunctionExpansion=\"0\"\n"
  276. << "\t\t\t\tPreprocessorDefinitions=\"WIN32,_DEBUG,_WINDOWS";
  277. }
  278. else if(strcmp(configName, "Release") == 0)
  279. {
  280. fout << "\t\t\t\tOptimization=\"2\"\n"
  281. << "\t\t\t\tRuntimeLibrary=\"2\"\n"
  282. << "\t\t\t\tInlineFunctionExpansion=\"1\"\n"
  283. << "\t\t\t\tPreprocessorDefinitions=\"WIN32,NDEBUG,_WINDOWS";
  284. }
  285. else if(strcmp(configName, "MinSizeRel") == 0)
  286. {
  287. fout << "\t\t\t\tOptimization=\"1\"\n"
  288. << "\t\t\t\tRuntimeLibrary=\"2\"\n"
  289. << "\t\t\t\tInlineFunctionExpansion=\"1\"\n"
  290. << "\t\t\t\tPreprocessorDefinitions=\"WIN32,NDEBUG,_WINDOWS";
  291. }
  292. else if(strcmp(configName, "RelWithDebInfo") == 0)
  293. {
  294. fout << "\t\t\t\tOptimization=\"2\"\n"
  295. << "\t\t\t\tRuntimeLibrary=\"2\"\n"
  296. << "\t\t\t\tInlineFunctionExpansion=\"1\"\n"
  297. << "\t\t\t\tPreprocessorDefinitions=\"WIN32,NDEBUG,_WINDOWS";
  298. }
  299. if(target.GetType() == cmTarget::SHARED_LIBRARY
  300. || target.GetType() == cmTarget::MODULE_LIBRARY)
  301. {
  302. std::string exportSymbol;
  303. if (const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  304. {
  305. exportSymbol = custom_export_name;
  306. }
  307. else
  308. {
  309. std::string id = libName;
  310. id += "_EXPORTS";
  311. exportSymbol = cmSystemTools::MakeCindentifier(id.c_str());
  312. }
  313. fout << "," << exportSymbol;
  314. }
  315. this->OutputDefineFlags(fout);
  316. fout << "\"\n";
  317. if(m_Makefile->IsOn("CMAKE_CXX_USE_RTTI"))
  318. {
  319. fout << "\t\t\t\tRuntimeTypeInfo=\"TRUE\"\n";
  320. }
  321. fout << "\t\t\t\tAssemblerListingLocation=\"" << configName << "\"\n";
  322. fout << "\t\t\t\tObjectFile=\"" << configName << "\\\"\n";
  323. if ( m_Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL") )
  324. {
  325. fout << "\t\t\t\tWarningLevel=\"" << m_Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL") << "\"\n";
  326. }
  327. fout << "\t\t\t\tDebugInformationFormat=\"3\"";
  328. fout << "/>\n"; // end of <Tool Name=VCCLCompilerTool
  329. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCCustomBuildTool\"/>\n";
  330. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCResourceCompilerTool\"\n"
  331. << "AdditionalIncludeDirectories=\"";
  332. for(i = includes.begin();i != includes.end(); ++i)
  333. {
  334. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  335. fout << ipath << ";";
  336. }
  337. fout << "\"\n/>\n";
  338. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCMIDLTool\"/>\n";
  339. this->OutputTargetRules(fout, target, libName);
  340. this->OutputBuildTool(fout, configName, libName, target);
  341. fout << "\t\t</Configuration>\n";
  342. }
  343. void cmLocalVisualStudio7Generator::OutputBuildTool(std::ostream& fout,
  344. const char* configName,
  345. const char *libName,
  346. const cmTarget &target)
  347. {
  348. std::string temp;
  349. std::string debugPostfix = "";
  350. bool debug = !strcmp(configName,"Debug");
  351. if (debug && m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX"))
  352. {
  353. debugPostfix = m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  354. }
  355. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  356. switch(target.GetType())
  357. {
  358. case cmTarget::STATIC_LIBRARY:
  359. {
  360. std::string libpath = m_LibraryOutputPath +
  361. "$(OutDir)/" + libName + debugPostfix + ".lib";
  362. fout << "\t\t\t<Tool\n"
  363. << "\t\t\t\tName=\"VCLibrarianTool\"\n"
  364. << "\t\t\t\t\tOutputFile=\""
  365. << this->ConvertToXMLOutputPathSingle(libpath.c_str()) << ".\"/>\n";
  366. break;
  367. }
  368. case cmTarget::SHARED_LIBRARY:
  369. case cmTarget::MODULE_LIBRARY:
  370. fout << "\t\t\t<Tool\n"
  371. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  372. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386";
  373. if(targetLinkFlags)
  374. {
  375. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  376. targetLinkFlags).c_str();
  377. }
  378. fout << "\"\n"
  379. << "\t\t\t\tAdditionalDependencies=\" odbc32.lib odbccp32.lib ";
  380. this->OutputLibraries(fout, configName, libName, target);
  381. fout << "\"\n";
  382. temp = m_LibraryOutputPath;
  383. temp += configName;
  384. temp += "/";
  385. temp += libName;
  386. temp += debugPostfix;
  387. temp += ".dll";
  388. fout << "\t\t\t\tOutputFile=\""
  389. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  390. fout << "\t\t\t\tLinkIncremental=\"1\"\n";
  391. fout << "\t\t\t\tSuppressStartupBanner=\"TRUE\"\n";
  392. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  393. this->OutputLibraryDirectories(fout, configName, libName, target);
  394. fout << "\"\n";
  395. this->OutputModuleDefinitionFile(fout, target);
  396. temp = m_LibraryOutputPath;
  397. temp += "$(OutDir)/";
  398. temp += libName;
  399. temp += debugPostfix;
  400. temp += ".pdb";
  401. fout << "\t\t\t\tProgramDatabaseFile=\"" <<
  402. this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  403. if(strcmp(configName, "Debug") == 0
  404. || strcmp(configName, "RelWithDebInfo") == 0)
  405. {
  406. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  407. }
  408. fout << "\t\t\t\tStackReserveSize=\""
  409. << m_Makefile->GetDefinition("CMAKE_CXX_STACK_SIZE") << "\"\n";
  410. temp = m_ExecutableOutputPath;
  411. temp += configName;
  412. temp += "/";
  413. temp += libName;
  414. temp += debugPostfix;
  415. temp += ".lib";
  416. fout << "\t\t\t\tImportLibrary=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
  417. break;
  418. case cmTarget::EXECUTABLE:
  419. case cmTarget::WIN32_EXECUTABLE:
  420. fout << "\t\t\t<Tool\n"
  421. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  422. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386";
  423. if(targetLinkFlags)
  424. {
  425. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  426. targetLinkFlags).c_str();
  427. }
  428. fout << "\"\n"
  429. << "\t\t\t\tAdditionalDependencies=\" odbc32.lib odbccp32.lib ";
  430. this->OutputLibraries(fout, configName, libName, target);
  431. fout << "\"\n";
  432. temp = m_ExecutableOutputPath;
  433. temp += configName;
  434. temp += "/";
  435. temp += libName;
  436. temp += ".exe";
  437. fout << "\t\t\t\tOutputFile=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  438. fout << "\t\t\t\tLinkIncremental=\"1\"\n";
  439. fout << "\t\t\t\tSuppressStartupBanner=\"TRUE\"\n";
  440. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  441. this->OutputLibraryDirectories(fout, configName, libName, target);
  442. fout << "\"\n";
  443. fout << "\t\t\t\tProgramDatabaseFile=\"" << m_LibraryOutputPath
  444. << "$(OutDir)\\" << libName << ".pdb\"\n";
  445. if(strcmp(configName, "Debug") == 0
  446. || strcmp(configName, "RelWithDebInfo") == 0)
  447. {
  448. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  449. }
  450. if( target.GetType() == cmTarget::EXECUTABLE)
  451. {
  452. fout << "\t\t\t\tSubSystem=\"1\"\n";
  453. }
  454. else
  455. {
  456. fout << "\t\t\t\tSubSystem=\"2\"\n";
  457. }
  458. fout << "\t\t\t\tStackReserveSize=\""
  459. << m_Makefile->GetDefinition("CMAKE_CXX_STACK_SIZE") << "\"/>\n";
  460. break;
  461. case cmTarget::UTILITY:
  462. break;
  463. }
  464. }
  465. void cmLocalVisualStudio7Generator::OutputModuleDefinitionFile(std::ostream& fout,
  466. const cmTarget &target)
  467. {
  468. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  469. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  470. i != classes.end(); i++)
  471. {
  472. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  473. {
  474. fout << "\t\t\t\tModuleDefinitionFile=\""
  475. << this->ConvertToXMLOutputPath((*i)->GetFullPath().c_str())
  476. << "\"\n";
  477. return;
  478. }
  479. }
  480. }
  481. void cmLocalVisualStudio7Generator::OutputLibraryDirectories(std::ostream& fout,
  482. const char*,
  483. const char*,
  484. const cmTarget &tgt)
  485. {
  486. bool hasone = false;
  487. if(m_LibraryOutputPath.size())
  488. {
  489. hasone = true;
  490. std::string temp = m_LibraryOutputPath;
  491. temp += "$(INTDIR)";
  492. fout << this->ConvertToXMLOutputPath(temp.c_str()) << "," <<
  493. this->ConvertToXMLOutputPath(m_LibraryOutputPath.c_str());
  494. }
  495. if(m_ExecutableOutputPath.size() &&
  496. (m_LibraryOutputPath != m_ExecutableOutputPath))
  497. {
  498. if (hasone)
  499. {
  500. fout << ",";
  501. }
  502. hasone = true;
  503. std::string temp = m_ExecutableOutputPath;
  504. temp += "$(INTDIR)";
  505. fout << this->ConvertToXMLOutputPath(temp.c_str()) << "," <<
  506. this->ConvertToXMLOutputPath(m_ExecutableOutputPath.c_str());
  507. }
  508. std::set<std::string> pathEmitted;
  509. std::vector<std::string>::const_iterator i;
  510. const std::vector<std::string>& libdirs = tgt.GetLinkDirectories();
  511. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  512. {
  513. std::string lpath = *i;
  514. if(lpath[lpath.size()-1] != '/')
  515. {
  516. lpath += "/";
  517. }
  518. if(pathEmitted.insert(lpath).second)
  519. {
  520. if(hasone)
  521. {
  522. fout << ",";
  523. }
  524. std::string lpathi = lpath + "$(INTDIR)";
  525. fout << this->ConvertToXMLOutputPath(lpathi.c_str()) << "," <<
  526. this->ConvertToXMLOutputPath(lpath.c_str());
  527. hasone = true;
  528. }
  529. }
  530. }
  531. void cmLocalVisualStudio7Generator::OutputLibraries(std::ostream& fout,
  532. const char* configName,
  533. const char* libName,
  534. const cmTarget &target)
  535. {
  536. const cmTarget::LinkLibraries& libs = target.GetLinkLibraries();
  537. cmTarget::LinkLibraries::const_iterator j;
  538. for(j = libs.begin(); j != libs.end(); ++j)
  539. {
  540. if(j->first != libName)
  541. {
  542. std::string lib = j->first;
  543. std::string debugPostfix = "";
  544. // if this is a library we are building then watch for a debugPostfix
  545. if (!strcmp(configName,"Debug"))
  546. {
  547. std::string libPath = j->first + "_CMAKE_PATH";
  548. const char* cacheValue
  549. = m_GlobalGenerator->GetCMakeInstance()->GetCacheDefinition(libPath.c_str());
  550. if(cacheValue && m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX"))
  551. {
  552. debugPostfix = m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  553. }
  554. }
  555. if(j->first.find(".lib") == std::string::npos)
  556. {
  557. lib += debugPostfix + ".lib";
  558. }
  559. lib = this->ConvertToXMLOutputPath(lib.c_str());
  560. if (j->second == cmTarget::GENERAL
  561. || (j->second == cmTarget::DEBUG && strcmp(configName, "Debug") == 0)
  562. || (j->second == cmTarget::OPTIMIZED && strcmp(configName, "Debug") != 0))
  563. {
  564. fout << lib << " ";
  565. }
  566. }
  567. }
  568. }
  569. void cmLocalVisualStudio7Generator::OutputDefineFlags(std::ostream& fout)
  570. {
  571. std::string defs = m_Makefile->GetDefineFlags();
  572. std::string::size_type pos = defs.find("-D");
  573. bool done = pos == std::string::npos;
  574. if(!done)
  575. {
  576. fout << ",";
  577. }
  578. while(!done)
  579. {
  580. std::string::size_type nextpos = defs.find("-D", pos+2);
  581. std::string define;
  582. if(nextpos != std::string::npos)
  583. {
  584. define = defs.substr(pos+2, nextpos - pos -3);
  585. }
  586. else
  587. {
  588. define = defs.substr(pos+2);
  589. done = true;
  590. }
  591. cmSystemTools::ReplaceString(define, "\"", "&quot;");
  592. fout << define << ",";
  593. if(!done)
  594. {
  595. pos = defs.find("-D", nextpos);
  596. }
  597. }
  598. }
  599. void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
  600. const char *libName,
  601. cmTarget &target)
  602. {
  603. // get the configurations
  604. std::vector<std::string> *configs =
  605. static_cast<cmGlobalVisualStudio7Generator *>
  606. (m_GlobalGenerator)->GetConfigurations();
  607. // if we should add regen rule then...
  608. const char *suppRegenRule =
  609. m_Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  610. if (!cmSystemTools::IsOn(suppRegenRule))
  611. {
  612. this->AddVCProjBuildRule();
  613. }
  614. // trace the visual studio dependencies
  615. std::string name = libName;
  616. name += ".vcproj.cmake";
  617. target.TraceVSDependencies(name, m_Makefile);
  618. // We may be modifying the source groups temporarily, so make a copy.
  619. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  620. // get the classes from the source lists then add them to the groups
  621. std::vector<cmSourceFile*> & classes = target.GetSourceFiles();
  622. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  623. i != classes.end(); i++)
  624. {
  625. // Add the file to the list of sources.
  626. std::string source = (*i)->GetFullPath();
  627. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  628. {
  629. m_ModuleDefinitionFile = (*i)->GetFullPath();
  630. }
  631. cmSourceGroup& sourceGroup =
  632. m_Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  633. sourceGroup.AddSource(source.c_str(), *i);
  634. }
  635. // open the project
  636. this->WriteProjectStart(fout, libName, target, sourceGroups);
  637. // write the configuration information
  638. this->WriteConfigurations(fout, libName, target);
  639. fout << "\t<Files>\n";
  640. // Loop through every source group.
  641. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  642. sg != sourceGroups.end(); ++sg)
  643. {
  644. const std::vector<const cmSourceFile *> &sourceFiles =
  645. sg->GetSourceFiles();
  646. // If the group is empty, don't write it at all.
  647. if(sourceFiles.empty())
  648. {
  649. continue;
  650. }
  651. // If the group has a name, write the header.
  652. std::string name = sg->GetName();
  653. if(name != "")
  654. {
  655. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  656. }
  657. // Loop through each source in the source group.
  658. for(std::vector<const cmSourceFile *>::const_iterator sf =
  659. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  660. {
  661. std::string source = (*sf)->GetFullPath();
  662. const cmCustomCommand *command = (*sf)->GetCustomCommand();
  663. std::string compileFlags;
  664. std::string additionalDeps;
  665. // Check for extra compiler flags.
  666. const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS");
  667. if(cflags)
  668. {
  669. compileFlags = cflags;
  670. }
  671. if(cmSystemTools::GetFileFormat((*sf)->GetSourceExtension().c_str())
  672. == cmSystemTools::CXX_FILE_FORMAT)
  673. {
  674. // force a C++ file type
  675. compileFlags += " /TP ";
  676. }
  677. // Check for extra object-file dependencies.
  678. const char* deps = (*sf)->GetProperty("OBJECT_DEPENDS");
  679. if(deps)
  680. {
  681. std::vector<std::string> depends;
  682. cmSystemTools::ExpandListArgument(deps, depends);
  683. if(!depends.empty())
  684. {
  685. std::vector<std::string>::iterator i = depends.begin();
  686. additionalDeps = this->ConvertToXMLOutputPath(i->c_str());
  687. for(++i;i != depends.end(); ++i)
  688. {
  689. additionalDeps += ";";
  690. additionalDeps += this->ConvertToXMLOutputPath(i->c_str());
  691. }
  692. }
  693. }
  694. if (source != libName || target.GetType() == cmTarget::UTILITY)
  695. {
  696. fout << "\t\t\t<File\n";
  697. std::string d = this->ConvertToXMLOutputPathSingle(source.c_str());
  698. // Tell MS-Dev what the source is. If the compiler knows how to
  699. // build it, then it will.
  700. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  701. if (command)
  702. {
  703. std::string totalCommandStr;
  704. totalCommandStr =
  705. cmSystemTools::ConvertToOutputPath(command->GetCommand().c_str());
  706. totalCommandStr += " ";
  707. totalCommandStr += command->GetArguments();
  708. totalCommandStr += "\n";
  709. const char* comment = command->GetComment().c_str();
  710. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  711. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  712. (*comment?comment:"Custom Rule"),
  713. command->GetDepends(),
  714. command->GetOutput().c_str(), flags);
  715. }
  716. else if(compileFlags.size() || additionalDeps.length())
  717. {
  718. for(std::vector<std::string>::iterator i = configs->begin();
  719. i != configs->end(); ++i)
  720. {
  721. fout << "\t\t\t\t<FileConfiguration\n"
  722. << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n"
  723. << "\t\t\t\t\t<Tool\n"
  724. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n";
  725. if(compileFlags.size())
  726. {
  727. fout << "\t\t\t\t\tAdditionalOptions=\""
  728. << compileFlags << "\"\n";
  729. }
  730. if(additionalDeps.length())
  731. {
  732. fout << "\t\t\t\t\tAdditionalDependencies=\""
  733. << additionalDeps.c_str() << "\"\n";
  734. }
  735. fout << "\t\t\t\t\t/>\n"
  736. << "\t\t\t\t</FileConfiguration>\n";
  737. }
  738. }
  739. fout << "\t\t\t</File>\n";
  740. }
  741. }
  742. // If the group has a name, write the footer.
  743. if(name != "")
  744. {
  745. this->WriteVCProjEndGroup(fout);
  746. }
  747. }
  748. fout << "\t</Files>\n";
  749. // Write the VCProj file's footer.
  750. this->WriteVCProjFooter(fout);
  751. }
  752. void cmLocalVisualStudio7Generator::
  753. WriteCustomRule(std::ostream& fout,
  754. const char* source,
  755. const char* command,
  756. const char* comment,
  757. const std::vector<std::string>& depends,
  758. const char *output,
  759. const char* compileFlags)
  760. {
  761. std::string cmd = command;
  762. cmSystemTools::ReplaceString(cmd, "\"", "&quot;");
  763. std::vector<std::string>::iterator i;
  764. std::vector<std::string> *configs =
  765. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator)->GetConfigurations();
  766. for(i = configs->begin(); i != configs->end(); ++i)
  767. {
  768. fout << "\t\t\t\t<FileConfiguration\n";
  769. fout << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n";
  770. if(compileFlags)
  771. {
  772. fout << "\t\t\t\t\t<Tool\n"
  773. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  774. << "\t\t\t\t\tAdditionalOptions=\""
  775. << compileFlags << "\"/>\n";
  776. }
  777. fout << "\t\t\t\t\t<Tool\n"
  778. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  779. << "\t\t\t\t\tDescription=\"Building " << comment;
  780. fout << " " << output;
  781. fout << "\"\n"
  782. << "\t\t\t\t\tCommandLine=\"" << cmd << "\n\"\n"
  783. << "\t\t\t\t\tAdditionalDependencies=\"";
  784. // Write out the dependencies for the rule.
  785. std::string temp;
  786. for(std::vector<std::string>::const_iterator d = depends.begin();
  787. d != depends.end(); ++d)
  788. {
  789. fout << this->ConvertToXMLOutputPath(d->c_str())
  790. << ";";
  791. }
  792. fout << "\"\n";
  793. fout << "\t\t\t\t\tOutputs=\"";
  794. if(output == 0)
  795. {
  796. fout << source << "_force";
  797. }
  798. // Write a rule for the output generated by this command.
  799. fout << this->ConvertToXMLOutputPathSingle(output);
  800. fout << "\"/>\n";
  801. fout << "\t\t\t\t</FileConfiguration>\n";
  802. }
  803. }
  804. void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
  805. const char* group,
  806. const char* )
  807. {
  808. fout << "\t\t<Filter\n"
  809. << "\t\t\tName=\"" << group << "\"\n"
  810. << "\t\t\tFilter=\"\">\n";
  811. }
  812. void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
  813. {
  814. fout << "\t\t</Filter>\n";
  815. }
  816. // look for custom rules on a target and collect them together
  817. void cmLocalVisualStudio7Generator::OutputTargetRules(std::ostream& fout,
  818. const cmTarget &target,
  819. const char * /* libName */)
  820. {
  821. if (target.GetType() > cmTarget::UTILITY)
  822. {
  823. return;
  824. }
  825. // add the pre build rules
  826. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"";
  827. bool init = false;
  828. for (std::vector<cmCustomCommand>::const_iterator cr =
  829. target.GetPreBuildCommands().begin();
  830. cr != target.GetPreBuildCommands().end(); ++cr)
  831. {
  832. cmCustomCommand cc(*cr);
  833. cc.ExpandVariables(*m_Makefile);
  834. if(!init)
  835. {
  836. fout << "\nCommandLine=\"";
  837. init = true;
  838. }
  839. std::string args = cc.GetArguments();
  840. cmSystemTools::ReplaceString(args, "\"", "&quot;");
  841. fout << this->ConvertToXMLOutputPath(cc.GetCommand().c_str()) << " " <<
  842. args << "\n";
  843. }
  844. if (init)
  845. {
  846. fout << "\"";
  847. }
  848. fout << "/>\n";
  849. // add the pre Link rules
  850. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreLinkEventTool\"";
  851. init = false;
  852. for (std::vector<cmCustomCommand>::const_iterator cr =
  853. target.GetPreLinkCommands().begin();
  854. cr != target.GetPreLinkCommands().end(); ++cr)
  855. {
  856. cmCustomCommand cc(*cr);
  857. cc.ExpandVariables(*m_Makefile);
  858. if(!init)
  859. {
  860. fout << "\nCommandLine=\"";
  861. init = true;
  862. }
  863. std::string args = cc.GetArguments();
  864. cmSystemTools::ReplaceString(args, "\"", "&quot;");
  865. fout << this->ConvertToXMLOutputPath(cc.GetCommand().c_str()) << " " <<
  866. args << "\n";
  867. }
  868. if (init)
  869. {
  870. fout << "\"";
  871. }
  872. fout << "/>\n";
  873. // add the PostBuild rules
  874. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  875. init = false;
  876. for (std::vector<cmCustomCommand>::const_iterator cr =
  877. target.GetPostBuildCommands().begin();
  878. cr != target.GetPostBuildCommands().end(); ++cr)
  879. {
  880. cmCustomCommand cc(*cr);
  881. cc.ExpandVariables(*m_Makefile);
  882. if(!init)
  883. {
  884. fout << "\nCommandLine=\"";
  885. init = true;
  886. }
  887. std::string args = cc.GetArguments();
  888. cmSystemTools::ReplaceString(args, "\"", "&quot;");
  889. fout << this->ConvertToXMLOutputPath(cc.GetCommand().c_str()) << " " <<
  890. args << "\n";
  891. }
  892. if (init)
  893. {
  894. fout << "\"";
  895. }
  896. fout << "/>\n";
  897. }
  898. void
  899. cmLocalVisualStudio7Generator::WriteProjectStart(std::ostream& fout,
  900. const char *libName,
  901. const cmTarget &,
  902. std::vector<cmSourceGroup> &)
  903. {
  904. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  905. << "<VisualStudioProject\n"
  906. << "\tProjectType=\"Visual C++\"\n";
  907. if(m_Version71)
  908. {
  909. fout << "\tVersion=\"7.10\"\n";
  910. }
  911. else
  912. {
  913. fout << "\tVersion=\"7.00\"\n";
  914. }
  915. fout << "\tName=\"" << libName << "\"\n"
  916. << "\tSccProjectName=\"\"\n"
  917. << "\tSccLocalPath=\"\"\n"
  918. << "\tKeyword=\"Win32Proj\">\n"
  919. << "\t<Platforms>\n"
  920. << "\t\t<Platform\n\t\t\tName=\"Win32\"/>\n"
  921. << "\t</Platforms>\n";
  922. }
  923. void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout)
  924. {
  925. fout << "\t<Globals>\n"
  926. << "\t</Globals>\n"
  927. << "</VisualStudioProject>\n";
  928. }
  929. std::string cmLocalVisualStudio7Generator::EscapeForXML(const char* s)
  930. {
  931. std::string ret = s;
  932. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  933. return ret;
  934. }
  935. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPath(const char* path)
  936. {
  937. std::string ret = cmSystemTools::ConvertToOutputPath(path);
  938. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  939. return ret;
  940. }
  941. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPathSingle(const char* path)
  942. {
  943. std::string ret = cmSystemTools::ConvertToOutputPath(path);
  944. cmSystemTools::ReplaceString(ret, "\"", "");
  945. return ret;
  946. }