cmExtraCodeBlocksGenerator.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 */
  81. void cmExtraCodeBlocksGenerator::CreateProjectFile(
  82. const std::vector<cmLocalGenerator*>& lgs)
  83. {
  84. const cmMakefile* mf=lgs[0]->GetMakefile();
  85. std::string outputDir=mf->GetStartOutputDirectory();
  86. std::string projectName=mf->GetProjectName();
  87. std::string filename=outputDir+"/";
  88. filename+=projectName+".cbp";
  89. std::string sessionFilename=outputDir+"/";
  90. sessionFilename+=projectName+".layout";
  91. this->CreateNewProjectFile(lgs, filename);
  92. }
  93. void cmExtraCodeBlocksGenerator
  94. ::CreateNewProjectFile(const std::vector<cmLocalGenerator*>& lgs,
  95. const std::string& filename)
  96. {
  97. const cmMakefile* mf=lgs[0]->GetMakefile();
  98. cmGeneratedFileStream fout(filename.c_str());
  99. if(!fout)
  100. {
  101. return;
  102. }
  103. // figure out the compiler
  104. std::string compiler = this->GetCBCompilerId(mf);
  105. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  106. fout<<"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n"
  107. "<CodeBlocks_project_file>\n"
  108. " <FileVersion major=\"1\" minor=\"6\" />\n"
  109. " <Project>\n"
  110. " <Option title=\"" << mf->GetProjectName()<<"\" />\n"
  111. " <Option makefile_is_custom=\"1\" />\n"
  112. " <Option compiler=\"" << compiler << "\" />\n"
  113. " <Build>\n";
  114. bool installTargetCreated = false;
  115. bool installStripTargetCreated = false;
  116. bool testTargetCreated = false;
  117. bool experimentalTargetCreated = false;
  118. bool nightlyTargetCreated = false;
  119. bool packageTargetCreated = false;
  120. bool packageSourceTargetCreated = false;
  121. bool rebuildCacheTargetCreated = false;
  122. this->AppendTarget(fout, "all", 0, make.c_str(), mf, compiler.c_str());
  123. // add all executable and library targets and some of the GLOBAL
  124. // and UTILITY targets
  125. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  126. lg!=lgs.end(); lg++)
  127. {
  128. cmMakefile* makefile=(*lg)->GetMakefile();
  129. cmTargets& targets=makefile->GetTargets();
  130. for (cmTargets::iterator ti = targets.begin();
  131. ti != targets.end(); ti++)
  132. {
  133. switch(ti->second.GetType())
  134. {
  135. case cmTarget::UTILITY:
  136. case cmTarget::GLOBAL_TARGET:
  137. // only add these targets once
  138. if ((ti->first=="install") && (installTargetCreated==false))
  139. {
  140. installTargetCreated=true;
  141. }
  142. else if ((ti->first=="install/strip")
  143. && (installStripTargetCreated==false))
  144. {
  145. installStripTargetCreated=true;
  146. }
  147. else if ((ti->first=="test") && (testTargetCreated==false))
  148. {
  149. testTargetCreated=true;
  150. }
  151. else if ((ti->first=="Experimental")
  152. && (experimentalTargetCreated==false))
  153. {
  154. experimentalTargetCreated=true;
  155. }
  156. else if ((ti->first=="Nightly") && (nightlyTargetCreated==false))
  157. {
  158. nightlyTargetCreated=true;
  159. }
  160. else if ((ti->first=="package") && (packageTargetCreated==false))
  161. {
  162. packageTargetCreated=true;
  163. }
  164. else if ((ti->first=="package_source")
  165. && (packageSourceTargetCreated==false))
  166. {
  167. packageSourceTargetCreated=true;
  168. }
  169. else if ((ti->first=="rebuild_cache")
  170. && (rebuildCacheTargetCreated==false))
  171. {
  172. rebuildCacheTargetCreated=true;
  173. }
  174. else
  175. {
  176. break;
  177. }
  178. this->AppendTarget(fout, ti->first.c_str(), 0,
  179. make.c_str(), makefile, compiler.c_str());
  180. break;
  181. case cmTarget::EXECUTABLE:
  182. case cmTarget::STATIC_LIBRARY:
  183. case cmTarget::SHARED_LIBRARY:
  184. case cmTarget::MODULE_LIBRARY:
  185. {
  186. this->AppendTarget(fout, ti->first.c_str(), &ti->second,
  187. make.c_str(), makefile, compiler.c_str());
  188. std::string fastTarget = ti->first;
  189. fastTarget += "/fast";
  190. this->AppendTarget(fout, fastTarget.c_str(), &ti->second,
  191. make.c_str(), makefile, compiler.c_str());
  192. }
  193. break;
  194. // ignore these:
  195. case cmTarget::INSTALL_FILES:
  196. case cmTarget::INSTALL_PROGRAMS:
  197. case cmTarget::INSTALL_DIRECTORY:
  198. default:
  199. break;
  200. }
  201. }
  202. }
  203. fout<<" </Build>\n";
  204. // Collect all used source files in the project
  205. std::map<std::string, std::string> sourceFiles;
  206. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  207. lg!=lgs.end(); lg++)
  208. {
  209. cmMakefile* makefile=(*lg)->GetMakefile();
  210. cmTargets& targets=makefile->GetTargets();
  211. for (cmTargets::iterator ti = targets.begin();
  212. ti != targets.end(); ti++)
  213. {
  214. switch(ti->second.GetType())
  215. {
  216. case cmTarget::EXECUTABLE:
  217. case cmTarget::STATIC_LIBRARY:
  218. case cmTarget::SHARED_LIBRARY:
  219. case cmTarget::MODULE_LIBRARY:
  220. {
  221. const std::vector<cmSourceFile*>&sources=ti->second.GetSourceFiles();
  222. for (std::vector<cmSourceFile*>::const_iterator si=sources.begin();
  223. si!=sources.end(); si++)
  224. {
  225. sourceFiles[(*si)->GetFullPath()] = ti->first;
  226. }
  227. }
  228. default: // intended fallthrough
  229. break;
  230. }
  231. }
  232. }
  233. // insert all used source files in the CodeBlocks project
  234. for (std::map<std::string, std::string>::const_iterator
  235. sit=sourceFiles.begin();
  236. sit!=sourceFiles.end();
  237. ++sit)
  238. {
  239. fout<<" <Unit filename=\""<<sit->first <<"\">\n"
  240. " </Unit>\n";
  241. }
  242. fout<<" </Project>\n"
  243. "</CodeBlocks_project_file>\n";
  244. }
  245. // Generate the xml code for one target.
  246. void cmExtraCodeBlocksGenerator::AppendTarget(cmGeneratedFileStream& fout,
  247. const char* targetName,
  248. cmTarget* target,
  249. const char* make,
  250. const cmMakefile* makefile,
  251. const char* compiler)
  252. {
  253. std::string makefileName = makefile->GetStartOutputDirectory();
  254. makefileName += "/Makefile";
  255. makefileName = cmSystemTools::ConvertToOutputPath(makefileName.c_str());
  256. fout<<" <Target title=\"" << targetName << "\">\n";
  257. if (target!=0)
  258. {
  259. int cbTargetType = this->GetCBTargetType(target);
  260. fout<<" <Option output=\"" << target->GetLocation(0)
  261. << "\" prefix_auto=\"0\" extension_auto=\"0\" />\n"
  262. " <Option working_dir=\""
  263. << makefile->GetStartOutputDirectory() << "\" />\n"
  264. " <Option object_output=\"./\" />\n"
  265. " <Option type=\"" << cbTargetType << "\" />\n"
  266. " <Option compiler=\"" << compiler << "\" />\n"
  267. " <Compiler>\n";
  268. // the include directories for this target
  269. const std::vector<std::string>& incDirs =
  270. target->GetMakefile()->GetIncludeDirectories();
  271. for(std::vector<std::string>::const_iterator dirIt=incDirs.begin();
  272. dirIt != incDirs.end();
  273. ++dirIt)
  274. {
  275. fout <<" <Add directory=\"" << dirIt->c_str() << "\" />\n";
  276. }
  277. fout<<" </Compiler>\n";
  278. }
  279. else // e.g. all and the GLOBAL and UTILITY targets
  280. {
  281. fout<<" <Option working_dir=\""
  282. << makefile->GetStartOutputDirectory() << "\" />\n"
  283. <<" <Option type=\"" << 4 << "\" />\n";
  284. }
  285. fout<<" <MakeCommands>\n"
  286. " <Build command=\""
  287. << this->BuildMakeCommand(make, makefileName.c_str(), targetName)
  288. << "\" />\n"
  289. " <CompileFile command=\""
  290. << this->BuildMakeCommand(make, makefileName.c_str(),"&quot;$file&quot;")
  291. << "\" />\n"
  292. " <Clean command=\""
  293. << this->BuildMakeCommand(make, makefileName.c_str(), "clean")
  294. << "\" />\n"
  295. " <DistClean command=\""
  296. << this->BuildMakeCommand(make, makefileName.c_str(), "clean")
  297. << "\" />\n"
  298. " </MakeCommands>\n"
  299. " </Target>\n";
  300. }
  301. // Translate the cmake compiler id into the CodeBlocks compiler id
  302. std::string cmExtraCodeBlocksGenerator::GetCBCompilerId(const cmMakefile* mf)
  303. {
  304. // figure out which language to use
  305. // for now care only for C and C++
  306. std::string compilerIdVar = "CMAKE_CXX_COMPILER_ID";
  307. if (this->GlobalGenerator->GetLanguageEnabled("CXX") == false)
  308. {
  309. compilerIdVar = "CMAKE_C_COMPILER_ID";
  310. }
  311. std::string hostSystemName = mf->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
  312. std::string systemName = mf->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  313. std::string compilerId = mf->GetRequiredDefinition(compilerIdVar.c_str());
  314. std::string compiler = "gcc";
  315. if (compilerId == "MSVC")
  316. {
  317. compiler = "msvc";
  318. }
  319. else if (compilerId == "Borland")
  320. {
  321. compiler = "bcc";
  322. }
  323. else if (compilerId == "SDCC")
  324. {
  325. compiler = "sdcc";
  326. }
  327. else if (compilerId == "Intel")
  328. {
  329. compiler = "icc";
  330. }
  331. else if (compilerId == "Watcom")
  332. {
  333. compiler = "ow";
  334. }
  335. else if (compilerId == "GNU")
  336. {
  337. compiler = "gcc";
  338. }
  339. return compiler;
  340. }
  341. // Translate the cmake target type into the CodeBlocks target type id
  342. int cmExtraCodeBlocksGenerator::GetCBTargetType(cmTarget* target)
  343. {
  344. if ( target->GetType()==cmTarget::EXECUTABLE)
  345. {
  346. if ((target->GetPropertyAsBool("WIN32_EXECUTABLE"))
  347. || (target->GetPropertyAsBool("MACOSX_BUNDLE")))
  348. {
  349. return 0;
  350. }
  351. else
  352. {
  353. return 1;
  354. }
  355. }
  356. else if ( target->GetType()==cmTarget::STATIC_LIBRARY)
  357. {
  358. return 2;
  359. }
  360. else if ((target->GetType()==cmTarget::SHARED_LIBRARY)
  361. || (target->GetType()==cmTarget::MODULE_LIBRARY))
  362. {
  363. return 3;
  364. }
  365. return 4;
  366. }
  367. // Create the command line for building the given target using the selected
  368. // make
  369. std::string cmExtraCodeBlocksGenerator::BuildMakeCommand(
  370. const std::string& make, const char* makefile, const char* target)
  371. {
  372. std::string command = make;
  373. if (strcmp(this->GlobalGenerator->GetName(), "NMake Makefiles")==0)
  374. {
  375. command += " /NOLOGO /f &quot;";
  376. command += makefile;
  377. command += "&quot; ";
  378. command += target;
  379. }
  380. else if (strcmp(this->GlobalGenerator->GetName(), "MinGW Makefiles")==0)
  381. {
  382. command += " -f ";
  383. command += makefile;
  384. command += " ";
  385. command += target;
  386. }
  387. else
  388. {
  389. command += " -f &quot;";
  390. command += makefile;
  391. command += "&quot; ";
  392. command += target;
  393. }
  394. return command;
  395. }