cmLocalVisualStudio7Generator.cxx 34 KB

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