cmMSDotNETGenerator.cxx 45 KB

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