cmLocalVisualStudio7Generator.cxx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. if ( m_Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL") )
  306. {
  307. fout << "\t\t\t\tWarningLevel=\"" << m_Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL") << "\"\n";
  308. }
  309. fout << "\t\t\t\tDetect64BitPortabilityProblems=\"TRUE\"\n"
  310. << "\t\t\t\tDebugInformationFormat=\"3\"";
  311. fout << "/>\n"; // end of <Tool Name=VCCLCompilerTool
  312. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCCustomBuildTool\"/>\n";
  313. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCResourceCompilerTool\"\n"
  314. << "AdditionalIncludeDirectories=\"";
  315. for(i = includes.begin();i != includes.end(); ++i)
  316. {
  317. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  318. fout << ipath << ";";
  319. }
  320. fout << "\"\n/>\n";
  321. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCMIDLTool\"/>\n";
  322. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  323. this->OutputTargetRules(fout, target, libName);
  324. fout << "/>\n";
  325. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"/>\n";
  326. this->OutputBuildTool(fout, configName, libName, target);
  327. fout << "\t\t</Configuration>\n";
  328. }
  329. void cmLocalVisualStudio7Generator::OutputBuildTool(std::ostream& fout,
  330. const char* configName,
  331. const char *libName,
  332. const cmTarget &target)
  333. {
  334. std::string temp;
  335. switch(target.GetType())
  336. {
  337. case cmTarget::STATIC_LIBRARY:
  338. {
  339. std::string libpath = m_LibraryOutputPath +
  340. "$(OutDir)/" + libName + ".lib";
  341. fout << "\t\t\t<Tool\n"
  342. << "\t\t\t\tName=\"VCLibrarianTool\"\n"
  343. << "\t\t\t\t\tOutputFile=\""
  344. << this->ConvertToXMLOutputPathSingle(libpath.c_str()) << ".\"/>\n";
  345. break;
  346. }
  347. case cmTarget::SHARED_LIBRARY:
  348. case cmTarget::MODULE_LIBRARY:
  349. fout << "\t\t\t<Tool\n"
  350. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  351. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386\"\n"
  352. << "\t\t\t\tAdditionalDependencies=\" odbc32.lib odbccp32.lib ";
  353. this->OutputLibraries(fout, configName, libName, target);
  354. fout << "\"\n";
  355. temp = m_LibraryOutputPath;
  356. temp += configName;
  357. temp += "/";
  358. temp += libName;
  359. temp += ".dll";
  360. fout << "\t\t\t\tOutputFile=\""
  361. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  362. fout << "\t\t\t\tLinkIncremental=\"1\"\n";
  363. fout << "\t\t\t\tSuppressStartupBanner=\"TRUE\"\n";
  364. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  365. this->OutputLibraryDirectories(fout, configName, libName, target);
  366. fout << "\"\n";
  367. this->OutputModuleDefinitionFile(fout, target);
  368. temp = m_LibraryOutputPath;
  369. temp += "$(OutDir)";
  370. temp += "/";
  371. temp += libName;
  372. temp += ".pdb";
  373. fout << "\t\t\t\tProgramDatabaseFile=\"" <<
  374. this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  375. if(strcmp(configName, "Debug") == 0
  376. || strcmp(configName, "RelWithDebInfo") == 0)
  377. {
  378. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  379. }
  380. fout << "\t\t\t\tStackReserveSize=\""
  381. << m_Makefile->GetDefinition("CMAKE_CXX_STACK_SIZE") << "\"\n";
  382. temp = m_ExecutableOutputPath;
  383. temp += configName;
  384. temp += "/";
  385. temp += libName;
  386. temp += ".lib";
  387. fout << "\t\t\t\tImportLibrary=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
  388. break;
  389. case cmTarget::EXECUTABLE:
  390. case cmTarget::WIN32_EXECUTABLE:
  391. fout << "\t\t\t<Tool\n"
  392. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  393. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386\"\n"
  394. << "\t\t\t\tAdditionalDependencies=\" odbc32.lib odbccp32.lib ";
  395. this->OutputLibraries(fout, configName, libName, target);
  396. fout << "\"\n";
  397. temp = m_ExecutableOutputPath;
  398. temp += configName;
  399. temp += "/";
  400. temp += libName;
  401. temp += ".exe";
  402. fout << "\t\t\t\tOutputFile=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  403. fout << "\t\t\t\tLinkIncremental=\"1\"\n";
  404. fout << "\t\t\t\tSuppressStartupBanner=\"TRUE\"\n";
  405. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  406. this->OutputLibraryDirectories(fout, configName, libName, target);
  407. fout << "\"\n";
  408. fout << "\t\t\t\tProgramDatabaseFile=\"" << m_LibraryOutputPath
  409. << "$(OutDir)\\" << libName << ".pdb\"\n";
  410. if(strcmp(configName, "Debug") == 0
  411. || strcmp(configName, "RelWithDebInfo") == 0)
  412. {
  413. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  414. }
  415. if( target.GetType() == cmTarget::EXECUTABLE)
  416. {
  417. fout << "\t\t\t\tSubSystem=\"1\"\n";
  418. }
  419. else
  420. {
  421. fout << "\t\t\t\tSubSystem=\"2\"\n";
  422. }
  423. fout << "\t\t\t\tStackReserveSize=\""
  424. << m_Makefile->GetDefinition("CMAKE_CXX_STACK_SIZE") << "\"/>\n";
  425. break;
  426. case cmTarget::UTILITY:
  427. break;
  428. }
  429. }
  430. void cmLocalVisualStudio7Generator::OutputModuleDefinitionFile(std::ostream& fout,
  431. const cmTarget &target)
  432. {
  433. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  434. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  435. i != classes.end(); i++)
  436. {
  437. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  438. {
  439. fout << "\t\t\t\tModuleDefinitionFile=\""
  440. << this->ConvertToXMLOutputPath((*i)->GetFullPath().c_str())
  441. << "\"\n";
  442. return;
  443. }
  444. }
  445. }
  446. void cmLocalVisualStudio7Generator::OutputLibraryDirectories(std::ostream& fout,
  447. const char*,
  448. const char*,
  449. const cmTarget &tgt)
  450. {
  451. bool hasone = false;
  452. if(m_LibraryOutputPath.size())
  453. {
  454. hasone = true;
  455. std::string temp = m_LibraryOutputPath;
  456. temp += "$(INTDIR)";
  457. fout << this->ConvertToXMLOutputPath(temp.c_str()) << "," <<
  458. this->ConvertToXMLOutputPath(m_LibraryOutputPath.c_str());
  459. }
  460. if(m_ExecutableOutputPath.size())
  461. {
  462. hasone = true;
  463. std::string temp = m_ExecutableOutputPath;
  464. temp += "$(INTDIR)";
  465. fout << this->ConvertToXMLOutputPath(temp.c_str()) << "," <<
  466. this->ConvertToXMLOutputPath(m_ExecutableOutputPath.c_str());
  467. }
  468. std::set<std::string> pathEmitted;
  469. std::vector<std::string>::const_iterator i;
  470. const std::vector<std::string>& libdirs = tgt.GetLinkDirectories();
  471. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  472. {
  473. std::string lpath = *i;
  474. if(lpath[lpath.size()-1] != '/')
  475. {
  476. lpath += "/";
  477. }
  478. if(pathEmitted.insert(lpath).second)
  479. {
  480. if(hasone)
  481. {
  482. fout << ",";
  483. }
  484. std::string lpathi = lpath + "$(INTDIR)";
  485. fout << this->ConvertToXMLOutputPath(lpathi.c_str()) << "," << lpath;
  486. hasone = true;
  487. }
  488. }
  489. }
  490. void cmLocalVisualStudio7Generator::OutputLibraries(std::ostream& fout,
  491. const char* configName,
  492. const char* libName,
  493. const cmTarget &target)
  494. {
  495. const cmTarget::LinkLibraries& libs = target.GetLinkLibraries();
  496. cmTarget::LinkLibraries::const_iterator j;
  497. for(j = libs.begin(); j != libs.end(); ++j)
  498. {
  499. if(j->first != libName)
  500. {
  501. std::string lib = j->first;
  502. if(j->first.find(".lib") == std::string::npos)
  503. {
  504. lib += ".lib";
  505. }
  506. lib = this->ConvertToXMLOutputPath(lib.c_str());
  507. if (j->second == cmTarget::GENERAL
  508. || (j->second == cmTarget::DEBUG && strcmp(configName, "DEBUG") == 0)
  509. || (j->second == cmTarget::OPTIMIZED && strcmp(configName, "DEBUG") != 0))
  510. {
  511. fout << lib << " ";
  512. }
  513. }
  514. }
  515. }
  516. void cmLocalVisualStudio7Generator::OutputDefineFlags(std::ostream& fout)
  517. {
  518. std::string defs = m_Makefile->GetDefineFlags();
  519. std::string::size_type pos = defs.find("-D");
  520. bool done = pos == std::string::npos;
  521. if(!done)
  522. {
  523. fout << ",";
  524. }
  525. while(!done)
  526. {
  527. std::string::size_type nextpos = defs.find("-D", pos+2);
  528. std::string define;
  529. if(nextpos != std::string::npos)
  530. {
  531. define = defs.substr(pos+2, nextpos - pos -3);
  532. }
  533. else
  534. {
  535. define = defs.substr(pos+2);
  536. done = true;
  537. }
  538. cmSystemTools::ReplaceString(define, "\"", "");
  539. fout << define << ",";
  540. if(!done)
  541. {
  542. pos = defs.find("-D", nextpos);
  543. }
  544. }
  545. }
  546. void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
  547. const char *libName,
  548. cmTarget &target)
  549. {
  550. // get the configurations
  551. std::vector<std::string> *configs =
  552. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator)->GetConfigurations();
  553. // We may be modifying the source groups temporarily, so make a copy.
  554. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  555. // get the classes from the source lists then add them to the groups
  556. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  557. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  558. i != classes.end(); i++)
  559. {
  560. // Add the file to the list of sources.
  561. std::string source = (*i)->GetFullPath();
  562. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  563. {
  564. m_ModuleDefinitionFile = (*i)->GetFullPath();
  565. }
  566. cmSourceGroup& sourceGroup = m_Makefile->FindSourceGroup(source.c_str(),
  567. sourceGroups);
  568. sourceGroup.AddSource(source.c_str(), *i);
  569. }
  570. // add any custom rules to the source groups
  571. for (std::vector<cmCustomCommand>::const_iterator cr =
  572. target.GetCustomCommands().begin();
  573. cr != target.GetCustomCommands().end(); ++cr)
  574. {
  575. cmSourceGroup& sourceGroup =
  576. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  577. sourceGroups);
  578. cmCustomCommand cc(*cr);
  579. cc.ExpandVariables(*m_Makefile);
  580. sourceGroup.AddCustomCommand(cc);
  581. }
  582. // open the project
  583. this->WriteProjectStart(fout, libName, target, sourceGroups);
  584. // write the configuration information
  585. this->WriteConfigurations(fout, libName, target);
  586. fout << "\t<Files>\n";
  587. // Find the group in which the CMakeLists.txt source belongs, and add
  588. // the rule to generate this VCProj file.
  589. for(std::vector<cmSourceGroup>::reverse_iterator sg = sourceGroups.rbegin();
  590. sg != sourceGroups.rend(); ++sg)
  591. {
  592. if(sg->Matches("CMakeLists.txt"))
  593. {
  594. this->AddVCProjBuildRule(*sg);
  595. break;
  596. }
  597. }
  598. // Loop through every source group.
  599. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  600. sg != sourceGroups.end(); ++sg)
  601. {
  602. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  603. // If the group is empty, don't write it at all.
  604. if(buildRules.empty())
  605. { continue; }
  606. // If the group has a name, write the header.
  607. std::string name = sg->GetName();
  608. if(name != "")
  609. {
  610. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  611. }
  612. // Loop through each build rule in the source group.
  613. for(cmSourceGroup::BuildRules::const_iterator cc =
  614. buildRules.begin(); cc != buildRules.end(); ++ cc)
  615. {
  616. std::string source = cc->first;
  617. const cmSourceGroup::Commands& commands = cc->second.m_Commands;
  618. const char* compileFlags = 0;
  619. std::string additionalDeps;
  620. if(cc->second.m_SourceFile)
  621. {
  622. // Check for extra compiler flags.
  623. compileFlags = cc->second.m_SourceFile->GetProperty("COMPILE_FLAGS");
  624. // Check for extra object-file dependencies.
  625. const char* deps =
  626. cc->second.m_SourceFile->GetProperty("OBJECT_DEPENDS");
  627. if(deps)
  628. {
  629. std::vector<std::string> depends;
  630. cmSystemTools::ExpandListArgument(deps, depends);
  631. if(!depends.empty())
  632. {
  633. std::vector<std::string>::iterator i = depends.begin();
  634. additionalDeps = this->ConvertToXMLOutputPath(i->c_str());
  635. for(++i;i != depends.end(); ++i)
  636. {
  637. additionalDeps += ";";
  638. additionalDeps += this->ConvertToXMLOutputPath(i->c_str());
  639. }
  640. }
  641. }
  642. }
  643. if (source != libName || target.GetType() == cmTarget::UTILITY)
  644. {
  645. fout << "\t\t\t<File\n";
  646. std::string d = this->ConvertToXMLOutputPath(source.c_str());
  647. // remove double quotes from the string
  648. cmSystemTools::ReplaceString(d, "\"", "");
  649. // Tell MS-Dev what the source is. If the compiler knows how to
  650. // build it, then it will.
  651. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  652. if (!commands.empty())
  653. {
  654. cmSourceGroup::CommandFiles totalCommand;
  655. std::string totalCommandStr;
  656. totalCommandStr = this->CombineCommands(commands, totalCommand,
  657. source.c_str());
  658. const char* comment = totalCommand.m_Comment.c_str();
  659. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  660. (*comment?comment:"Custom Rule"),
  661. totalCommand.m_Depends,
  662. totalCommand.m_Outputs, compileFlags);
  663. }
  664. else if(compileFlags || additionalDeps.length())
  665. {
  666. for(std::vector<std::string>::iterator i = configs->begin();
  667. i != configs->end(); ++i)
  668. {
  669. fout << "\t\t\t\t<FileConfiguration\n"
  670. << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n"
  671. << "\t\t\t\t\t<Tool\n"
  672. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n";
  673. if(compileFlags)
  674. {
  675. fout << "\t\t\t\t\tAdditionalOptions=\""
  676. << compileFlags << "\"\n";
  677. }
  678. if(additionalDeps.length())
  679. {
  680. fout << "\t\t\t\t\tAdditionalDependencies=\""
  681. << additionalDeps.c_str() << "\"\n";
  682. }
  683. fout << "\t\t\t\t\t/>\n"
  684. << "\t\t\t\t</FileConfiguration>\n";
  685. }
  686. }
  687. fout << "\t\t\t</File>\n";
  688. }
  689. }
  690. // If the group has a name, write the footer.
  691. if(name != "")
  692. {
  693. this->WriteVCProjEndGroup(fout);
  694. }
  695. }
  696. fout << "\t</Files>\n";
  697. // Write the VCProj file's footer.
  698. this->WriteVCProjFooter(fout);
  699. }
  700. void cmLocalVisualStudio7Generator::WriteCustomRule(std::ostream& fout,
  701. const char* source,
  702. const char* command,
  703. const char* comment,
  704. const std::set<std::string>& depends,
  705. const std::set<std::string>& outputs,
  706. const char* compileFlags)
  707. {
  708. std::string cmd = command;
  709. cmSystemTools::ReplaceString(cmd, "\"", "&quot;");
  710. std::vector<std::string>::iterator i;
  711. std::vector<std::string> *configs =
  712. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator)->GetConfigurations();
  713. for(i = configs->begin(); i != configs->end(); ++i)
  714. {
  715. fout << "\t\t\t\t<FileConfiguration\n";
  716. fout << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n";
  717. if(compileFlags)
  718. {
  719. fout << "\t\t\t\t\t<Tool\n"
  720. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  721. << "\t\t\t\t\tAdditionalOptions=\""
  722. << compileFlags << "\"/>\n";
  723. }
  724. fout << "\t\t\t\t\t<Tool\n"
  725. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  726. << "\t\t\t\t\tDescription=\"Building " << comment;
  727. std::set<std::string>::const_iterator it;
  728. for ( it = outputs.begin(); it != outputs.end(); it ++ )
  729. {
  730. fout << " " << *it;
  731. }
  732. fout << "\"\n"
  733. << "\t\t\t\t\tCommandLine=\"" << cmd << "\n\"\n"
  734. << "\t\t\t\t\tAdditionalDependencies=\"";
  735. // Write out the dependencies for the rule.
  736. std::string temp;
  737. for(std::set<std::string>::const_iterator d = depends.begin();
  738. d != depends.end(); ++d)
  739. {
  740. fout << this->ConvertToXMLOutputPath(d->c_str())
  741. << ";";
  742. }
  743. fout << "\"\n";
  744. fout << "\t\t\t\t\tOutputs=\"";
  745. if(outputs.size() == 0)
  746. {
  747. fout << source << "_force";
  748. }
  749. bool first = true;
  750. // Write a rule for every output generated by this command.
  751. for(std::set<std::string>::const_iterator output = outputs.begin();
  752. output != outputs.end(); ++output)
  753. {
  754. if(!first)
  755. {
  756. fout << ";";
  757. }
  758. else
  759. {
  760. first = false;
  761. }
  762. fout << this->ConvertToXMLOutputPath(output->c_str());
  763. }
  764. fout << "\"/>\n";
  765. fout << "\t\t\t\t</FileConfiguration>\n";
  766. }
  767. }
  768. void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
  769. const char* group,
  770. const char* )
  771. {
  772. fout << "\t\t<Filter\n"
  773. << "\t\t\tName=\"" << group << "\"\n"
  774. << "\t\t\tFilter=\"\">\n";
  775. }
  776. void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
  777. {
  778. fout << "\t\t</Filter>\n";
  779. }
  780. std::string
  781. cmLocalVisualStudio7Generator::CombineCommands(
  782. const cmSourceGroup::Commands &commands,
  783. cmSourceGroup::CommandFiles &totalCommand,
  784. const char *source)
  785. {
  786. // Loop through every custom command generating code from the
  787. // current source.
  788. // build up the depends and outputs and commands
  789. std::string totalCommandStr = "";
  790. std::string temp;
  791. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  792. c != commands.end(); ++c)
  793. {
  794. temp=
  795. cmSystemTools::ConvertToOutputPath(c->second.m_Command.c_str());
  796. totalCommandStr += temp;
  797. totalCommandStr += " ";
  798. totalCommandStr += c->second.m_Arguments;
  799. totalCommandStr += "\n";
  800. totalCommand.Merge(c->second);
  801. totalCommand.m_Comment = c->second.m_Comment.c_str();
  802. }
  803. // Create a dummy file with the name of the source if it does
  804. // not exist
  805. if(totalCommand.m_Outputs.empty())
  806. {
  807. std::string dummyFile = m_Makefile->GetStartOutputDirectory();
  808. dummyFile += "/";
  809. dummyFile += source;
  810. if(!cmSystemTools::FileExists(dummyFile.c_str()))
  811. {
  812. std::ofstream fout(dummyFile.c_str());
  813. fout << "Dummy file created by cmake as unused source for utility command.\n";
  814. }
  815. }
  816. return totalCommandStr;
  817. }
  818. // look for custom rules on a target and collect them together
  819. void cmLocalVisualStudio7Generator::OutputTargetRules(std::ostream& fout,
  820. const cmTarget &target,
  821. const char *libName)
  822. {
  823. if (target.GetType() >= cmTarget::UTILITY)
  824. {
  825. return;
  826. }
  827. // Find the group in which the lix exe custom rules belong
  828. bool init = false;
  829. for (std::vector<cmCustomCommand>::const_iterator cr =
  830. target.GetCustomCommands().begin();
  831. cr != target.GetCustomCommands().end(); ++cr)
  832. {
  833. cmCustomCommand cc(*cr);
  834. cc.ExpandVariables(*m_Makefile);
  835. if (cc.GetSourceName() == libName)
  836. {
  837. if(!init)
  838. {
  839. fout << "\nCommandLine=\"";
  840. init = true;
  841. }
  842. std::string args = cc.GetArguments();
  843. cmSystemTools::ReplaceString(args, "\"", "&quot;");
  844. fout << this->ConvertToXMLOutputPath(cc.GetCommand().c_str()) << " " << args << "\n";
  845. }
  846. }
  847. if (init)
  848. {
  849. fout << "\"";
  850. }
  851. }
  852. void
  853. cmLocalVisualStudio7Generator::WriteProjectStart(std::ostream& fout,
  854. const char *libName,
  855. const cmTarget &,
  856. std::vector<cmSourceGroup> &)
  857. {
  858. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  859. << "<VisualStudioProject\n"
  860. << "\tProjectType=\"Visual C++\"\n"
  861. << "\tVersion=\"7.00\"\n"
  862. << "\tName=\"" << libName << "\"\n"
  863. << "\tSccProjectName=\"\"\n"
  864. << "\tSccLocalPath=\"\"\n"
  865. << "\tKeyword=\"Win32Proj\">\n"
  866. << "\t<Platforms>\n"
  867. << "\t\t<Platform\n\t\t\tName=\"Win32\"/>\n"
  868. << "\t</Platforms>\n";
  869. }
  870. void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout)
  871. {
  872. fout << "\t<Globals>\n"
  873. << "\t</Globals>\n"
  874. << "</VisualStudioProject>\n";
  875. }
  876. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPath(const char* path)
  877. {
  878. std::string ret = cmSystemTools::ConvertToOutputPath(path);
  879. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  880. return ret;
  881. }
  882. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPathSingle(const char* path)
  883. {
  884. std::string ret = cmSystemTools::ConvertToOutputPath(path);
  885. cmSystemTools::ReplaceString(ret, "\"", "");
  886. return ret;
  887. }