cmMSDotNETGenerator.cxx 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  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. const char* cacheValue
  381. = m_Makefile->GetDefinition(j->first.c_str());
  382. if(cacheValue)
  383. {
  384. fout << "\t\t{" << this->CreateGUID(dspname) << "}." << depcount << " = {"
  385. << this->CreateGUID(j->first.c_str()) << "}\n";
  386. depcount++;
  387. }
  388. }
  389. }
  390. }
  391. std::set<cmStdString>::const_iterator i, end;
  392. // write utility dependencies.
  393. i = target.GetUtilities().begin();
  394. end = target.GetUtilities().end();
  395. for(;i!= end; ++i)
  396. {
  397. if(*i != dspname)
  398. {
  399. fout << "\t\t{" << this->CreateGUID(dspname) << "}." << depcount << " = {"
  400. << this->CreateGUID(i->c_str()) << "}\n";
  401. depcount++;
  402. }
  403. }
  404. }
  405. // Write a dsp file into the SLN file,
  406. // Note, that dependencies from executables to
  407. // the libraries it uses are also done here
  408. void cmMSDotNETGenerator::WriteProjectConfigurations(std::ostream& fout, const char* name)
  409. {
  410. std::string guid = this->CreateGUID(name);
  411. for(std::vector<std::string>::iterator i = m_Configurations.begin();
  412. i != m_Configurations.end(); ++i)
  413. {
  414. fout << "\t\t{" << guid << "}." << *i << ".ActiveCfg = " << *i << "|Win32\n"
  415. << "\t\t{" << guid << "}." << *i << ".Build.0 = " << *i << "|Win32\n";
  416. }
  417. }
  418. // Write a dsp file into the SLN file,
  419. // Note, that dependencies from executables to
  420. // the libraries it uses are also done here
  421. void cmMSDotNETGenerator::WriteExternalProject(std::ostream& ,
  422. const char* ,
  423. const char* ,
  424. const std::vector<std::string>& )
  425. {
  426. cmSystemTools::Error("WriteExternalProject not implemented");
  427. // fout << "#########################################################"
  428. // "######################\n\n";
  429. // fout << "Project: \"" << name << "\"="
  430. // << location << " - Package Owner=<4>\n\n";
  431. // fout << "Package=<5>\n{{{\n}}}\n\n";
  432. // fout << "Package=<4>\n";
  433. // fout << "{{{\n";
  434. // std::vector<std::string>::const_iterator i, end;
  435. // // write dependencies.
  436. // i = dependencies.begin();
  437. // end = dependencies.end();
  438. // for(;i!= end; ++i)
  439. // {
  440. // fout << "Begin Project Dependency\n";
  441. // fout << "Project_Dep_Name " << *i << "\n";
  442. // fout << "End Project Dependency\n";
  443. // }
  444. // fout << "}}}\n\n";
  445. }
  446. // Standard end of dsw file
  447. void cmMSDotNETGenerator::WriteSLNFooter(std::ostream& fout)
  448. {
  449. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  450. << "\tEndGlobalSection\n"
  451. << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  452. << "\tEndGlobalSection\n"
  453. << "EndGlobal\n";
  454. }
  455. // ouput standard header for dsw file
  456. void cmMSDotNETGenerator::WriteSLNHeader(std::ostream& fout)
  457. {
  458. fout << "Microsoft Visual Studio Solution File, Format Version 7.00\n";
  459. }
  460. std::string cmMSDotNETGenerator::CreateGUID(const char* name)
  461. {
  462. std::map<cmStdString, cmStdString>::iterator i = m_GUIDMap.find(name);
  463. if(i != m_GUIDMap.end())
  464. {
  465. return i->second;
  466. }
  467. std::string ret;
  468. UUID uid;
  469. unsigned char *uidstr;
  470. UuidCreate(&uid);
  471. UuidToString(&uid,&uidstr);
  472. ret = reinterpret_cast<char*>(uidstr);
  473. RpcStringFree(&uidstr);
  474. ret = cmSystemTools::UpperCase(ret);
  475. m_GUIDMap[name] = ret;
  476. return ret;
  477. }
  478. // TODO
  479. // for CommandLine= need to repleace quotes with &quot
  480. // write out configurations
  481. void cmMSDotNETGenerator::OutputVCProjFile()
  482. {
  483. // If not an in source build, then create the output directory
  484. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  485. m_Makefile->GetHomeDirectory()) != 0)
  486. {
  487. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  488. {
  489. cmSystemTools::Error("Error creating directory ",
  490. m_Makefile->GetStartOutputDirectory());
  491. }
  492. }
  493. m_LibraryOutputPath = "";
  494. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  495. {
  496. m_LibraryOutputPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  497. }
  498. if(m_LibraryOutputPath.size())
  499. {
  500. // make sure there is a trailing slash
  501. if(m_LibraryOutputPath[m_LibraryOutputPath.size()-1] != '/')
  502. {
  503. m_LibraryOutputPath += "/";
  504. }
  505. }
  506. m_ExecutableOutputPath = "";
  507. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  508. {
  509. m_ExecutableOutputPath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  510. }
  511. if(m_ExecutableOutputPath.size())
  512. {
  513. // make sure there is a trailing slash
  514. if(m_ExecutableOutputPath[m_ExecutableOutputPath.size()-1] != '/')
  515. {
  516. m_ExecutableOutputPath += "/";
  517. }
  518. }
  519. // Create the VCProj or set of VCProj's for libraries and executables
  520. // clear project names
  521. m_CreatedProjectNames.clear();
  522. // build any targets
  523. cmTargets &tgts = m_Makefile->GetTargets();
  524. for(cmTargets::iterator l = tgts.begin();
  525. l != tgts.end(); l++)
  526. {
  527. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  528. // so don't build a projectfile for it
  529. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  530. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  531. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  532. {
  533. this->CreateSingleVCProj(l->first.c_str(),l->second);
  534. }
  535. }
  536. }
  537. void cmMSDotNETGenerator::CreateSingleVCProj(const char *lname, cmTarget &target)
  538. {
  539. // add to the list of projects
  540. std::string pname = lname;
  541. m_CreatedProjectNames.push_back(pname);
  542. // create the dsp.cmake file
  543. std::string fname;
  544. fname = m_Makefile->GetStartOutputDirectory();
  545. fname += "/";
  546. fname += lname;
  547. fname += ".vcproj";
  548. // save the name of the real dsp file
  549. std::string realVCProj = fname;
  550. fname += ".cmake";
  551. std::ofstream fout(fname.c_str());
  552. if(!fout)
  553. {
  554. cmSystemTools::Error("Error Writing ", fname.c_str());
  555. }
  556. this->WriteVCProjFile(fout,lname,target);
  557. fout.close();
  558. // if the dsp file has changed, then write it.
  559. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realVCProj.c_str());
  560. }
  561. void cmMSDotNETGenerator::AddVCProjBuildRule(cmSourceGroup& sourceGroup)
  562. {
  563. std::string dspname = *(m_CreatedProjectNames.end()-1);
  564. if(dspname == "ALL_BUILD")
  565. {
  566. return;
  567. }
  568. dspname += ".vcproj.cmake";
  569. std::string makefileIn = m_Makefile->GetStartDirectory();
  570. makefileIn += "/";
  571. makefileIn += "CMakeLists.txt";
  572. makefileIn = cmSystemTools::ConvertToOutputPath(makefileIn.c_str());
  573. std::string dsprule = "${CMAKE_COMMAND}";
  574. m_Makefile->ExpandVariablesInString(dsprule);
  575. dsprule = cmSystemTools::ConvertToOutputPath(dsprule.c_str());
  576. std::string args = makefileIn;
  577. args += " -H";
  578. args +=
  579. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeDirectory());
  580. args += " -S";
  581. args +=
  582. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartDirectory());
  583. args += " -O";
  584. args +=
  585. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartOutputDirectory());
  586. args += " -B";
  587. args +=
  588. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory());
  589. args += "";
  590. m_Makefile->ExpandVariablesInString(args);
  591. std::string configFile =
  592. m_Makefile->GetDefinition("CMAKE_ROOT");
  593. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  594. std::vector<std::string> listFiles = m_Makefile->GetListFiles();
  595. bool found = false;
  596. for(std::vector<std::string>::iterator i = listFiles.begin();
  597. i != listFiles.end(); ++i)
  598. {
  599. if(*i == configFile)
  600. {
  601. found = true;
  602. }
  603. }
  604. if(!found)
  605. {
  606. listFiles.push_back(configFile);
  607. }
  608. std::vector<std::string> outputs;
  609. outputs.push_back(dspname);
  610. cmCustomCommand cc(makefileIn.c_str(), dsprule.c_str(),
  611. args.c_str(),
  612. listFiles,
  613. outputs);
  614. sourceGroup.AddCustomCommand(cc);
  615. }
  616. void cmMSDotNETGenerator::WriteConfigurations(std::ostream& fout,
  617. const char *libName,
  618. const cmTarget &target)
  619. {
  620. fout << "\t<Configurations>\n";
  621. for( std::vector<std::string>::iterator i = m_Configurations.begin();
  622. i != m_Configurations.end(); ++i)
  623. {
  624. this->WriteConfiguration(fout, i->c_str(), libName, target);
  625. }
  626. fout << "\t</Configurations>\n";
  627. }
  628. void cmMSDotNETGenerator::WriteConfiguration(std::ostream& fout,
  629. const char* configName,
  630. const char *libName,
  631. const cmTarget &target)
  632. {
  633. const char* mfcFlag = m_Makefile->GetDefinition("CMAKE_MFC_FLAG");
  634. if(!mfcFlag)
  635. {
  636. mfcFlag = "0";
  637. }
  638. fout << "\t\t<Configuration\n"
  639. << "\t\t\tName=\"" << configName << "|Win32\"\n"
  640. << "\t\t\tOutputDirectory=\"" << configName << "\"\n";
  641. // This is an internal type to Visual Studio, it seems that:
  642. // 4 == static library
  643. // 2 == dll
  644. // 1 == executable
  645. // 10 == utility
  646. const char* configType = "10";
  647. switch(target.GetType())
  648. {
  649. case cmTarget::STATIC_LIBRARY:
  650. configType = "4";
  651. break;
  652. case cmTarget::SHARED_LIBRARY:
  653. case cmTarget::MODULE_LIBRARY:
  654. configType = "2";
  655. break;
  656. case cmTarget::EXECUTABLE:
  657. case cmTarget::WIN32_EXECUTABLE:
  658. configType = "1";
  659. break;
  660. case cmTarget::UTILITY:
  661. configType = "10";
  662. default:
  663. break;
  664. }
  665. fout << "\t\t\tIntermediateDirectory=\".\\" << configName << "\"\n"
  666. << "\t\t\tConfigurationType=\"" << configType << "\"\n"
  667. << "\t\t\tUseOfMFC=\"" << mfcFlag << "\"\n"
  668. << "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n"
  669. << "\t\t\tCharacterSet=\"2\">\n";
  670. fout << "\t\t\t<Tool\n"
  671. << "\t\t\t\tName=\"VCCLCompilerTool\"\n"
  672. << "\t\t\t\tAdditionalOptions=\""
  673. << m_Makefile->GetDefinition("CMAKE_CXX_FLAGS")
  674. << " -DCMAKE_INTDIR=\\&quot;" << configName << "\\&quot;"
  675. << "\"\n";
  676. fout << "\t\t\t\tAdditionalIncludeDirectories=\"";
  677. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  678. std::vector<std::string>::iterator i = includes.begin();
  679. for(;i != includes.end(); ++i)
  680. {
  681. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  682. fout << ipath << ";";
  683. }
  684. fout << "\"\n";
  685. // Optimization = 0 None Debug /O0
  686. // Optimization = 1 MinSize /O1
  687. // Optimization = 2 MaxSpeed /O2
  688. // Optimization = 3 Max Optimization /O3
  689. // RuntimeLibrary = 0 /MT multithread
  690. // RuntimeLibrary = 1 /MTd multithread debug
  691. // RuntimeLibrary = 2 /MD multithread dll
  692. // RuntimeLibrary = 3 /MDd multithread dll debug
  693. // RuntimeLibrary = 4 /ML single thread
  694. // RuntimeLibrary = 5 /MLd single thread debug
  695. // InlineFunctionExpansion = 0 none
  696. // InlineFunctionExpansion = 1 when inline keyword
  697. // InlineFunctionExpansion = 2 any time you can
  698. if(strcmp(configName, "Debug") == 0)
  699. {
  700. fout << "\t\t\t\tOptimization=\"0\"\n"
  701. << "\t\t\t\tRuntimeLibrary=\"3\"\n"
  702. << "\t\t\t\tInlineFunctionExpansion=\"0\"\n"
  703. << "\t\t\t\tPreprocessorDefinitions=\"WIN32,_DEBUG,_WINDOWS";
  704. }
  705. else if(strcmp(configName, "Release") == 0)
  706. {
  707. fout << "\t\t\t\tOptimization=\"2\"\n"
  708. << "\t\t\t\tRuntimeLibrary=\"2\"\n"
  709. << "\t\t\t\tInlineFunctionExpansion=\"1\"\n"
  710. << "\t\t\t\tPreprocessorDefinitions=\"WIN32,NDEBUG,_WINDOWS";
  711. }
  712. else if(strcmp(configName, "MinSizeRel") == 0)
  713. {
  714. fout << "\t\t\t\tOptimization=\"1\"\n"
  715. << "\t\t\t\tRuntimeLibrary=\"2\"\n"
  716. << "\t\t\t\tInlineFunctionExpansion=\"1\"\n"
  717. << "\t\t\t\tPreprocessorDefinitions=\"WIN32,NDEBUG,_WINDOWS";
  718. }
  719. else if(strcmp(configName, "RelWithDebInfo") == 0)
  720. {
  721. fout << "\t\t\t\tOptimization=\"2\"\n"
  722. << "\t\t\t\tRuntimeLibrary=\"2\"\n"
  723. << "\t\t\t\tInlineFunctionExpansion=\"1\"\n"
  724. << "\t\t\t\tPreprocessorDefinitions=\"WIN32,NDEBUG,_WINDOWS";
  725. }
  726. if(target.GetType() == cmTarget::SHARED_LIBRARY
  727. || target.GetType() == cmTarget::MODULE_LIBRARY)
  728. {
  729. fout << "," << libName << "_EXPORTS";
  730. }
  731. this->OutputDefineFlags(fout);
  732. fout << "\"\n";
  733. if(m_Makefile->IsOn("CMAKE_CXX_USE_RTTI"))
  734. {
  735. fout << "\t\t\t\tRuntimeTypeInfo=\"TRUE\"\n";
  736. }
  737. fout << "\t\t\t\tAssemblerListingLocation=\"" << configName << "\"\n";
  738. fout << "\t\t\t\tObjectFile=\"" << configName << "\\\"\n";
  739. fout << "\t\t\t\tWarningLevel=\"" << m_Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL") << "\"\n";
  740. fout << "\t\t\t\tDetect64BitPortabilityProblems=\"TRUE\"\n"
  741. << "\t\t\t\tDebugInformationFormat=\"3\"";
  742. fout << "/>\n"; // end of <Tool Name=VCCLCompilerTool
  743. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCCustomBuildTool\"/>\n";
  744. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCMIDLTool\"/>\n";
  745. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  746. this->OutputTargetRules(fout, target, libName);
  747. fout << "/>\n";
  748. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"/>\n";
  749. this->OutputBuildTool(fout, configName, libName, target);
  750. fout << "\t\t</Configuration>\n";
  751. }
  752. void cmMSDotNETGenerator::OutputBuildTool(std::ostream& fout,
  753. const char* configName,
  754. const char *libName,
  755. const cmTarget &target)
  756. {
  757. std::string temp;
  758. switch(target.GetType())
  759. {
  760. case cmTarget::STATIC_LIBRARY:
  761. {
  762. std::string libpath = m_LibraryOutputPath +
  763. "$(OutDir)/" + libName + ".lib";
  764. fout << "\t\t\t<Tool\n"
  765. << "\t\t\t\tName=\"VCLibrarianTool\"\n"
  766. << "\t\t\t\t\tOutputFile=\""
  767. << this->ConvertToXMLOutputPathSingle(libpath.c_str()) << ".\"/>\n";
  768. break;
  769. }
  770. case cmTarget::SHARED_LIBRARY:
  771. case cmTarget::MODULE_LIBRARY:
  772. fout << "\t\t\t<Tool\n"
  773. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  774. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386\"\n"
  775. << "\t\t\t\tAdditionalDependencies=\" odbc32.lib odbccp32.lib ";
  776. this->OutputLibraries(fout, configName, libName, target);
  777. fout << "\"\n";
  778. temp = m_LibraryOutputPath;
  779. temp += configName;
  780. temp += "/";
  781. temp += libName;
  782. temp += ".dll";
  783. fout << "\t\t\t\tOutputFile=\""
  784. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  785. fout << "\t\t\t\tLinkIncremental=\"1\"\n";
  786. fout << "\t\t\t\tSuppressStartupBanner=\"TRUE\"\n";
  787. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  788. this->OutputLibraryDirectories(fout, configName, libName, target);
  789. fout << "\"\n";
  790. this->OutputModuleDefinitionFile(fout, target);
  791. temp = m_LibraryOutputPath;
  792. temp += "$(OutDir)";
  793. temp += "/";
  794. temp += libName;
  795. temp += ".pdb";
  796. fout << "\t\t\t\tProgramDatabaseFile=\"" <<
  797. this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  798. if(strcmp(configName, "Debug") == 0
  799. || strcmp(configName, "RelWithDebInfo") == 0)
  800. {
  801. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  802. }
  803. fout << "\t\t\t\tStackReserveSize=\""
  804. << m_Makefile->GetDefinition("CMAKE_CXX_STACK_SIZE") << "\"\n";
  805. temp = m_ExecutableOutputPath;
  806. temp += configName;
  807. temp += "/";
  808. temp += libName;
  809. temp += ".lib";
  810. fout << "\t\t\t\tImportLibrary=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
  811. break;
  812. case cmTarget::EXECUTABLE:
  813. case cmTarget::WIN32_EXECUTABLE:
  814. fout << "\t\t\t<Tool\n"
  815. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  816. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386\"\n"
  817. << "\t\t\t\tAdditionalDependencies=\" odbc32.lib odbccp32.lib ";
  818. this->OutputLibraries(fout, configName, libName, target);
  819. fout << "\"\n";
  820. temp = m_ExecutableOutputPath;
  821. temp += configName;
  822. temp += "/";
  823. temp += libName;
  824. temp += ".exe";
  825. fout << "\t\t\t\tOutputFile=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  826. fout << "\t\t\t\tLinkIncremental=\"1\"\n";
  827. fout << "\t\t\t\tSuppressStartupBanner=\"TRUE\"\n";
  828. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  829. this->OutputLibraryDirectories(fout, configName, libName, target);
  830. fout << "\"\n";
  831. fout << "\t\t\t\tProgramDatabaseFile=\"" << m_LibraryOutputPath
  832. << "$(OutDir)\\" << libName << ".pdb\"\n";
  833. if(strcmp(configName, "Debug") == 0
  834. || strcmp(configName, "RelWithDebInfo") == 0)
  835. {
  836. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  837. }
  838. if( target.GetType() == cmTarget::EXECUTABLE)
  839. {
  840. fout << "\t\t\t\tSubSystem=\"1\"\n";
  841. }
  842. else
  843. {
  844. fout << "\t\t\t\tSubSystem=\"2\"\n";
  845. }
  846. fout << "\t\t\t\tStackReserveSize=\""
  847. << m_Makefile->GetDefinition("CMAKE_CXX_STACK_SIZE") << "\"/>\n";
  848. break;
  849. case cmTarget::UTILITY:
  850. break;
  851. }
  852. }
  853. void cmMSDotNETGenerator::OutputModuleDefinitionFile(std::ostream& fout,
  854. const cmTarget &target)
  855. {
  856. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  857. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  858. i != classes.end(); i++)
  859. {
  860. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  861. {
  862. fout << "\t\t\t\tModuleDefinitionFile=\""
  863. << this->ConvertToXMLOutputPath((*i)->GetFullPath().c_str())
  864. << "\"\n";
  865. return;
  866. }
  867. }
  868. }
  869. void cmMSDotNETGenerator::OutputLibraryDirectories(std::ostream& fout,
  870. const char*,
  871. const char*,
  872. const cmTarget &tgt)
  873. {
  874. bool hasone = false;
  875. if(m_LibraryOutputPath.size())
  876. {
  877. hasone = true;
  878. std::string temp = m_LibraryOutputPath;
  879. temp += "$(INTDIR)";
  880. fout << this->ConvertToXMLOutputPath(temp.c_str()) << "," <<
  881. this->ConvertToXMLOutputPath(m_LibraryOutputPath.c_str());
  882. }
  883. if(m_ExecutableOutputPath.size())
  884. {
  885. hasone = true;
  886. std::string temp = m_ExecutableOutputPath;
  887. temp += "$(INTDIR)";
  888. fout << this->ConvertToXMLOutputPath(temp.c_str()) << "," <<
  889. this->ConvertToXMLOutputPath(m_ExecutableOutputPath.c_str());
  890. }
  891. std::set<std::string> pathEmitted;
  892. std::vector<std::string>::const_iterator i;
  893. const std::vector<std::string>& libdirs = tgt.GetLinkDirectories();
  894. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  895. {
  896. std::string lpath = *i;
  897. if(lpath[lpath.size()-1] != '/')
  898. {
  899. lpath += "/";
  900. }
  901. if(pathEmitted.insert(lpath).second)
  902. {
  903. if(hasone)
  904. {
  905. fout << ",";
  906. }
  907. std::string lpathi = lpath + "$(INTDIR)";
  908. fout << this->ConvertToXMLOutputPath(lpathi.c_str()) << "," << lpath;
  909. hasone = true;
  910. }
  911. }
  912. }
  913. void cmMSDotNETGenerator::OutputLibraries(std::ostream& fout,
  914. const char* configName,
  915. const char* libName,
  916. const cmTarget &target)
  917. {
  918. const cmTarget::LinkLibraries& libs = target.GetLinkLibraries();
  919. cmTarget::LinkLibraries::const_iterator j;
  920. for(j = libs.begin(); j != libs.end(); ++j)
  921. {
  922. if(j->first != libName)
  923. {
  924. std::string lib = j->first;
  925. if(j->first.find(".lib") == std::string::npos)
  926. {
  927. lib += ".lib";
  928. }
  929. lib = this->ConvertToXMLOutputPath(lib.c_str());
  930. if (j->second == cmTarget::GENERAL
  931. || (j->second == cmTarget::DEBUG && strcmp(configName, "DEBUG") == 0)
  932. || (j->second == cmTarget::OPTIMIZED && strcmp(configName, "DEBUG") != 0))
  933. {
  934. fout << lib << " ";
  935. }
  936. }
  937. }
  938. }
  939. void cmMSDotNETGenerator::OutputDefineFlags(std::ostream& fout)
  940. {
  941. std::string defs = m_Makefile->GetDefineFlags();
  942. std::string::size_type pos = defs.find("-D");
  943. bool done = pos == std::string::npos;
  944. if(!done)
  945. {
  946. fout << ",";
  947. }
  948. while(!done)
  949. {
  950. std::string::size_type nextpos = defs.find("-D", pos+2);
  951. std::string define;
  952. if(nextpos != std::string::npos)
  953. {
  954. define = defs.substr(pos+2, nextpos - pos -3);
  955. }
  956. else
  957. {
  958. define = defs.substr(pos+2);
  959. done = true;
  960. }
  961. fout << define << ",";
  962. if(!done)
  963. {
  964. pos = defs.find("-D", nextpos);
  965. }
  966. }
  967. }
  968. void cmMSDotNETGenerator::WriteVCProjFile(std::ostream& fout,
  969. const char *libName,
  970. cmTarget &target)
  971. {
  972. // We may be modifying the source groups temporarily, so make a copy.
  973. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  974. // get the classes from the source lists then add them to the groups
  975. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  976. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  977. i != classes.end(); i++)
  978. {
  979. // Add the file to the list of sources.
  980. std::string source = (*i)->GetFullPath();
  981. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  982. {
  983. m_ModuleDefinitionFile = (*i)->GetFullPath();
  984. }
  985. cmSourceGroup& sourceGroup = m_Makefile->FindSourceGroup(source.c_str(),
  986. sourceGroups);
  987. sourceGroup.AddSource(source.c_str(), *i);
  988. }
  989. // add any custom rules to the source groups
  990. for (std::vector<cmCustomCommand>::const_iterator cr =
  991. target.GetCustomCommands().begin();
  992. cr != target.GetCustomCommands().end(); ++cr)
  993. {
  994. cmSourceGroup& sourceGroup =
  995. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  996. sourceGroups);
  997. cmCustomCommand cc(*cr);
  998. cc.ExpandVariables(*m_Makefile);
  999. sourceGroup.AddCustomCommand(cc);
  1000. }
  1001. // open the project
  1002. this->WriteProjectStart(fout, libName, target, sourceGroups);
  1003. // write the configuration information
  1004. this->WriteConfigurations(fout, libName, target);
  1005. fout << "\t<Files>\n";
  1006. // Find the group in which the CMakeLists.txt source belongs, and add
  1007. // the rule to generate this VCProj file.
  1008. for(std::vector<cmSourceGroup>::reverse_iterator sg = sourceGroups.rbegin();
  1009. sg != sourceGroups.rend(); ++sg)
  1010. {
  1011. if(sg->Matches("CMakeLists.txt"))
  1012. {
  1013. this->AddVCProjBuildRule(*sg);
  1014. break;
  1015. }
  1016. }
  1017. // Loop through every source group.
  1018. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  1019. sg != sourceGroups.end(); ++sg)
  1020. {
  1021. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  1022. // If the group is empty, don't write it at all.
  1023. if(buildRules.empty())
  1024. { continue; }
  1025. // If the group has a name, write the header.
  1026. std::string name = sg->GetName();
  1027. if(name != "")
  1028. {
  1029. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  1030. }
  1031. // Loop through each build rule in the source group.
  1032. for(cmSourceGroup::BuildRules::const_iterator cc =
  1033. buildRules.begin(); cc != buildRules.end(); ++ cc)
  1034. {
  1035. std::string source = cc->first;
  1036. const cmSourceGroup::Commands& commands = cc->second.m_Commands;
  1037. const char* compileFlags = 0;
  1038. if(cc->second.m_SourceFile)
  1039. {
  1040. compileFlags = cc->second.m_SourceFile->GetCompileFlags();
  1041. }
  1042. if (source != libName || target.GetType() == cmTarget::UTILITY)
  1043. {
  1044. fout << "\t\t\t<File\n";
  1045. std::string d = cmSystemTools::ConvertToOutputPath(source.c_str());
  1046. // remove double quotes from the string
  1047. cmSystemTools::ReplaceString(d, "\"", "");
  1048. // Tell MS-Dev what the source is. If the compiler knows how to
  1049. // build it, then it will.
  1050. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  1051. if (!commands.empty())
  1052. {
  1053. cmSourceGroup::CommandFiles totalCommand;
  1054. std::string totalCommandStr;
  1055. totalCommandStr = this->CombineCommands(commands, totalCommand,
  1056. source.c_str());
  1057. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  1058. totalCommand.m_Depends,
  1059. totalCommand.m_Outputs, compileFlags);
  1060. }
  1061. else if(compileFlags)
  1062. {
  1063. for(std::vector<std::string>::iterator i
  1064. = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  1065. {
  1066. fout << "\t\t\t\t<FileConfiguration\n"
  1067. << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n"
  1068. << "\t\t\t\t\t<Tool\n"
  1069. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  1070. << "\t\t\t\t\tAdditionalOptions=\""
  1071. << compileFlags << "\"/>\n"
  1072. << "\t\t\t\t</FileConfiguration>\n";
  1073. }
  1074. }
  1075. fout << "\t\t\t</File>\n";
  1076. }
  1077. }
  1078. // If the group has a name, write the footer.
  1079. if(name != "")
  1080. {
  1081. this->WriteVCProjEndGroup(fout);
  1082. }
  1083. }
  1084. fout << "\t</Files>\n";
  1085. // Write the VCProj file's footer.
  1086. this->WriteVCProjFooter(fout);
  1087. }
  1088. void cmMSDotNETGenerator::WriteCustomRule(std::ostream& fout,
  1089. const char* source,
  1090. const char* command,
  1091. const std::set<std::string>& depends,
  1092. const std::set<std::string>& outputs,
  1093. const char* compileFlags)
  1094. {
  1095. std::string cmd = command;
  1096. cmSystemTools::ReplaceString(cmd, "\"", "&quot;");
  1097. std::vector<std::string>::iterator i;
  1098. for(i = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  1099. {
  1100. fout << "\t\t\t\t<FileConfiguration\n";
  1101. fout << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n";
  1102. if(compileFlags)
  1103. {
  1104. fout << "\t\t\t\t\t<Tool\n"
  1105. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  1106. << "\t\t\t\t\tAdditionalOptions=\""
  1107. << compileFlags << "\"/>\n";
  1108. }
  1109. fout << "\t\t\t\t\t<Tool\n"
  1110. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  1111. << "\t\t\t\t\tCommandLine=\"" << cmd << "\n\"\n"
  1112. << "\t\t\t\t\tAdditionalDependencies=\"";
  1113. // Write out the dependencies for the rule.
  1114. std::string temp;
  1115. for(std::set<std::string>::const_iterator d = depends.begin();
  1116. d != depends.end(); ++d)
  1117. {
  1118. fout << this->ConvertToXMLOutputPath(d->c_str())
  1119. << ";";
  1120. }
  1121. fout << "\"\n";
  1122. fout << "\t\t\t\t\tOutputs=\"";
  1123. if(outputs.size() == 0)
  1124. {
  1125. fout << source << "_force";
  1126. }
  1127. bool first = true;
  1128. // Write a rule for every output generated by this command.
  1129. for(std::set<std::string>::const_iterator output = outputs.begin();
  1130. output != outputs.end(); ++output)
  1131. {
  1132. if(!first)
  1133. {
  1134. fout << ";";
  1135. }
  1136. else
  1137. {
  1138. first = false;
  1139. }
  1140. fout << output->c_str();
  1141. }
  1142. fout << "\"/>\n";
  1143. fout << "\t\t\t\t</FileConfiguration>\n";
  1144. }
  1145. }
  1146. void cmMSDotNETGenerator::WriteVCProjBeginGroup(std::ostream& fout,
  1147. const char* group,
  1148. const char* )
  1149. {
  1150. fout << "\t\t<Filter\n"
  1151. << "\t\t\tName=\"" << group << "\"\n"
  1152. << "\t\t\tFilter=\"\">\n";
  1153. }
  1154. void cmMSDotNETGenerator::WriteVCProjEndGroup(std::ostream& fout)
  1155. {
  1156. fout << "\t\t</Filter>\n";
  1157. }
  1158. std::string
  1159. cmMSDotNETGenerator::CombineCommands(const cmSourceGroup::Commands &commands,
  1160. cmSourceGroup::CommandFiles &totalCommand,
  1161. const char *source)
  1162. {
  1163. // Loop through every custom command generating code from the
  1164. // current source.
  1165. // build up the depends and outputs and commands
  1166. std::string totalCommandStr = "";
  1167. std::string temp;
  1168. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  1169. c != commands.end(); ++c)
  1170. {
  1171. temp=
  1172. cmSystemTools::ConvertToOutputPath(c->second.m_Command.c_str());
  1173. totalCommandStr += temp;
  1174. totalCommandStr += " ";
  1175. totalCommandStr += c->second.m_Arguments;
  1176. totalCommand.Merge(c->second);
  1177. }
  1178. // Create a dummy file with the name of the source if it does
  1179. // not exist
  1180. if(totalCommand.m_Outputs.empty())
  1181. {
  1182. std::string dummyFile = m_Makefile->GetStartOutputDirectory();
  1183. dummyFile += "/";
  1184. dummyFile += source;
  1185. if(!cmSystemTools::FileExists(dummyFile.c_str()))
  1186. {
  1187. std::ofstream fout(dummyFile.c_str());
  1188. fout << "Dummy file created by cmake as unused source for utility command.\n";
  1189. }
  1190. }
  1191. return totalCommandStr;
  1192. }
  1193. // look for custom rules on a target and collect them together
  1194. void cmMSDotNETGenerator::OutputTargetRules(std::ostream& fout,
  1195. const cmTarget &target,
  1196. const char *libName)
  1197. {
  1198. if (target.GetType() >= cmTarget::UTILITY)
  1199. {
  1200. return;
  1201. }
  1202. // Find the group in which the lix exe custom rules belong
  1203. bool init = false;
  1204. for (std::vector<cmCustomCommand>::const_iterator cr =
  1205. target.GetCustomCommands().begin();
  1206. cr != target.GetCustomCommands().end(); ++cr)
  1207. {
  1208. cmCustomCommand cc(*cr);
  1209. cc.ExpandVariables(*m_Makefile);
  1210. if (cc.GetSourceName() == libName)
  1211. {
  1212. if(!init)
  1213. {
  1214. fout << "\nCommandLine=\"";
  1215. init = true;
  1216. }
  1217. std::string args = cc.GetArguments();
  1218. cmSystemTools::ReplaceString(args, "\"", "&quot;");
  1219. fout << this->ConvertToXMLOutputPath(cc.GetCommand().c_str()) << " " << args << "\n";
  1220. }
  1221. }
  1222. if (init)
  1223. {
  1224. fout << "\"";
  1225. }
  1226. }
  1227. void cmMSDotNETGenerator::WriteProjectStart(std::ostream& fout, const char *libName,
  1228. const cmTarget &,
  1229. std::vector<cmSourceGroup> &)
  1230. {
  1231. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  1232. << "<VisualStudioProject\n"
  1233. << "\tProjectType=\"Visual C++\"\n"
  1234. << "\tVersion=\"7.00\"\n"
  1235. << "\tName=\"" << libName << "\"\n"
  1236. << "\tSccProjectName=\"\"\n"
  1237. << "\tSccLocalPath=\"\"\n"
  1238. << "\tKeyword=\"Win32Proj\">\n"
  1239. << "\t<Platforms>\n"
  1240. << "\t\t<Platform\n\t\t\tName=\"Win32\"/>\n"
  1241. << "\t</Platforms>\n";
  1242. }
  1243. void cmMSDotNETGenerator::WriteVCProjFooter(std::ostream& fout)
  1244. {
  1245. fout << "\t<Globals>\n"
  1246. << "\t</Globals>\n"
  1247. << "</VisualStudioProject>\n";
  1248. }
  1249. std::string cmMSDotNETGenerator::ConvertToXMLOutputPath(const char* path)
  1250. {
  1251. std::string ret = cmSystemTools::ConvertToOutputPath(path);
  1252. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1253. return ret;
  1254. }
  1255. std::string cmMSDotNETGenerator::ConvertToXMLOutputPathSingle(const char* path)
  1256. {
  1257. std::string ret = cmSystemTools::ConvertToOutputPath(path);
  1258. cmSystemTools::ReplaceString(ret, "\"", "");
  1259. return ret;
  1260. }