cmLocalVisualStudio7Generator.cxx 33 KB

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