cmLocalVisualStudio7Generator.cxx 33 KB

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