cmMSDotNETGenerator.cxx 42 KB

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