cmGlobalVisualStudio7Generator.cxx 19 KB

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