cmLocalVisualStudio7Generator.cxx 29 KB

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