cmMSDotNETGenerator.cxx 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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& target
  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* dir,
  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& fout,
  411. const char* name,
  412. const char* location,
  413. const std::vector<std::string>& dependencies)
  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") << "\"\n";
  663. fout << "\t\t\t\tAdditionalIncludeDirectories=\"";
  664. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  665. std::vector<std::string>::iterator i = includes.begin();
  666. for(;i != includes.end(); ++i)
  667. {
  668. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  669. fout << ipath << ";";
  670. }
  671. fout << "\"\n";
  672. // Optimization = 0 None Debug /O0
  673. // Optimization = 1 MinSize /O1
  674. // Optimization = 2 MaxSpeed /O2
  675. // Optimization = 3 Max Optimization /O3
  676. // RuntimeLibrary = 0 /MT multithread
  677. // RuntimeLibrary = 1 /MTd multithread debug
  678. // RuntimeLibrary = 2 /MD multithread dll
  679. // RuntimeLibrary = 3 /MDd multithread dll debug
  680. // RuntimeLibrary = 4 /ML single thread
  681. // RuntimeLibrary = 5 /MLd single thread debug
  682. // InlineFunctionExpansion = 0 none
  683. // InlineFunctionExpansion = 1 when inline keyword
  684. // InlineFunctionExpansion = 2 any time you can
  685. if(strcmp(configName, "Debug") == 0)
  686. {
  687. fout << "\t\t\t\tOptimization=\"0\"\n"
  688. << "\t\t\t\tRuntimeLibrary=\"3\"\n"
  689. << "\t\t\t\tInlineFunctionExpansion=\"0\"\n"
  690. << "\t\t\t\tPreprocessorDefinitions=\"WIN32,_DEBUG,_WINDOWS";
  691. }
  692. else if(strcmp(configName, "Release") == 0)
  693. {
  694. fout << "\t\t\t\tOptimization=\"2\"\n"
  695. << "\t\t\t\tRuntimeLibrary=\"2\"\n"
  696. << "\t\t\t\tInlineFunctionExpansion=\"1\"\n"
  697. << "\t\t\t\tPreprocessorDefinitions=\"WIN32,NDEBUG,_WINDOWS";
  698. }
  699. else if(strcmp(configName, "MinSizeRel") == 0)
  700. {
  701. fout << "\t\t\t\tOptimization=\"1\"\n"
  702. << "\t\t\t\tRuntimeLibrary=\"2\"\n"
  703. << "\t\t\t\tInlineFunctionExpansion=\"1\"\n"
  704. << "\t\t\t\tPreprocessorDefinitions=\"WIN32,NDEBUG,_WINDOWS";
  705. }
  706. else if(strcmp(configName, "RelWithDebInfo") == 0)
  707. {
  708. fout << "\t\t\t\tOptimization=\"2\"\n"
  709. << "\t\t\t\tRuntimeLibrary=\"2\"\n"
  710. << "\t\t\t\tInlineFunctionExpansion=\"1\"\n"
  711. << "\t\t\t\tPreprocessorDefinitions=\"WIN32,NDEBUG,_WINDOWS";
  712. }
  713. if(target.GetType() == cmTarget::SHARED_LIBRARY
  714. || target.GetType() == cmTarget::MODULE_LIBRARY)
  715. {
  716. fout << "," << libName << "_EXPORTS";
  717. }
  718. this->OutputDefineFlags(fout);
  719. fout << "\"\n";
  720. if(m_Makefile->IsOn("CMAKE_CXX_USE_RTTI"))
  721. {
  722. fout << "\t\t\t\tRuntimeTypeInfo=\"TRUE\"\n";
  723. }
  724. fout << "\t\t\t\tAssemblerListingLocation=\"" << configName << "\"\n";
  725. fout << "\t\t\t\tObjectFile=\"" << configName << "\\\"\n";
  726. fout << "\t\t\t\tWarningLevel=\"" << m_Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL") << "\"\n";
  727. fout << "\t\t\t\tDetect64BitPortabilityProblems=\"TRUE\"\n"
  728. << "\t\t\t\tDebugInformationFormat=\"3\"";
  729. fout << "/>\n"; // end of <Tool Name=VCCLCompilerTool
  730. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCCustomBuildTool\"/>\n";
  731. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCMIDLTool\"/>\n";
  732. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  733. this->OutputTargetRules(fout, target, libName);
  734. fout << "/>\n";
  735. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"/>\n";
  736. this->OutputBuildTool(fout, configName, libName, target);
  737. fout << "\t\t</Configuration>\n";
  738. }
  739. void cmMSDotNETGenerator::OutputBuildTool(std::ostream& fout,
  740. const char* configName,
  741. const char *libName,
  742. const cmTarget &target)
  743. {
  744. switch(target.GetType())
  745. {
  746. case cmTarget::STATIC_LIBRARY:
  747. {
  748. std::string libpath = m_LibraryOutputPath +
  749. "$(OutDir)/" + libName + ".lib";
  750. fout << "\t\t\t<Tool\n"
  751. << "\t\t\t\tName=\"VCLibrarianTool\"\n"
  752. << "\t\t\t\t\tOutputFile=\""
  753. << this->ConvertToXMLOutputPath(libpath.c_str()) << ".\"/>\n";
  754. break;
  755. }
  756. case cmTarget::SHARED_LIBRARY:
  757. case cmTarget::MODULE_LIBRARY:
  758. fout << "\t\t\t<Tool\n"
  759. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  760. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386\"\n"
  761. << "\t\t\t\tAdditionalDependencies=\" odbc32.lib odbccp32.lib ";
  762. this->OutputLibraries(fout, configName, libName, target);
  763. fout << "\"\n";
  764. fout << "\t\t\t\tOutputFile=\""
  765. << m_ExecutableOutputPath << configName << "/"
  766. << libName << ".dll\"\n";
  767. fout << "\t\t\t\tLinkIncremental=\"1\"\n";
  768. fout << "\t\t\t\tSuppressStartupBanner=\"TRUE\"\n";
  769. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  770. this->OutputLibraryDirectories(fout, configName, libName, target);
  771. fout << "\"\n";
  772. this->OutputModuleDefinitionFile(fout, target);
  773. fout << "\t\t\t\tProgramDatabaseFile=\"" << m_LibraryOutputPath
  774. << "$(OutDir)\\" << libName << ".pdb\"\n";
  775. if(strcmp(configName, "Debug") == 0)
  776. {
  777. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  778. }
  779. fout << "\t\t\t\tStackReserveSize=\""
  780. << m_Makefile->GetDefinition("CMAKE_CXX_STACK_SIZE") << "\"\n";
  781. fout << "\t\t\t\tImportLibrary=\""
  782. << m_ExecutableOutputPath << configName << "/"
  783. << libName << ".lib\"/>\n";
  784. break;
  785. case cmTarget::EXECUTABLE:
  786. case cmTarget::WIN32_EXECUTABLE:
  787. fout << "\t\t\t<Tool\n"
  788. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  789. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386\"\n"
  790. << "\t\t\t\tAdditionalDependencies=\" odbc32.lib odbccp32.lib ";
  791. this->OutputLibraries(fout, configName, libName, target);
  792. fout << "\"\n";
  793. fout << "\t\t\t\tOutputFile=\""
  794. << m_ExecutableOutputPath << configName << "/" << libName << ".exe\"\n";
  795. fout << "\t\t\t\tLinkIncremental=\"1\"\n";
  796. fout << "\t\t\t\tSuppressStartupBanner=\"TRUE\"\n";
  797. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  798. this->OutputLibraryDirectories(fout, configName, libName, target);
  799. fout << "\"\n";
  800. fout << "\t\t\t\tProgramDatabaseFile=\"" << m_LibraryOutputPath
  801. << "$(OutDir)\\" << libName << ".pdb\"\n";
  802. if(strcmp(configName, "Debug") == 0)
  803. {
  804. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  805. }
  806. if( target.GetType() == cmTarget::EXECUTABLE)
  807. {
  808. fout << "\t\t\t\tSubSystem=\"1\"\n";
  809. }
  810. else
  811. {
  812. fout << "\t\t\t\tSubSystem=\"2\"\n";
  813. }
  814. fout << "\t\t\t\tStackReserveSize=\""
  815. << m_Makefile->GetDefinition("CMAKE_CXX_STACK_SIZE") << "\"/>\n";
  816. break;
  817. case cmTarget::UTILITY:
  818. break;
  819. }
  820. }
  821. void cmMSDotNETGenerator::OutputModuleDefinitionFile(std::ostream& fout,
  822. const cmTarget &target)
  823. {
  824. std::vector<cmSourceFile> const& classes = target.GetSourceFiles();
  825. for(std::vector<cmSourceFile>::const_iterator i = classes.begin();
  826. i != classes.end(); i++)
  827. {
  828. if(cmSystemTools::UpperCase(i->GetSourceExtension()) == "DEF")
  829. {
  830. fout << "\t\t\t\tModuleDefinitionFile=\""
  831. << this->ConvertToXMLOutputPath(i->GetFullPath().c_str())
  832. << "\"\n";
  833. return;
  834. }
  835. }
  836. }
  837. void cmMSDotNETGenerator::OutputLibraryDirectories(std::ostream& fout,
  838. const char* configName,
  839. const char* libName,
  840. const cmTarget &target)
  841. {
  842. bool hasone = false;
  843. if(m_LibraryOutputPath.size())
  844. {
  845. hasone = true;
  846. fout << m_LibraryOutputPath << "$(INTDIR)," << m_LibraryOutputPath;
  847. }
  848. if(m_ExecutableOutputPath.size())
  849. {
  850. hasone = true;
  851. fout << m_ExecutableOutputPath << "$(INTDIR)," << m_ExecutableOutputPath;
  852. }
  853. std::set<std::string> pathEmitted;
  854. std::vector<std::string>::iterator i;
  855. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  856. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  857. {
  858. std::string lpath = *i;
  859. if(lpath[lpath.size()-1] != '/')
  860. {
  861. lpath += "/";
  862. }
  863. if(pathEmitted.insert(lpath).second)
  864. {
  865. if(hasone)
  866. {
  867. fout << ",";
  868. }
  869. std::string lpathi = lpath + "$(INTDIR)";
  870. fout << this->ConvertToXMLOutputPath(lpathi.c_str()) << "," << lpath;
  871. hasone = true;
  872. }
  873. }
  874. }
  875. void cmMSDotNETGenerator::OutputLibraries(std::ostream& fout,
  876. const char* configName,
  877. const char* libName,
  878. const cmTarget &target)
  879. {
  880. const cmTarget::LinkLibraries& libs = target.GetLinkLibraries();
  881. cmTarget::LinkLibraries::const_iterator j;
  882. for(j = libs.begin(); j != libs.end(); ++j)
  883. {
  884. if(j->first != libName)
  885. {
  886. std::string lib = j->first;
  887. if(j->first.find(".lib") == std::string::npos)
  888. {
  889. lib += ".lib";
  890. }
  891. lib = this->ConvertToXMLOutputPath(lib.c_str());
  892. if (j->second == cmTarget::GENERAL
  893. || (j->second == cmTarget::DEBUG && strcmp(configName, "DEBUG") == 0)
  894. || (j->second == cmTarget::OPTIMIZED && strcmp(configName, "DEBUG") != 0))
  895. {
  896. fout << lib << " ";
  897. }
  898. }
  899. }
  900. }
  901. void cmMSDotNETGenerator::OutputDefineFlags(std::ostream& fout)
  902. {
  903. std::string defs = m_Makefile->GetDefineFlags();
  904. std::string::size_type pos = defs.find("-D");
  905. bool done = pos == std::string::npos;
  906. if(!done)
  907. {
  908. fout << ",";
  909. }
  910. while(!done)
  911. {
  912. std::string::size_type nextpos = defs.find("-D", pos+2);
  913. std::string define;
  914. if(nextpos != std::string::npos)
  915. {
  916. define = defs.substr(pos+2, nextpos - pos -3);
  917. }
  918. else
  919. {
  920. define = defs.substr(pos+2);
  921. done = true;
  922. }
  923. fout << define << ",";
  924. if(!done)
  925. {
  926. pos = defs.find("-D", nextpos);
  927. }
  928. }
  929. }
  930. void cmMSDotNETGenerator::WriteVCProjFile(std::ostream& fout,
  931. const char *libName,
  932. cmTarget &target)
  933. {
  934. // We may be modifying the source groups temporarily, so make a copy.
  935. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  936. // get the classes from the source lists then add them to the groups
  937. std::vector<cmSourceFile> const& classes = target.GetSourceFiles();
  938. for(std::vector<cmSourceFile>::const_iterator i = classes.begin();
  939. i != classes.end(); i++)
  940. {
  941. // Add the file to the list of sources.
  942. std::string source = i->GetFullPath();
  943. if(cmSystemTools::UpperCase(i->GetSourceExtension()) == "DEF")
  944. {
  945. m_ModuleDefinitionFile = i->GetFullPath();
  946. }
  947. cmSourceGroup& sourceGroup = m_Makefile->FindSourceGroup(source.c_str(),
  948. sourceGroups);
  949. sourceGroup.AddSource(source.c_str(), &(*i));
  950. }
  951. // add any custom rules to the source groups
  952. for (std::vector<cmCustomCommand>::const_iterator cr =
  953. target.GetCustomCommands().begin();
  954. cr != target.GetCustomCommands().end(); ++cr)
  955. {
  956. cmSourceGroup& sourceGroup =
  957. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  958. sourceGroups);
  959. cmCustomCommand cc(*cr);
  960. cc.ExpandVariables(*m_Makefile);
  961. sourceGroup.AddCustomCommand(cc);
  962. }
  963. // open the project
  964. this->WriteProjectStart(fout, libName, target, sourceGroups);
  965. // write the configuration information
  966. this->WriteConfigurations(fout, libName, target);
  967. fout << "\t<Files>\n";
  968. // Find the group in which the CMakeLists.txt source belongs, and add
  969. // the rule to generate this VCProj file.
  970. for(std::vector<cmSourceGroup>::reverse_iterator sg = sourceGroups.rbegin();
  971. sg != sourceGroups.rend(); ++sg)
  972. {
  973. if(sg->Matches("CMakeLists.txt"))
  974. {
  975. this->AddVCProjBuildRule(*sg);
  976. break;
  977. }
  978. }
  979. // Loop through every source group.
  980. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  981. sg != sourceGroups.end(); ++sg)
  982. {
  983. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  984. // If the group is empty, don't write it at all.
  985. if(buildRules.empty())
  986. { continue; }
  987. // If the group has a name, write the header.
  988. std::string name = sg->GetName();
  989. if(name != "")
  990. {
  991. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  992. }
  993. // Loop through each build rule in the source group.
  994. for(cmSourceGroup::BuildRules::const_iterator cc =
  995. buildRules.begin(); cc != buildRules.end(); ++ cc)
  996. {
  997. std::string source = cc->first;
  998. const cmSourceGroup::Commands& commands = cc->second.m_Commands;
  999. const char* compileFlags = 0;
  1000. if(cc->second.m_SourceFile)
  1001. {
  1002. compileFlags = cc->second.m_SourceFile->GetCompileFlags();
  1003. }
  1004. if (source != libName || target.GetType() == cmTarget::UTILITY)
  1005. {
  1006. fout << "\t\t\t<File\n";
  1007. std::string d = this->ConvertToXMLOutputPath(source.c_str());
  1008. // Tell MS-Dev what the source is. If the compiler knows how to
  1009. // build it, then it will.
  1010. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  1011. if (!commands.empty())
  1012. {
  1013. cmSourceGroup::CommandFiles totalCommand;
  1014. std::string totalCommandStr;
  1015. totalCommandStr = this->CombineCommands(commands, totalCommand,
  1016. source.c_str());
  1017. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  1018. totalCommand.m_Depends,
  1019. totalCommand.m_Outputs, compileFlags);
  1020. }
  1021. else if(compileFlags)
  1022. {
  1023. for(std::vector<std::string>::iterator i
  1024. = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  1025. {
  1026. fout << "\t\t\t\t<FileConfiguration\n"
  1027. << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n"
  1028. << "\t\t\t\t\t<Tool\n"
  1029. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  1030. << "\t\t\t\t\tAdditionalOptions=\""
  1031. << compileFlags << "\"/>\n"
  1032. << "\t\t\t\t</FileConfiguration>\n";
  1033. }
  1034. }
  1035. fout << "\t\t\t</File>\n";
  1036. }
  1037. }
  1038. // If the group has a name, write the footer.
  1039. if(name != "")
  1040. {
  1041. this->WriteVCProjEndGroup(fout);
  1042. }
  1043. }
  1044. fout << "\t</Files>\n";
  1045. // Write the VCProj file's footer.
  1046. this->WriteVCProjFooter(fout);
  1047. }
  1048. void cmMSDotNETGenerator::WriteCustomRule(std::ostream& fout,
  1049. const char* source,
  1050. const char* command,
  1051. const std::set<std::string>& depends,
  1052. const std::set<std::string>& outputs,
  1053. const char* compileFlags)
  1054. {
  1055. std::string cmd = command;
  1056. cmSystemTools::ReplaceString(cmd, "\"", "&quot;");
  1057. std::vector<std::string>::iterator i;
  1058. for(i = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  1059. {
  1060. fout << "\t\t\t\t<FileConfiguration\n";
  1061. fout << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n";
  1062. if(compileFlags)
  1063. {
  1064. fout << "\t\t\t\t\t<Tool\n"
  1065. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  1066. << "\t\t\t\t\tAdditionalOptions=\""
  1067. << compileFlags << "\"/>\n";
  1068. }
  1069. fout << "\t\t\t\t\t<Tool\n"
  1070. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  1071. << "\t\t\t\t\tCommandLine=\"" << cmd << "\n\"\n"
  1072. << "\t\t\t\t\tAdditionalDependencies=\"";
  1073. // Write out the dependencies for the rule.
  1074. std::string temp;
  1075. for(std::set<std::string>::const_iterator d = depends.begin();
  1076. d != depends.end(); ++d)
  1077. {
  1078. fout << this->ConvertToXMLOutputPath(d->c_str())
  1079. << ";";
  1080. }
  1081. fout << "\"\n";
  1082. fout << "\t\t\t\t\tOutputs=\"";
  1083. if(outputs.size() == 0)
  1084. {
  1085. fout << source << "_force";
  1086. }
  1087. bool first = true;
  1088. // Write a rule for every output generated by this command.
  1089. for(std::set<std::string>::const_iterator output = outputs.begin();
  1090. output != outputs.end(); ++output)
  1091. {
  1092. if(!first)
  1093. {
  1094. fout << ";";
  1095. }
  1096. else
  1097. {
  1098. first = true;
  1099. }
  1100. fout << output->c_str();
  1101. }
  1102. fout << "\"/>\n";
  1103. fout << "\t\t\t\t</FileConfiguration>\n";
  1104. }
  1105. }
  1106. void cmMSDotNETGenerator::WriteVCProjBeginGroup(std::ostream& fout,
  1107. const char* group,
  1108. const char* filter)
  1109. {
  1110. fout << "\t\t<Filter\n"
  1111. << "\t\t\tName=\"" << group << "\"\n"
  1112. << "\t\t\tFilter=\"\">\n";
  1113. }
  1114. void cmMSDotNETGenerator::WriteVCProjEndGroup(std::ostream& fout)
  1115. {
  1116. fout << "\t\t</Filter>\n";
  1117. }
  1118. std::string
  1119. cmMSDotNETGenerator::CombineCommands(const cmSourceGroup::Commands &commands,
  1120. cmSourceGroup::CommandFiles &totalCommand,
  1121. const char *source)
  1122. {
  1123. // Loop through every custom command generating code from the
  1124. // current source.
  1125. // build up the depends and outputs and commands
  1126. std::string totalCommandStr = "";
  1127. std::string temp;
  1128. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  1129. c != commands.end(); ++c)
  1130. {
  1131. temp=
  1132. cmSystemTools::ConvertToOutputPath(c->second.m_Command.c_str());
  1133. totalCommandStr += temp;
  1134. totalCommandStr += " ";
  1135. totalCommandStr += c->second.m_Arguments;
  1136. totalCommand.Merge(c->second);
  1137. }
  1138. // Create a dummy file with the name of the source if it does
  1139. // not exist
  1140. if(totalCommand.m_Outputs.empty())
  1141. {
  1142. std::string dummyFile = m_Makefile->GetStartOutputDirectory();
  1143. dummyFile += "/";
  1144. dummyFile += source;
  1145. if(!cmSystemTools::FileExists(dummyFile.c_str()))
  1146. {
  1147. std::ofstream fout(dummyFile.c_str());
  1148. fout << "Dummy file created by cmake as unused source for utility command.\n";
  1149. }
  1150. }
  1151. return totalCommandStr;
  1152. }
  1153. // look for custom rules on a target and collect them together
  1154. void cmMSDotNETGenerator::OutputTargetRules(std::ostream& fout,
  1155. const cmTarget &target,
  1156. const char *libName)
  1157. {
  1158. if (target.GetType() >= cmTarget::UTILITY)
  1159. {
  1160. return;
  1161. }
  1162. // Find the group in which the lix exe custom rules belong
  1163. bool init = false;
  1164. for (std::vector<cmCustomCommand>::const_iterator cr =
  1165. target.GetCustomCommands().begin();
  1166. cr != target.GetCustomCommands().end(); ++cr)
  1167. {
  1168. cmCustomCommand cc(*cr);
  1169. cc.ExpandVariables(*m_Makefile);
  1170. if (cc.GetSourceName() == libName)
  1171. {
  1172. if(!init)
  1173. {
  1174. fout << "\nCommandLine=\"";
  1175. init = true;
  1176. }
  1177. fout << cc.GetCommand() << " " << cc.GetArguments() << "\n";
  1178. }
  1179. }
  1180. if (init)
  1181. {
  1182. fout << "\"";
  1183. }
  1184. }
  1185. void cmMSDotNETGenerator::WriteProjectStart(std::ostream& fout, const char *libName,
  1186. const cmTarget &target,
  1187. std::vector<cmSourceGroup> &)
  1188. {
  1189. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  1190. << "<VisualStudioProject\n"
  1191. << "\tProjectType=\"Visual C++\"\n"
  1192. << "\tVersion=\"7.00\"\n"
  1193. << "\tName=\"" << libName << "\"\n"
  1194. << "\tSccProjectName=\"\"\n"
  1195. << "\tSccLocalPath=\"\"\n"
  1196. << "\tKeyword=\"Win32Proj\">\n"
  1197. << "\t<Platforms>\n"
  1198. << "\t\t<Platform\n\t\t\tName=\"Win32\"/>\n"
  1199. << "\t</Platforms>\n";
  1200. }
  1201. void cmMSDotNETGenerator::WriteVCProjFooter(std::ostream& fout)
  1202. {
  1203. fout << "\t<Globals>\n"
  1204. << "\t</Globals>\n"
  1205. << "</VisualStudioProject>\n";
  1206. }
  1207. std::string cmMSDotNETGenerator::ConvertToXMLOutputPath(const char* path)
  1208. {
  1209. std::string ret = cmSystemTools::ConvertToOutputPath(path);
  1210. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1211. return ret;
  1212. }