cmLocalVisualStudio7Generator.cxx 29 KB

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