cmLocalVisualStudio7Generator.cxx 34 KB

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