cmGlobalVisualStudio7Generator.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 "cmGlobalVisualStudio7Generator.h"
  14. #include "cmLocalVisualStudio7Generator.h"
  15. #include "cmMakefile.h"
  16. #include "cmake.h"
  17. #include "windows.h"
  18. void cmGlobalVisualStudio7Generator::EnableLanguage(const char*,
  19. cmMakefile *mf)
  20. {
  21. // now load the settings
  22. if(!mf->GetDefinition("CMAKE_ROOT"))
  23. {
  24. cmSystemTools::Error(
  25. "CMAKE_ROOT has not been defined, bad GUI or driver program");
  26. return;
  27. }
  28. if(!this->GetLanguageEnabled("CXX"))
  29. {
  30. std::string fpath =
  31. mf->GetDefinition("CMAKE_ROOT");
  32. fpath += "/Templates/CMakeDotNetSystemConfig.cmake";
  33. mf->ReadListFile(NULL,fpath.c_str());
  34. this->SetLanguageEnabled("CXX");
  35. }
  36. }
  37. int cmGlobalVisualStudio7Generator::TryCompile(const char *,
  38. const char *bindir,
  39. const char *projectName,
  40. const char *targetName,
  41. std::string *output)
  42. {
  43. // now build the test
  44. std::string makeCommand =
  45. m_CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  46. if(makeCommand.size() == 0)
  47. {
  48. cmSystemTools::Error(
  49. "Generator cannot find the appropriate make command.");
  50. return 1;
  51. }
  52. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  53. std::string lowerCaseCommand = makeCommand;
  54. cmSystemTools::LowerCase(lowerCaseCommand);
  55. /**
  56. * Run an executable command and put the stdout in output.
  57. */
  58. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  59. cmSystemTools::ChangeDirectory(bindir);
  60. // if there are spaces in the makeCommand, assume a full path
  61. // and convert it to a path with no spaces in it as the
  62. // RunCommand does not like spaces
  63. #if defined(_WIN32) && !defined(__CYGWIN__)
  64. if(makeCommand.find(' ') != std::string::npos)
  65. {
  66. cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
  67. }
  68. #endif
  69. makeCommand += " ";
  70. makeCommand += projectName;
  71. makeCommand += ".sln /build Debug /project ";
  72. if (targetName)
  73. {
  74. makeCommand += targetName;
  75. }
  76. else
  77. {
  78. makeCommand += "ALL_BUILD";
  79. }
  80. int retVal;
  81. if (!cmSystemTools::RunCommand(makeCommand.c_str(), *output, retVal,
  82. 0, false))
  83. {
  84. cmSystemTools::Error("Generator: execution of devenv failed.");
  85. // return to the original directory
  86. cmSystemTools::ChangeDirectory(cwd.c_str());
  87. return 1;
  88. }
  89. cmSystemTools::ChangeDirectory(cwd.c_str());
  90. return retVal;
  91. }
  92. ///! Create a local generator appropriate to this Global Generator
  93. cmLocalGenerator *cmGlobalVisualStudio7Generator::CreateLocalGenerator()
  94. {
  95. cmLocalGenerator *lg = new cmLocalVisualStudio7Generator;
  96. lg->SetGlobalGenerator(this);
  97. return lg;
  98. }
  99. void cmGlobalVisualStudio7Generator::SetupTests()
  100. {
  101. std::string ctest =
  102. m_LocalGenerators[0]->GetMakefile()->GetDefinition("CMAKE_COMMAND");
  103. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  104. ctest += "/";
  105. ctest += "ctest";
  106. ctest += cmSystemTools::GetExecutableExtension();
  107. if(!cmSystemTools::FileExists(ctest.c_str()))
  108. {
  109. ctest =
  110. m_LocalGenerators[0]->GetMakefile()->GetDefinition("CMAKE_COMMAND");
  111. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  112. ctest += "/Debug/";
  113. ctest += "ctest";
  114. ctest += cmSystemTools::GetExecutableExtension();
  115. }
  116. if(!cmSystemTools::FileExists(ctest.c_str()))
  117. {
  118. ctest =
  119. m_LocalGenerators[0]->GetMakefile()->GetDefinition("CMAKE_COMMAND");
  120. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  121. ctest += "/Release/";
  122. ctest += "ctest";
  123. ctest += cmSystemTools::GetExecutableExtension();
  124. }
  125. // if we found ctest
  126. if (cmSystemTools::FileExists(ctest.c_str()))
  127. {
  128. // Create a full path filename for output Testfile
  129. std::string fname;
  130. fname = m_CMakeInstance->GetStartOutputDirectory();
  131. fname += "/";
  132. fname += "DartTestfile.txt";
  133. // If the file doesn't exist, then ENABLE_TESTING hasn't been run
  134. if (cmSystemTools::FileExists(fname.c_str()))
  135. {
  136. m_LocalGenerators[0]->GetMakefile()->
  137. AddUtilityCommand("RUN_TESTS", ctest.c_str(), "-D $(IntDir)",false);
  138. }
  139. }
  140. }
  141. void cmGlobalVisualStudio7Generator::GenerateConfigurations()
  142. {
  143. // process the configurations
  144. std::string configTypes =
  145. m_CMakeInstance->GetCacheDefinition("CMAKE_CONFIGURATION_TYPES");
  146. std::string::size_type start = 0;
  147. std::string::size_type endpos = 0;
  148. while(endpos != std::string::npos)
  149. {
  150. endpos = configTypes.find(' ', start);
  151. std::string config;
  152. std::string::size_type len;
  153. if(endpos != std::string::npos)
  154. {
  155. len = endpos - start;
  156. }
  157. else
  158. {
  159. len = configTypes.size() - start;
  160. }
  161. config = configTypes.substr(start, len);
  162. if(config == "Debug" || config == "Release" ||
  163. config == "MinSizeRel" || config == "RelWithDebInfo")
  164. {
  165. // only add unique configurations
  166. if(std::find(m_Configurations.begin(),
  167. m_Configurations.end(), config) == m_Configurations.end())
  168. {
  169. m_Configurations.push_back(config);
  170. }
  171. }
  172. else
  173. {
  174. cmSystemTools::Error(
  175. "Invalid configuration type in CMAKE_CONFIGURATION_TYPES: ",
  176. config.c_str(),
  177. " (Valid types are Debug,Release,MinSizeRel,RelWithDebInfo)");
  178. }
  179. start = endpos+1;
  180. }
  181. if(m_Configurations.size() == 0)
  182. {
  183. m_Configurations.push_back("Debug");
  184. m_Configurations.push_back("Release");
  185. }
  186. }
  187. void cmGlobalVisualStudio7Generator::Generate()
  188. {
  189. // Generate the possible configuraitons
  190. this->GenerateConfigurations();
  191. // add a special target that depends on ALL projects for easy build
  192. // of Debug only
  193. m_LocalGenerators[0]->GetMakefile()->
  194. AddUtilityCommand("ALL_BUILD", "echo","\"Build all projects\"",false);
  195. // add the Run Tests command
  196. this->SetupTests();
  197. // first do the superclass method
  198. this->cmGlobalGenerator::Generate();
  199. // Now write out the DSW
  200. this->OutputSLNFile();
  201. }
  202. // output the SLN file
  203. void cmGlobalVisualStudio7Generator::OutputSLNFile()
  204. {
  205. // if this is an out of source build, create the output directory
  206. if(strcmp(m_CMakeInstance->GetStartOutputDirectory(),
  207. m_CMakeInstance->GetHomeDirectory()) != 0)
  208. {
  209. if(!cmSystemTools::MakeDirectory(m_CMakeInstance->GetStartOutputDirectory()))
  210. {
  211. cmSystemTools::Error("Error creating output directory for SLN file",
  212. m_CMakeInstance->GetStartOutputDirectory());
  213. }
  214. }
  215. // create the dsw file name
  216. std::string fname;
  217. fname = m_CMakeInstance->GetStartOutputDirectory();
  218. fname += "/";
  219. if(strlen(m_LocalGenerators[0]->GetMakefile()->GetProjectName()) == 0)
  220. {
  221. m_LocalGenerators[0]->GetMakefile()->SetProjectName("Project");
  222. }
  223. fname += m_LocalGenerators[0]->GetMakefile()->GetProjectName();
  224. fname += ".sln";
  225. std::ofstream fout(fname.c_str());
  226. if(!fout)
  227. {
  228. cmSystemTools::Error("Error can not open SLN file for write: "
  229. ,fname.c_str());
  230. return;
  231. }
  232. this->WriteSLNFile(fout);
  233. }
  234. // Write a SLN file to the stream
  235. void cmGlobalVisualStudio7Generator::WriteSLNFile(std::ostream& fout)
  236. {
  237. // Write out the header for a SLN file
  238. this->WriteSLNHeader(fout);
  239. // Get the home directory with the trailing slash
  240. std::string homedir = m_CMakeInstance->GetHomeDirectory();
  241. homedir += "/";
  242. // For each cmMakefile, create a VCProj for it, and
  243. // add it to this SLN file
  244. unsigned int i;
  245. for(i = 0; i < m_LocalGenerators.size(); ++i)
  246. {
  247. cmMakefile* mf = m_LocalGenerators[i]->GetMakefile();
  248. // Get the source directory from the makefile
  249. std::string dir = mf->GetStartDirectory();
  250. // remove the home directory and / from the source directory
  251. // this gives a relative path
  252. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  253. // Get the list of create dsp files names from the cmVCProjWriter, more
  254. // than one dsp could have been created per input CMakeLists.txt file
  255. // for each target
  256. std::vector<std::string> dspnames =
  257. static_cast<cmLocalVisualStudio7Generator *>(m_LocalGenerators[i])
  258. ->GetCreatedProjectNames();
  259. cmTargets &tgts = m_LocalGenerators[i]->GetMakefile()->GetTargets();
  260. cmTargets::iterator l = tgts.begin();
  261. for(std::vector<std::string>::iterator si = dspnames.begin();
  262. l != tgts.end(); ++l)
  263. {
  264. // special handling for the current makefile
  265. if(mf == m_LocalGenerators[0]->GetMakefile())
  266. {
  267. dir = "."; // no subdirectory for project generated
  268. // if this is the special ALL_BUILD utility, then
  269. // make it depend on every other non UTILITY project.
  270. // This is done by adding the names to the GetUtilities
  271. // vector on the makefile
  272. if(l->first == "ALL_BUILD")
  273. {
  274. unsigned int j;
  275. for(j = 0; j < m_LocalGenerators.size(); ++j)
  276. {
  277. const cmTargets &atgts =
  278. m_LocalGenerators[j]->GetMakefile()->GetTargets();
  279. for(cmTargets::const_iterator al = atgts.begin();
  280. al != atgts.end(); ++al)
  281. {
  282. if (al->second.IsInAll())
  283. {
  284. if (al->second.GetType() == cmTarget::UTILITY)
  285. {
  286. l->second.AddUtility(al->first.c_str());
  287. }
  288. else
  289. {
  290. l->second.AddLinkLibrary(al->first,cmTarget::GENERAL);
  291. }
  292. }
  293. }
  294. }
  295. }
  296. }
  297. // Write the project into the SLN file
  298. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  299. {
  300. cmCustomCommand cc = l->second.GetCustomCommands()[0];
  301. // dodgy use of the cmCustomCommand's members to store the
  302. // arguments from the INCLUDE_EXTERNAL_MSPROJECT command
  303. std::vector<std::string> stuff = cc.GetDepends();
  304. std::vector<std::string> depends = cc.GetOutputs();
  305. this->WriteExternalProject(fout, stuff[0].c_str(),
  306. stuff[1].c_str(), depends);
  307. ++si;
  308. }
  309. else
  310. {
  311. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  312. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  313. {
  314. this->WriteProject(fout, si->c_str(), dir.c_str(),l->second);
  315. ++si;
  316. }
  317. }
  318. }
  319. }
  320. fout << "Global\n"
  321. << "\tGlobalSection(SolutionConfiguration) = preSolution\n";
  322. int c = 0;
  323. for(std::vector<std::string>::iterator i = m_Configurations.begin();
  324. i != m_Configurations.end(); ++i)
  325. {
  326. fout << "\t\tConfigName." << c << " = " << *i << "\n";
  327. c++;
  328. }
  329. fout << "\tEndGlobalSection\n"
  330. << "\tGlobalSection(ProjectDependencies) = postSolution\n";
  331. // loop over again and compute the depends
  332. for(i = 0; i < m_LocalGenerators.size(); ++i)
  333. {
  334. cmMakefile* mf = m_LocalGenerators[i]->GetMakefile();
  335. cmLocalVisualStudio7Generator* pg =
  336. static_cast<cmLocalVisualStudio7Generator*>(m_LocalGenerators[i]);
  337. // Get the list of create dsp files names from the cmVCProjWriter, more
  338. // than one dsp could have been created per input CMakeLists.txt file
  339. // for each target
  340. std::vector<std::string> dspnames =
  341. pg->GetCreatedProjectNames();
  342. cmTargets &tgts = pg->GetMakefile()->GetTargets();
  343. cmTargets::iterator l = tgts.begin();
  344. std::string dir = mf->GetStartDirectory();
  345. for(std::vector<std::string>::iterator si = dspnames.begin();
  346. l != tgts.end(); ++l)
  347. {
  348. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  349. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  350. {
  351. this->WriteProjectDepends(fout, si->c_str(), dir.c_str(),l->second);
  352. ++si;
  353. }
  354. }
  355. }
  356. fout << "\tEndGlobalSection\n";
  357. fout << "\tGlobalSection(ProjectConfiguration) = postSolution\n";
  358. // loop over again and compute the depends
  359. for(i = 0; i < m_LocalGenerators.size(); ++i)
  360. {
  361. cmMakefile* mf = m_LocalGenerators[i]->GetMakefile();
  362. cmLocalVisualStudio7Generator* pg =
  363. static_cast<cmLocalVisualStudio7Generator*>(m_LocalGenerators[i]);
  364. // Get the list of create dsp files names from the cmVCProjWriter, more
  365. // than one dsp could have been created per input CMakeLists.txt file
  366. // for each target
  367. std::vector<std::string> dspnames =
  368. pg->GetCreatedProjectNames();
  369. cmTargets &tgts = pg->GetMakefile()->GetTargets();
  370. cmTargets::iterator l = tgts.begin();
  371. std::string dir = mf->GetStartDirectory();
  372. for(std::vector<std::string>::iterator si = dspnames.begin();
  373. l != tgts.end(); ++l)
  374. {
  375. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  376. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  377. {
  378. this->WriteProjectConfigurations(fout, si->c_str());
  379. ++si;
  380. }
  381. }
  382. }
  383. fout << "\tEndGlobalSection\n";
  384. // Write the footer for the SLN file
  385. this->WriteSLNFooter(fout);
  386. }
  387. // Write a dsp file into the SLN file,
  388. // Note, that dependencies from executables to
  389. // the libraries it uses are also done here
  390. void cmGlobalVisualStudio7Generator::WriteProject(std::ostream& fout,
  391. const char* dspname,
  392. const char* dir,
  393. const cmTarget&)
  394. {
  395. std::string d = cmSystemTools::ConvertToOutputPath(dir);
  396. fout << "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\" = \""
  397. << dspname << "\", \""
  398. << d << "\\" << dspname << ".vcproj\", \"{"
  399. << this->CreateGUID(dspname) << "}\"\nEndProject\n";
  400. }
  401. // Write a dsp file into the SLN file,
  402. // Note, that dependencies from executables to
  403. // the libraries it uses are also done here
  404. void cmGlobalVisualStudio7Generator::WriteProjectDepends(std::ostream& fout,
  405. const char* dspname,
  406. const char* ,
  407. const cmTarget& target
  408. )
  409. {
  410. int depcount = 0;
  411. // insert Begin Project Dependency Project_Dep_Name project stuff here
  412. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  413. {
  414. cmTarget::LinkLibraries::const_iterator j, jend;
  415. j = target.GetLinkLibraries().begin();
  416. jend = target.GetLinkLibraries().end();
  417. for(;j!= jend; ++j)
  418. {
  419. if(j->first != dspname)
  420. {
  421. // is the library part of this SLN ? If so add dependency
  422. std::string libPath = j->first + "_CMAKE_PATH";
  423. const char* cacheValue
  424. = m_CMakeInstance->GetCacheDefinition(libPath.c_str());
  425. if(cacheValue)
  426. {
  427. fout << "\t\t{" << this->CreateGUID(dspname) << "}." << depcount << " = {"
  428. << this->CreateGUID(j->first.c_str()) << "}\n";
  429. depcount++;
  430. }
  431. }
  432. }
  433. }
  434. std::set<cmStdString>::const_iterator i, end;
  435. // write utility dependencies.
  436. i = target.GetUtilities().begin();
  437. end = target.GetUtilities().end();
  438. for(;i!= end; ++i)
  439. {
  440. if(*i != dspname)
  441. {
  442. fout << "\t\t{" << this->CreateGUID(dspname) << "}." << depcount << " = {"
  443. << this->CreateGUID(i->c_str()) << "}\n";
  444. depcount++;
  445. }
  446. }
  447. }
  448. // Write a dsp file into the SLN file,
  449. // Note, that dependencies from executables to
  450. // the libraries it uses are also done here
  451. void
  452. cmGlobalVisualStudio7Generator::WriteProjectConfigurations(std::ostream& fout,
  453. const char* name)
  454. {
  455. std::string guid = this->CreateGUID(name);
  456. for(std::vector<std::string>::iterator i = m_Configurations.begin();
  457. i != m_Configurations.end(); ++i)
  458. {
  459. fout << "\t\t{" << guid << "}." << *i << ".ActiveCfg = " << *i << "|Win32\n"
  460. << "\t\t{" << guid << "}." << *i << ".Build.0 = " << *i << "|Win32\n";
  461. }
  462. }
  463. // Write a dsp file into the SLN file,
  464. // Note, that dependencies from executables to
  465. // the libraries it uses are also done here
  466. void cmGlobalVisualStudio7Generator::WriteExternalProject(std::ostream& ,
  467. const char* ,
  468. const char* ,
  469. const std::vector<std::string>& )
  470. {
  471. cmSystemTools::Error("WriteExternalProject not implemented");
  472. // fout << "#########################################################"
  473. // "######################\n\n";
  474. // fout << "Project: \"" << name << "\"="
  475. // << location << " - Package Owner=<4>\n\n";
  476. // fout << "Package=<5>\n{{{\n}}}\n\n";
  477. // fout << "Package=<4>\n";
  478. // fout << "{{{\n";
  479. // std::vector<std::string>::const_iterator i, end;
  480. // // write dependencies.
  481. // i = dependencies.begin();
  482. // end = dependencies.end();
  483. // for(;i!= end; ++i)
  484. // {
  485. // fout << "Begin Project Dependency\n";
  486. // fout << "Project_Dep_Name " << *i << "\n";
  487. // fout << "End Project Dependency\n";
  488. // }
  489. // fout << "}}}\n\n";
  490. }
  491. // Standard end of dsw file
  492. void cmGlobalVisualStudio7Generator::WriteSLNFooter(std::ostream& fout)
  493. {
  494. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  495. << "\tEndGlobalSection\n"
  496. << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  497. << "\tEndGlobalSection\n"
  498. << "EndGlobal\n";
  499. }
  500. // ouput standard header for dsw file
  501. void cmGlobalVisualStudio7Generator::WriteSLNHeader(std::ostream& fout)
  502. {
  503. fout << "Microsoft Visual Studio Solution File, Format Version 7.00\n";
  504. }
  505. std::string cmGlobalVisualStudio7Generator::CreateGUID(const char* name)
  506. {
  507. std::map<cmStdString, cmStdString>::iterator i = m_GUIDMap.find(name);
  508. if(i != m_GUIDMap.end())
  509. {
  510. return i->second;
  511. }
  512. std::string ret;
  513. UUID uid;
  514. unsigned char *uidstr;
  515. UuidCreate(&uid);
  516. UuidToString(&uid,&uidstr);
  517. ret = reinterpret_cast<char*>(uidstr);
  518. RpcStringFree(&uidstr);
  519. ret = cmSystemTools::UpperCase(ret);
  520. m_GUIDMap[name] = ret;
  521. return ret;
  522. }
  523. void cmGlobalVisualStudio7Generator::LocalGenerate()
  524. {
  525. // load the possible configuraitons
  526. this->GenerateConfigurations();
  527. this->cmGlobalGenerator::LocalGenerate();
  528. }
  529. std::vector<std::string> *cmGlobalVisualStudio7Generator::GetConfigurations()
  530. {
  531. return &m_Configurations;
  532. };