cmGlobalVisualStudio7Generator.cxx 18 KB

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