cmMSDotNETGenerator.cxx 44 KB

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