cmMSDotNETGenerator.cxx 43 KB

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