cmGlobalVisualStudio7Generator.cxx 18 KB

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