cmExtraCodeLiteGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2004-2009 Kitware, Inc.
  4. Copyright 2004 Alexander Neundorf ([email protected])
  5. Copyright 2013 Eran Ifrah ([email protected])
  6. Distributed under the OSI-approved BSD License (the "License");
  7. see accompanying file Copyright.txt for details.
  8. This software is distributed WITHOUT ANY WARRANTY; without even the
  9. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the License for more information.
  11. ============================================================================*/
  12. #include "cmExtraCodeLiteGenerator.h"
  13. #include "cmGlobalUnixMakefileGenerator3.h"
  14. #include "cmLocalUnixMakefileGenerator3.h"
  15. #include "cmMakefile.h"
  16. #include "cmake.h"
  17. #include "cmSourceFile.h"
  18. #include "cmGeneratedFileStream.h"
  19. #include "cmSystemTools.h"
  20. #include <cmsys/SystemTools.hxx>
  21. #include <cmsys/SystemInformation.hxx>
  22. #include <cmsys/Directory.hxx>
  23. #include "cmStandardIncludes.h"
  24. #include "cmXMLSafe.h"
  25. //----------------------------------------------------------------------------
  26. void cmExtraCodeLiteGenerator::GetDocumentation(cmDocumentationEntry& entry,
  27. const std::string&) const
  28. {
  29. entry.Name = this->GetName();
  30. entry.Brief = "Generates CodeLite project files.";
  31. }
  32. cmExtraCodeLiteGenerator::cmExtraCodeLiteGenerator()
  33. : cmExternalMakefileProjectGenerator()
  34. , ConfigName("NoConfig")
  35. , CpuCount(2)
  36. {
  37. #if defined(_WIN32)
  38. this->SupportedGlobalGenerators.push_back("MinGW Makefiles");
  39. this->SupportedGlobalGenerators.push_back("NMake Makefiles");
  40. #endif
  41. this->SupportedGlobalGenerators.push_back("Ninja");
  42. this->SupportedGlobalGenerators.push_back("Unix Makefiles");
  43. }
  44. void cmExtraCodeLiteGenerator::Generate()
  45. {
  46. // Hold root tree information for creating the workspace
  47. std::string workspaceProjectName;
  48. std::string workspaceOutputDir;
  49. std::string workspaceFileName;
  50. std::string workspaceSourcePath;
  51. std::string lprjdebug;
  52. cmGeneratedFileStream fout;
  53. // loop projects and locate the root project.
  54. // and extract the information for creating the worspace
  55. for (std::map<std::string, std::vector<cmLocalGenerator*> >::const_iterator
  56. it = this->GlobalGenerator->GetProjectMap().begin();
  57. it!= this->GlobalGenerator->GetProjectMap().end();
  58. ++it)
  59. {
  60. const cmMakefile* mf =it->second[0]->GetMakefile();
  61. this->ConfigName = GetConfigurationName( mf );
  62. if (strcmp(mf->GetCurrentBinaryDirectory(),
  63. mf->GetHomeOutputDirectory()) == 0)
  64. {
  65. workspaceOutputDir = mf->GetCurrentBinaryDirectory();
  66. workspaceProjectName = mf->GetProjectName();
  67. workspaceSourcePath = mf->GetHomeDirectory();
  68. workspaceFileName = workspaceOutputDir+"/";
  69. workspaceFileName += workspaceProjectName + ".workspace";
  70. this->WorkspacePath = mf->GetCurrentBinaryDirectory();;
  71. fout.Open(workspaceFileName.c_str(), false, false);
  72. fout << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  73. "<CodeLite_Workspace Name=\"" << workspaceProjectName << "\" >\n";
  74. }
  75. }
  76. // for each sub project in the workspace create a codelite project
  77. for (std::map<std::string, std::vector<cmLocalGenerator*> >::const_iterator
  78. it = this->GlobalGenerator->GetProjectMap().begin();
  79. it!= this->GlobalGenerator->GetProjectMap().end();
  80. ++it)
  81. {
  82. // retrive project information
  83. const cmMakefile* mf = it->second[0]->GetMakefile();
  84. std::string outputDir = mf->GetCurrentBinaryDirectory();
  85. std::string projectName = mf->GetProjectName();
  86. std::string filename = outputDir + "/" + projectName + ".project";
  87. // Make the project file relative to the workspace
  88. filename = cmSystemTools::RelativePath(this->WorkspacePath.c_str(),
  89. filename.c_str());
  90. // create a project file
  91. this->CreateProjectFile(it->second);
  92. fout << " <Project Name=\"" << projectName << "\" Path=\""
  93. << filename << "\" Active=\"No\"/>\n";
  94. lprjdebug += "<Project Name=\"" + projectName
  95. + "\" ConfigName=\"" + this->ConfigName + "\"/>\n";
  96. }
  97. fout << " <BuildMatrix>\n"
  98. " <WorkspaceConfiguration Name=\""
  99. << this->ConfigName << "\" Selected=\"yes\">\n"
  100. " " << lprjdebug << ""
  101. " </WorkspaceConfiguration>\n"
  102. " </BuildMatrix>\n"
  103. "</CodeLite_Workspace>\n";
  104. }
  105. /* create the project file */
  106. void cmExtraCodeLiteGenerator::CreateProjectFile(
  107. const std::vector<cmLocalGenerator*>& lgs)
  108. {
  109. const cmMakefile* mf = lgs[0]->GetMakefile();
  110. std::string outputDir = mf->GetCurrentBinaryDirectory();
  111. std::string projectName = mf->GetProjectName();
  112. std::string filename = outputDir + "/";
  113. filename += projectName + ".project";
  114. this->CreateNewProjectFile(lgs, filename);
  115. }
  116. void cmExtraCodeLiteGenerator
  117. ::CreateNewProjectFile(const std::vector<cmLocalGenerator*>& lgs,
  118. const std::string& filename)
  119. {
  120. const cmMakefile* mf=lgs[0]->GetMakefile();
  121. cmGeneratedFileStream fout(filename.c_str());
  122. if(!fout)
  123. {
  124. return;
  125. }
  126. ////////////////////////////////////
  127. fout << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  128. "<CodeLite_Project Name=\"" << mf->GetProjectName()
  129. << "\" InternalType=\"\">\n";
  130. // Collect all used source files in the project
  131. // Sort them into two containers, one for C/C++ implementation files
  132. // which may have an acompanying header, one for all other files
  133. std::string projectType;
  134. std::map<std::string, cmSourceFile*> cFiles;
  135. std::set<std::string> otherFiles;
  136. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  137. lg!=lgs.end(); lg++)
  138. {
  139. cmMakefile* makefile=(*lg)->GetMakefile();
  140. cmTargets& targets=makefile->GetTargets();
  141. for (cmTargets::iterator ti = targets.begin();
  142. ti != targets.end(); ti++)
  143. {
  144. switch(ti->second.GetType())
  145. {
  146. case cmTarget::EXECUTABLE:
  147. {
  148. projectType = "Executable";
  149. }
  150. break;
  151. case cmTarget::STATIC_LIBRARY:
  152. {
  153. projectType = "Static Library";
  154. }
  155. break;
  156. case cmTarget::SHARED_LIBRARY:
  157. {
  158. projectType = "Dynamic Library";
  159. }
  160. break;
  161. case cmTarget::MODULE_LIBRARY:
  162. {
  163. projectType = "Dynamic Library";
  164. }
  165. break;
  166. default: // intended fallthrough
  167. break;
  168. }
  169. switch(ti->second.GetType())
  170. {
  171. case cmTarget::EXECUTABLE:
  172. case cmTarget::STATIC_LIBRARY:
  173. case cmTarget::SHARED_LIBRARY:
  174. case cmTarget::MODULE_LIBRARY:
  175. {
  176. std::vector<cmSourceFile*> sources;
  177. cmGeneratorTarget* gt =
  178. this->GlobalGenerator->GetGeneratorTarget(&ti->second);
  179. gt->GetSourceFiles(sources,
  180. makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  181. for (std::vector<cmSourceFile*>::const_iterator si=sources.begin();
  182. si!=sources.end(); si++)
  183. {
  184. // check whether it is a C/C++ implementation file
  185. bool isCFile = false;
  186. std::string lang = (*si)->GetLanguage();
  187. if (lang == "C" || lang == "CXX")
  188. {
  189. std::string srcext = (*si)->GetExtension();
  190. for(std::vector<std::string>::const_iterator
  191. ext = mf->GetSourceExtensions().begin();
  192. ext != mf->GetSourceExtensions().end();
  193. ++ext)
  194. {
  195. if (srcext == *ext)
  196. {
  197. isCFile = true;
  198. break;
  199. }
  200. }
  201. }
  202. // then put it accordingly into one of the two containers
  203. if (isCFile)
  204. {
  205. cFiles[(*si)->GetFullPath()] = *si ;
  206. }
  207. else
  208. {
  209. otherFiles.insert((*si)->GetFullPath());
  210. }
  211. }
  212. }
  213. default: // intended fallthrough
  214. break;
  215. }
  216. }
  217. }
  218. // The following loop tries to add header files matching to implementation
  219. // files to the project. It does that by iterating over all source files,
  220. // replacing the file name extension with ".h" and checks whether such a
  221. // file exists. If it does, it is inserted into the map of files.
  222. // A very similar version of that code exists also in the kdevelop
  223. // project generator.
  224. for (std::map<std::string, cmSourceFile*>::const_iterator
  225. sit=cFiles.begin();
  226. sit!=cFiles.end();
  227. ++sit)
  228. {
  229. std::string headerBasename=cmSystemTools::GetFilenamePath(sit->first);
  230. headerBasename+="/";
  231. headerBasename+=cmSystemTools::GetFilenameWithoutExtension(sit->first);
  232. // check if there's a matching header around
  233. for(std::vector<std::string>::const_iterator
  234. ext = mf->GetHeaderExtensions().begin();
  235. ext != mf->GetHeaderExtensions().end();
  236. ++ext)
  237. {
  238. std::string hname=headerBasename;
  239. hname += ".";
  240. hname += *ext;
  241. // if it's already in the set, don't check if it exists on disk
  242. std::set<std::string>::const_iterator headerIt=otherFiles.find(hname);
  243. if (headerIt != otherFiles.end())
  244. {
  245. break;
  246. }
  247. if(cmSystemTools::FileExists(hname.c_str()))
  248. {
  249. otherFiles.insert(hname);
  250. break;
  251. }
  252. }
  253. }
  254. // Get the project path ( we need it later to convert files to
  255. // their relative path)
  256. std::string projectPath = cmSystemTools::GetFilenamePath(filename);
  257. // Create 2 virtual folders: src and include
  258. // and place all the implementation files into the src
  259. // folder, the rest goes to the include folder
  260. fout<< " <VirtualDirectory Name=\"src\">\n";
  261. // insert all source files in the codelite project
  262. // first the C/C++ implementation files, then all others
  263. for (std::map<std::string, cmSourceFile*>::const_iterator
  264. sit=cFiles.begin();
  265. sit!=cFiles.end();
  266. ++sit)
  267. {
  268. std::string relativePath =
  269. cmSystemTools::RelativePath(projectPath.c_str(), sit->first.c_str());
  270. fout<< " <File Name=\"" << relativePath << "\"/>\n";
  271. }
  272. fout<< " </VirtualDirectory>\n";
  273. fout<< " <VirtualDirectory Name=\"include\">\n";
  274. for (std::set<std::string>::const_iterator
  275. sit=otherFiles.begin();
  276. sit!=otherFiles.end();
  277. ++sit)
  278. {
  279. std::string relativePath =
  280. cmSystemTools::RelativePath(projectPath.c_str(), sit->c_str());
  281. fout << " <File Name=\"" << relativePath << "\"/>\n";
  282. }
  283. fout << " </VirtualDirectory>\n";
  284. // Get the number of CPUs. We use this information for the make -jN
  285. // command
  286. cmsys::SystemInformation info;
  287. info.RunCPUCheck();
  288. this->CpuCount = info.GetNumberOfLogicalCPU() *
  289. info.GetNumberOfPhysicalCPU();
  290. std::string cleanCommand = GetCleanCommand(mf);
  291. std::string buildCommand = GetBuildCommand(mf);
  292. std::string rebuildCommand = GetRebuildCommand(mf);
  293. std::string singleFileCommand = GetSingleFileBuildCommand(mf);
  294. std::string codeliteCompilerName = this->GetCodeLiteCompilerName(mf);
  295. fout << "\n"
  296. " <Settings Type=\"" << projectType << "\">\n"
  297. " <Configuration Name=\"" << this->ConfigName << "\" CompilerType=\""
  298. << codeliteCompilerName << "\" DebuggerType=\"GNU gdb debugger\" "
  299. "Type=\""
  300. << projectType << "\" BuildCmpWithGlobalSettings=\"append\" "
  301. "BuildLnkWithGlobalSettings=\"append\" "
  302. "BuildResWithGlobalSettings=\"append\">\n"
  303. " <Compiler Options=\"-g\" "
  304. "Required=\"yes\" PreCompiledHeader=\"\">\n"
  305. " <IncludePath Value=\".\"/>\n"
  306. " </Compiler>\n"
  307. " <Linker Options=\"\" Required=\"yes\"/>\n"
  308. " <ResourceCompiler Options=\"\" Required=\"no\"/>\n"
  309. " <General OutputFile=\"$(IntermediateDirectory)/$(ProjectName)\" "
  310. "IntermediateDirectory=\"./\" Command=\"./$(ProjectName)\" "
  311. "CommandArguments=\"\" WorkingDirectory=\"$(IntermediateDirectory)\" "
  312. "PauseExecWhenProcTerminates=\"yes\"/>\n"
  313. " <Debugger IsRemote=\"no\" RemoteHostName=\"\" "
  314. "RemoteHostPort=\"\" DebuggerPath=\"\">\n"
  315. " <PostConnectCommands/>\n"
  316. " <StartupCommands/>\n"
  317. " </Debugger>\n"
  318. " <PreBuild/>\n"
  319. " <PostBuild/>\n"
  320. " <CustomBuild Enabled=\"yes\">\n"
  321. " <RebuildCommand>" << rebuildCommand << "</RebuildCommand>\n"
  322. " <CleanCommand>" << cleanCommand << "</CleanCommand>\n"
  323. " <BuildCommand>" << buildCommand << "</BuildCommand>\n"
  324. " <SingleFileCommand>" << singleFileCommand
  325. << "</SingleFileCommand>\n"
  326. " <PreprocessFileCommand/>\n"
  327. " <WorkingDirectory>$(WorkspacePath)</WorkingDirectory>\n"
  328. " </CustomBuild>\n"
  329. " <AdditionalRules>\n"
  330. " <CustomPostBuild/>\n"
  331. " <CustomPreBuild/>\n"
  332. " </AdditionalRules>\n"
  333. " </Configuration>\n"
  334. " <GlobalSettings>\n"
  335. " <Compiler Options=\"\">\n"
  336. " <IncludePath Value=\".\"/>\n"
  337. " </Compiler>\n"
  338. " <Linker Options=\"\">\n"
  339. " <LibraryPath Value=\".\"/>\n"
  340. " </Linker>\n"
  341. " <ResourceCompiler Options=\"\"/>\n"
  342. " </GlobalSettings>\n"
  343. " </Settings>\n"
  344. "</CodeLite_Project>\n";
  345. }
  346. std::string
  347. cmExtraCodeLiteGenerator::GetCodeLiteCompilerName(const cmMakefile* mf) const
  348. {
  349. // figure out which language to use
  350. // for now care only for C and C++
  351. std::string compilerIdVar = "CMAKE_CXX_COMPILER_ID";
  352. if (this->GlobalGenerator->GetLanguageEnabled("CXX") == false)
  353. {
  354. compilerIdVar = "CMAKE_C_COMPILER_ID";
  355. }
  356. std::string compilerId = mf->GetSafeDefinition(compilerIdVar);
  357. std::string compiler = "gnu g++"; // default to g++
  358. // Since we need the compiler for parsing purposes only
  359. // it does not matter if we use clang or clang++, same as
  360. // "gnu gcc" vs "gnu g++"
  361. if (compilerId == "MSVC")
  362. {
  363. compiler = "VC++";
  364. }
  365. else if (compilerId == "Clang")
  366. {
  367. compiler = "clang++";
  368. }
  369. else if (compilerId == "GNU")
  370. {
  371. compiler = "gnu g++";
  372. }
  373. return compiler;
  374. }
  375. std::string
  376. cmExtraCodeLiteGenerator::GetConfigurationName(const cmMakefile* mf) const
  377. {
  378. std::string confName = mf->GetSafeDefinition("CMAKE_BUILD_TYPE");
  379. // Trim the configuration name from whitespaces (left and right)
  380. confName.erase(0, confName.find_first_not_of(" \t\r\v\n"));
  381. confName.erase(confName.find_last_not_of(" \t\r\v\n")+1);
  382. if ( confName.empty() )
  383. {
  384. confName = "NoConfig";
  385. }
  386. return confName;
  387. }
  388. std::string
  389. cmExtraCodeLiteGenerator::GetBuildCommand(const cmMakefile* mf) const
  390. {
  391. std::string generator = mf->GetSafeDefinition("CMAKE_GENERATOR");
  392. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  393. std::string buildCommand = make; // Default
  394. if ( generator == "NMake Makefiles" ||
  395. generator == "Ninja" )
  396. {
  397. buildCommand = make;
  398. }
  399. else if ( generator == "MinGW Makefiles" ||
  400. generator == "Unix Makefiles" )
  401. {
  402. std::ostringstream ss;
  403. ss << make << " -j " << this->CpuCount;
  404. buildCommand = ss.str();
  405. }
  406. return buildCommand;
  407. }
  408. std::string
  409. cmExtraCodeLiteGenerator::GetCleanCommand(const cmMakefile* mf) const
  410. {
  411. return GetBuildCommand(mf) + " clean";
  412. }
  413. std::string
  414. cmExtraCodeLiteGenerator::GetRebuildCommand(const cmMakefile* mf) const
  415. {
  416. return GetCleanCommand(mf) + cmXMLSafe(" && ").str() + GetBuildCommand(mf);
  417. }
  418. std::string
  419. cmExtraCodeLiteGenerator::GetSingleFileBuildCommand
  420. (const cmMakefile* mf) const
  421. {
  422. std::string buildCommand;
  423. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  424. std::string generator = mf->GetSafeDefinition("CMAKE_GENERATOR");
  425. if ( generator == "Unix Makefiles" || generator == "MinGW Makefiles" )
  426. {
  427. std::ostringstream ss;
  428. ss << make << " -f$(ProjectPath)/Makefile $(CurrentFileName).cpp.o";
  429. buildCommand = ss.str();
  430. }
  431. return buildCommand;
  432. }