cmLocalVisualStudio7Generator.cxx 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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. // Write a rule for the output generated by this command.
  872. fout << this->ConvertToXMLOutputPathSingle(output);
  873. fout << "\"/>\n";
  874. fout << "\t\t\t\t</FileConfiguration>\n";
  875. }
  876. }
  877. void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
  878. const char* group,
  879. const char* )
  880. {
  881. fout << "\t\t<Filter\n"
  882. << "\t\t\tName=\"" << group << "\"\n"
  883. << "\t\t\tFilter=\"\">\n";
  884. }
  885. void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
  886. {
  887. fout << "\t\t</Filter>\n";
  888. }
  889. // look for custom rules on a target and collect them together
  890. void cmLocalVisualStudio7Generator::OutputTargetRules(std::ostream& fout,
  891. const cmTarget &target,
  892. const char */* libName */)
  893. {
  894. if (target.GetType() > cmTarget::UTILITY)
  895. {
  896. return;
  897. }
  898. // add the pre build rules
  899. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"";
  900. bool init = false;
  901. for (std::vector<cmCustomCommand>::const_iterator cr =
  902. target.GetPreBuildCommands().begin();
  903. cr != target.GetPreBuildCommands().end(); ++cr)
  904. {
  905. cmCustomCommand cc(*cr);
  906. cc.ExpandVariables(*m_Makefile);
  907. if(!init)
  908. {
  909. fout << "\nCommandLine=\"";
  910. init = true;
  911. }
  912. std::string args = cc.GetArguments();
  913. cmSystemTools::ReplaceString(args, "\"", "&quot;");
  914. fout << this->ConvertToXMLOutputPath(cc.GetCommand().c_str()) << " " <<
  915. args << "\n";
  916. }
  917. if (init)
  918. {
  919. fout << "\"";
  920. }
  921. fout << "/>\n";
  922. // add the pre Link rules
  923. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreLinkEventTool\"";
  924. init = false;
  925. for (std::vector<cmCustomCommand>::const_iterator cr =
  926. target.GetPreLinkCommands().begin();
  927. cr != target.GetPreLinkCommands().end(); ++cr)
  928. {
  929. cmCustomCommand cc(*cr);
  930. cc.ExpandVariables(*m_Makefile);
  931. if(!init)
  932. {
  933. fout << "\nCommandLine=\"";
  934. init = true;
  935. }
  936. std::string args = cc.GetArguments();
  937. cmSystemTools::ReplaceString(args, "\"", "&quot;");
  938. fout << this->ConvertToXMLOutputPath(cc.GetCommand().c_str()) << " " <<
  939. args << "\n";
  940. }
  941. if (init)
  942. {
  943. fout << "\"";
  944. }
  945. fout << "/>\n";
  946. // add the PostBuild rules
  947. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  948. init = false;
  949. for (std::vector<cmCustomCommand>::const_iterator cr =
  950. target.GetPostBuildCommands().begin();
  951. cr != target.GetPostBuildCommands().end(); ++cr)
  952. {
  953. cmCustomCommand cc(*cr);
  954. cc.ExpandVariables(*m_Makefile);
  955. if(!init)
  956. {
  957. fout << "\nCommandLine=\"";
  958. init = true;
  959. }
  960. std::string args = cc.GetArguments();
  961. cmSystemTools::ReplaceString(args, "\"", "&quot;");
  962. fout << this->ConvertToXMLOutputPath(cc.GetCommand().c_str()) << " " <<
  963. args << "\n";
  964. }
  965. if (init)
  966. {
  967. fout << "\"";
  968. }
  969. fout << "/>\n";
  970. }
  971. void
  972. cmLocalVisualStudio7Generator::WriteProjectStart(std::ostream& fout,
  973. const char *libName,
  974. const cmTarget &,
  975. std::vector<cmSourceGroup> &)
  976. {
  977. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  978. << "<VisualStudioProject\n"
  979. << "\tProjectType=\"Visual C++\"\n";
  980. if(m_Version71)
  981. {
  982. fout << "\tVersion=\"7.10\"\n";
  983. }
  984. else
  985. {
  986. fout << "\tVersion=\"7.00\"\n";
  987. }
  988. fout << "\tName=\"" << libName << "\"\n"
  989. << "\tSccProjectName=\"\"\n"
  990. << "\tSccLocalPath=\"\"\n"
  991. << "\tKeyword=\"Win32Proj\">\n"
  992. << "\t<Platforms>\n"
  993. << "\t\t<Platform\n\t\t\tName=\"Win32\"/>\n"
  994. << "\t</Platforms>\n";
  995. }
  996. void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout)
  997. {
  998. fout << "\t<Globals>\n"
  999. << "\t</Globals>\n"
  1000. << "</VisualStudioProject>\n";
  1001. }
  1002. std::string cmLocalVisualStudio7Generator::EscapeForXML(const char* s)
  1003. {
  1004. std::string ret = s;
  1005. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1006. return ret;
  1007. }
  1008. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPath(const char* path)
  1009. {
  1010. std::string ret = cmSystemTools::ConvertToOutputPath(path);
  1011. return cmLocalVisualStudio7Generator::EscapeForXML(ret.c_str());
  1012. }
  1013. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPathSingle(const char* path)
  1014. {
  1015. std::string ret = cmSystemTools::ConvertToOutputPath(path);
  1016. cmSystemTools::ReplaceString(ret, "\"", "");
  1017. return ret;
  1018. }