cmLocalVisualStudio7Generator.cxx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. fout << define << ",";
  523. if(!done)
  524. {
  525. pos = defs.find("-D", nextpos);
  526. }
  527. }
  528. }
  529. void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
  530. const char *libName,
  531. cmTarget &target)
  532. {
  533. // get the configurations
  534. std::vector<std::string> *configs =
  535. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator)->GetConfigurations();
  536. // We may be modifying the source groups temporarily, so make a copy.
  537. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  538. // get the classes from the source lists then add them to the groups
  539. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  540. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  541. i != classes.end(); i++)
  542. {
  543. // Add the file to the list of sources.
  544. std::string source = (*i)->GetFullPath();
  545. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  546. {
  547. m_ModuleDefinitionFile = (*i)->GetFullPath();
  548. }
  549. cmSourceGroup& sourceGroup = m_Makefile->FindSourceGroup(source.c_str(),
  550. sourceGroups);
  551. sourceGroup.AddSource(source.c_str(), *i);
  552. }
  553. // add any custom rules to the source groups
  554. for (std::vector<cmCustomCommand>::const_iterator cr =
  555. target.GetCustomCommands().begin();
  556. cr != target.GetCustomCommands().end(); ++cr)
  557. {
  558. cmSourceGroup& sourceGroup =
  559. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  560. sourceGroups);
  561. cmCustomCommand cc(*cr);
  562. cc.ExpandVariables(*m_Makefile);
  563. sourceGroup.AddCustomCommand(cc);
  564. }
  565. // open the project
  566. this->WriteProjectStart(fout, libName, target, sourceGroups);
  567. // write the configuration information
  568. this->WriteConfigurations(fout, libName, target);
  569. fout << "\t<Files>\n";
  570. // Find the group in which the CMakeLists.txt source belongs, and add
  571. // the rule to generate this VCProj file.
  572. for(std::vector<cmSourceGroup>::reverse_iterator sg = sourceGroups.rbegin();
  573. sg != sourceGroups.rend(); ++sg)
  574. {
  575. if(sg->Matches("CMakeLists.txt"))
  576. {
  577. this->AddVCProjBuildRule(*sg);
  578. break;
  579. }
  580. }
  581. // Loop through every source group.
  582. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  583. sg != sourceGroups.end(); ++sg)
  584. {
  585. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  586. // If the group is empty, don't write it at all.
  587. if(buildRules.empty())
  588. { continue; }
  589. // If the group has a name, write the header.
  590. std::string name = sg->GetName();
  591. if(name != "")
  592. {
  593. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  594. }
  595. // Loop through each build rule in the source group.
  596. for(cmSourceGroup::BuildRules::const_iterator cc =
  597. buildRules.begin(); cc != buildRules.end(); ++ cc)
  598. {
  599. std::string source = cc->first;
  600. const cmSourceGroup::Commands& commands = cc->second.m_Commands;
  601. const char* compileFlags = 0;
  602. if(cc->second.m_SourceFile)
  603. {
  604. compileFlags = cc->second.m_SourceFile->GetProperty("COMPILE_FLAGS");
  605. }
  606. if (source != libName || target.GetType() == cmTarget::UTILITY)
  607. {
  608. fout << "\t\t\t<File\n";
  609. std::string d = cmSystemTools::ConvertToOutputPath(source.c_str());
  610. // remove double quotes from the string
  611. cmSystemTools::ReplaceString(d, "\"", "");
  612. // Tell MS-Dev what the source is. If the compiler knows how to
  613. // build it, then it will.
  614. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  615. if (!commands.empty())
  616. {
  617. cmSourceGroup::CommandFiles totalCommand;
  618. std::string totalCommandStr;
  619. totalCommandStr = this->CombineCommands(commands, totalCommand,
  620. source.c_str());
  621. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  622. totalCommand.m_Depends,
  623. totalCommand.m_Outputs, compileFlags);
  624. }
  625. else if(compileFlags)
  626. {
  627. for(std::vector<std::string>::iterator i = configs->begin();
  628. i != configs->end(); ++i)
  629. {
  630. fout << "\t\t\t\t<FileConfiguration\n"
  631. << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n"
  632. << "\t\t\t\t\t<Tool\n"
  633. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  634. << "\t\t\t\t\tAdditionalOptions=\""
  635. << compileFlags << "\"/>\n"
  636. << "\t\t\t\t</FileConfiguration>\n";
  637. }
  638. }
  639. fout << "\t\t\t</File>\n";
  640. }
  641. }
  642. // If the group has a name, write the footer.
  643. if(name != "")
  644. {
  645. this->WriteVCProjEndGroup(fout);
  646. }
  647. }
  648. fout << "\t</Files>\n";
  649. // Write the VCProj file's footer.
  650. this->WriteVCProjFooter(fout);
  651. }
  652. void cmLocalVisualStudio7Generator::WriteCustomRule(std::ostream& fout,
  653. const char* source,
  654. const char* command,
  655. const std::set<std::string>& depends,
  656. const std::set<std::string>& outputs,
  657. const char* compileFlags)
  658. {
  659. std::string cmd = command;
  660. cmSystemTools::ReplaceString(cmd, "\"", "&quot;");
  661. std::vector<std::string>::iterator i;
  662. std::vector<std::string> *configs =
  663. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator)->GetConfigurations();
  664. for(i = configs->begin(); i != configs->end(); ++i)
  665. {
  666. fout << "\t\t\t\t<FileConfiguration\n";
  667. fout << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n";
  668. if(compileFlags)
  669. {
  670. fout << "\t\t\t\t\t<Tool\n"
  671. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  672. << "\t\t\t\t\tAdditionalOptions=\""
  673. << compileFlags << "\"/>\n";
  674. }
  675. fout << "\t\t\t\t\t<Tool\n"
  676. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  677. << "\t\t\t\t\tCommandLine=\"" << cmd << "\n\"\n"
  678. << "\t\t\t\t\tAdditionalDependencies=\"";
  679. // Write out the dependencies for the rule.
  680. std::string temp;
  681. for(std::set<std::string>::const_iterator d = depends.begin();
  682. d != depends.end(); ++d)
  683. {
  684. fout << this->ConvertToXMLOutputPath(d->c_str())
  685. << ";";
  686. }
  687. fout << "\"\n";
  688. fout << "\t\t\t\t\tOutputs=\"";
  689. if(outputs.size() == 0)
  690. {
  691. fout << source << "_force";
  692. }
  693. bool first = true;
  694. // Write a rule for every output generated by this command.
  695. for(std::set<std::string>::const_iterator output = outputs.begin();
  696. output != outputs.end(); ++output)
  697. {
  698. if(!first)
  699. {
  700. fout << ";";
  701. }
  702. else
  703. {
  704. first = false;
  705. }
  706. fout << output->c_str();
  707. }
  708. fout << "\"/>\n";
  709. fout << "\t\t\t\t</FileConfiguration>\n";
  710. }
  711. }
  712. void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
  713. const char* group,
  714. const char* )
  715. {
  716. fout << "\t\t<Filter\n"
  717. << "\t\t\tName=\"" << group << "\"\n"
  718. << "\t\t\tFilter=\"\">\n";
  719. }
  720. void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
  721. {
  722. fout << "\t\t</Filter>\n";
  723. }
  724. std::string
  725. cmLocalVisualStudio7Generator::CombineCommands(const cmSourceGroup::Commands &commands,
  726. cmSourceGroup::CommandFiles &totalCommand,
  727. const char *source)
  728. {
  729. // Loop through every custom command generating code from the
  730. // current source.
  731. // build up the depends and outputs and commands
  732. std::string totalCommandStr = "";
  733. std::string temp;
  734. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  735. c != commands.end(); ++c)
  736. {
  737. temp=
  738. cmSystemTools::ConvertToOutputPath(c->second.m_Command.c_str());
  739. totalCommandStr += temp;
  740. totalCommandStr += " ";
  741. totalCommandStr += c->second.m_Arguments;
  742. totalCommandStr += "\n";
  743. totalCommand.Merge(c->second);
  744. }
  745. // Create a dummy file with the name of the source if it does
  746. // not exist
  747. if(totalCommand.m_Outputs.empty())
  748. {
  749. std::string dummyFile = m_Makefile->GetStartOutputDirectory();
  750. dummyFile += "/";
  751. dummyFile += source;
  752. if(!cmSystemTools::FileExists(dummyFile.c_str()))
  753. {
  754. std::ofstream fout(dummyFile.c_str());
  755. fout << "Dummy file created by cmake as unused source for utility command.\n";
  756. }
  757. }
  758. return totalCommandStr;
  759. }
  760. // look for custom rules on a target and collect them together
  761. void cmLocalVisualStudio7Generator::OutputTargetRules(std::ostream& fout,
  762. const cmTarget &target,
  763. const char *libName)
  764. {
  765. if (target.GetType() >= cmTarget::UTILITY)
  766. {
  767. return;
  768. }
  769. // Find the group in which the lix exe custom rules belong
  770. bool init = false;
  771. for (std::vector<cmCustomCommand>::const_iterator cr =
  772. target.GetCustomCommands().begin();
  773. cr != target.GetCustomCommands().end(); ++cr)
  774. {
  775. cmCustomCommand cc(*cr);
  776. cc.ExpandVariables(*m_Makefile);
  777. if (cc.GetSourceName() == libName)
  778. {
  779. if(!init)
  780. {
  781. fout << "\nCommandLine=\"";
  782. init = true;
  783. }
  784. std::string args = cc.GetArguments();
  785. cmSystemTools::ReplaceString(args, "\"", "&quot;");
  786. fout << this->ConvertToXMLOutputPath(cc.GetCommand().c_str()) << " " << args << "\n";
  787. }
  788. }
  789. if (init)
  790. {
  791. fout << "\"";
  792. }
  793. }
  794. void
  795. cmLocalVisualStudio7Generator::WriteProjectStart(std::ostream& fout,
  796. const char *libName,
  797. const cmTarget &,
  798. std::vector<cmSourceGroup> &)
  799. {
  800. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  801. << "<VisualStudioProject\n"
  802. << "\tProjectType=\"Visual C++\"\n"
  803. << "\tVersion=\"7.00\"\n"
  804. << "\tName=\"" << libName << "\"\n"
  805. << "\tSccProjectName=\"\"\n"
  806. << "\tSccLocalPath=\"\"\n"
  807. << "\tKeyword=\"Win32Proj\">\n"
  808. << "\t<Platforms>\n"
  809. << "\t\t<Platform\n\t\t\tName=\"Win32\"/>\n"
  810. << "\t</Platforms>\n";
  811. }
  812. void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout)
  813. {
  814. fout << "\t<Globals>\n"
  815. << "\t</Globals>\n"
  816. << "</VisualStudioProject>\n";
  817. }
  818. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPath(const char* path)
  819. {
  820. std::string ret = cmSystemTools::ConvertToOutputPath(path);
  821. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  822. return ret;
  823. }
  824. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPathSingle(const char* path)
  825. {
  826. std::string ret = cmSystemTools::ConvertToOutputPath(path);
  827. cmSystemTools::ReplaceString(ret, "\"", "");
  828. return ret;
  829. }