cmMSDotNETGenerator.cxx 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmMSDotNETGenerator.h"
  14. #include "cmStandardIncludes.h"
  15. #include "cmMakefile.h"
  16. #include "cmCacheManager.h"
  17. #include "windows.h"
  18. #include "cmSystemTools.h"
  19. #include "cmRegularExpression.h"
  20. #include "cmSourceGroup.h"
  21. cmMSDotNETGenerator::cmMSDotNETGenerator()
  22. {
  23. // default to building a sln project file
  24. BuildProjOn();
  25. }
  26. void cmMSDotNETGenerator::GenerateMakefile()
  27. {
  28. std::string configTypes = m_Makefile->GetDefinition("CMAKE_CONFIGURATION_TYPES");
  29. std::string::size_type start = 0;
  30. std::string::size_type endpos = 0;
  31. while(endpos != std::string::npos)
  32. {
  33. endpos = configTypes.find(' ', start);
  34. std::string config;
  35. std::string::size_type len;
  36. if(endpos != std::string::npos)
  37. {
  38. len = endpos - start;
  39. }
  40. else
  41. {
  42. len = configTypes.size() - start;
  43. }
  44. config = configTypes.substr(start, len);
  45. if(config == "Debug" || config == "Release" ||
  46. config == "MinSizeRel" || config == "RelWithDebInfo")
  47. {
  48. m_Configurations.push_back(config);
  49. }
  50. else
  51. {
  52. cmSystemTools::Error("Invalid configuration type in CMAKE_CONFIGURATION_TYPES: ",
  53. config.c_str(),
  54. " (Valid types are Debug,Release,MinSizeRel,RelWithDebInfo)");
  55. }
  56. start = endpos+1;
  57. }
  58. if(m_Configurations.size() == 0)
  59. {
  60. m_Configurations.push_back("Debug");
  61. m_Configurations.push_back("Release");
  62. }
  63. if(m_BuildSLN)
  64. {
  65. this->OutputSLNFile();
  66. }
  67. else
  68. {
  69. this->OutputVCProjFile();
  70. }
  71. }
  72. cmMSDotNETGenerator::~cmMSDotNETGenerator()
  73. {
  74. }
  75. void cmMSDotNETGenerator::SetLocal(bool local)
  76. {
  77. m_BuildSLN = !local;
  78. }
  79. void cmMSDotNETGenerator::ComputeSystemInfo()
  80. {
  81. // now load the settings
  82. if(!m_Makefile->GetDefinition("CMAKE_ROOT"))
  83. {
  84. cmSystemTools::Error(
  85. "CMAKE_ROOT has not been defined, bad GUI or driver program");
  86. return;
  87. }
  88. std::string fpath =
  89. m_Makefile->GetDefinition("CMAKE_ROOT");
  90. fpath += "/Templates/CMakeDotNetSystemConfig.cmake";
  91. m_Makefile->ReadListFile(NULL,fpath.c_str());
  92. }
  93. // output the SLN file
  94. void cmMSDotNETGenerator::OutputSLNFile()
  95. {
  96. // if this is an out of source build, create the output directory
  97. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  98. m_Makefile->GetHomeDirectory()) != 0)
  99. {
  100. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  101. {
  102. cmSystemTools::Error("Error creating output directory for SLN file",
  103. m_Makefile->GetStartOutputDirectory());
  104. }
  105. }
  106. // create the dsw file name
  107. std::string fname;
  108. fname = m_Makefile->GetStartOutputDirectory();
  109. fname += "/";
  110. if(strlen(m_Makefile->GetProjectName()) == 0)
  111. {
  112. m_Makefile->SetProjectName("Project");
  113. }
  114. fname += m_Makefile->GetProjectName();
  115. fname += ".sln";
  116. std::ofstream fout(fname.c_str());
  117. if(!fout)
  118. {
  119. cmSystemTools::Error("Error can not open SLN file for write: "
  120. ,fname.c_str());
  121. return;
  122. }
  123. this->WriteSLNFile(fout);
  124. }
  125. // Write a SLN file to the stream
  126. void cmMSDotNETGenerator::WriteSLNFile(std::ostream& fout)
  127. {
  128. // Write out the header for a SLN file
  129. this->WriteSLNHeader(fout);
  130. // Create a list of cmMakefile created from all the
  131. // CMakeLists.txt files that are in sub directories of
  132. // this one.
  133. std::vector<cmMakefile*> allListFiles;
  134. // add this makefile to the list
  135. allListFiles.push_back(m_Makefile);
  136. // add a special target that depends on ALL projects for easy build
  137. // of Debug only
  138. m_Makefile->AddUtilityCommand("ALL_BUILD", "echo", "\"Build all projects\"",
  139. false);
  140. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  141. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  142. ctest += "/";
  143. ctest += "ctest";
  144. ctest += cmSystemTools::GetExecutableExtension();
  145. if(!cmSystemTools::FileExists(ctest.c_str()))
  146. {
  147. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  148. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  149. ctest += "/Debug/";
  150. ctest += "ctest";
  151. ctest += cmSystemTools::GetExecutableExtension();
  152. }
  153. if(!cmSystemTools::FileExists(ctest.c_str()))
  154. {
  155. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  156. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  157. ctest += "/Release/";
  158. ctest += "ctest";
  159. ctest += cmSystemTools::GetExecutableExtension();
  160. }
  161. m_Makefile->AddUtilityCommand("RUN_TESTS", ctest.c_str(), "-D $(IntDir)",
  162. false);
  163. m_Makefile->FindSubDirectoryCMakeListsFiles(allListFiles);
  164. // For each cmMakefile, create a VCProj for it, and
  165. // add it to this SLN file
  166. std::vector<cmMakefile*>::iterator k;
  167. for(k = allListFiles.begin();
  168. k != allListFiles.end(); ++k)
  169. {
  170. cmMakefile* mf = *k;
  171. cmMSDotNETGenerator* pg = 0;
  172. // if not this makefile, then create a new generator
  173. if(m_Makefile != mf)
  174. {
  175. // Create an MS generator with SLN off, so it only creates dsp files
  176. pg = new cmMSDotNETGenerator;
  177. }
  178. else
  179. {
  180. pg = static_cast<cmMSDotNETGenerator*>(m_Makefile->GetMakefileGenerator());
  181. }
  182. // make sure the generator is building dsp files
  183. pg->BuildSLNOff();
  184. mf->SetMakefileGenerator(pg);
  185. mf->GenerateMakefile();
  186. // Get the source directory from the makefile
  187. std::string dir = mf->GetStartDirectory();
  188. // Get the home directory with the trailing slash
  189. std::string homedir = m_Makefile->GetHomeDirectory();
  190. homedir += "/";
  191. // remove the home directory and / from the source directory
  192. // this gives a relative path
  193. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  194. // Get the list of create dsp files names from the cmVCProjWriter, more
  195. // than one dsp could have been created per input CMakeLists.txt file
  196. // for each target
  197. std::vector<std::string> dspnames =
  198. pg->GetCreatedProjectNames();
  199. cmTargets &tgts = pg->GetMakefile()->GetTargets();
  200. cmTargets::iterator l = tgts.begin();
  201. for(std::vector<std::string>::iterator si = dspnames.begin();
  202. l != tgts.end(); ++l)
  203. {
  204. // special handling for the current makefile
  205. if(mf == m_Makefile)
  206. {
  207. dir = "."; // no subdirectory for project generated
  208. // if this is the special ALL_BUILD utility, then
  209. // make it depend on every other non UTILITY project.
  210. // This is done by adding the names to the GetUtilities
  211. // vector on the makefile
  212. if(l->first == "ALL_BUILD")
  213. {
  214. for(std::vector<cmMakefile*>::iterator a = allListFiles.begin();
  215. a != allListFiles.end(); ++a)
  216. {
  217. const cmTargets &atgts = (*a)->GetTargets();
  218. for(cmTargets::const_iterator al = atgts.begin();
  219. al != atgts.end(); ++al)
  220. {
  221. if (al->second.IsInAll())
  222. {
  223. if (al->second.GetType() == cmTarget::UTILITY)
  224. {
  225. l->second.AddUtility(al->first.c_str());
  226. }
  227. else
  228. {
  229. l->second.GetLinkLibraries().push_back(
  230. cmTarget::LinkLibraries::value_type(al->first,
  231. cmTarget::GENERAL));
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. // Write the project into the SLN file
  239. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  240. {
  241. cmCustomCommand cc = l->second.GetCustomCommands()[0];
  242. // dodgy use of the cmCustomCommand's members to store the
  243. // arguments from the INCLUDE_EXTERNAL_MSPROJECT command
  244. std::vector<std::string> stuff = cc.GetDepends();
  245. std::vector<std::string> depends = cc.GetOutputs();
  246. this->WriteExternalProject(fout, stuff[0].c_str(), stuff[1].c_str(), depends);
  247. ++si;
  248. }
  249. else if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  250. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  251. {
  252. this->WriteProject(fout, si->c_str(), dir.c_str(),
  253. pg,l->second);
  254. ++si;
  255. }
  256. }
  257. }
  258. fout << "Global\n"
  259. << "\tGlobalSection(SolutionConfiguration) = preSolution\n";
  260. int c = 0;
  261. for(std::vector<std::string>::iterator i = m_Configurations.begin();
  262. i != m_Configurations.end(); ++i)
  263. {
  264. fout << "\t\tConfigName." << c << " = " << *i << "\n";
  265. c++;
  266. }
  267. fout << "\tEndGlobalSection\n"
  268. << "\tGlobalSection(ProjectDependencies) = postSolution\n";
  269. // loop over again and compute the depends
  270. for(k = allListFiles.begin(); k != allListFiles.end(); ++k)
  271. {
  272. cmMakefile* mf = *k;
  273. cmMSDotNETGenerator* pg =
  274. static_cast<cmMSDotNETGenerator*>(mf->GetMakefileGenerator());
  275. // Get the list of create dsp files names from the cmVCProjWriter, more
  276. // than one dsp could have been created per input CMakeLists.txt file
  277. // for each target
  278. std::vector<std::string> dspnames =
  279. pg->GetCreatedProjectNames();
  280. cmTargets &tgts = pg->GetMakefile()->GetTargets();
  281. cmTargets::iterator l = tgts.begin();
  282. std::string dir = mf->GetStartDirectory();
  283. for(std::vector<std::string>::iterator si = dspnames.begin();
  284. l != tgts.end(); ++l)
  285. {
  286. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  287. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  288. {
  289. this->WriteProjectDepends(fout, si->c_str(), dir.c_str(),
  290. pg,l->second);
  291. ++si;
  292. }
  293. }
  294. }
  295. fout << "\tEndGlobalSection\n";
  296. fout << "\tGlobalSection(ProjectConfiguration) = postSolution\n";
  297. // loop over again and compute the depends
  298. for(k = allListFiles.begin(); k != allListFiles.end(); ++k)
  299. {
  300. cmMakefile* mf = *k;
  301. cmMSDotNETGenerator* pg =
  302. static_cast<cmMSDotNETGenerator*>(mf->GetMakefileGenerator());
  303. // Get the list of create dsp files names from the cmVCProjWriter, more
  304. // than one dsp could have been created per input CMakeLists.txt file
  305. // for each target
  306. std::vector<std::string> dspnames =
  307. pg->GetCreatedProjectNames();
  308. cmTargets &tgts = pg->GetMakefile()->GetTargets();
  309. cmTargets::iterator l = tgts.begin();
  310. std::string dir = mf->GetStartDirectory();
  311. for(std::vector<std::string>::iterator si = dspnames.begin();
  312. l != tgts.end(); ++l)
  313. {
  314. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  315. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  316. {
  317. this->WriteProjectConfigurations(fout, si->c_str());
  318. ++si;
  319. }
  320. }
  321. // delete the cmMakefile which also deletes the cmMSProjectGenerator
  322. if(mf != m_Makefile)
  323. {
  324. delete mf;
  325. }
  326. }
  327. fout << "\tEndGlobalSection\n";
  328. // Write the footer for the SLN file
  329. this->WriteSLNFooter(fout);
  330. }
  331. // Write a dsp file into the SLN file,
  332. // Note, that dependencies from executables to
  333. // the libraries it uses are also done here
  334. void cmMSDotNETGenerator::WriteProject(std::ostream& fout,
  335. const char* dspname,
  336. const char* dir,
  337. cmMSDotNETGenerator*,
  338. const cmTarget&
  339. )
  340. {
  341. std::string d = cmSystemTools::ConvertToOutputPath(dir);
  342. fout << "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\" = \""
  343. << dspname << "\", \""
  344. << d << "\\" << dspname << ".vcproj\", \"{"
  345. << this->CreateGUID(dspname) << "}\"\nEndProject\n";
  346. }
  347. // Write a dsp file into the SLN file,
  348. // Note, that dependencies from executables to
  349. // the libraries it uses are also done here
  350. void cmMSDotNETGenerator::WriteProjectDepends(std::ostream& fout,
  351. const char* dspname,
  352. const char* ,
  353. cmMSDotNETGenerator*,
  354. const cmTarget& target
  355. )
  356. {
  357. int depcount = 0;
  358. // insert Begin Project Dependency Project_Dep_Name project stuff here
  359. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  360. {
  361. cmTarget::LinkLibraries::const_iterator j, jend;
  362. j = target.GetLinkLibraries().begin();
  363. jend = target.GetLinkLibraries().end();
  364. for(;j!= jend; ++j)
  365. {
  366. if(j->first != dspname)
  367. {
  368. // is the library part of this SLN ? If so add dependency
  369. const char* cacheValue
  370. = m_Makefile->GetDefinition(j->first.c_str());
  371. if(cacheValue)
  372. {
  373. fout << "\t\t{" << this->CreateGUID(dspname) << "}." << depcount << " = {"
  374. << this->CreateGUID(j->first.c_str()) << "}\n";
  375. depcount++;
  376. }
  377. }
  378. }
  379. }
  380. std::set<std::string>::const_iterator i, end;
  381. // write utility dependencies.
  382. i = target.GetUtilities().begin();
  383. end = target.GetUtilities().end();
  384. for(;i!= end; ++i)
  385. {
  386. if(*i != dspname)
  387. {
  388. fout << "\t\t{" << this->CreateGUID(dspname) << "}." << depcount << " = {"
  389. << this->CreateGUID(i->c_str()) << "}\n";
  390. depcount++;
  391. }
  392. }
  393. }
  394. // Write a dsp file into the SLN file,
  395. // Note, that dependencies from executables to
  396. // the libraries it uses are also done here
  397. void cmMSDotNETGenerator::WriteProjectConfigurations(std::ostream& fout, const char* name)
  398. {
  399. std::string guid = this->CreateGUID(name);
  400. for(std::vector<std::string>::iterator i = m_Configurations.begin();
  401. i != m_Configurations.end(); ++i)
  402. {
  403. fout << "\t\t{" << guid << "}." << *i << ".ActiveCfg = " << *i << "|Win32\n"
  404. << "\t\t{" << guid << "}." << *i << ".Build.0 = " << *i << "|Win32\n";
  405. }
  406. }
  407. // Write a dsp file into the SLN file,
  408. // Note, that dependencies from executables to
  409. // the libraries it uses are also done here
  410. void cmMSDotNETGenerator::WriteExternalProject(std::ostream& ,
  411. const char* ,
  412. const char* ,
  413. const std::vector<std::string>& )
  414. {
  415. cmSystemTools::Error("WriteExternalProject not implemented");
  416. // fout << "#########################################################"
  417. // "######################\n\n";
  418. // fout << "Project: \"" << name << "\"="
  419. // << location << " - Package Owner=<4>\n\n";
  420. // fout << "Package=<5>\n{{{\n}}}\n\n";
  421. // fout << "Package=<4>\n";
  422. // fout << "{{{\n";
  423. // std::vector<std::string>::const_iterator i, end;
  424. // // write dependencies.
  425. // i = dependencies.begin();
  426. // end = dependencies.end();
  427. // for(;i!= end; ++i)
  428. // {
  429. // fout << "Begin Project Dependency\n";
  430. // fout << "Project_Dep_Name " << *i << "\n";
  431. // fout << "End Project Dependency\n";
  432. // }
  433. // fout << "}}}\n\n";
  434. }
  435. // Standard end of dsw file
  436. void cmMSDotNETGenerator::WriteSLNFooter(std::ostream& fout)
  437. {
  438. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  439. << "\tEndGlobalSection\n"
  440. << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  441. << "\tEndGlobalSection\n"
  442. << "EndGlobal\n";
  443. }
  444. // ouput standard header for dsw file
  445. void cmMSDotNETGenerator::WriteSLNHeader(std::ostream& fout)
  446. {
  447. fout << "Microsoft Visual Studio Solution File, Format Version 7.00\n";
  448. }
  449. std::string cmMSDotNETGenerator::CreateGUID(const char* name)
  450. {
  451. std::map<cmStdString, cmStdString>::iterator i = m_GUIDMap.find(name);
  452. if(i != m_GUIDMap.end())
  453. {
  454. return i->second;
  455. }
  456. std::string ret;
  457. UUID uid;
  458. unsigned char *uidstr;
  459. UuidCreate(&uid);
  460. UuidToString(&uid,&uidstr);
  461. ret = reinterpret_cast<char*>(uidstr);
  462. RpcStringFree(&uidstr);
  463. ret = cmSystemTools::UpperCase(ret);
  464. m_GUIDMap[name] = ret;
  465. return ret;
  466. }
  467. // TODO
  468. // for CommandLine= need to repleace quotes with &quot
  469. // write out configurations
  470. void cmMSDotNETGenerator::OutputVCProjFile()
  471. {
  472. // If not an in source build, then create the output directory
  473. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  474. m_Makefile->GetHomeDirectory()) != 0)
  475. {
  476. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  477. {
  478. cmSystemTools::Error("Error creating directory ",
  479. m_Makefile->GetStartOutputDirectory());
  480. }
  481. }
  482. m_LibraryOutputPath = "";
  483. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  484. {
  485. m_LibraryOutputPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  486. }
  487. if(m_LibraryOutputPath.size())
  488. {
  489. // make sure there is a trailing slash
  490. if(m_LibraryOutputPath[m_LibraryOutputPath.size()-1] != '/')
  491. {
  492. m_LibraryOutputPath += "/";
  493. }
  494. }
  495. m_ExecutableOutputPath = "";
  496. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  497. {
  498. m_ExecutableOutputPath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  499. }
  500. if(m_ExecutableOutputPath.size())
  501. {
  502. // make sure there is a trailing slash
  503. if(m_ExecutableOutputPath[m_ExecutableOutputPath.size()-1] != '/')
  504. {
  505. m_ExecutableOutputPath += "/";
  506. }
  507. }
  508. // Create the VCProj or set of VCProj's for libraries and executables
  509. // clear project names
  510. m_CreatedProjectNames.clear();
  511. // build any targets
  512. cmTargets &tgts = m_Makefile->GetTargets();
  513. for(cmTargets::iterator l = tgts.begin();
  514. l != tgts.end(); l++)
  515. {
  516. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  517. // so don't build a projectfile for it
  518. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  519. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  520. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  521. {
  522. this->CreateSingleVCProj(l->first.c_str(),l->second);
  523. }
  524. }
  525. }
  526. void cmMSDotNETGenerator::CreateSingleVCProj(const char *lname, cmTarget &target)
  527. {
  528. // add to the list of projects
  529. std::string pname = lname;
  530. m_CreatedProjectNames.push_back(pname);
  531. // create the dsp.cmake file
  532. std::string fname;
  533. fname = m_Makefile->GetStartOutputDirectory();
  534. fname += "/";
  535. fname += lname;
  536. fname += ".vcproj";
  537. // save the name of the real dsp file
  538. std::string realVCProj = fname;
  539. fname += ".cmake";
  540. std::ofstream fout(fname.c_str());
  541. if(!fout)
  542. {
  543. cmSystemTools::Error("Error Writing ", fname.c_str());
  544. }
  545. this->WriteVCProjFile(fout,lname,target);
  546. fout.close();
  547. // if the dsp file has changed, then write it.
  548. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realVCProj.c_str());
  549. }
  550. void cmMSDotNETGenerator::AddVCProjBuildRule(cmSourceGroup& sourceGroup)
  551. {
  552. std::string dspname = *(m_CreatedProjectNames.end()-1);
  553. if(dspname == "ALL_BUILD")
  554. {
  555. return;
  556. }
  557. dspname += ".vcproj.cmake";
  558. std::string makefileIn = m_Makefile->GetStartDirectory();
  559. makefileIn += "/";
  560. makefileIn += "CMakeLists.txt";
  561. makefileIn = cmSystemTools::ConvertToOutputPath(makefileIn.c_str());
  562. std::string dsprule = "${CMAKE_COMMAND}";
  563. m_Makefile->ExpandVariablesInString(dsprule);
  564. dsprule = cmSystemTools::ConvertToOutputPath(dsprule.c_str());
  565. std::string args = makefileIn;
  566. args += " -H\"";
  567. args +=
  568. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeDirectory());
  569. args += "\" -S\"";
  570. args +=
  571. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartDirectory());
  572. args += "\" -O\"";
  573. args +=
  574. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartOutputDirectory());
  575. args += "\" -B\"";
  576. args +=
  577. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory());
  578. args += "\"";
  579. m_Makefile->ExpandVariablesInString(args);
  580. std::string configFile =
  581. m_Makefile->GetDefinition("CMAKE_ROOT");
  582. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  583. std::vector<std::string> listFiles = m_Makefile->GetListFiles();
  584. bool found = false;
  585. for(std::vector<std::string>::iterator i = listFiles.begin();
  586. i != listFiles.end(); ++i)
  587. {
  588. if(*i == configFile)
  589. {
  590. found = true;
  591. }
  592. }
  593. if(!found)
  594. {
  595. listFiles.push_back(configFile);
  596. }
  597. std::vector<std::string> outputs;
  598. outputs.push_back(dspname);
  599. cmCustomCommand cc(makefileIn.c_str(), dsprule.c_str(),
  600. args.c_str(),
  601. listFiles,
  602. outputs);
  603. sourceGroup.AddCustomCommand(cc);
  604. }
  605. void cmMSDotNETGenerator::WriteConfigurations(std::ostream& fout,
  606. const char *libName,
  607. const cmTarget &target)
  608. {
  609. fout << "\t<Configurations>\n";
  610. for( std::vector<std::string>::iterator i = m_Configurations.begin();
  611. i != m_Configurations.end(); ++i)
  612. {
  613. this->WriteConfiguration(fout, i->c_str(), libName, target);
  614. }
  615. fout << "\t</Configurations>\n";
  616. }
  617. void cmMSDotNETGenerator::WriteConfiguration(std::ostream& fout,
  618. const char* configName,
  619. const char *libName,
  620. const cmTarget &target)
  621. {
  622. const char* mfcFlag = m_Makefile->GetDefinition("CMAKE_MFC_FLAG");
  623. if(!mfcFlag)
  624. {
  625. mfcFlag = "0";
  626. }
  627. fout << "\t\t<Configuration\n"
  628. << "\t\t\tName=\"" << configName << "|Win32\"\n"
  629. << "\t\t\tOutputDirectory=\"" << configName << "\"\n";
  630. // This is an internal type to Visual Studio, it seems that:
  631. // 4 == static library
  632. // 2 == dll
  633. // 1 == executable
  634. // 10 == utility
  635. const char* configType = "10";
  636. switch(target.GetType())
  637. {
  638. case cmTarget::STATIC_LIBRARY:
  639. configType = "4";
  640. break;
  641. case cmTarget::SHARED_LIBRARY:
  642. case cmTarget::MODULE_LIBRARY:
  643. configType = "2";
  644. break;
  645. case cmTarget::EXECUTABLE:
  646. case cmTarget::WIN32_EXECUTABLE:
  647. configType = "1";
  648. break;
  649. case cmTarget::UTILITY:
  650. configType = "10";
  651. default:
  652. break;
  653. }
  654. fout << "\t\t\tIntermediateDirectory=\".\\" << configName << "\"\n"
  655. << "\t\t\tConfigurationType=\"" << configType << "\"\n"
  656. << "\t\t\tUseOfMFC=\"" << mfcFlag << "\"\n"
  657. << "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n"
  658. << "\t\t\tCharacterSet=\"2\">\n";
  659. fout << "\t\t\t<Tool\n"
  660. << "\t\t\t\tName=\"VCCLCompilerTool\"\n"
  661. << "\t\t\t\tAdditionalOptions=\""
  662. << m_Makefile->GetDefinition("CMAKE_CXX_FLAGS")
  663. << " -DCMAKE_INTDIR=\\&quot;" << configName << "\\&quot;"
  664. << "\"\n";
  665. fout << "\t\t\t\tAdditionalIncludeDirectories=\"";
  666. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  667. std::vector<std::string>::iterator i = includes.begin();
  668. for(;i != includes.end(); ++i)
  669. {
  670. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  671. fout << ipath << ";";
  672. }
  673. fout << "\"\n";
  674. // Optimization = 0 None Debug /O0
  675. // Optimization = 1 MinSize /O1
  676. // Optimization = 2 MaxSpeed /O2
  677. // Optimization = 3 Max Optimization /O3
  678. // RuntimeLibrary = 0 /MT multithread
  679. // RuntimeLibrary = 1 /MTd multithread debug
  680. // RuntimeLibrary = 2 /MD multithread dll
  681. // RuntimeLibrary = 3 /MDd multithread dll debug
  682. // RuntimeLibrary = 4 /ML single thread
  683. // RuntimeLibrary = 5 /MLd single thread debug
  684. // InlineFunctionExpansion = 0 none
  685. // InlineFunctionExpansion = 1 when inline keyword
  686. // InlineFunctionExpansion = 2 any time you can
  687. if(strcmp(configName, "Debug") == 0)
  688. {
  689. fout << "\t\t\t\tOptimization=\"0\"\n"
  690. << "\t\t\t\tRuntimeLibrary=\"3\"\n"
  691. << "\t\t\t\tInlineFunctionExpansion=\"0\"\n"
  692. << "\t\t\t\tPreprocessorDefinitions=\"WIN32,_DEBUG,_WINDOWS";
  693. }
  694. else if(strcmp(configName, "Release") == 0)
  695. {
  696. fout << "\t\t\t\tOptimization=\"2\"\n"
  697. << "\t\t\t\tRuntimeLibrary=\"2\"\n"
  698. << "\t\t\t\tInlineFunctionExpansion=\"1\"\n"
  699. << "\t\t\t\tPreprocessorDefinitions=\"WIN32,NDEBUG,_WINDOWS";
  700. }
  701. else if(strcmp(configName, "MinSizeRel") == 0)
  702. {
  703. fout << "\t\t\t\tOptimization=\"1\"\n"
  704. << "\t\t\t\tRuntimeLibrary=\"2\"\n"
  705. << "\t\t\t\tInlineFunctionExpansion=\"1\"\n"
  706. << "\t\t\t\tPreprocessorDefinitions=\"WIN32,NDEBUG,_WINDOWS";
  707. }
  708. else if(strcmp(configName, "RelWithDebInfo") == 0)
  709. {
  710. fout << "\t\t\t\tOptimization=\"2\"\n"
  711. << "\t\t\t\tRuntimeLibrary=\"2\"\n"
  712. << "\t\t\t\tInlineFunctionExpansion=\"1\"\n"
  713. << "\t\t\t\tPreprocessorDefinitions=\"WIN32,NDEBUG,_WINDOWS";
  714. }
  715. if(target.GetType() == cmTarget::SHARED_LIBRARY
  716. || target.GetType() == cmTarget::MODULE_LIBRARY)
  717. {
  718. fout << "," << libName << "_EXPORTS";
  719. }
  720. this->OutputDefineFlags(fout);
  721. fout << "\"\n";
  722. if(m_Makefile->IsOn("CMAKE_CXX_USE_RTTI"))
  723. {
  724. fout << "\t\t\t\tRuntimeTypeInfo=\"TRUE\"\n";
  725. }
  726. fout << "\t\t\t\tAssemblerListingLocation=\"" << configName << "\"\n";
  727. fout << "\t\t\t\tObjectFile=\"" << configName << "\\\"\n";
  728. fout << "\t\t\t\tWarningLevel=\"" << m_Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL") << "\"\n";
  729. fout << "\t\t\t\tDetect64BitPortabilityProblems=\"TRUE\"\n"
  730. << "\t\t\t\tDebugInformationFormat=\"3\"";
  731. fout << "/>\n"; // end of <Tool Name=VCCLCompilerTool
  732. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCCustomBuildTool\"/>\n";
  733. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCMIDLTool\"/>\n";
  734. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  735. this->OutputTargetRules(fout, target, libName);
  736. fout << "/>\n";
  737. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"/>\n";
  738. this->OutputBuildTool(fout, configName, libName, target);
  739. fout << "\t\t</Configuration>\n";
  740. }
  741. void cmMSDotNETGenerator::OutputBuildTool(std::ostream& fout,
  742. const char* configName,
  743. const char *libName,
  744. const cmTarget &target)
  745. {
  746. switch(target.GetType())
  747. {
  748. case cmTarget::STATIC_LIBRARY:
  749. {
  750. std::string libpath = m_LibraryOutputPath +
  751. "$(OutDir)/" + libName + ".lib";
  752. fout << "\t\t\t<Tool\n"
  753. << "\t\t\t\tName=\"VCLibrarianTool\"\n"
  754. << "\t\t\t\t\tOutputFile=\""
  755. << this->ConvertToXMLOutputPath(libpath.c_str()) << ".\"/>\n";
  756. break;
  757. }
  758. case cmTarget::SHARED_LIBRARY:
  759. case cmTarget::MODULE_LIBRARY:
  760. fout << "\t\t\t<Tool\n"
  761. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  762. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386\"\n"
  763. << "\t\t\t\tAdditionalDependencies=\" odbc32.lib odbccp32.lib ";
  764. this->OutputLibraries(fout, configName, libName, target);
  765. fout << "\"\n";
  766. fout << "\t\t\t\tOutputFile=\""
  767. << m_ExecutableOutputPath << configName << "/"
  768. << libName << ".dll\"\n";
  769. fout << "\t\t\t\tLinkIncremental=\"1\"\n";
  770. fout << "\t\t\t\tSuppressStartupBanner=\"TRUE\"\n";
  771. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  772. this->OutputLibraryDirectories(fout, configName, libName, target);
  773. fout << "\"\n";
  774. this->OutputModuleDefinitionFile(fout, target);
  775. fout << "\t\t\t\tProgramDatabaseFile=\"" << m_LibraryOutputPath
  776. << "$(OutDir)\\" << libName << ".pdb\"\n";
  777. if(strcmp(configName, "Debug") == 0)
  778. {
  779. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  780. }
  781. fout << "\t\t\t\tStackReserveSize=\""
  782. << m_Makefile->GetDefinition("CMAKE_CXX_STACK_SIZE") << "\"\n";
  783. fout << "\t\t\t\tImportLibrary=\""
  784. << m_ExecutableOutputPath << configName << "/"
  785. << libName << ".lib\"/>\n";
  786. break;
  787. case cmTarget::EXECUTABLE:
  788. case cmTarget::WIN32_EXECUTABLE:
  789. fout << "\t\t\t<Tool\n"
  790. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  791. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386\"\n"
  792. << "\t\t\t\tAdditionalDependencies=\" odbc32.lib odbccp32.lib ";
  793. this->OutputLibraries(fout, configName, libName, target);
  794. fout << "\"\n";
  795. fout << "\t\t\t\tOutputFile=\""
  796. << m_ExecutableOutputPath << configName << "/" << libName << ".exe\"\n";
  797. fout << "\t\t\t\tLinkIncremental=\"1\"\n";
  798. fout << "\t\t\t\tSuppressStartupBanner=\"TRUE\"\n";
  799. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  800. this->OutputLibraryDirectories(fout, configName, libName, target);
  801. fout << "\"\n";
  802. fout << "\t\t\t\tProgramDatabaseFile=\"" << m_LibraryOutputPath
  803. << "$(OutDir)\\" << libName << ".pdb\"\n";
  804. if(strcmp(configName, "Debug") == 0)
  805. {
  806. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  807. }
  808. if( target.GetType() == cmTarget::EXECUTABLE)
  809. {
  810. fout << "\t\t\t\tSubSystem=\"1\"\n";
  811. }
  812. else
  813. {
  814. fout << "\t\t\t\tSubSystem=\"2\"\n";
  815. }
  816. fout << "\t\t\t\tStackReserveSize=\""
  817. << m_Makefile->GetDefinition("CMAKE_CXX_STACK_SIZE") << "\"/>\n";
  818. break;
  819. case cmTarget::UTILITY:
  820. break;
  821. }
  822. }
  823. void cmMSDotNETGenerator::OutputModuleDefinitionFile(std::ostream& fout,
  824. const cmTarget &target)
  825. {
  826. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  827. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  828. i != classes.end(); i++)
  829. {
  830. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  831. {
  832. fout << "\t\t\t\tModuleDefinitionFile=\""
  833. << this->ConvertToXMLOutputPath((*i)->GetFullPath().c_str())
  834. << "\"\n";
  835. return;
  836. }
  837. }
  838. }
  839. void cmMSDotNETGenerator::OutputLibraryDirectories(std::ostream& fout,
  840. const char*,
  841. const char*,
  842. const cmTarget &)
  843. {
  844. bool hasone = false;
  845. if(m_LibraryOutputPath.size())
  846. {
  847. hasone = true;
  848. fout << m_LibraryOutputPath << "$(INTDIR)," << m_LibraryOutputPath;
  849. }
  850. if(m_ExecutableOutputPath.size())
  851. {
  852. hasone = true;
  853. fout << m_ExecutableOutputPath << "$(INTDIR)," << m_ExecutableOutputPath;
  854. }
  855. std::set<std::string> pathEmitted;
  856. std::vector<std::string>::iterator i;
  857. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  858. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  859. {
  860. std::string lpath = *i;
  861. if(lpath[lpath.size()-1] != '/')
  862. {
  863. lpath += "/";
  864. }
  865. if(pathEmitted.insert(lpath).second)
  866. {
  867. if(hasone)
  868. {
  869. fout << ",";
  870. }
  871. std::string lpathi = lpath + "$(INTDIR)";
  872. fout << this->ConvertToXMLOutputPath(lpathi.c_str()) << "," << lpath;
  873. hasone = true;
  874. }
  875. }
  876. }
  877. void cmMSDotNETGenerator::OutputLibraries(std::ostream& fout,
  878. const char* configName,
  879. const char* libName,
  880. const cmTarget &target)
  881. {
  882. const cmTarget::LinkLibraries& libs = target.GetLinkLibraries();
  883. cmTarget::LinkLibraries::const_iterator j;
  884. for(j = libs.begin(); j != libs.end(); ++j)
  885. {
  886. if(j->first != libName)
  887. {
  888. std::string lib = j->first;
  889. if(j->first.find(".lib") == std::string::npos)
  890. {
  891. lib += ".lib";
  892. }
  893. lib = this->ConvertToXMLOutputPath(lib.c_str());
  894. if (j->second == cmTarget::GENERAL
  895. || (j->second == cmTarget::DEBUG && strcmp(configName, "DEBUG") == 0)
  896. || (j->second == cmTarget::OPTIMIZED && strcmp(configName, "DEBUG") != 0))
  897. {
  898. fout << lib << " ";
  899. }
  900. }
  901. }
  902. }
  903. void cmMSDotNETGenerator::OutputDefineFlags(std::ostream& fout)
  904. {
  905. std::string defs = m_Makefile->GetDefineFlags();
  906. std::string::size_type pos = defs.find("-D");
  907. bool done = pos == std::string::npos;
  908. if(!done)
  909. {
  910. fout << ",";
  911. }
  912. while(!done)
  913. {
  914. std::string::size_type nextpos = defs.find("-D", pos+2);
  915. std::string define;
  916. if(nextpos != std::string::npos)
  917. {
  918. define = defs.substr(pos+2, nextpos - pos -3);
  919. }
  920. else
  921. {
  922. define = defs.substr(pos+2);
  923. done = true;
  924. }
  925. fout << define << ",";
  926. if(!done)
  927. {
  928. pos = defs.find("-D", nextpos);
  929. }
  930. }
  931. }
  932. void cmMSDotNETGenerator::WriteVCProjFile(std::ostream& fout,
  933. const char *libName,
  934. cmTarget &target)
  935. {
  936. // We may be modifying the source groups temporarily, so make a copy.
  937. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  938. // get the classes from the source lists then add them to the groups
  939. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  940. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  941. i != classes.end(); i++)
  942. {
  943. // Add the file to the list of sources.
  944. std::string source = (*i)->GetFullPath();
  945. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  946. {
  947. m_ModuleDefinitionFile = (*i)->GetFullPath();
  948. }
  949. cmSourceGroup& sourceGroup = m_Makefile->FindSourceGroup(source.c_str(),
  950. sourceGroups);
  951. sourceGroup.AddSource(source.c_str(), *i);
  952. }
  953. // add any custom rules to the source groups
  954. for (std::vector<cmCustomCommand>::const_iterator cr =
  955. target.GetCustomCommands().begin();
  956. cr != target.GetCustomCommands().end(); ++cr)
  957. {
  958. cmSourceGroup& sourceGroup =
  959. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  960. sourceGroups);
  961. cmCustomCommand cc(*cr);
  962. cc.ExpandVariables(*m_Makefile);
  963. sourceGroup.AddCustomCommand(cc);
  964. }
  965. // open the project
  966. this->WriteProjectStart(fout, libName, target, sourceGroups);
  967. // write the configuration information
  968. this->WriteConfigurations(fout, libName, target);
  969. fout << "\t<Files>\n";
  970. // Find the group in which the CMakeLists.txt source belongs, and add
  971. // the rule to generate this VCProj file.
  972. for(std::vector<cmSourceGroup>::reverse_iterator sg = sourceGroups.rbegin();
  973. sg != sourceGroups.rend(); ++sg)
  974. {
  975. if(sg->Matches("CMakeLists.txt"))
  976. {
  977. this->AddVCProjBuildRule(*sg);
  978. break;
  979. }
  980. }
  981. // Loop through every source group.
  982. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  983. sg != sourceGroups.end(); ++sg)
  984. {
  985. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  986. // If the group is empty, don't write it at all.
  987. if(buildRules.empty())
  988. { continue; }
  989. // If the group has a name, write the header.
  990. std::string name = sg->GetName();
  991. if(name != "")
  992. {
  993. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  994. }
  995. // Loop through each build rule in the source group.
  996. for(cmSourceGroup::BuildRules::const_iterator cc =
  997. buildRules.begin(); cc != buildRules.end(); ++ cc)
  998. {
  999. std::string source = cc->first;
  1000. const cmSourceGroup::Commands& commands = cc->second.m_Commands;
  1001. const char* compileFlags = 0;
  1002. if(cc->second.m_SourceFile)
  1003. {
  1004. compileFlags = cc->second.m_SourceFile->GetCompileFlags();
  1005. }
  1006. if (source != libName || target.GetType() == cmTarget::UTILITY)
  1007. {
  1008. fout << "\t\t\t<File\n";
  1009. std::string d = this->ConvertToXMLOutputPath(source.c_str());
  1010. // Tell MS-Dev what the source is. If the compiler knows how to
  1011. // build it, then it will.
  1012. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  1013. if (!commands.empty())
  1014. {
  1015. cmSourceGroup::CommandFiles totalCommand;
  1016. std::string totalCommandStr;
  1017. totalCommandStr = this->CombineCommands(commands, totalCommand,
  1018. source.c_str());
  1019. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  1020. totalCommand.m_Depends,
  1021. totalCommand.m_Outputs, compileFlags);
  1022. }
  1023. else if(compileFlags)
  1024. {
  1025. for(std::vector<std::string>::iterator i
  1026. = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  1027. {
  1028. fout << "\t\t\t\t<FileConfiguration\n"
  1029. << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n"
  1030. << "\t\t\t\t\t<Tool\n"
  1031. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  1032. << "\t\t\t\t\tAdditionalOptions=\""
  1033. << compileFlags << "\"/>\n"
  1034. << "\t\t\t\t</FileConfiguration>\n";
  1035. }
  1036. }
  1037. fout << "\t\t\t</File>\n";
  1038. }
  1039. }
  1040. // If the group has a name, write the footer.
  1041. if(name != "")
  1042. {
  1043. this->WriteVCProjEndGroup(fout);
  1044. }
  1045. }
  1046. fout << "\t</Files>\n";
  1047. // Write the VCProj file's footer.
  1048. this->WriteVCProjFooter(fout);
  1049. }
  1050. void cmMSDotNETGenerator::WriteCustomRule(std::ostream& fout,
  1051. const char* source,
  1052. const char* command,
  1053. const std::set<std::string>& depends,
  1054. const std::set<std::string>& outputs,
  1055. const char* compileFlags)
  1056. {
  1057. std::string cmd = command;
  1058. cmSystemTools::ReplaceString(cmd, "\"", "&quot;");
  1059. std::vector<std::string>::iterator i;
  1060. for(i = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  1061. {
  1062. fout << "\t\t\t\t<FileConfiguration\n";
  1063. fout << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n";
  1064. if(compileFlags)
  1065. {
  1066. fout << "\t\t\t\t\t<Tool\n"
  1067. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  1068. << "\t\t\t\t\tAdditionalOptions=\""
  1069. << compileFlags << "\"/>\n";
  1070. }
  1071. fout << "\t\t\t\t\t<Tool\n"
  1072. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  1073. << "\t\t\t\t\tCommandLine=\"" << cmd << "\n\"\n"
  1074. << "\t\t\t\t\tAdditionalDependencies=\"";
  1075. // Write out the dependencies for the rule.
  1076. std::string temp;
  1077. for(std::set<std::string>::const_iterator d = depends.begin();
  1078. d != depends.end(); ++d)
  1079. {
  1080. fout << this->ConvertToXMLOutputPath(d->c_str())
  1081. << ";";
  1082. }
  1083. fout << "\"\n";
  1084. fout << "\t\t\t\t\tOutputs=\"";
  1085. if(outputs.size() == 0)
  1086. {
  1087. fout << source << "_force";
  1088. }
  1089. bool first = true;
  1090. // Write a rule for every output generated by this command.
  1091. for(std::set<std::string>::const_iterator output = outputs.begin();
  1092. output != outputs.end(); ++output)
  1093. {
  1094. if(!first)
  1095. {
  1096. fout << ";";
  1097. }
  1098. else
  1099. {
  1100. first = false;
  1101. }
  1102. fout << output->c_str();
  1103. }
  1104. fout << "\"/>\n";
  1105. fout << "\t\t\t\t</FileConfiguration>\n";
  1106. }
  1107. }
  1108. void cmMSDotNETGenerator::WriteVCProjBeginGroup(std::ostream& fout,
  1109. const char* group,
  1110. const char* )
  1111. {
  1112. fout << "\t\t<Filter\n"
  1113. << "\t\t\tName=\"" << group << "\"\n"
  1114. << "\t\t\tFilter=\"\">\n";
  1115. }
  1116. void cmMSDotNETGenerator::WriteVCProjEndGroup(std::ostream& fout)
  1117. {
  1118. fout << "\t\t</Filter>\n";
  1119. }
  1120. std::string
  1121. cmMSDotNETGenerator::CombineCommands(const cmSourceGroup::Commands &commands,
  1122. cmSourceGroup::CommandFiles &totalCommand,
  1123. const char *source)
  1124. {
  1125. // Loop through every custom command generating code from the
  1126. // current source.
  1127. // build up the depends and outputs and commands
  1128. std::string totalCommandStr = "";
  1129. std::string temp;
  1130. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  1131. c != commands.end(); ++c)
  1132. {
  1133. temp=
  1134. cmSystemTools::ConvertToOutputPath(c->second.m_Command.c_str());
  1135. totalCommandStr += temp;
  1136. totalCommandStr += " ";
  1137. totalCommandStr += c->second.m_Arguments;
  1138. totalCommand.Merge(c->second);
  1139. }
  1140. // Create a dummy file with the name of the source if it does
  1141. // not exist
  1142. if(totalCommand.m_Outputs.empty())
  1143. {
  1144. std::string dummyFile = m_Makefile->GetStartOutputDirectory();
  1145. dummyFile += "/";
  1146. dummyFile += source;
  1147. if(!cmSystemTools::FileExists(dummyFile.c_str()))
  1148. {
  1149. std::ofstream fout(dummyFile.c_str());
  1150. fout << "Dummy file created by cmake as unused source for utility command.\n";
  1151. }
  1152. }
  1153. return totalCommandStr;
  1154. }
  1155. // look for custom rules on a target and collect them together
  1156. void cmMSDotNETGenerator::OutputTargetRules(std::ostream& fout,
  1157. const cmTarget &target,
  1158. const char *libName)
  1159. {
  1160. if (target.GetType() >= cmTarget::UTILITY)
  1161. {
  1162. return;
  1163. }
  1164. // Find the group in which the lix exe custom rules belong
  1165. bool init = false;
  1166. for (std::vector<cmCustomCommand>::const_iterator cr =
  1167. target.GetCustomCommands().begin();
  1168. cr != target.GetCustomCommands().end(); ++cr)
  1169. {
  1170. cmCustomCommand cc(*cr);
  1171. cc.ExpandVariables(*m_Makefile);
  1172. if (cc.GetSourceName() == libName)
  1173. {
  1174. if(!init)
  1175. {
  1176. fout << "\nCommandLine=\"";
  1177. init = true;
  1178. }
  1179. fout << cc.GetCommand() << " " << cc.GetArguments() << "\n";
  1180. }
  1181. }
  1182. if (init)
  1183. {
  1184. fout << "\"";
  1185. }
  1186. }
  1187. void cmMSDotNETGenerator::WriteProjectStart(std::ostream& fout, const char *libName,
  1188. const cmTarget &,
  1189. std::vector<cmSourceGroup> &)
  1190. {
  1191. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  1192. << "<VisualStudioProject\n"
  1193. << "\tProjectType=\"Visual C++\"\n"
  1194. << "\tVersion=\"7.00\"\n"
  1195. << "\tName=\"" << libName << "\"\n"
  1196. << "\tSccProjectName=\"\"\n"
  1197. << "\tSccLocalPath=\"\"\n"
  1198. << "\tKeyword=\"Win32Proj\">\n"
  1199. << "\t<Platforms>\n"
  1200. << "\t\t<Platform\n\t\t\tName=\"Win32\"/>\n"
  1201. << "\t</Platforms>\n";
  1202. }
  1203. void cmMSDotNETGenerator::WriteVCProjFooter(std::ostream& fout)
  1204. {
  1205. fout << "\t<Globals>\n"
  1206. << "\t</Globals>\n"
  1207. << "</VisualStudioProject>\n";
  1208. }
  1209. std::string cmMSDotNETGenerator::ConvertToXMLOutputPath(const char* path)
  1210. {
  1211. std::string ret = cmSystemTools::ConvertToOutputPath(path);
  1212. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1213. return ret;
  1214. }