cmLocalVisualStudio7Generator.cxx 33 KB

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