cmMSDotNETGenerator.cxx 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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. m_Configurations.push_back("Debug");
  24. m_Configurations.push_back("Release");
  25. m_Configurations.push_back("MinSizeRel");
  26. m_Configurations.push_back("RelWithDebInfo");
  27. // default to building a sln project file
  28. BuildProjOn();
  29. }
  30. void cmMSDotNETGenerator::GenerateMakefile()
  31. {
  32. if(m_BuildSLN)
  33. {
  34. this->OutputSLNFile();
  35. }
  36. else
  37. {
  38. this->OutputVCProjFile();
  39. }
  40. }
  41. cmMSDotNETGenerator::~cmMSDotNETGenerator()
  42. {
  43. }
  44. void cmMSDotNETGenerator::SetLocal(bool local)
  45. {
  46. m_BuildSLN = !local;
  47. }
  48. void cmMSDotNETGenerator::ComputeSystemInfo()
  49. {
  50. // now load the settings
  51. if(!m_Makefile->GetDefinition("CMAKE_ROOT"))
  52. {
  53. cmSystemTools::Error(
  54. "CMAKE_ROOT has not been defined, bad GUI or driver program");
  55. return;
  56. }
  57. std::string fpath =
  58. m_Makefile->GetDefinition("CMAKE_ROOT");
  59. fpath += "/Templates/CMakeWindowsSystemConfig.cmake";
  60. m_Makefile->ReadListFile(NULL,fpath.c_str());
  61. }
  62. // output the SLN file
  63. void cmMSDotNETGenerator::OutputSLNFile()
  64. {
  65. // if this is an out of source build, create the output directory
  66. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  67. m_Makefile->GetHomeDirectory()) != 0)
  68. {
  69. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  70. {
  71. cmSystemTools::Error("Error creating output directory for SLN file",
  72. m_Makefile->GetStartOutputDirectory());
  73. }
  74. }
  75. // create the dsw file name
  76. std::string fname;
  77. fname = m_Makefile->GetStartOutputDirectory();
  78. fname += "/";
  79. if(strlen(m_Makefile->GetProjectName()) == 0)
  80. {
  81. m_Makefile->SetProjectName("Project");
  82. }
  83. fname += m_Makefile->GetProjectName();
  84. fname += ".sln";
  85. std::ofstream fout(fname.c_str());
  86. if(!fout)
  87. {
  88. cmSystemTools::Error("Error can not open SLN file for write: "
  89. ,fname.c_str());
  90. return;
  91. }
  92. this->WriteSLNFile(fout);
  93. }
  94. // Write a SLN file to the stream
  95. void cmMSDotNETGenerator::WriteSLNFile(std::ostream& fout)
  96. {
  97. // Write out the header for a SLN file
  98. this->WriteSLNHeader(fout);
  99. // Create a list of cmMakefile created from all the
  100. // CMakeLists.txt files that are in sub directories of
  101. // this one.
  102. std::vector<cmMakefile*> allListFiles;
  103. // add this makefile to the list
  104. allListFiles.push_back(m_Makefile);
  105. // add a special target that depends on ALL projects for easy build
  106. // of Debug only
  107. m_Makefile->AddUtilityCommand("ALL_BUILD", "echo", "\"Build all projects\"",
  108. false);
  109. m_Makefile->FindSubDirectoryCMakeListsFiles(allListFiles);
  110. // For each cmMakefile, create a VCProj for it, and
  111. // add it to this SLN file
  112. std::vector<cmMakefile*>::iterator k;
  113. for(k = allListFiles.begin();
  114. k != allListFiles.end(); ++k)
  115. {
  116. cmMakefile* mf = *k;
  117. cmMSDotNETGenerator* pg = 0;
  118. // if not this makefile, then create a new generator
  119. if(m_Makefile != mf)
  120. {
  121. // Create an MS generator with SLN off, so it only creates dsp files
  122. pg = new cmMSDotNETGenerator;
  123. }
  124. else
  125. {
  126. pg = static_cast<cmMSDotNETGenerator*>(m_Makefile->GetMakefileGenerator());
  127. }
  128. // make sure the generator is building dsp files
  129. pg->BuildSLNOff();
  130. mf->SetMakefileGenerator(pg);
  131. mf->GenerateMakefile();
  132. // Get the source directory from the makefile
  133. std::string dir = mf->GetStartDirectory();
  134. // Get the home directory with the trailing slash
  135. std::string homedir = m_Makefile->GetHomeDirectory();
  136. homedir += "/";
  137. // remove the home directory and / from the source directory
  138. // this gives a relative path
  139. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  140. // Get the list of create dsp files names from the cmVCProjWriter, more
  141. // than one dsp could have been created per input CMakeLists.txt file
  142. // for each target
  143. std::vector<std::string> dspnames =
  144. pg->GetCreatedProjectNames();
  145. cmTargets &tgts = pg->GetMakefile()->GetTargets();
  146. cmTargets::iterator l = tgts.begin();
  147. for(std::vector<std::string>::iterator si = dspnames.begin();
  148. l != tgts.end(); ++l)
  149. {
  150. // special handling for the current makefile
  151. if(mf == m_Makefile)
  152. {
  153. dir = "."; // no subdirectory for project generated
  154. // if this is the special ALL_BUILD utility, then
  155. // make it depend on every other non UTILITY project.
  156. // This is done by adding the names to the GetUtilities
  157. // vector on the makefile
  158. if(l->first == "ALL_BUILD")
  159. {
  160. for(std::vector<cmMakefile*>::iterator a = allListFiles.begin();
  161. a != allListFiles.end(); ++a)
  162. {
  163. const cmTargets &atgts = (*a)->GetTargets();
  164. for(cmTargets::const_iterator al = atgts.begin();
  165. al != atgts.end(); ++al)
  166. {
  167. if (al->second.IsInAll())
  168. {
  169. if (al->second.GetType() == cmTarget::UTILITY)
  170. {
  171. l->second.AddUtility(al->first.c_str());
  172. }
  173. else
  174. {
  175. l->second.GetLinkLibraries().push_back(
  176. cmTarget::LinkLibraries::value_type(al->first,
  177. cmTarget::GENERAL));
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. // Write the project into the SLN file
  185. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  186. {
  187. cmCustomCommand cc = l->second.GetCustomCommands()[0];
  188. // dodgy use of the cmCustomCommand's members to store the
  189. // arguments from the INCLUDE_EXTERNAL_MSPROJECT command
  190. std::vector<std::string> stuff = cc.GetDepends();
  191. std::vector<std::string> depends = cc.GetOutputs();
  192. this->WriteExternalProject(fout, stuff[0].c_str(), stuff[1].c_str(), depends);
  193. ++si;
  194. }
  195. else if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  196. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  197. {
  198. this->WriteProject(fout, si->c_str(), dir.c_str(),
  199. pg,l->second);
  200. ++si;
  201. }
  202. }
  203. }
  204. fout << "Global\n"
  205. << "\tGlobalSection(SolutionConfiguration) = preSolution\n"
  206. << "\t\tConfigName.0 = Debug\n"
  207. << "\t\tConfigName.1 = MinSizeRel\n"
  208. << "\t\tConfigName.2 = Release\n"
  209. << "\t\tConfigName.3 = RelWithDebInfo\n"
  210. << "\tEndGlobalSection\n"
  211. << "\tGlobalSection(ProjectDependencies) = postSolution\n";
  212. // loop over again and compute the depends
  213. for(k = allListFiles.begin(); k != allListFiles.end(); ++k)
  214. {
  215. cmMakefile* mf = *k;
  216. cmMSDotNETGenerator* pg =
  217. static_cast<cmMSDotNETGenerator*>(mf->GetMakefileGenerator());
  218. // Get the list of create dsp files names from the cmVCProjWriter, more
  219. // than one dsp could have been created per input CMakeLists.txt file
  220. // for each target
  221. std::vector<std::string> dspnames =
  222. pg->GetCreatedProjectNames();
  223. cmTargets &tgts = pg->GetMakefile()->GetTargets();
  224. cmTargets::iterator l = tgts.begin();
  225. std::string dir = mf->GetStartDirectory();
  226. for(std::vector<std::string>::iterator si = dspnames.begin();
  227. l != tgts.end(); ++l)
  228. {
  229. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  230. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  231. {
  232. this->WriteProjectDepends(fout, si->c_str(), dir.c_str(),
  233. pg,l->second);
  234. ++si;
  235. }
  236. }
  237. }
  238. fout << "\tEndGlobalSection\n";
  239. fout << "\tGlobalSection(ProjectConfiguration) = postSolution\n";
  240. // loop over again and compute the depends
  241. for(k = allListFiles.begin(); k != allListFiles.end(); ++k)
  242. {
  243. cmMakefile* mf = *k;
  244. cmMSDotNETGenerator* pg =
  245. static_cast<cmMSDotNETGenerator*>(mf->GetMakefileGenerator());
  246. // Get the list of create dsp files names from the cmVCProjWriter, more
  247. // than one dsp could have been created per input CMakeLists.txt file
  248. // for each target
  249. std::vector<std::string> dspnames =
  250. pg->GetCreatedProjectNames();
  251. cmTargets &tgts = pg->GetMakefile()->GetTargets();
  252. cmTargets::iterator l = tgts.begin();
  253. std::string dir = mf->GetStartDirectory();
  254. for(std::vector<std::string>::iterator si = dspnames.begin();
  255. l != tgts.end(); ++l)
  256. {
  257. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  258. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  259. {
  260. this->WriteProjectConfigurations(fout, si->c_str());
  261. ++si;
  262. }
  263. }
  264. // delete the cmMakefile which also deletes the cmMSProjectGenerator
  265. if(mf != m_Makefile)
  266. {
  267. delete mf;
  268. }
  269. }
  270. fout << "\tEndGlobalSection\n";
  271. // Write the footer for the SLN file
  272. this->WriteSLNFooter(fout);
  273. }
  274. // Write a dsp file into the SLN file,
  275. // Note, that dependencies from executables to
  276. // the libraries it uses are also done here
  277. void cmMSDotNETGenerator::WriteProject(std::ostream& fout,
  278. const char* dspname,
  279. const char* dir,
  280. cmMSDotNETGenerator*,
  281. const cmTarget& target
  282. )
  283. {
  284. std::string d = dir;
  285. cmSystemTools::ConvertToWindowsSlashes(d);
  286. fout << "Project(\"{" << this->CreateGUID(m_Makefile->GetProjectName())
  287. << "}\") = \"" << dspname << "\", \""
  288. << d << "\\" << dspname << ".vcproj\", \"{"
  289. << this->CreateGUID(dspname) << "}\"\nEndProject\n";
  290. }
  291. // Write a dsp file into the SLN file,
  292. // Note, that dependencies from executables to
  293. // the libraries it uses are also done here
  294. void cmMSDotNETGenerator::WriteProjectDepends(std::ostream& fout,
  295. const char* dspname,
  296. const char* dir,
  297. cmMSDotNETGenerator*,
  298. const cmTarget& target
  299. )
  300. {
  301. int depcount = 0;
  302. // insert Begin Project Dependency Project_Dep_Name project stuff here
  303. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  304. {
  305. cmTarget::LinkLibraries::const_iterator j, jend;
  306. j = target.GetLinkLibraries().begin();
  307. jend = target.GetLinkLibraries().end();
  308. for(;j!= jend; ++j)
  309. {
  310. if(j->first != dspname)
  311. {
  312. // is the library part of this SLN ? If so add dependency
  313. const char* cacheValue
  314. = m_Makefile->GetDefinition(j->first.c_str());
  315. if(cacheValue)
  316. {
  317. fout << "\t\t{" << this->CreateGUID(dspname) << "}." << depcount << " = {"
  318. << this->CreateGUID(j->first.c_str()) << "}\n";
  319. depcount++;
  320. }
  321. }
  322. }
  323. }
  324. std::set<std::string>::const_iterator i, end;
  325. // write utility dependencies.
  326. i = target.GetUtilities().begin();
  327. end = target.GetUtilities().end();
  328. for(;i!= end; ++i)
  329. {
  330. if(*i != dspname)
  331. {
  332. fout << "\t\t{" << this->CreateGUID(dspname) << "}." << depcount << " = {"
  333. << this->CreateGUID(i->c_str()) << "}\n";
  334. depcount++;
  335. }
  336. }
  337. }
  338. // Write a dsp file into the SLN file,
  339. // Note, that dependencies from executables to
  340. // the libraries it uses are also done here
  341. void cmMSDotNETGenerator::WriteProjectConfigurations(std::ostream& fout, const char* name)
  342. {
  343. std::string guid = this->CreateGUID(name);
  344. fout << "\t\t{" << guid << "}.Debug.ActiveCfg = Debug|Win32\n"
  345. << "\t\t{" << guid << "}.Debug.Build.0 = Debug|Win32\n"
  346. << "\t\t{" << guid << "}.MinSizeRel.ActiveCfg = Debug|Win32\n"
  347. << "\t\t{" << guid << "}.MinSizeRel.Build.0 = Debug|Win32\n"
  348. << "\t\t{" << guid << "}.Release.ActiveCfg = Debug|Win32\n"
  349. << "\t\t{" << guid << "}.Release.Build.0 = Debug|Win32\n"
  350. << "\t\t{" << guid << "}.RelWithDebInfo.ActiveCfg = Debug|Win32\n"
  351. << "\t\t{" << guid << "}.RelWithDebInfo.Build.0 = Debug|Win32\n";
  352. }
  353. // Write a dsp file into the SLN file,
  354. // Note, that dependencies from executables to
  355. // the libraries it uses are also done here
  356. void cmMSDotNETGenerator::WriteExternalProject(std::ostream& fout,
  357. const char* name,
  358. const char* location,
  359. const std::vector<std::string>& dependencies)
  360. {
  361. cmSystemTools::Error("WriteExternalProject not implemented");
  362. // fout << "#########################################################"
  363. // "######################\n\n";
  364. // fout << "Project: \"" << name << "\"="
  365. // << location << " - Package Owner=<4>\n\n";
  366. // fout << "Package=<5>\n{{{\n}}}\n\n";
  367. // fout << "Package=<4>\n";
  368. // fout << "{{{\n";
  369. // std::vector<std::string>::const_iterator i, end;
  370. // // write dependencies.
  371. // i = dependencies.begin();
  372. // end = dependencies.end();
  373. // for(;i!= end; ++i)
  374. // {
  375. // fout << "Begin Project Dependency\n";
  376. // fout << "Project_Dep_Name " << *i << "\n";
  377. // fout << "End Project Dependency\n";
  378. // }
  379. // fout << "}}}\n\n";
  380. }
  381. // Standard end of dsw file
  382. void cmMSDotNETGenerator::WriteSLNFooter(std::ostream& fout)
  383. {
  384. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  385. << "\tEndGlobalSection\n"
  386. << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  387. << "\tEndGlobalSection\n"
  388. << "EndGlobal\n";
  389. }
  390. // ouput standard header for dsw file
  391. void cmMSDotNETGenerator::WriteSLNHeader(std::ostream& fout)
  392. {
  393. fout << "Microsoft Visual Studio Solution File, Format Version 7.00\n";
  394. }
  395. std::string cmMSDotNETGenerator::CreateGUID(const char* name)
  396. {
  397. std::map<cmStdString, cmStdString>::iterator i = m_GUIDMap.find(name);
  398. if(i != m_GUIDMap.end())
  399. {
  400. return i->second;
  401. }
  402. std::string ret;
  403. UUID uid;
  404. unsigned char *uidstr;
  405. UuidCreate(&uid);
  406. UuidToString(&uid,&uidstr);
  407. ret = reinterpret_cast<char*>(uidstr);
  408. RpcStringFree(&uidstr);
  409. m_GUIDMap[name] = ret;
  410. return ret;
  411. }
  412. // TODO
  413. // for CommandLine= need to repleace quotes with &quot
  414. // write out configurations
  415. void cmMSDotNETGenerator::OutputVCProjFile()
  416. {
  417. // If not an in source build, then create the output directory
  418. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  419. m_Makefile->GetHomeDirectory()) != 0)
  420. {
  421. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  422. {
  423. cmSystemTools::Error("Error creating directory ",
  424. m_Makefile->GetStartOutputDirectory());
  425. }
  426. }
  427. // Setup /I and /LIBPATH options for the resulting VCProj file
  428. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  429. std::vector<std::string>::iterator i;
  430. for(i = includes.begin(); i != includes.end(); ++i)
  431. {
  432. std::string tmp = cmSystemTools::EscapeSpaces(i->c_str());
  433. cmSystemTools::ConvertToWindowsSlashesAndCleanUp(tmp);
  434. m_IncludeOptions += ",";
  435. // quote if not already quoted
  436. if (tmp[0] != '"')
  437. {
  438. m_IncludeOptions += tmp;
  439. }
  440. else
  441. {
  442. m_IncludeOptions += tmp;
  443. }
  444. }
  445. // Create the VCProj or set of VCProj's for libraries and executables
  446. // clear project names
  447. m_CreatedProjectNames.clear();
  448. // build any targets
  449. cmTargets &tgts = m_Makefile->GetTargets();
  450. for(cmTargets::iterator l = tgts.begin();
  451. l != tgts.end(); l++)
  452. {
  453. switch(l->second.GetType())
  454. {
  455. case cmTarget::STATIC_LIBRARY:
  456. this->SetBuildType(STATIC_LIBRARY, l->first.c_str());
  457. break;
  458. case cmTarget::SHARED_LIBRARY:
  459. this->SetBuildType(DLL, l->first.c_str());
  460. break;
  461. case cmTarget::EXECUTABLE:
  462. this->SetBuildType(EXECUTABLE,l->first.c_str());
  463. break;
  464. case cmTarget::WIN32_EXECUTABLE:
  465. this->SetBuildType(WIN32_EXECUTABLE,l->first.c_str());
  466. break;
  467. case cmTarget::UTILITY:
  468. this->SetBuildType(UTILITY, l->first.c_str());
  469. break;
  470. case cmTarget::INSTALL_FILES:
  471. break;
  472. case cmTarget::INSTALL_PROGRAMS:
  473. break;
  474. default:
  475. cmSystemTools::Error("Bad target type", l->first.c_str());
  476. break;
  477. }
  478. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  479. // so don't build a projectfile for it
  480. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  481. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  482. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  483. {
  484. this->CreateSingleVCProj(l->first.c_str(),l->second);
  485. }
  486. }
  487. }
  488. void cmMSDotNETGenerator::CreateSingleVCProj(const char *lname, cmTarget &target)
  489. {
  490. // add to the list of projects
  491. std::string pname = lname;
  492. m_CreatedProjectNames.push_back(pname);
  493. // create the dsp.cmake file
  494. std::string fname;
  495. fname = m_Makefile->GetStartOutputDirectory();
  496. fname += "/";
  497. fname += lname;
  498. fname += ".vcproj";
  499. // save the name of the real dsp file
  500. std::string realVCProj = fname;
  501. fname += ".cmake";
  502. std::ofstream fout(fname.c_str());
  503. if(!fout)
  504. {
  505. cmSystemTools::Error("Error Writing ", fname.c_str());
  506. }
  507. this->WriteVCProjFile(fout,lname,target);
  508. fout.close();
  509. // if the dsp file has changed, then write it.
  510. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realVCProj.c_str());
  511. }
  512. void cmMSDotNETGenerator::AddVCProjBuildRule(cmSourceGroup& sourceGroup)
  513. {
  514. std::string dspname = *(m_CreatedProjectNames.end()-1);
  515. if(dspname == "ALL_BUILD")
  516. {
  517. return;
  518. }
  519. dspname += ".vcproj.cmake";
  520. std::string makefileIn = m_Makefile->GetStartDirectory();
  521. makefileIn += "/";
  522. makefileIn += "CMakeLists.txt";
  523. makefileIn = cmSystemTools::HandleNetworkPaths(makefileIn.c_str());
  524. makefileIn = cmSystemTools::EscapeSpaces(makefileIn.c_str());
  525. std::string dsprule = "${CMAKE_COMMAND}";
  526. m_Makefile->ExpandVariablesInString(dsprule);
  527. dsprule = cmSystemTools::HandleNetworkPaths(dsprule.c_str());
  528. std::string args = makefileIn;
  529. args += " -H\"";
  530. args += cmSystemTools::HandleNetworkPaths(m_Makefile->GetHomeDirectory());
  531. args += "\" -S\"";
  532. args += cmSystemTools::HandleNetworkPaths(m_Makefile->GetStartDirectory());
  533. args += "\" -O\"";
  534. args += cmSystemTools::HandleNetworkPaths(m_Makefile->GetStartOutputDirectory());
  535. args += "\" -B\"";
  536. args += cmSystemTools::HandleNetworkPaths(m_Makefile->GetHomeOutputDirectory());
  537. args += "\"";
  538. m_Makefile->ExpandVariablesInString(args);
  539. std::string configFile =
  540. m_Makefile->GetDefinition("CMAKE_ROOT");
  541. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  542. std::vector<std::string> listFiles = m_Makefile->GetListFiles();
  543. bool found = false;
  544. for(std::vector<std::string>::iterator i = listFiles.begin();
  545. i != listFiles.end(); ++i)
  546. {
  547. if(*i == configFile)
  548. {
  549. found = true;
  550. }
  551. }
  552. if(!found)
  553. {
  554. listFiles.push_back(configFile);
  555. }
  556. std::vector<std::string> outputs;
  557. outputs.push_back(dspname);
  558. cmCustomCommand cc(makefileIn.c_str(), dsprule.c_str(),
  559. args.c_str(),
  560. listFiles,
  561. outputs);
  562. sourceGroup.AddCustomCommand(cc);
  563. }
  564. void cmMSDotNETGenerator::WriteConfigurations(std::ostream& fout,
  565. const char *libName,
  566. const cmTarget &target)
  567. {
  568. fout << "\t<Configurations>\n";
  569. for( std::vector<std::string>::iterator i = m_Configurations.begin();
  570. i != m_Configurations.end(); ++i)
  571. {
  572. this->WriteConfiguration(fout, i->c_str(), libName, target);
  573. }
  574. fout << "\t</Configurations>\n";
  575. }
  576. void cmMSDotNETGenerator::WriteConfiguration(std::ostream& fout,
  577. const char* configName,
  578. const char *libName,
  579. const cmTarget &target)
  580. {
  581. fout << "\t\t<Configuration>\n"
  582. << "\t\t\tName=\"" << configName << "|Win32\"\n"
  583. << "\t\t\tOutputDirectory=\"" << configName << "\"\n"
  584. << "\t\t\tIntermediateDirectory=\"" << configName << "\"\n"
  585. << "\t\t\tConfigurationType=\"2\"\n"
  586. << "\t\t\tCharacterSet=\"2\">\n";
  587. fout << "\t\t\t<Tool\n"
  588. << "\t\t\t\tName=\"VCCLCompilerTool\"\n"
  589. << "\t\t\t\tAdditionalOptions=\"" <<
  590. m_Makefile->GetDefinition("CMAKE_CXX_FLAGS") << "\"\n";
  591. if(strcmp(configName, "Debug") == 0)
  592. {
  593. fout << "\t\t\t\tOptimization=\"0\"\n"
  594. << "\t\t\t\tPreprocessorDefinitions=\"WIN32;_DEBUG;_WINDOWS";
  595. }
  596. else if(strcmp(configName, "Release") == 0)
  597. {
  598. fout << "\t\t\t\tOptimization=\"2\"\n"
  599. << "\t\t\t\tInlineFunctionExpansion=\"1\"\n"
  600. << "\t\t\t\tPreprocessorDefinitions=\"WIN32;NDEBUG;_WINDOWS";
  601. }
  602. else if(strcmp(configName, "MinSizeRel") == 0)
  603. {
  604. fout << "\t\t\t\tOptimization=\"1\"\n"
  605. << "\t\t\t\tInlineFunctionExpansion=\"1\"\n"
  606. << "\t\t\t\tPreprocessorDefinitions=\"WIN32;NDEBUG;_WINDOWS";
  607. }
  608. else if(strcmp(configName, "RelWithDebInfo") == 0)
  609. {
  610. fout << "\t\t\t\tOptimization=\"2\"\n"
  611. << "\t\t\t\tInlineFunctionExpansion=\"1\"\n"
  612. << "\t\t\t\tPreprocessorDefinitions=\"WIN32;NDEBUG;_WINDOWS";
  613. }
  614. this->OutputDefineFlags(fout);
  615. fout << "\"\n";
  616. fout << "\t\t\t\tAdditionalIncludeDirectories=\"";
  617. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  618. std::vector<std::string>::iterator i = includes.begin();
  619. if(i != includes.end())
  620. {
  621. fout << "&quot;" << *i << "&quot;";
  622. }
  623. for(;i != includes.end(); ++i)
  624. {
  625. fout << ";&quot;" << *i << "&quot;";
  626. }
  627. fout << "\"\n";
  628. fout << "\t\t\t\tWarningLevel=\"3\"\n";
  629. fout << "\t\t\t\tDetect64BitPortabilityProblems=\"TRUE\"\n"
  630. << "\t\t\t\tDebugInformationFormat=\"3\"";
  631. fout << "/>\n"; // end of <Tool Name=VCCLCompilerTool
  632. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCCustomBuildTool\"/>\n";
  633. this->OutputBuildTool(fout, libName, target);
  634. fout << "\t\t</Configuration>\n";
  635. }
  636. void cmMSDotNETGenerator::OutputBuildTool(std::ostream& fout,
  637. const char *libName,
  638. const cmTarget &target)
  639. {
  640. std::string libPath = "";
  641. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  642. {
  643. libPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  644. }
  645. if(libPath.size())
  646. {
  647. // make sure there is a trailing slash
  648. if(libPath[libPath.size()-1] != '/')
  649. {
  650. libPath += "/";
  651. }
  652. libPath = cmSystemTools::HandleNetworkPaths(libPath.c_str());
  653. }
  654. std::string exePath = "";
  655. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  656. {
  657. exePath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  658. }
  659. if(exePath.size())
  660. {
  661. // make sure there is a trailing slash
  662. if(exePath[exePath.size()-1] != '/')
  663. {
  664. exePath += "/";
  665. }
  666. }
  667. exePath = cmSystemTools::HandleNetworkPaths(exePath.c_str());
  668. switch(m_BuildType)
  669. {
  670. case STATIC_LIBRARY:
  671. fout << "\t\t\t<Tool\n"
  672. << "\t\t\t\tName=\"VCLibrarianTool\"\n"
  673. << "\t\t\t\t\tOutputFile=\"" << exePath << libName << ".lib\"/>\n";
  674. break;
  675. case DLL:
  676. break;
  677. case EXECUTABLE:
  678. fout << "\t\t\t<Tool\n"
  679. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  680. << "AdditionalOptions=\"/MACHINE:I386\"\n"
  681. << "AdditionalDependencies=\" odbc32.lib odbccp32.lib ";
  682. fout << "**** FINISH cmMSDotNETGenerator **** \n";
  683. break;
  684. case WIN32_EXECUTABLE:
  685. break;
  686. case UTILITY:
  687. break;
  688. }
  689. }
  690. void cmMSDotNETGenerator::OutputDefineFlags(std::ostream& fout)
  691. {
  692. std::string defs = m_Makefile->GetDefineFlags();
  693. std::string::size_type pos = defs.find("-D");
  694. bool done = pos == std::string::npos;
  695. while(!done)
  696. {
  697. std::string::size_type nextpos = defs.find("-D", pos+2);
  698. std::string define;
  699. if(nextpos != std::string::npos)
  700. {
  701. define = defs.substr(pos+2, nextpos - pos -2);
  702. }
  703. else
  704. {
  705. define = defs.substr(pos+2);
  706. done = true;
  707. }
  708. fout << define << ";";
  709. if(!done)
  710. {
  711. pos = defs.find("-D", nextpos);
  712. }
  713. }
  714. }
  715. void cmMSDotNETGenerator::WriteVCProjFile(std::ostream& fout,
  716. const char *libName,
  717. cmTarget &target)
  718. {
  719. // We may be modifying the source groups temporarily, so make a copy.
  720. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  721. // get the classes from the source lists then add them to the groups
  722. std::vector<cmSourceFile> classes = target.GetSourceFiles();
  723. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  724. i != classes.end(); i++)
  725. {
  726. // Add the file to the list of sources.
  727. std::string source = i->GetFullPath();
  728. cmSourceGroup& sourceGroup = m_Makefile->FindSourceGroup(source.c_str(),
  729. sourceGroups);
  730. sourceGroup.AddSource(source.c_str());
  731. }
  732. // add any custom rules to the source groups
  733. for (std::vector<cmCustomCommand>::const_iterator cr =
  734. target.GetCustomCommands().begin();
  735. cr != target.GetCustomCommands().end(); ++cr)
  736. {
  737. cmSourceGroup& sourceGroup =
  738. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  739. sourceGroups);
  740. cmCustomCommand cc(*cr);
  741. cc.ExpandVariables(*m_Makefile);
  742. sourceGroup.AddCustomCommand(cc);
  743. }
  744. // open the project
  745. this->WriteProjectStart(fout, libName, target, sourceGroups);
  746. // write the configuration information
  747. this->WriteConfigurations(fout, libName, target);
  748. fout << "\t<Files>\n";
  749. // Find the group in which the CMakeLists.txt source belongs, and add
  750. // the rule to generate this VCProj file.
  751. for(std::vector<cmSourceGroup>::reverse_iterator sg = sourceGroups.rbegin();
  752. sg != sourceGroups.rend(); ++sg)
  753. {
  754. if(sg->Matches("CMakeLists.txt"))
  755. {
  756. this->AddVCProjBuildRule(*sg);
  757. break;
  758. }
  759. }
  760. // Loop through every source group.
  761. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  762. sg != sourceGroups.end(); ++sg)
  763. {
  764. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  765. // If the group is empty, don't write it at all.
  766. if(buildRules.empty())
  767. { continue; }
  768. // If the group has a name, write the header.
  769. std::string name = sg->GetName();
  770. if(name != "")
  771. {
  772. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  773. }
  774. // Loop through each build rule in the source group.
  775. for(cmSourceGroup::BuildRules::const_iterator cc =
  776. buildRules.begin(); cc != buildRules.end(); ++ cc)
  777. {
  778. std::string source = cc->first;
  779. const cmSourceGroup::Commands& commands = cc->second;
  780. if (source != libName || target.GetType() == cmTarget::UTILITY)
  781. {
  782. fout << "\t\t\t<File\n";
  783. // Tell MS-Dev what the source is. If the compiler knows how to
  784. // build it, then it will.
  785. fout << "\t\t\t\tRelativePath=\"" << cmSystemTools::EscapeSpaces(source.c_str()) << "\">\n";
  786. if (!commands.empty())
  787. {
  788. cmSourceGroup::CommandFiles totalCommand;
  789. std::string totalCommandStr;
  790. totalCommandStr = this->CombineCommands(commands, totalCommand,
  791. source.c_str());
  792. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  793. totalCommand.m_Depends,
  794. totalCommand.m_Outputs);
  795. }
  796. fout << "\t\t\t</File>\n";
  797. }
  798. }
  799. // If the group has a name, write the footer.
  800. if(name != "")
  801. {
  802. this->WriteVCProjEndGroup(fout);
  803. }
  804. }
  805. fout << "\t</Files>\n";
  806. // Write the VCProj file's footer.
  807. this->WriteVCProjFooter(fout);
  808. }
  809. void cmMSDotNETGenerator::WriteCustomRule(std::ostream& fout,
  810. const char* source,
  811. const char* command,
  812. const std::set<std::string>& depends,
  813. const std::set<std::string>& outputs)
  814. {
  815. std::string cmd = command;
  816. cmSystemTools::ReplaceString(cmd, "\"", "&quot;");
  817. std::vector<std::string>::iterator i;
  818. for(i = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  819. {
  820. fout << "\t\t\t\t<FileConfiguration\n";
  821. fout << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n";
  822. fout << "\t\t\t\t\t<Tool\n"
  823. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  824. << "\t\t\t\t\tCommandLine=\"" << cmd << "\n\"\n"
  825. << "\t\t\t\t\tAdditionalDependencies=\"";
  826. // Write out the dependencies for the rule.
  827. std::string temp;
  828. for(std::set<std::string>::const_iterator d = depends.begin();
  829. d != depends.end(); ++d)
  830. {
  831. temp = *d;
  832. fout << cmSystemTools::EscapeSpaces(cmSystemTools::ConvertToWindowsSlashes(temp))
  833. << ";";
  834. }
  835. fout << "\"\n";
  836. fout << "\t\t\t\t\tOutputs=\"";
  837. bool first = true;
  838. // Write a rule for every output generated by this command.
  839. for(std::set<std::string>::const_iterator output = outputs.begin();
  840. output != outputs.end(); ++output)
  841. {
  842. if(!first)
  843. {
  844. fout << ";";
  845. }
  846. else
  847. {
  848. first = true;
  849. }
  850. fout << output->c_str();
  851. }
  852. fout << "\"/>\n";
  853. fout << "\t\t\t\t</FileConfiguration>\n";
  854. }
  855. }
  856. void cmMSDotNETGenerator::WriteVCProjBeginGroup(std::ostream& fout,
  857. const char* group,
  858. const char* filter)
  859. {
  860. fout << "\t\t<Filter\n"
  861. << "\t\t\tName=\"" << group << "\"\n"
  862. << "\t\t\tFilter=\"\">\n";
  863. }
  864. void cmMSDotNETGenerator::WriteVCProjEndGroup(std::ostream& fout)
  865. {
  866. fout << "\t\t</Filter>\n";
  867. }
  868. void cmMSDotNETGenerator::SetBuildType(BuildType b, const char *libName)
  869. {
  870. m_BuildType = b;
  871. }
  872. std::string
  873. cmMSDotNETGenerator::CombineCommands(const cmSourceGroup::Commands &commands,
  874. cmSourceGroup::CommandFiles &totalCommand,
  875. const char *source)
  876. {
  877. // Loop through every custom command generating code from the
  878. // current source.
  879. // build up the depends and outputs and commands
  880. std::string totalCommandStr = "";
  881. std::string temp;
  882. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  883. c != commands.end(); ++c)
  884. {
  885. temp= c->second.m_Command;
  886. cmSystemTools::ConvertToWindowsSlashes(temp);
  887. temp = cmSystemTools::EscapeSpaces(temp.c_str());
  888. totalCommandStr += temp;
  889. totalCommandStr += " ";
  890. totalCommandStr += c->second.m_Arguments;
  891. totalCommand.Merge(c->second);
  892. }
  893. // Create a dummy file with the name of the source if it does
  894. // not exist
  895. if(totalCommand.m_Outputs.empty())
  896. {
  897. std::string dummyFile = m_Makefile->GetStartOutputDirectory();
  898. dummyFile += "/";
  899. dummyFile += source;
  900. if(!cmSystemTools::FileExists(dummyFile.c_str()))
  901. {
  902. std::ofstream fout(dummyFile.c_str());
  903. fout << "Dummy file created by cmake as unused source for utility command.\n";
  904. }
  905. }
  906. return totalCommandStr;
  907. }
  908. // look for custom rules on a target and collect them together
  909. std::string
  910. cmMSDotNETGenerator::CreateTargetRules(const cmTarget &target,
  911. const char *libName)
  912. {
  913. std::string customRuleCode = "";
  914. if (target.GetType() >= cmTarget::UTILITY)
  915. {
  916. return customRuleCode;
  917. }
  918. // Find the group in which the lix exe custom rules belong
  919. bool init = false;
  920. for (std::vector<cmCustomCommand>::const_iterator cr =
  921. target.GetCustomCommands().begin();
  922. cr != target.GetCustomCommands().end(); ++cr)
  923. {
  924. cmCustomCommand cc(*cr);
  925. cc.ExpandVariables(*m_Makefile);
  926. if (cc.GetSourceName() == libName)
  927. {
  928. if (!init)
  929. {
  930. // header stuff
  931. customRuleCode = "# Begin Special Build Tool\nPostBuild_Cmds=";
  932. init = true;
  933. }
  934. else
  935. {
  936. customRuleCode += "\t";
  937. }
  938. customRuleCode += cc.GetCommand() + " " + cc.GetArguments();
  939. }
  940. }
  941. if (init)
  942. {
  943. customRuleCode += "\n# End Special Build Tool\n";
  944. }
  945. return customRuleCode;
  946. }
  947. void cmMSDotNETGenerator::WriteProjectStart(std::ostream& fout, const char *libName,
  948. const cmTarget &target,
  949. std::vector<cmSourceGroup> &)
  950. {
  951. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  952. << "<VisualStudioProject\n"
  953. << "\tProjectType=\"Visual C++\"\n"
  954. << "\tVersion=\"7.00\"\n"
  955. << "\tName=\"" << libName << "\"\n"
  956. << "\tSccProjectName=\"\"\n"
  957. << "\tSccLocalPath=\"\"\n"
  958. << "\tKeyword=\"AtlProj\">\n"
  959. << "\t<Platforms>\n"
  960. << "\t\t<Platform\n\t\t\tName=\"Win32\"/>\n"
  961. << "\t</Platforms>\n";
  962. }
  963. void cmMSDotNETGenerator::WriteVCProjFooter(std::ostream& fout)
  964. {
  965. fout << "\t<Globals>\n"
  966. << "\t</Globals>\n"
  967. << "</VisualStudioProject>\n";
  968. }