cmLocalVisualStudio7Generator.cxx 35 KB

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