cmLocalVisualStudio7Generator.cxx 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. for (std::vector<cmCustomCommand>::iterator ic =
  641. target.GetPostBuildCommands().begin();
  642. ic != target.GetPostBuildCommands().end(); ++ic)
  643. {
  644. cmCustomCommand &c = *ic;
  645. for (std::vector<std::string>::iterator i = c.GetDepends().begin();
  646. i != c.GetDepends().end(); ++i)
  647. {
  648. srcFilesToProcess.push(*i);
  649. }
  650. }
  651. }
  652. while (!srcFilesToProcess.empty())
  653. {
  654. // is this source the output of a custom command
  655. cmSourceFile* outsf =
  656. m_Makefile->GetSourceFileWithOutput(srcFilesToProcess.front().c_str());
  657. if (outsf)
  658. {
  659. // is it not already in the target?
  660. if (std::find(classes.begin(),classes.end(),outsf) == classes.end())
  661. {
  662. // then add the source to this target and add it to the queue
  663. classes.push_back(outsf);
  664. std::string name = outsf->GetSourceName();
  665. if (outsf->GetSourceExtension() != "rule")
  666. {
  667. name += ".";
  668. name += outsf->GetSourceExtension();
  669. }
  670. std::string temp =
  671. cmSystemTools::GetFilenamePath(outsf->GetFullPath());
  672. temp += "/";
  673. temp += name;
  674. srcFilesToProcess.push(temp);
  675. }
  676. // add its dependencies to the list to check
  677. unsigned int i;
  678. for (i = 0; i < outsf->GetCustomCommand()->GetDepends().size(); ++i)
  679. {
  680. std::string dep = cmSystemTools::GetFilenameName(
  681. outsf->GetCustomCommand()->GetDepends()[i]);
  682. if (cmSystemTools::GetFilenameLastExtension(dep) == ".exe")
  683. {
  684. dep = cmSystemTools::GetFilenameWithoutLastExtension(dep);
  685. }
  686. // watch for target dependencies,
  687. std::string libPath = dep + "_CMAKE_PATH";
  688. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  689. if (cacheValue)
  690. {
  691. // add the depend as a utility on the target
  692. target.AddUtility(dep.c_str());
  693. }
  694. else
  695. {
  696. srcFilesToProcess.push(outsf->GetCustomCommand()->GetDepends()[i]);
  697. }
  698. }
  699. }
  700. // finished with this SF move to the next
  701. srcFilesToProcess.pop();
  702. }
  703. // get the classes from the source lists then add them to the groups
  704. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  705. i != classes.end(); i++)
  706. {
  707. // Add the file to the list of sources.
  708. std::string source = (*i)->GetFullPath();
  709. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  710. {
  711. m_ModuleDefinitionFile = (*i)->GetFullPath();
  712. }
  713. cmSourceGroup& sourceGroup =
  714. m_Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  715. sourceGroup.AddSource(source.c_str(), *i);
  716. }
  717. // open the project
  718. this->WriteProjectStart(fout, libName, target, sourceGroups);
  719. // write the configuration information
  720. this->WriteConfigurations(fout, libName, target);
  721. fout << "\t<Files>\n";
  722. // Loop through every source group.
  723. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  724. sg != sourceGroups.end(); ++sg)
  725. {
  726. const std::vector<const cmSourceFile *> &sourceFiles =
  727. sg->GetSourceFiles();
  728. // If the group is empty, don't write it at all.
  729. if(sourceFiles.empty())
  730. {
  731. continue;
  732. }
  733. // If the group has a name, write the header.
  734. std::string name = sg->GetName();
  735. if(name != "")
  736. {
  737. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  738. }
  739. // Loop through each source in the source group.
  740. for(std::vector<const cmSourceFile *>::const_iterator sf =
  741. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  742. {
  743. std::string source = (*sf)->GetFullPath();
  744. const cmCustomCommand *command = (*sf)->GetCustomCommand();
  745. std::string compileFlags;
  746. std::string additionalDeps;
  747. // Check for extra compiler flags.
  748. const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS");
  749. if(cflags)
  750. {
  751. compileFlags = cflags;
  752. }
  753. if(cmSystemTools::GetFileFormat((*sf)->GetSourceExtension().c_str())
  754. == cmSystemTools::CXX_FILE_FORMAT)
  755. {
  756. // force a C++ file type
  757. compileFlags += " /TP ";
  758. }
  759. // Check for extra object-file dependencies.
  760. const char* deps = (*sf)->GetProperty("OBJECT_DEPENDS");
  761. if(deps)
  762. {
  763. std::vector<std::string> depends;
  764. cmSystemTools::ExpandListArgument(deps, depends);
  765. if(!depends.empty())
  766. {
  767. std::vector<std::string>::iterator i = depends.begin();
  768. additionalDeps = this->ConvertToXMLOutputPath(i->c_str());
  769. for(++i;i != depends.end(); ++i)
  770. {
  771. additionalDeps += ";";
  772. additionalDeps += this->ConvertToXMLOutputPath(i->c_str());
  773. }
  774. }
  775. }
  776. if (source != libName || target.GetType() == cmTarget::UTILITY)
  777. {
  778. fout << "\t\t\t<File\n";
  779. std::string d = this->ConvertToXMLOutputPathSingle(source.c_str());
  780. // Tell MS-Dev what the source is. If the compiler knows how to
  781. // build it, then it will.
  782. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  783. if (command)
  784. {
  785. std::string totalCommandStr;
  786. totalCommandStr =
  787. cmSystemTools::ConvertToOutputPath(command->GetCommand().c_str());
  788. totalCommandStr += " ";
  789. totalCommandStr += command->GetArguments();
  790. totalCommandStr += "\n";
  791. const char* comment = command->GetComment().c_str();
  792. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  793. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  794. (*comment?comment:"Custom Rule"),
  795. command->GetDepends(),
  796. command->GetOutput().c_str(), flags);
  797. }
  798. else if(compileFlags.size() || additionalDeps.length())
  799. {
  800. for(std::vector<std::string>::iterator i = configs->begin();
  801. i != configs->end(); ++i)
  802. {
  803. fout << "\t\t\t\t<FileConfiguration\n"
  804. << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n"
  805. << "\t\t\t\t\t<Tool\n"
  806. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n";
  807. if(compileFlags.size())
  808. {
  809. fout << "\t\t\t\t\tAdditionalOptions=\""
  810. << compileFlags << "\"\n";
  811. }
  812. if(additionalDeps.length())
  813. {
  814. fout << "\t\t\t\t\tAdditionalDependencies=\""
  815. << additionalDeps.c_str() << "\"\n";
  816. }
  817. fout << "\t\t\t\t\t/>\n"
  818. << "\t\t\t\t</FileConfiguration>\n";
  819. }
  820. }
  821. fout << "\t\t\t</File>\n";
  822. }
  823. }
  824. // If the group has a name, write the footer.
  825. if(name != "")
  826. {
  827. this->WriteVCProjEndGroup(fout);
  828. }
  829. }
  830. fout << "\t</Files>\n";
  831. // Write the VCProj file's footer.
  832. this->WriteVCProjFooter(fout);
  833. }
  834. void cmLocalVisualStudio7Generator::
  835. WriteCustomRule(std::ostream& fout,
  836. const char* source,
  837. const char* command,
  838. const char* comment,
  839. const std::vector<std::string>& depends,
  840. const char *output,
  841. const char* compileFlags)
  842. {
  843. std::string cmd = command;
  844. cmSystemTools::ReplaceString(cmd, "\"", "&quot;");
  845. std::vector<std::string>::iterator i;
  846. std::vector<std::string> *configs =
  847. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator)->GetConfigurations();
  848. for(i = configs->begin(); i != configs->end(); ++i)
  849. {
  850. fout << "\t\t\t\t<FileConfiguration\n";
  851. fout << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n";
  852. if(compileFlags)
  853. {
  854. fout << "\t\t\t\t\t<Tool\n"
  855. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  856. << "\t\t\t\t\tAdditionalOptions=\""
  857. << compileFlags << "\"/>\n";
  858. }
  859. fout << "\t\t\t\t\t<Tool\n"
  860. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  861. << "\t\t\t\t\tDescription=\"Building " << comment;
  862. fout << " " << output;
  863. fout << "\"\n"
  864. << "\t\t\t\t\tCommandLine=\"" << cmd << "\n\"\n"
  865. << "\t\t\t\t\tAdditionalDependencies=\"";
  866. // Write out the dependencies for the rule.
  867. std::string temp;
  868. for(std::vector<std::string>::const_iterator d = depends.begin();
  869. d != depends.end(); ++d)
  870. {
  871. fout << this->ConvertToXMLOutputPath(d->c_str())
  872. << ";";
  873. }
  874. fout << "\"\n";
  875. fout << "\t\t\t\t\tOutputs=\"";
  876. if(output == 0)
  877. {
  878. fout << source << "_force";
  879. }
  880. // Write a rule for the output generated by this command.
  881. fout << this->ConvertToXMLOutputPathSingle(output);
  882. fout << "\"/>\n";
  883. fout << "\t\t\t\t</FileConfiguration>\n";
  884. }
  885. }
  886. void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
  887. const char* group,
  888. const char* )
  889. {
  890. fout << "\t\t<Filter\n"
  891. << "\t\t\tName=\"" << group << "\"\n"
  892. << "\t\t\tFilter=\"\">\n";
  893. }
  894. void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
  895. {
  896. fout << "\t\t</Filter>\n";
  897. }
  898. // look for custom rules on a target and collect them together
  899. void cmLocalVisualStudio7Generator::OutputTargetRules(std::ostream& fout,
  900. const cmTarget &target,
  901. const char * /* libName */)
  902. {
  903. if (target.GetType() > cmTarget::UTILITY)
  904. {
  905. return;
  906. }
  907. // add the pre build rules
  908. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"";
  909. bool init = false;
  910. for (std::vector<cmCustomCommand>::const_iterator cr =
  911. target.GetPreBuildCommands().begin();
  912. cr != target.GetPreBuildCommands().end(); ++cr)
  913. {
  914. cmCustomCommand cc(*cr);
  915. cc.ExpandVariables(*m_Makefile);
  916. if(!init)
  917. {
  918. fout << "\nCommandLine=\"";
  919. init = true;
  920. }
  921. std::string args = cc.GetArguments();
  922. cmSystemTools::ReplaceString(args, "\"", "&quot;");
  923. fout << this->ConvertToXMLOutputPath(cc.GetCommand().c_str()) << " " <<
  924. args << "\n";
  925. }
  926. if (init)
  927. {
  928. fout << "\"";
  929. }
  930. fout << "/>\n";
  931. // add the pre Link rules
  932. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreLinkEventTool\"";
  933. init = false;
  934. for (std::vector<cmCustomCommand>::const_iterator cr =
  935. target.GetPreLinkCommands().begin();
  936. cr != target.GetPreLinkCommands().end(); ++cr)
  937. {
  938. cmCustomCommand cc(*cr);
  939. cc.ExpandVariables(*m_Makefile);
  940. if(!init)
  941. {
  942. fout << "\nCommandLine=\"";
  943. init = true;
  944. }
  945. std::string args = cc.GetArguments();
  946. cmSystemTools::ReplaceString(args, "\"", "&quot;");
  947. fout << this->ConvertToXMLOutputPath(cc.GetCommand().c_str()) << " " <<
  948. args << "\n";
  949. }
  950. if (init)
  951. {
  952. fout << "\"";
  953. }
  954. fout << "/>\n";
  955. // add the PostBuild rules
  956. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  957. init = false;
  958. for (std::vector<cmCustomCommand>::const_iterator cr =
  959. target.GetPostBuildCommands().begin();
  960. cr != target.GetPostBuildCommands().end(); ++cr)
  961. {
  962. cmCustomCommand cc(*cr);
  963. cc.ExpandVariables(*m_Makefile);
  964. if(!init)
  965. {
  966. fout << "\nCommandLine=\"";
  967. init = true;
  968. }
  969. std::string args = cc.GetArguments();
  970. cmSystemTools::ReplaceString(args, "\"", "&quot;");
  971. fout << this->ConvertToXMLOutputPath(cc.GetCommand().c_str()) << " " <<
  972. args << "\n";
  973. }
  974. if (init)
  975. {
  976. fout << "\"";
  977. }
  978. fout << "/>\n";
  979. }
  980. void
  981. cmLocalVisualStudio7Generator::WriteProjectStart(std::ostream& fout,
  982. const char *libName,
  983. const cmTarget &,
  984. std::vector<cmSourceGroup> &)
  985. {
  986. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  987. << "<VisualStudioProject\n"
  988. << "\tProjectType=\"Visual C++\"\n";
  989. if(m_Version71)
  990. {
  991. fout << "\tVersion=\"7.10\"\n";
  992. }
  993. else
  994. {
  995. fout << "\tVersion=\"7.00\"\n";
  996. }
  997. fout << "\tName=\"" << libName << "\"\n"
  998. << "\tSccProjectName=\"\"\n"
  999. << "\tSccLocalPath=\"\"\n"
  1000. << "\tKeyword=\"Win32Proj\">\n"
  1001. << "\t<Platforms>\n"
  1002. << "\t\t<Platform\n\t\t\tName=\"Win32\"/>\n"
  1003. << "\t</Platforms>\n";
  1004. }
  1005. void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout)
  1006. {
  1007. fout << "\t<Globals>\n"
  1008. << "\t</Globals>\n"
  1009. << "</VisualStudioProject>\n";
  1010. }
  1011. std::string cmLocalVisualStudio7Generator::EscapeForXML(const char* s)
  1012. {
  1013. std::string ret = s;
  1014. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1015. return ret;
  1016. }
  1017. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPath(const char* path)
  1018. {
  1019. std::string ret = cmSystemTools::ConvertToOutputPath(path);
  1020. return cmLocalVisualStudio7Generator::EscapeForXML(ret.c_str());
  1021. }
  1022. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPathSingle(const char* path)
  1023. {
  1024. std::string ret = cmSystemTools::ConvertToOutputPath(path);
  1025. cmSystemTools::ReplaceString(ret, "\"", "");
  1026. return ret;
  1027. }