cmExtraCodeBlocksGenerator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2004-2009 Kitware, Inc.
  4. Copyright 2004 Alexander Neundorf ([email protected])
  5. Distributed under the OSI-approved BSD License (the "License");
  6. see accompanying file Copyright.txt for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even the
  8. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. See the License for more information.
  10. ============================================================================*/
  11. #include "cmExtraCodeBlocksGenerator.h"
  12. #include "cmGlobalUnixMakefileGenerator3.h"
  13. #include "cmLocalUnixMakefileGenerator3.h"
  14. #include "cmMakefile.h"
  15. #include "cmake.h"
  16. #include "cmSourceFile.h"
  17. #include "cmGeneratedFileStream.h"
  18. #include "cmTarget.h"
  19. #include "cmSystemTools.h"
  20. #include "cmXMLSafe.h"
  21. #include <cmsys/SystemTools.hxx>
  22. /* Some useful URLs:
  23. Homepage:
  24. http://www.codeblocks.org
  25. File format docs:
  26. http://wiki.codeblocks.org/index.php?title=File_formats_description
  27. http://wiki.codeblocks.org/index.php?title=Workspace_file
  28. http://wiki.codeblocks.org/index.php?title=Project_file
  29. Discussion:
  30. http://forums.codeblocks.org/index.php/topic,6789.0.html
  31. */
  32. //----------------------------------------------------------------------------
  33. void cmExtraCodeBlocksGenerator
  34. ::GetDocumentation(cmDocumentationEntry& entry, const char*) const
  35. {
  36. entry.Name = this->GetName();
  37. entry.Brief = "Generates CodeBlocks project files.";
  38. entry.Full =
  39. "Project files for CodeBlocks will be created in the top directory "
  40. "and in every subdirectory which features a CMakeLists.txt file "
  41. "containing a PROJECT() call. "
  42. "Additionally a hierarchy of makefiles is generated into the "
  43. "build tree. The appropriate make program can build the project through "
  44. "the default make target. A \"make install\" target is also provided.";
  45. }
  46. cmExtraCodeBlocksGenerator::cmExtraCodeBlocksGenerator()
  47. :cmExternalMakefileProjectGenerator()
  48. {
  49. #if defined(_WIN32)
  50. this->SupportedGlobalGenerators.push_back("MinGW Makefiles");
  51. this->SupportedGlobalGenerators.push_back("NMake Makefiles");
  52. // disable until somebody actually tests it:
  53. // this->SupportedGlobalGenerators.push_back("MSYS Makefiles");
  54. #endif
  55. this->SupportedGlobalGenerators.push_back("Ninja");
  56. this->SupportedGlobalGenerators.push_back("Unix Makefiles");
  57. }
  58. void cmExtraCodeBlocksGenerator::Generate()
  59. {
  60. // for each sub project in the project create a codeblocks project
  61. for (std::map<cmStdString, std::vector<cmLocalGenerator*> >::const_iterator
  62. it = this->GlobalGenerator->GetProjectMap().begin();
  63. it!= this->GlobalGenerator->GetProjectMap().end();
  64. ++it)
  65. {
  66. // create a project file
  67. this->CreateProjectFile(it->second);
  68. }
  69. }
  70. /* create the project file */
  71. void cmExtraCodeBlocksGenerator::CreateProjectFile(
  72. const std::vector<cmLocalGenerator*>& lgs)
  73. {
  74. const cmMakefile* mf=lgs[0]->GetMakefile();
  75. std::string outputDir=mf->GetStartOutputDirectory();
  76. std::string projectName=mf->GetProjectName();
  77. std::string filename=outputDir+"/";
  78. filename+=projectName+".cbp";
  79. std::string sessionFilename=outputDir+"/";
  80. sessionFilename+=projectName+".layout";
  81. this->CreateNewProjectFile(lgs, filename);
  82. }
  83. /* Tree is used to create a "Virtual Folder" in CodeBlocks, in which all
  84. CMake files this project depends on will be put. This means additionally
  85. to the "Sources" and "Headers" virtual folders of CodeBlocks, there will
  86. now also be a "CMake Files" virtual folder.
  87. Patch by Daniel Teske <daniel.teske AT nokia.com> (which use C::B project
  88. files in QtCreator).*/
  89. struct Tree
  90. {
  91. std::string path; //only one component of the path
  92. std::vector<Tree> folders;
  93. std::vector<std::string> files;
  94. void InsertPath(const std::vector<std::string>& splitted,
  95. std::vector<std::string>::size_type start,
  96. const std::string& fileName);
  97. void BuildVirtualFolder(std::string& virtualFolders) const;
  98. void BuildVirtualFolderImpl(std::string& virtualFolders,
  99. const std::string& prefix) const;
  100. void BuildUnit(std::string& unitString, const std::string& fsPath) const;
  101. void BuildUnitImpl(std::string& unitString,
  102. const std::string& virtualFolderPath,
  103. const std::string& fsPath) const;
  104. };
  105. void Tree::InsertPath(const std::vector<std::string>& splitted,
  106. std::vector<std::string>::size_type start,
  107. const std::string& fileName)
  108. {
  109. if (start == splitted.size())
  110. {
  111. files.push_back(fileName);
  112. return;
  113. }
  114. for (std::vector<Tree>::iterator
  115. it = folders.begin();
  116. it != folders.end();
  117. ++it)
  118. {
  119. if ((*it).path == splitted[start])
  120. {
  121. if (start + 1 < splitted.size())
  122. {
  123. it->InsertPath(splitted, start + 1, fileName);
  124. return;
  125. }
  126. else
  127. {
  128. // last part of splitted
  129. it->files.push_back(fileName);
  130. return;
  131. }
  132. }
  133. }
  134. // Not found in folders, thus insert
  135. Tree newFolder;
  136. newFolder.path = splitted[start];
  137. if (start + 1 < splitted.size())
  138. {
  139. newFolder.InsertPath(splitted, start + 1, fileName);
  140. folders.push_back(newFolder);
  141. return;
  142. }
  143. else
  144. {
  145. // last part of splitted
  146. newFolder.files.push_back(fileName);
  147. folders.push_back(newFolder);
  148. return;
  149. }
  150. }
  151. void Tree::BuildVirtualFolder(std::string& virtualFolders) const
  152. {
  153. virtualFolders += "<Option virtualFolders=\"CMake Files\\;";
  154. for (std::vector<Tree>::const_iterator it = folders.begin();
  155. it != folders.end();
  156. ++it)
  157. {
  158. it->BuildVirtualFolderImpl(virtualFolders, "");
  159. }
  160. virtualFolders += "\" />";
  161. }
  162. void Tree::BuildVirtualFolderImpl(std::string& virtualFolders,
  163. const std::string& prefix) const
  164. {
  165. virtualFolders += "CMake Files\\" + prefix + path + "\\;";
  166. for (std::vector<Tree>::const_iterator it = folders.begin();
  167. it != folders.end();
  168. ++it)
  169. {
  170. it->BuildVirtualFolderImpl(virtualFolders, prefix + path + "\\");
  171. }
  172. }
  173. void Tree::BuildUnit(std::string& unitString, const std::string& fsPath) const
  174. {
  175. for (std::vector<std::string>::const_iterator it = files.begin();
  176. it != files.end();
  177. ++it)
  178. {
  179. unitString += " <Unit filename=\"" + fsPath + *it + "\">\n";
  180. unitString += " <Option virtualFolder=\"CMake Files\\\" />\n";
  181. unitString += " </Unit>\n";
  182. }
  183. for (std::vector<Tree>::const_iterator it = folders.begin();
  184. it != folders.end();
  185. ++it)
  186. {
  187. it->BuildUnitImpl(unitString, "", fsPath);
  188. }
  189. }
  190. void Tree::BuildUnitImpl(std::string& unitString,
  191. const std::string& virtualFolderPath,
  192. const std::string& fsPath) const
  193. {
  194. for (std::vector<std::string>::const_iterator it = files.begin();
  195. it != files.end();
  196. ++it)
  197. {
  198. unitString += " <Unit filename=\"" +fsPath+path+ "/" + *it + "\">\n";
  199. unitString += " <Option virtualFolder=\"CMake Files\\"
  200. + virtualFolderPath + path + "\\\" />\n";
  201. unitString += " </Unit>\n";
  202. }
  203. for (std::vector<Tree>::const_iterator it = folders.begin();
  204. it != folders.end();
  205. ++it)
  206. {
  207. it->BuildUnitImpl(unitString,
  208. virtualFolderPath + path + "\\", fsPath + path + "/");
  209. }
  210. }
  211. void cmExtraCodeBlocksGenerator
  212. ::CreateNewProjectFile(const std::vector<cmLocalGenerator*>& lgs,
  213. const std::string& filename)
  214. {
  215. const cmMakefile* mf=lgs[0]->GetMakefile();
  216. cmGeneratedFileStream fout(filename.c_str());
  217. if(!fout)
  218. {
  219. return;
  220. }
  221. Tree tree;
  222. // build tree of virtual folders
  223. for (std::map<cmStdString, std::vector<cmLocalGenerator*> >::const_iterator
  224. it = this->GlobalGenerator->GetProjectMap().begin();
  225. it != this->GlobalGenerator->GetProjectMap().end();
  226. ++it)
  227. {
  228. // Collect all files
  229. std::vector<std::string> listFiles;
  230. for (std::vector<cmLocalGenerator *>::const_iterator
  231. jt = it->second.begin();
  232. jt != it->second.end();
  233. ++jt)
  234. {
  235. const std::vector<std::string> & files =
  236. (*jt)->GetMakefile()->GetListFiles();
  237. listFiles.insert(listFiles.end(), files.begin(), files.end());
  238. }
  239. // Convert
  240. const char* cmakeRoot = mf->GetDefinition("CMAKE_ROOT");
  241. for (std::vector<std::string>::const_iterator jt = listFiles.begin();
  242. jt != listFiles.end();
  243. ++jt)
  244. {
  245. // don't put cmake's own files into the project (#12110):
  246. if (jt->find(cmakeRoot) == 0)
  247. {
  248. continue;
  249. }
  250. const std::string &relative = cmSystemTools::RelativePath(
  251. it->second[0]->GetMakefile()->GetHomeDirectory(),
  252. jt->c_str());
  253. std::vector<std::string> splitted;
  254. cmSystemTools::SplitPath(relative.c_str(), splitted, false);
  255. // Split filename from path
  256. std::string fileName = *(splitted.end()-1);
  257. splitted.erase(splitted.end() - 1, splitted.end());
  258. // We don't want paths with CMakeFiles in them
  259. // or do we?
  260. // In speedcrunch those where purely internal
  261. if (splitted.size() >= 1
  262. && relative.find("CMakeFiles") == std::string::npos)
  263. {
  264. tree.InsertPath(splitted, 1, fileName);
  265. }
  266. }
  267. }
  268. // Now build a virtual tree string
  269. std::string virtualFolders;
  270. tree.BuildVirtualFolder(virtualFolders);
  271. // And one for <Unit>
  272. std::string unitFiles;
  273. tree.BuildUnit(unitFiles, std::string(mf->GetHomeDirectory()) + "/");
  274. // figure out the compiler
  275. std::string compiler = this->GetCBCompilerId(mf);
  276. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  277. fout<<"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n"
  278. "<CodeBlocks_project_file>\n"
  279. " <FileVersion major=\"1\" minor=\"6\" />\n"
  280. " <Project>\n"
  281. " <Option title=\"" << mf->GetProjectName()<<"\" />\n"
  282. " <Option makefile_is_custom=\"1\" />\n"
  283. " <Option compiler=\"" << compiler << "\" />\n"
  284. " "<<virtualFolders<<"\n"
  285. " <Build>\n";
  286. this->AppendTarget(fout, "all", 0, make.c_str(), mf, compiler.c_str());
  287. // add all executable and library targets and some of the GLOBAL
  288. // and UTILITY targets
  289. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  290. lg!=lgs.end(); lg++)
  291. {
  292. cmMakefile* makefile=(*lg)->GetMakefile();
  293. cmTargets& targets=makefile->GetTargets();
  294. for (cmTargets::iterator ti = targets.begin();
  295. ti != targets.end(); ti++)
  296. {
  297. switch(ti->second.GetType())
  298. {
  299. case cmTarget::GLOBAL_TARGET:
  300. {
  301. bool insertTarget = false;
  302. // Only add the global targets from CMAKE_BINARY_DIR,
  303. // not from the subdirs
  304. if (strcmp(makefile->GetStartOutputDirectory(),
  305. makefile->GetHomeOutputDirectory())==0)
  306. {
  307. insertTarget = true;
  308. // only add the "edit_cache" target if it's not ccmake, because
  309. // this will not work within the IDE
  310. if (ti->first == "edit_cache")
  311. {
  312. const char* editCommand = makefile->GetDefinition
  313. ("CMAKE_EDIT_COMMAND");
  314. if (editCommand == 0)
  315. {
  316. insertTarget = false;
  317. }
  318. else if (strstr(editCommand, "ccmake")!=NULL)
  319. {
  320. insertTarget = false;
  321. }
  322. }
  323. }
  324. if (insertTarget)
  325. {
  326. this->AppendTarget(fout, ti->first.c_str(), 0,
  327. make.c_str(), makefile, compiler.c_str());
  328. }
  329. }
  330. break;
  331. case cmTarget::UTILITY:
  332. // Add all utility targets, except the Nightly/Continuous/
  333. // Experimental-"sub"targets as e.g. NightlyStart
  334. if (((ti->first.find("Nightly")==0) &&(ti->first!="Nightly"))
  335. || ((ti->first.find("Continuous")==0)&&(ti->first!="Continuous"))
  336. || ((ti->first.find("Experimental")==0)
  337. && (ti->first!="Experimental")))
  338. {
  339. break;
  340. }
  341. this->AppendTarget(fout, ti->first.c_str(), 0,
  342. make.c_str(), makefile, compiler.c_str());
  343. break;
  344. case cmTarget::EXECUTABLE:
  345. case cmTarget::STATIC_LIBRARY:
  346. case cmTarget::SHARED_LIBRARY:
  347. case cmTarget::MODULE_LIBRARY:
  348. case cmTarget::OBJECT_LIBRARY:
  349. {
  350. this->AppendTarget(fout, ti->first.c_str(), &ti->second,
  351. make.c_str(), makefile, compiler.c_str());
  352. std::string fastTarget = ti->first;
  353. fastTarget += "/fast";
  354. this->AppendTarget(fout, fastTarget.c_str(), &ti->second,
  355. make.c_str(), makefile, compiler.c_str());
  356. }
  357. break;
  358. default:
  359. break;
  360. }
  361. }
  362. }
  363. fout<<" </Build>\n";
  364. // Collect all used source files in the project
  365. // Sort them into two containers, one for C/C++ implementation files
  366. // which may have an acompanying header, one for all other files
  367. std::map<std::string, cmSourceFile*> cFiles;
  368. std::set<std::string> otherFiles;
  369. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  370. lg!=lgs.end(); lg++)
  371. {
  372. cmMakefile* makefile=(*lg)->GetMakefile();
  373. cmTargets& targets=makefile->GetTargets();
  374. for (cmTargets::iterator ti = targets.begin();
  375. ti != targets.end(); ti++)
  376. {
  377. switch(ti->second.GetType())
  378. {
  379. case cmTarget::EXECUTABLE:
  380. case cmTarget::STATIC_LIBRARY:
  381. case cmTarget::SHARED_LIBRARY:
  382. case cmTarget::MODULE_LIBRARY:
  383. case cmTarget::OBJECT_LIBRARY:
  384. case cmTarget::UTILITY: // can have sources since 2.6.3
  385. {
  386. const std::vector<cmSourceFile*>&sources=ti->second.GetSourceFiles();
  387. for (std::vector<cmSourceFile*>::const_iterator si=sources.begin();
  388. si!=sources.end(); si++)
  389. {
  390. // don't add source files which have the GENERATED property set:
  391. if ((*si)->GetPropertyAsBool("GENERATED"))
  392. {
  393. continue;
  394. }
  395. // check whether it is a C/C++ implementation file
  396. bool isCFile = false;
  397. if ((*si)->GetLanguage() && (*(*si)->GetLanguage() == 'C'))
  398. {
  399. for(std::vector<std::string>::const_iterator
  400. ext = mf->GetSourceExtensions().begin();
  401. ext != mf->GetSourceExtensions().end();
  402. ++ext)
  403. {
  404. if ((*si)->GetExtension() == *ext)
  405. {
  406. isCFile = true;
  407. break;
  408. }
  409. }
  410. }
  411. // then put it accordingly into one of the two containers
  412. if (isCFile)
  413. {
  414. cFiles[(*si)->GetFullPath()] = *si ;
  415. }
  416. else
  417. {
  418. otherFiles.insert((*si)->GetFullPath());
  419. }
  420. }
  421. }
  422. default: // intended fallthrough
  423. break;
  424. }
  425. }
  426. }
  427. // The following loop tries to add header files matching to implementation
  428. // files to the project. It does that by iterating over all source files,
  429. // replacing the file name extension with ".h" and checks whether such a
  430. // file exists. If it does, it is inserted into the map of files.
  431. // A very similar version of that code exists also in the kdevelop
  432. // project generator.
  433. for (std::map<std::string, cmSourceFile*>::const_iterator
  434. sit=cFiles.begin();
  435. sit!=cFiles.end();
  436. ++sit)
  437. {
  438. std::string headerBasename=cmSystemTools::GetFilenamePath(sit->first);
  439. headerBasename+="/";
  440. headerBasename+=cmSystemTools::GetFilenameWithoutExtension(sit->first);
  441. // check if there's a matching header around
  442. for(std::vector<std::string>::const_iterator
  443. ext = mf->GetHeaderExtensions().begin();
  444. ext != mf->GetHeaderExtensions().end();
  445. ++ext)
  446. {
  447. std::string hname=headerBasename;
  448. hname += ".";
  449. hname += *ext;
  450. // if it's already in the set, don't check if it exists on disk
  451. std::set<std::string>::const_iterator headerIt=otherFiles.find(hname);
  452. if (headerIt != otherFiles.end())
  453. {
  454. break;
  455. }
  456. if(cmSystemTools::FileExists(hname.c_str()))
  457. {
  458. otherFiles.insert(hname);
  459. break;
  460. }
  461. }
  462. }
  463. // insert all source files in the CodeBlocks project
  464. // first the C/C++ implementation files, then all others
  465. for (std::map<std::string, cmSourceFile*>::const_iterator
  466. sit=cFiles.begin();
  467. sit!=cFiles.end();
  468. ++sit)
  469. {
  470. fout<<" <Unit filename=\""<< sit->first <<"\">\n"
  471. " </Unit>\n";
  472. }
  473. for (std::set<std::string>::const_iterator
  474. sit=otherFiles.begin();
  475. sit!=otherFiles.end();
  476. ++sit)
  477. {
  478. fout<<" <Unit filename=\""<< sit->c_str() <<"\">\n"
  479. " </Unit>\n";
  480. }
  481. // Add CMakeLists.txt
  482. fout<<unitFiles;
  483. fout<<" </Project>\n"
  484. "</CodeBlocks_project_file>\n";
  485. }
  486. // Write a dummy file for OBJECT libraries, so C::B can reference some file
  487. std::string cmExtraCodeBlocksGenerator::CreateDummyTargetFile(
  488. cmMakefile* mf, cmTarget* target) const
  489. {
  490. // this file doesn't seem to be used by C::B in custom makefile mode,
  491. // but we generate a unique file for each OBJECT library so in case
  492. // C::B uses it in some way, the targets don't interfere with each other.
  493. std::string filename = mf->GetCurrentOutputDirectory();
  494. filename += "/";
  495. filename += mf->GetLocalGenerator()->GetTargetDirectory(*target);
  496. filename += "/";
  497. filename += target->GetName();
  498. filename += ".objlib";
  499. cmGeneratedFileStream fout(filename.c_str());
  500. if(fout)
  501. {
  502. fout << "# This is a dummy file for the OBJECT library "
  503. << target->GetName()
  504. << " for the CMake CodeBlocks project generator.\n"
  505. << "# Don't edit, this file will be overwritten.\n";
  506. }
  507. return filename;
  508. }
  509. // Generate the xml code for one target.
  510. void cmExtraCodeBlocksGenerator::AppendTarget(cmGeneratedFileStream& fout,
  511. const char* targetName,
  512. cmTarget* target,
  513. const char* make,
  514. const cmMakefile* makefile,
  515. const char* compiler)
  516. {
  517. std::string makefileName = makefile->GetStartOutputDirectory();
  518. makefileName += "/Makefile";
  519. fout<<" <Target title=\"" << targetName << "\">\n";
  520. if (target!=0)
  521. {
  522. int cbTargetType = this->GetCBTargetType(target);
  523. std::string workingDir = makefile->GetStartOutputDirectory();
  524. if ( target->GetType()==cmTarget::EXECUTABLE)
  525. {
  526. // Determine the directory where the executable target is created, and
  527. // set the working directory to this dir.
  528. const char* runtimeOutputDir = makefile->GetDefinition(
  529. "CMAKE_RUNTIME_OUTPUT_DIRECTORY");
  530. if (runtimeOutputDir != 0)
  531. {
  532. workingDir = runtimeOutputDir;
  533. }
  534. else
  535. {
  536. const char* executableOutputDir = makefile->GetDefinition(
  537. "EXECUTABLE_OUTPUT_PATH");
  538. if (executableOutputDir != 0)
  539. {
  540. workingDir = executableOutputDir;
  541. }
  542. }
  543. }
  544. const char* buildType = makefile->GetDefinition("CMAKE_BUILD_TYPE");
  545. std::string location;
  546. if ( target->GetType()==cmTarget::OBJECT_LIBRARY)
  547. {
  548. location = this->CreateDummyTargetFile(const_cast<cmMakefile*>(makefile),
  549. target);
  550. }
  551. else
  552. {
  553. location = target->GetLocation(buildType);
  554. }
  555. fout<<" <Option output=\"" << location
  556. << "\" prefix_auto=\"0\" extension_auto=\"0\" />\n"
  557. " <Option working_dir=\"" << workingDir << "\" />\n"
  558. " <Option object_output=\"./\" />\n"
  559. " <Option type=\"" << cbTargetType << "\" />\n"
  560. " <Option compiler=\"" << compiler << "\" />\n"
  561. " <Compiler>\n";
  562. cmGeneratorTarget *gtgt = this->GlobalGenerator
  563. ->GetGeneratorTarget(target);
  564. // the compilerdefines for this target
  565. std::string cdefs = target->GetCompileDefinitions(buildType);
  566. if(!cdefs.empty())
  567. {
  568. // Expand the list.
  569. std::vector<std::string> defs;
  570. cmSystemTools::ExpandListArgument(cdefs.c_str(), defs);
  571. for(std::vector<std::string>::const_iterator di = defs.begin();
  572. di != defs.end(); ++di)
  573. {
  574. cmXMLSafe safedef(di->c_str());
  575. fout <<" <Add option=\"-D" << safedef.str() << "\" />\n";
  576. }
  577. }
  578. // the include directories for this target
  579. std::set<std::string> uniqIncludeDirs;
  580. std::vector<std::string> includes;
  581. target->GetMakefile()->GetLocalGenerator()->
  582. GetIncludeDirectories(includes, gtgt, "C", buildType);
  583. for(std::vector<std::string>::const_iterator dirIt=includes.begin();
  584. dirIt != includes.end();
  585. ++dirIt)
  586. {
  587. uniqIncludeDirs.insert(*dirIt);
  588. }
  589. std::string systemIncludeDirs = makefile->GetSafeDefinition(
  590. "CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS");
  591. if (!systemIncludeDirs.empty())
  592. {
  593. std::vector<std::string> dirs;
  594. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  595. for(std::vector<std::string>::const_iterator dirIt=dirs.begin();
  596. dirIt != dirs.end();
  597. ++dirIt)
  598. {
  599. uniqIncludeDirs.insert(*dirIt);
  600. }
  601. }
  602. systemIncludeDirs = makefile->GetSafeDefinition(
  603. "CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS");
  604. if (!systemIncludeDirs.empty())
  605. {
  606. std::vector<std::string> dirs;
  607. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  608. for(std::vector<std::string>::const_iterator dirIt=dirs.begin();
  609. dirIt != dirs.end();
  610. ++dirIt)
  611. {
  612. uniqIncludeDirs.insert(*dirIt);
  613. }
  614. }
  615. for(std::set<std::string>::const_iterator dirIt=uniqIncludeDirs.begin();
  616. dirIt != uniqIncludeDirs.end();
  617. ++dirIt)
  618. {
  619. fout <<" <Add directory=\"" << dirIt->c_str() << "\" />\n";
  620. }
  621. fout<<" </Compiler>\n";
  622. }
  623. else // e.g. all and the GLOBAL and UTILITY targets
  624. {
  625. fout<<" <Option working_dir=\""
  626. << makefile->GetStartOutputDirectory() << "\" />\n"
  627. <<" <Option type=\"" << 4 << "\" />\n";
  628. }
  629. fout<<" <MakeCommands>\n"
  630. " <Build command=\""
  631. << this->BuildMakeCommand(make, makefileName.c_str(), targetName)
  632. << "\" />\n"
  633. " <CompileFile command=\""
  634. << this->BuildMakeCommand(make, makefileName.c_str(),"&quot;$file&quot;")
  635. << "\" />\n"
  636. " <Clean command=\""
  637. << this->BuildMakeCommand(make, makefileName.c_str(), "clean")
  638. << "\" />\n"
  639. " <DistClean command=\""
  640. << this->BuildMakeCommand(make, makefileName.c_str(), "clean")
  641. << "\" />\n"
  642. " </MakeCommands>\n"
  643. " </Target>\n";
  644. }
  645. // Translate the cmake compiler id into the CodeBlocks compiler id
  646. std::string cmExtraCodeBlocksGenerator::GetCBCompilerId(const cmMakefile* mf)
  647. {
  648. // figure out which language to use
  649. // for now care only for C and C++
  650. std::string compilerIdVar = "CMAKE_CXX_COMPILER_ID";
  651. if (this->GlobalGenerator->GetLanguageEnabled("CXX") == false)
  652. {
  653. compilerIdVar = "CMAKE_C_COMPILER_ID";
  654. }
  655. std::string hostSystemName = mf->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
  656. std::string systemName = mf->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  657. std::string compilerId = mf->GetSafeDefinition(compilerIdVar.c_str());
  658. std::string compiler = "gcc"; // default to gcc
  659. if (compilerId == "MSVC")
  660. {
  661. compiler = "msvc8";
  662. }
  663. else if (compilerId == "Borland")
  664. {
  665. compiler = "bcc";
  666. }
  667. else if (compilerId == "SDCC")
  668. {
  669. compiler = "sdcc";
  670. }
  671. else if (compilerId == "Intel")
  672. {
  673. compiler = "icc";
  674. }
  675. else if (compilerId == "Watcom")
  676. {
  677. compiler = "ow";
  678. }
  679. else if (compilerId == "GNU")
  680. {
  681. compiler = "gcc";
  682. }
  683. return compiler;
  684. }
  685. // Translate the cmake target type into the CodeBlocks target type id
  686. int cmExtraCodeBlocksGenerator::GetCBTargetType(cmTarget* target)
  687. {
  688. if ( target->GetType()==cmTarget::EXECUTABLE)
  689. {
  690. if ((target->GetPropertyAsBool("WIN32_EXECUTABLE"))
  691. || (target->GetPropertyAsBool("MACOSX_BUNDLE")))
  692. {
  693. return 0;
  694. }
  695. else
  696. {
  697. return 1;
  698. }
  699. }
  700. else if (( target->GetType()==cmTarget::STATIC_LIBRARY)
  701. || (target->GetType()==cmTarget::OBJECT_LIBRARY))
  702. {
  703. return 2;
  704. }
  705. else if ((target->GetType()==cmTarget::SHARED_LIBRARY)
  706. || (target->GetType()==cmTarget::MODULE_LIBRARY))
  707. {
  708. return 3;
  709. }
  710. return 4;
  711. }
  712. // Create the command line for building the given target using the selected
  713. // make
  714. std::string cmExtraCodeBlocksGenerator::BuildMakeCommand(
  715. const std::string& make, const char* makefile, const char* target)
  716. {
  717. std::string command = make;
  718. if (strcmp(this->GlobalGenerator->GetName(), "NMake Makefiles")==0)
  719. {
  720. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  721. command += " /NOLOGO /f &quot;";
  722. command += makefileName;
  723. command += "&quot; ";
  724. command += " VERBOSE=1 ";
  725. command += target;
  726. }
  727. else if (strcmp(this->GlobalGenerator->GetName(), "MinGW Makefiles")==0)
  728. {
  729. // no escaping of spaces in this case, see
  730. // http://public.kitware.com/Bug/view.php?id=10014
  731. std::string makefileName = makefile;
  732. command += " -f &quot;";
  733. command += makefileName;
  734. command += "&quot; ";
  735. command += " VERBOSE=1 ";
  736. command += target;
  737. }
  738. else if (strcmp(this->GlobalGenerator->GetName(), "Ninja")==0)
  739. {
  740. command += " -v ";
  741. command += target;
  742. }
  743. else
  744. {
  745. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  746. command += " -f &quot;";
  747. command += makefileName;
  748. command += "&quot; ";
  749. command += " VERBOSE=1 ";
  750. command += target;
  751. }
  752. return command;
  753. }