cmExtraCodeBlocksGenerator.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. Copyright (c) 2004 Alexander Neundorf [email protected], All rights reserved.
  9. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  10. This software is distributed WITHOUT ANY WARRANTY; without even
  11. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. PURPOSE. See the above copyright notices for more information.
  13. =========================================================================*/
  14. #include "cmExtraCodeBlocksGenerator.h"
  15. #include "cmGlobalUnixMakefileGenerator3.h"
  16. #include "cmLocalUnixMakefileGenerator3.h"
  17. #include "cmMakefile.h"
  18. #include "cmake.h"
  19. #include "cmSourceFile.h"
  20. #include "cmGeneratedFileStream.h"
  21. #include "cmTarget.h"
  22. #include "cmSystemTools.h"
  23. #include <cmsys/SystemTools.hxx>
  24. /* Some useful URLs:
  25. Homepage:
  26. http://www.codeblocks.org
  27. File format docs:
  28. http://wiki.codeblocks.org/index.php?title=File_formats_description
  29. http://wiki.codeblocks.org/index.php?title=Workspace_file
  30. http://wiki.codeblocks.org/index.php?title=Project_file
  31. Discussion:
  32. http://forums.codeblocks.org/index.php/topic,6789.0.html
  33. */
  34. //----------------------------------------------------------------------------
  35. void cmExtraCodeBlocksGenerator
  36. ::GetDocumentation(cmDocumentationEntry& entry, const char*) const
  37. {
  38. entry.Name = this->GetName();
  39. entry.Brief = "Generates CodeBlocks project files.";
  40. entry.Full =
  41. "Project files for CodeBlocks will be created in the top directory "
  42. "and in every subdirectory which features a CMakeLists.txt file "
  43. "containing a PROJECT() call. "
  44. "Additionally a hierarchy of makefiles is generated into the "
  45. "build tree. The appropriate make program can build the project through "
  46. "the default make target. A \"make install\" target is also provided.";
  47. }
  48. cmExtraCodeBlocksGenerator::cmExtraCodeBlocksGenerator()
  49. :cmExternalMakefileProjectGenerator()
  50. {
  51. #if defined(_WIN32)
  52. this->SupportedGlobalGenerators.push_back("MinGW Makefiles");
  53. // disable until somebody actually tests it:
  54. // this->SupportedGlobalGenerators.push_back("NMake Makefiles");
  55. // this->SupportedGlobalGenerators.push_back("MSYS Makefiles");
  56. #endif
  57. this->SupportedGlobalGenerators.push_back("Unix Makefiles");
  58. }
  59. void cmExtraCodeBlocksGenerator::SetGlobalGenerator(
  60. cmGlobalGenerator* generator)
  61. {
  62. cmExternalMakefileProjectGenerator::SetGlobalGenerator(generator);
  63. cmGlobalUnixMakefileGenerator3* mf = (cmGlobalUnixMakefileGenerator3*)
  64. generator;
  65. mf->SetToolSupportsColor(false);
  66. mf->SetForceVerboseMakefiles(true);
  67. }
  68. void cmExtraCodeBlocksGenerator::Generate()
  69. {
  70. // for each sub project in the project create a codeblocks project
  71. for (std::map<cmStdString, std::vector<cmLocalGenerator*> >::const_iterator
  72. it = this->GlobalGenerator->GetProjectMap().begin();
  73. it!= this->GlobalGenerator->GetProjectMap().end();
  74. ++it)
  75. {
  76. // create a project file
  77. this->CreateProjectFile(it->second);
  78. }
  79. }
  80. /* create the project file, if it already exists, merge it with the
  81. existing one, otherwise create a new one */
  82. void cmExtraCodeBlocksGenerator::CreateProjectFile(
  83. const std::vector<cmLocalGenerator*>& lgs)
  84. {
  85. const cmMakefile* mf=lgs[0]->GetMakefile();
  86. std::string outputDir=mf->GetStartOutputDirectory();
  87. std::string projectDir=mf->GetHomeDirectory();
  88. std::string projectName=mf->GetProjectName();
  89. std::string filename=outputDir+"/";
  90. filename+=projectName+".cbp";
  91. std::string sessionFilename=outputDir+"/";
  92. sessionFilename+=projectName+".layout";
  93. /* if (cmSystemTools::FileExists(filename.c_str()))
  94. {
  95. this->MergeProjectFiles(outputDir, projectDir, filename,
  96. cmakeFilePattern, sessionFilename);
  97. }
  98. else */
  99. {
  100. this->CreateNewProjectFile(lgs, filename);
  101. }
  102. }
  103. void cmExtraCodeBlocksGenerator
  104. ::CreateNewProjectFile(const std::vector<cmLocalGenerator*>& lgs,
  105. const std::string& filename)
  106. {
  107. const cmMakefile* mf=lgs[0]->GetMakefile();
  108. cmGeneratedFileStream fout(filename.c_str());
  109. if(!fout)
  110. {
  111. return;
  112. }
  113. // figure out the compiler
  114. std::string compiler = this->GetCBCompilerId(mf);
  115. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  116. fout<<"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n"
  117. "<CodeBlocks_project_file>\n"
  118. " <FileVersion major=\"1\" minor=\"6\" />\n"
  119. " <Project>\n"
  120. " <Option title=\"" << mf->GetProjectName()<<"\" />\n"
  121. " <Option makefile_is_custom=\"1\" />\n"
  122. " <Option compiler=\"" << compiler << "\" />\n"
  123. " <Build>\n";
  124. bool installTargetCreated = false;
  125. bool installStripTargetCreated = false;
  126. bool testTargetCreated = false;
  127. bool experimentalTargetCreated = false;
  128. bool nightlyTargetCreated = false;
  129. bool packageTargetCreated = false;
  130. bool packageSourceTargetCreated = false;
  131. bool rebuildCacheTargetCreated = false;
  132. this->AppendTarget(fout, "all", 0, make.c_str(), mf, compiler.c_str());
  133. // add all executable and library targets and some of the GLOBAL
  134. // and UTILITY targets
  135. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  136. lg!=lgs.end(); lg++)
  137. {
  138. cmMakefile* makefile=(*lg)->GetMakefile();
  139. cmTargets& targets=makefile->GetTargets();
  140. for (cmTargets::iterator ti = targets.begin();
  141. ti != targets.end(); ti++)
  142. {
  143. switch(ti->second.GetType())
  144. {
  145. case cmTarget::UTILITY:
  146. case cmTarget::GLOBAL_TARGET:
  147. // only add these targets once
  148. if ((ti->first=="install") && (installTargetCreated==false))
  149. {
  150. installTargetCreated=true;
  151. }
  152. else if ((ti->first=="install/strip")
  153. && (installStripTargetCreated==false))
  154. {
  155. installStripTargetCreated=true;
  156. }
  157. else if ((ti->first=="test") && (testTargetCreated==false))
  158. {
  159. testTargetCreated=true;
  160. }
  161. else if ((ti->first=="Experimental")
  162. && (experimentalTargetCreated==false))
  163. {
  164. experimentalTargetCreated=true;
  165. }
  166. else if ((ti->first=="Nightly") && (nightlyTargetCreated==false))
  167. {
  168. nightlyTargetCreated=true;
  169. }
  170. else if ((ti->first=="package") && (packageTargetCreated==false))
  171. {
  172. packageTargetCreated=true;
  173. }
  174. else if ((ti->first=="package_source")
  175. && (packageSourceTargetCreated==false))
  176. {
  177. packageSourceTargetCreated=true;
  178. }
  179. else if ((ti->first=="rebuild_cache")
  180. && (rebuildCacheTargetCreated==false))
  181. {
  182. rebuildCacheTargetCreated=true;
  183. }
  184. else
  185. {
  186. break;
  187. }
  188. this->AppendTarget(fout, ti->first.c_str(), 0,
  189. make.c_str(), makefile, compiler.c_str());
  190. break;
  191. case cmTarget::EXECUTABLE:
  192. case cmTarget::STATIC_LIBRARY:
  193. case cmTarget::SHARED_LIBRARY:
  194. case cmTarget::MODULE_LIBRARY:
  195. {
  196. this->AppendTarget(fout, ti->first.c_str(), &ti->second,
  197. make.c_str(), makefile, compiler.c_str());
  198. std::string fastTarget = ti->first;
  199. fastTarget += "/fast";
  200. this->AppendTarget(fout, fastTarget.c_str(), &ti->second,
  201. make.c_str(), makefile, compiler.c_str());
  202. }
  203. break;
  204. // ignore these:
  205. case cmTarget::INSTALL_FILES:
  206. case cmTarget::INSTALL_PROGRAMS:
  207. case cmTarget::INSTALL_DIRECTORY:
  208. default:
  209. break;
  210. }
  211. }
  212. }
  213. fout<<" </Build>\n";
  214. // Collect all used source files in the project
  215. std::map<std::string, std::string> sourceFiles;
  216. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  217. lg!=lgs.end(); lg++)
  218. {
  219. cmMakefile* makefile=(*lg)->GetMakefile();
  220. cmTargets& targets=makefile->GetTargets();
  221. for (cmTargets::iterator ti = targets.begin();
  222. ti != targets.end(); ti++)
  223. {
  224. switch(ti->second.GetType())
  225. {
  226. case cmTarget::EXECUTABLE:
  227. case cmTarget::STATIC_LIBRARY:
  228. case cmTarget::SHARED_LIBRARY:
  229. case cmTarget::MODULE_LIBRARY:
  230. {
  231. const std::vector<cmSourceFile*>&sources=ti->second.GetSourceFiles();
  232. for (std::vector<cmSourceFile*>::const_iterator si=sources.begin();
  233. si!=sources.end(); si++)
  234. {
  235. sourceFiles[(*si)->GetFullPath()] = ti->first;
  236. }
  237. }
  238. default: // intended fallthrough
  239. break;
  240. }
  241. }
  242. }
  243. // insert all used source files in the CodeBlocks project
  244. for (std::map<std::string, std::string>::const_iterator
  245. sit=sourceFiles.begin();
  246. sit!=sourceFiles.end();
  247. ++sit)
  248. {
  249. fout<<" <Unit filename=\""<<sit->first <<"\">\n"
  250. " </Unit>\n";
  251. }
  252. fout<<" </Project>\n"
  253. "</CodeBlocks_project_file>\n";
  254. }
  255. // Generate the xml code for one target.
  256. void cmExtraCodeBlocksGenerator::AppendTarget(cmGeneratedFileStream& fout,
  257. const char* targetName,
  258. cmTarget* target,
  259. const char* make,
  260. const cmMakefile* makefile,
  261. const char* compiler)
  262. {
  263. std::string makefileName = makefile->GetStartOutputDirectory();
  264. makefileName += "/Makefile";
  265. makefileName = cmSystemTools::ConvertToOutputPath(makefileName.c_str());
  266. fout<<" <Target title=\"" << targetName << "\">\n";
  267. if (target!=0)
  268. {
  269. int cbTargetType = this->GetCBTargetType(target);
  270. fout<<" <Option output=\"" << target->GetLocation(0)
  271. << "\" prefix_auto=\"0\" extension_auto=\"0\" />\n"
  272. " <Option working_dir=\""
  273. << makefile->GetStartOutputDirectory() << "\" />\n"
  274. " <Option object_output=\"./\" />\n"
  275. " <Option type=\"" << cbTargetType << "\" />\n"
  276. " <Option compiler=\"" << compiler << "\" />\n"
  277. " <Compiler>\n";
  278. // the include directories for this target
  279. const std::vector<std::string>& incDirs =
  280. target->GetMakefile()->GetIncludeDirectories();
  281. for(std::vector<std::string>::const_iterator dirIt=incDirs.begin();
  282. dirIt != incDirs.end();
  283. ++dirIt)
  284. {
  285. fout <<" <Add directory=\"" << dirIt->c_str() << "\" />\n";
  286. }
  287. fout<<" </Compiler>\n";
  288. }
  289. else // e.g. all and the GLOBAL and UTILITY targets
  290. {
  291. fout<<" <Option working_dir=\""
  292. << makefile->GetStartOutputDirectory() << "\" />\n"
  293. <<" <Option type=\"" << 4 << "\" />\n";
  294. }
  295. fout<<" <MakeCommands>\n"
  296. " <Build command=\""
  297. << this->BuildMakeCommand(make, makefileName.c_str(), targetName)
  298. << "\" />\n"
  299. " <CompileFile command=\""
  300. << this->BuildMakeCommand(make, makefileName.c_str(),"&quot;$file&quot;")
  301. << "\" />\n"
  302. " <Clean command=\""
  303. << this->BuildMakeCommand(make, makefileName.c_str(), "clean")
  304. << "\" />\n"
  305. " <DistClean command=\""
  306. << this->BuildMakeCommand(make, makefileName.c_str(), "clean")
  307. << "\" />\n"
  308. " </MakeCommands>\n"
  309. " </Target>\n";
  310. }
  311. // Translate the cmake compiler id into the CodeBlocks compiler id
  312. std::string cmExtraCodeBlocksGenerator::GetCBCompilerId(const cmMakefile* mf)
  313. {
  314. // figure out which language to use
  315. // for now care only for C and C++
  316. std::string compilerIdVar = "CMAKE_CXX_COMPILER_ID";
  317. if (this->GlobalGenerator->GetLanguageEnabled("CXX") == false)
  318. {
  319. compilerIdVar = "CMAKE_C_COMPILER_ID";
  320. }
  321. std::string hostSystemName = mf->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
  322. std::string systemName = mf->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  323. std::string compilerId = mf->GetRequiredDefinition(compilerIdVar.c_str());
  324. std::string compiler = "gcc";
  325. if (compilerId == "MSVC")
  326. {
  327. compiler = "msvc";
  328. }
  329. else if (compilerId == "Borland")
  330. {
  331. compiler = "bcc";
  332. }
  333. else if (compilerId == "SDCC")
  334. {
  335. compiler = "sdcc";
  336. }
  337. else if (compilerId == "Intel")
  338. {
  339. compiler = "icc";
  340. }
  341. else if (compilerId == "Watcom")
  342. {
  343. compiler = "ow";
  344. }
  345. else if (compilerId == "GNU")
  346. {
  347. compiler = "gcc";
  348. }
  349. return compiler;
  350. }
  351. // Translate the cmake target type into the CodeBlocks target type id
  352. int cmExtraCodeBlocksGenerator::GetCBTargetType(cmTarget* target)
  353. {
  354. if ( target->GetType()==cmTarget::EXECUTABLE)
  355. {
  356. if ((target->GetPropertyAsBool("WIN32_EXECUTABLE"))
  357. || (target->GetPropertyAsBool("MACOSX_BUNDLE")))
  358. {
  359. return 0;
  360. }
  361. else
  362. {
  363. return 1;
  364. }
  365. }
  366. else if ( target->GetType()==cmTarget::STATIC_LIBRARY)
  367. {
  368. return 2;
  369. }
  370. else if ((target->GetType()==cmTarget::SHARED_LIBRARY)
  371. || (target->GetType()==cmTarget::MODULE_LIBRARY))
  372. {
  373. return 3;
  374. }
  375. return 4;
  376. }
  377. // Create the command line for building the given target using the selected
  378. // make
  379. std::string cmExtraCodeBlocksGenerator::BuildMakeCommand(
  380. const std::string& make, const char* makefile, const char* target)
  381. {
  382. std::string command = make;
  383. if (strcmp(this->GlobalGenerator->GetName(), "NMake Makefiles")==0)
  384. {
  385. command += " /NOLOGO /f &quot;";
  386. command += makefile;
  387. command += "&quot; ";
  388. command += target;
  389. }
  390. else
  391. {
  392. command += " -f &quot;";
  393. command += makefile;
  394. command += "&quot; ";
  395. command += target;
  396. }
  397. return command;
  398. }