cmExtraCodeLiteGenerator.cxx 16 KB

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