1
0

cmLocalVisualStudio7Generator.cxx 30 KB

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