cmGlobalVisualStudio7Generator.cxx 18 KB

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