cmLocalVisualStudio7Generator.cxx 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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. std::string extraLinkOptions;
  356. if((target.GetType() == cmTarget::EXECUTABLE) ||
  357. (target.GetType() == cmTarget::WIN32_EXECUTABLE))
  358. {
  359. extraLinkOptions = m_Makefile->GetDefinition("CMAKE_EXE_LINKER_FLAGS");
  360. }
  361. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  362. {
  363. extraLinkOptions = m_Makefile->GetDefinition("CMAKE_SHARED_LINKER_FLAGS");
  364. }
  365. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  366. {
  367. extraLinkOptions = m_Makefile->GetDefinition("CMAKE_MODULE_LINKER_FLAGS");
  368. }
  369. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  370. if(targetLinkFlags)
  371. {
  372. extraLinkOptions += " ";
  373. extraLinkOptions += targetLinkFlags;
  374. }
  375. switch(target.GetType())
  376. {
  377. case cmTarget::STATIC_LIBRARY:
  378. {
  379. std::string libpath = m_LibraryOutputPath +
  380. "$(OutDir)/" + libName + debugPostfix + ".lib";
  381. fout << "\t\t\t<Tool\n"
  382. << "\t\t\t\tName=\"VCLibrarianTool\"\n"
  383. << "\t\t\t\t\tOutputFile=\""
  384. << this->ConvertToXMLOutputPathSingle(libpath.c_str()) << ".\"/>\n";
  385. break;
  386. }
  387. case cmTarget::SHARED_LIBRARY:
  388. case cmTarget::MODULE_LIBRARY:
  389. fout << "\t\t\t<Tool\n"
  390. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  391. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386";
  392. if(extraLinkOptions.size())
  393. {
  394. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  395. extraLinkOptions.c_str()).c_str();
  396. }
  397. fout << "\"\n"
  398. << "\t\t\t\tAdditionalDependencies=\" odbc32.lib odbccp32.lib ";
  399. this->OutputLibraries(fout, configName, libName, target);
  400. fout << "\"\n";
  401. temp = m_LibraryOutputPath;
  402. temp += configName;
  403. temp += "/";
  404. temp += libName;
  405. temp += debugPostfix;
  406. temp += ".dll";
  407. fout << "\t\t\t\tOutputFile=\""
  408. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  409. fout << "\t\t\t\tLinkIncremental=\"1\"\n";
  410. fout << "\t\t\t\tSuppressStartupBanner=\"TRUE\"\n";
  411. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  412. this->OutputLibraryDirectories(fout, configName, libName, target);
  413. fout << "\"\n";
  414. this->OutputModuleDefinitionFile(fout, target);
  415. temp = m_LibraryOutputPath;
  416. temp += "$(OutDir)/";
  417. temp += libName;
  418. temp += debugPostfix;
  419. temp += ".pdb";
  420. fout << "\t\t\t\tProgramDatabaseFile=\"" <<
  421. this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  422. if(strcmp(configName, "Debug") == 0
  423. || strcmp(configName, "RelWithDebInfo") == 0)
  424. {
  425. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  426. }
  427. fout << "\t\t\t\tStackReserveSize=\""
  428. << m_Makefile->GetDefinition("CMAKE_CXX_STACK_SIZE") << "\"\n";
  429. temp = m_ExecutableOutputPath;
  430. temp += configName;
  431. temp += "/";
  432. temp += libName;
  433. temp += debugPostfix;
  434. temp += ".lib";
  435. fout << "\t\t\t\tImportLibrary=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
  436. break;
  437. case cmTarget::EXECUTABLE:
  438. case cmTarget::WIN32_EXECUTABLE:
  439. fout << "\t\t\t<Tool\n"
  440. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  441. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386";
  442. if(extraLinkOptions.size())
  443. {
  444. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  445. extraLinkOptions.c_str()).c_str();
  446. }
  447. fout << "\"\n"
  448. << "\t\t\t\tAdditionalDependencies=\" odbc32.lib odbccp32.lib ";
  449. this->OutputLibraries(fout, configName, libName, target);
  450. fout << "\"\n";
  451. temp = m_ExecutableOutputPath;
  452. temp += configName;
  453. temp += "/";
  454. temp += libName;
  455. temp += ".exe";
  456. fout << "\t\t\t\tOutputFile=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  457. fout << "\t\t\t\tLinkIncremental=\"1\"\n";
  458. fout << "\t\t\t\tSuppressStartupBanner=\"TRUE\"\n";
  459. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  460. this->OutputLibraryDirectories(fout, configName, libName, target);
  461. fout << "\"\n";
  462. fout << "\t\t\t\tProgramDatabaseFile=\"" << m_LibraryOutputPath
  463. << "$(OutDir)\\" << libName << ".pdb\"\n";
  464. if(strcmp(configName, "Debug") == 0
  465. || strcmp(configName, "RelWithDebInfo") == 0)
  466. {
  467. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  468. }
  469. if( target.GetType() == cmTarget::EXECUTABLE)
  470. {
  471. fout << "\t\t\t\tSubSystem=\"1\"\n";
  472. }
  473. else
  474. {
  475. fout << "\t\t\t\tSubSystem=\"2\"\n";
  476. }
  477. fout << "\t\t\t\tStackReserveSize=\""
  478. << m_Makefile->GetDefinition("CMAKE_CXX_STACK_SIZE") << "\"/>\n";
  479. break;
  480. case cmTarget::UTILITY:
  481. break;
  482. }
  483. }
  484. void cmLocalVisualStudio7Generator::OutputModuleDefinitionFile(std::ostream& fout,
  485. const cmTarget &target)
  486. {
  487. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  488. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  489. i != classes.end(); i++)
  490. {
  491. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  492. {
  493. fout << "\t\t\t\tModuleDefinitionFile=\""
  494. << this->ConvertToXMLOutputPath((*i)->GetFullPath().c_str())
  495. << "\"\n";
  496. return;
  497. }
  498. }
  499. }
  500. void cmLocalVisualStudio7Generator::OutputLibraryDirectories(std::ostream& fout,
  501. const char*,
  502. const char*,
  503. const cmTarget &tgt)
  504. {
  505. bool hasone = false;
  506. if(m_LibraryOutputPath.size())
  507. {
  508. hasone = true;
  509. std::string temp = m_LibraryOutputPath;
  510. temp += "$(INTDIR)";
  511. fout << this->ConvertToXMLOutputPath(temp.c_str()) << "," <<
  512. this->ConvertToXMLOutputPath(m_LibraryOutputPath.c_str());
  513. }
  514. if(m_ExecutableOutputPath.size() &&
  515. (m_LibraryOutputPath != m_ExecutableOutputPath))
  516. {
  517. if (hasone)
  518. {
  519. fout << ",";
  520. }
  521. hasone = true;
  522. std::string temp = m_ExecutableOutputPath;
  523. temp += "$(INTDIR)";
  524. fout << this->ConvertToXMLOutputPath(temp.c_str()) << "," <<
  525. this->ConvertToXMLOutputPath(m_ExecutableOutputPath.c_str());
  526. }
  527. std::set<std::string> pathEmitted;
  528. std::vector<std::string>::const_iterator i;
  529. const std::vector<std::string>& libdirs = tgt.GetLinkDirectories();
  530. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  531. {
  532. std::string lpath = *i;
  533. if(lpath[lpath.size()-1] != '/')
  534. {
  535. lpath += "/";
  536. }
  537. if(pathEmitted.insert(lpath).second)
  538. {
  539. if(hasone)
  540. {
  541. fout << ",";
  542. }
  543. std::string lpathi = lpath + "$(INTDIR)";
  544. fout << this->ConvertToXMLOutputPath(lpathi.c_str()) << "," <<
  545. this->ConvertToXMLOutputPath(lpath.c_str());
  546. hasone = true;
  547. }
  548. }
  549. }
  550. void cmLocalVisualStudio7Generator::OutputLibraries(std::ostream& fout,
  551. const char* configName,
  552. const char* libName,
  553. const cmTarget &target)
  554. {
  555. const cmTarget::LinkLibraries& libs = target.GetLinkLibraries();
  556. cmTarget::LinkLibraries::const_iterator j;
  557. for(j = libs.begin(); j != libs.end(); ++j)
  558. {
  559. if(j->first != libName)
  560. {
  561. std::string lib = j->first;
  562. std::string debugPostfix = "";
  563. // if this is a library we are building then watch for a debugPostfix
  564. if (!strcmp(configName,"Debug"))
  565. {
  566. std::string libPath = j->first + "_CMAKE_PATH";
  567. const char* cacheValue
  568. = m_GlobalGenerator->GetCMakeInstance()->GetCacheDefinition(libPath.c_str());
  569. if(cacheValue && m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX"))
  570. {
  571. debugPostfix = m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  572. }
  573. }
  574. if(j->first.find(".lib") == std::string::npos)
  575. {
  576. lib += debugPostfix + ".lib";
  577. }
  578. lib = this->ConvertToXMLOutputPath(lib.c_str());
  579. if (j->second == cmTarget::GENERAL
  580. || (j->second == cmTarget::DEBUG && strcmp(configName, "Debug") == 0)
  581. || (j->second == cmTarget::OPTIMIZED && strcmp(configName, "Debug") != 0))
  582. {
  583. fout << lib << " ";
  584. }
  585. }
  586. }
  587. }
  588. void cmLocalVisualStudio7Generator::OutputDefineFlags(std::ostream& fout)
  589. {
  590. std::string defs = m_Makefile->GetDefineFlags();
  591. std::string::size_type pos = defs.find("-D");
  592. bool done = pos == std::string::npos;
  593. if(!done)
  594. {
  595. fout << ",";
  596. }
  597. while(!done)
  598. {
  599. std::string::size_type nextpos = defs.find("-D", pos+2);
  600. std::string define;
  601. if(nextpos != std::string::npos)
  602. {
  603. define = defs.substr(pos+2, nextpos - pos -3);
  604. }
  605. else
  606. {
  607. define = defs.substr(pos+2);
  608. done = true;
  609. }
  610. cmSystemTools::ReplaceString(define, "\"", "&quot;");
  611. fout << define << ",";
  612. if(!done)
  613. {
  614. pos = defs.find("-D", nextpos);
  615. }
  616. }
  617. }
  618. void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
  619. const char *libName,
  620. cmTarget &target)
  621. {
  622. // get the configurations
  623. std::vector<std::string> *configs =
  624. static_cast<cmGlobalVisualStudio7Generator *>
  625. (m_GlobalGenerator)->GetConfigurations();
  626. // if we should add regen rule then...
  627. const char *suppRegenRule =
  628. m_Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  629. if (!cmSystemTools::IsOn(suppRegenRule))
  630. {
  631. this->AddVCProjBuildRule();
  632. }
  633. // trace the visual studio dependencies
  634. std::string name = libName;
  635. name += ".vcproj.cmake";
  636. target.TraceVSDependencies(name, m_Makefile);
  637. // We may be modifying the source groups temporarily, so make a copy.
  638. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  639. // get the classes from the source lists then add them to the groups
  640. std::vector<cmSourceFile*> & classes = target.GetSourceFiles();
  641. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  642. i != classes.end(); i++)
  643. {
  644. // Add the file to the list of sources.
  645. std::string source = (*i)->GetFullPath();
  646. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  647. {
  648. m_ModuleDefinitionFile = (*i)->GetFullPath();
  649. }
  650. cmSourceGroup& sourceGroup =
  651. m_Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  652. sourceGroup.AssignSource(*i);
  653. }
  654. // open the project
  655. this->WriteProjectStart(fout, libName, target, sourceGroups);
  656. // write the configuration information
  657. this->WriteConfigurations(fout, libName, target);
  658. fout << "\t<Files>\n";
  659. // Loop through every source group.
  660. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  661. sg != sourceGroups.end(); ++sg)
  662. {
  663. const std::vector<const cmSourceFile *> &sourceFiles =
  664. sg->GetSourceFiles();
  665. // If the group is empty, don't write it at all.
  666. if(sourceFiles.empty())
  667. {
  668. continue;
  669. }
  670. // If the group has a name, write the header.
  671. std::string name = sg->GetName();
  672. if(name != "")
  673. {
  674. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  675. }
  676. // Loop through each source in the source group.
  677. for(std::vector<const cmSourceFile *>::const_iterator sf =
  678. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  679. {
  680. std::string source = (*sf)->GetFullPath();
  681. const cmCustomCommand *command = (*sf)->GetCustomCommand();
  682. std::string compileFlags;
  683. std::string additionalDeps;
  684. // Check for extra compiler flags.
  685. const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS");
  686. if(cflags)
  687. {
  688. compileFlags = cflags;
  689. }
  690. if(cmSystemTools::GetFileFormat((*sf)->GetSourceExtension().c_str())
  691. == cmSystemTools::CXX_FILE_FORMAT)
  692. {
  693. // force a C++ file type
  694. compileFlags += " /TP ";
  695. }
  696. // Check for extra object-file dependencies.
  697. const char* deps = (*sf)->GetProperty("OBJECT_DEPENDS");
  698. if(deps)
  699. {
  700. std::vector<std::string> depends;
  701. cmSystemTools::ExpandListArgument(deps, depends);
  702. if(!depends.empty())
  703. {
  704. std::vector<std::string>::iterator i = depends.begin();
  705. additionalDeps = this->ConvertToXMLOutputPath(i->c_str());
  706. for(++i;i != depends.end(); ++i)
  707. {
  708. additionalDeps += ";";
  709. additionalDeps += this->ConvertToXMLOutputPath(i->c_str());
  710. }
  711. }
  712. }
  713. if (source != libName || target.GetType() == cmTarget::UTILITY)
  714. {
  715. fout << "\t\t\t<File\n";
  716. std::string d = this->ConvertToXMLOutputPathSingle(source.c_str());
  717. // Tell MS-Dev what the source is. If the compiler knows how to
  718. // build it, then it will.
  719. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  720. if (command)
  721. {
  722. std::string totalCommandStr;
  723. totalCommandStr =
  724. cmSystemTools::ConvertToOutputPath(command->GetCommand().c_str());
  725. totalCommandStr += " ";
  726. totalCommandStr += command->GetArguments();
  727. totalCommandStr += "\n";
  728. const char* comment = command->GetComment().c_str();
  729. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  730. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  731. (*comment?comment:"Custom Rule"),
  732. command->GetDepends(),
  733. command->GetOutput().c_str(), flags);
  734. }
  735. else if(compileFlags.size() || additionalDeps.length())
  736. {
  737. for(std::vector<std::string>::iterator i = configs->begin();
  738. i != configs->end(); ++i)
  739. {
  740. fout << "\t\t\t\t<FileConfiguration\n"
  741. << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n"
  742. << "\t\t\t\t\t<Tool\n"
  743. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n";
  744. if(compileFlags.size())
  745. {
  746. fout << "\t\t\t\t\tAdditionalOptions=\""
  747. << compileFlags << "\"\n";
  748. }
  749. if(additionalDeps.length())
  750. {
  751. fout << "\t\t\t\t\tAdditionalDependencies=\""
  752. << additionalDeps.c_str() << "\"\n";
  753. }
  754. fout << "\t\t\t\t\t/>\n"
  755. << "\t\t\t\t</FileConfiguration>\n";
  756. }
  757. }
  758. fout << "\t\t\t</File>\n";
  759. }
  760. }
  761. // If the group has a name, write the footer.
  762. if(name != "")
  763. {
  764. this->WriteVCProjEndGroup(fout);
  765. }
  766. }
  767. fout << "\t</Files>\n";
  768. // Write the VCProj file's footer.
  769. this->WriteVCProjFooter(fout);
  770. }
  771. void cmLocalVisualStudio7Generator::
  772. WriteCustomRule(std::ostream& fout,
  773. const char* source,
  774. const char* command,
  775. const char* comment,
  776. const std::vector<std::string>& depends,
  777. const char *output,
  778. const char* compileFlags)
  779. {
  780. std::string cmd = command;
  781. cmSystemTools::ReplaceString(cmd, "\"", "&quot;");
  782. std::vector<std::string>::iterator i;
  783. std::vector<std::string> *configs =
  784. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator)->GetConfigurations();
  785. for(i = configs->begin(); i != configs->end(); ++i)
  786. {
  787. fout << "\t\t\t\t<FileConfiguration\n";
  788. fout << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n";
  789. if(compileFlags)
  790. {
  791. fout << "\t\t\t\t\t<Tool\n"
  792. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  793. << "\t\t\t\t\tAdditionalOptions=\""
  794. << compileFlags << "\"/>\n";
  795. }
  796. fout << "\t\t\t\t\t<Tool\n"
  797. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  798. << "\t\t\t\t\tDescription=\"Building " << comment;
  799. fout << " " << output;
  800. fout << "\"\n"
  801. << "\t\t\t\t\tCommandLine=\"" << cmd << "\n\"\n"
  802. << "\t\t\t\t\tAdditionalDependencies=\"";
  803. // Write out the dependencies for the rule.
  804. std::string temp;
  805. for(std::vector<std::string>::const_iterator d = depends.begin();
  806. d != depends.end(); ++d)
  807. {
  808. std::string dep = cmSystemTools::GetFilenameName(*d);
  809. if (cmSystemTools::GetFilenameLastExtension(dep) == ".exe")
  810. {
  811. dep = cmSystemTools::GetFilenameWithoutLastExtension(dep);
  812. }
  813. std::string libPath = dep + "_CMAKE_PATH";
  814. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  815. if (cacheValue)
  816. {
  817. std::string exePath = "";
  818. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  819. {
  820. exePath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  821. }
  822. if(exePath.size())
  823. {
  824. libPath = exePath;
  825. }
  826. else
  827. {
  828. libPath = cacheValue;
  829. }
  830. libPath += "/";
  831. libPath += "$(INTDIR)";
  832. libPath += dep;
  833. libPath += ".exe";
  834. fout << this->ConvertToXMLOutputPath(libPath.c_str())
  835. << ";";
  836. }
  837. else
  838. {
  839. fout << this->ConvertToXMLOutputPath(d->c_str())
  840. << ";";
  841. }
  842. }
  843. fout << "\"\n";
  844. fout << "\t\t\t\t\tOutputs=\"";
  845. if(output == 0)
  846. {
  847. fout << source << "_force";
  848. }
  849. // Write a rule for the output generated by this command.
  850. fout << this->ConvertToXMLOutputPathSingle(output);
  851. fout << "\"/>\n";
  852. fout << "\t\t\t\t</FileConfiguration>\n";
  853. }
  854. }
  855. void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
  856. const char* group,
  857. const char* )
  858. {
  859. fout << "\t\t<Filter\n"
  860. << "\t\t\tName=\"" << group << "\"\n"
  861. << "\t\t\tFilter=\"\">\n";
  862. }
  863. void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
  864. {
  865. fout << "\t\t</Filter>\n";
  866. }
  867. // look for custom rules on a target and collect them together
  868. void cmLocalVisualStudio7Generator::OutputTargetRules(std::ostream& fout,
  869. const cmTarget &target,
  870. const char * /* libName */)
  871. {
  872. if (target.GetType() > cmTarget::UTILITY)
  873. {
  874. return;
  875. }
  876. // add the pre build rules
  877. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"";
  878. bool init = false;
  879. for (std::vector<cmCustomCommand>::const_iterator cr =
  880. target.GetPreBuildCommands().begin();
  881. cr != target.GetPreBuildCommands().end(); ++cr)
  882. {
  883. cmCustomCommand cc(*cr);
  884. cc.ExpandVariables(*m_Makefile);
  885. if(!init)
  886. {
  887. fout << "\nCommandLine=\"";
  888. init = true;
  889. }
  890. std::string args = cc.GetArguments();
  891. cmSystemTools::ReplaceString(args, "\"", "&quot;");
  892. fout << this->ConvertToXMLOutputPath(cc.GetCommand().c_str()) << " " <<
  893. args << "\n";
  894. }
  895. if (init)
  896. {
  897. fout << "\"";
  898. }
  899. fout << "/>\n";
  900. // add the pre Link rules
  901. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreLinkEventTool\"";
  902. init = false;
  903. for (std::vector<cmCustomCommand>::const_iterator cr =
  904. target.GetPreLinkCommands().begin();
  905. cr != target.GetPreLinkCommands().end(); ++cr)
  906. {
  907. cmCustomCommand cc(*cr);
  908. cc.ExpandVariables(*m_Makefile);
  909. if(!init)
  910. {
  911. fout << "\nCommandLine=\"";
  912. init = true;
  913. }
  914. std::string args = cc.GetArguments();
  915. cmSystemTools::ReplaceString(args, "\"", "&quot;");
  916. fout << this->ConvertToXMLOutputPath(cc.GetCommand().c_str()) << " " <<
  917. args << "\n";
  918. }
  919. if (init)
  920. {
  921. fout << "\"";
  922. }
  923. fout << "/>\n";
  924. // add the PostBuild rules
  925. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  926. init = false;
  927. for (std::vector<cmCustomCommand>::const_iterator cr =
  928. target.GetPostBuildCommands().begin();
  929. cr != target.GetPostBuildCommands().end(); ++cr)
  930. {
  931. cmCustomCommand cc(*cr);
  932. cc.ExpandVariables(*m_Makefile);
  933. if(!init)
  934. {
  935. fout << "\nCommandLine=\"";
  936. init = true;
  937. }
  938. std::string args = cc.GetArguments();
  939. cmSystemTools::ReplaceString(args, "\"", "&quot;");
  940. fout << this->ConvertToXMLOutputPath(cc.GetCommand().c_str()) << " " <<
  941. args << "\n";
  942. }
  943. if (init)
  944. {
  945. fout << "\"";
  946. }
  947. fout << "/>\n";
  948. }
  949. void
  950. cmLocalVisualStudio7Generator::WriteProjectStart(std::ostream& fout,
  951. const char *libName,
  952. const cmTarget &,
  953. std::vector<cmSourceGroup> &)
  954. {
  955. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  956. << "<VisualStudioProject\n"
  957. << "\tProjectType=\"Visual C++\"\n";
  958. if(m_Version71)
  959. {
  960. fout << "\tVersion=\"7.10\"\n";
  961. }
  962. else
  963. {
  964. fout << "\tVersion=\"7.00\"\n";
  965. }
  966. fout << "\tName=\"" << libName << "\"\n"
  967. << "\tSccProjectName=\"\"\n"
  968. << "\tSccLocalPath=\"\"\n"
  969. << "\tKeyword=\"Win32Proj\">\n"
  970. << "\t<Platforms>\n"
  971. << "\t\t<Platform\n\t\t\tName=\"Win32\"/>\n"
  972. << "\t</Platforms>\n";
  973. }
  974. void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout)
  975. {
  976. fout << "\t<Globals>\n"
  977. << "\t</Globals>\n"
  978. << "</VisualStudioProject>\n";
  979. }
  980. std::string cmLocalVisualStudio7Generator::EscapeForXML(const char* s)
  981. {
  982. std::string ret = s;
  983. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  984. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  985. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  986. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  987. return ret;
  988. }
  989. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPath(const char* path)
  990. {
  991. std::string ret = cmSystemTools::ConvertToOutputPath(path);
  992. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  993. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  994. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  995. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  996. return ret;
  997. }
  998. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPathSingle(const char* path)
  999. {
  1000. std::string ret = cmSystemTools::ConvertToOutputPath(path);
  1001. cmSystemTools::ReplaceString(ret, "\"", "");
  1002. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1003. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1004. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1005. return ret;
  1006. }