cmLocalVisualStudio7Generator.cxx 32 KB

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