cmGlobalVisualStudio7Generator.cxx 19 KB

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