cmExtraCodeLiteGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. cmTargets& targets=makefile->GetTargets();
  139. for (cmTargets::iterator ti = targets.begin();
  140. ti != targets.end(); ti++)
  141. {
  142. switch(ti->second.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->second.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 =
  176. this->GlobalGenerator->GetGeneratorTarget(&ti->second);
  177. gt->GetSourceFiles(sources,
  178. makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  179. for (std::vector<cmSourceFile*>::const_iterator si=sources.begin();
  180. si!=sources.end(); si++)
  181. {
  182. // check whether it is a C/C++ implementation file
  183. bool isCFile = false;
  184. std::string lang = (*si)->GetLanguage();
  185. if (lang == "C" || lang == "CXX")
  186. {
  187. std::string srcext = (*si)->GetExtension();
  188. for(std::vector<std::string>::const_iterator
  189. ext = mf->GetSourceExtensions().begin();
  190. ext != mf->GetSourceExtensions().end();
  191. ++ext)
  192. {
  193. if (srcext == *ext)
  194. {
  195. isCFile = true;
  196. break;
  197. }
  198. }
  199. }
  200. // then put it accordingly into one of the two containers
  201. if (isCFile)
  202. {
  203. cFiles[(*si)->GetFullPath()] = *si ;
  204. }
  205. else
  206. {
  207. otherFiles.insert((*si)->GetFullPath());
  208. }
  209. }
  210. }
  211. default: // intended fallthrough
  212. break;
  213. }
  214. }
  215. }
  216. // The following loop tries to add header files matching to implementation
  217. // files to the project. It does that by iterating over all source files,
  218. // replacing the file name extension with ".h" and checks whether such a
  219. // file exists. If it does, it is inserted into the map of files.
  220. // A very similar version of that code exists also in the kdevelop
  221. // project generator.
  222. for (std::map<std::string, cmSourceFile*>::const_iterator
  223. sit=cFiles.begin();
  224. sit!=cFiles.end();
  225. ++sit)
  226. {
  227. std::string headerBasename=cmSystemTools::GetFilenamePath(sit->first);
  228. headerBasename+="/";
  229. headerBasename+=cmSystemTools::GetFilenameWithoutExtension(sit->first);
  230. // check if there's a matching header around
  231. for(std::vector<std::string>::const_iterator
  232. ext = mf->GetHeaderExtensions().begin();
  233. ext != mf->GetHeaderExtensions().end();
  234. ++ext)
  235. {
  236. std::string hname=headerBasename;
  237. hname += ".";
  238. hname += *ext;
  239. // if it's already in the set, don't check if it exists on disk
  240. std::set<std::string>::const_iterator headerIt=otherFiles.find(hname);
  241. if (headerIt != otherFiles.end())
  242. {
  243. break;
  244. }
  245. if(cmSystemTools::FileExists(hname.c_str()))
  246. {
  247. otherFiles.insert(hname);
  248. break;
  249. }
  250. }
  251. }
  252. // Get the project path ( we need it later to convert files to
  253. // their relative path)
  254. std::string projectPath = cmSystemTools::GetFilenamePath(filename);
  255. // Create 2 virtual folders: src and include
  256. // and place all the implementation files into the src
  257. // folder, the rest goes to the include folder
  258. fout<< " <VirtualDirectory Name=\"src\">\n";
  259. // insert all source files in the codelite project
  260. // first the C/C++ implementation files, then all others
  261. for (std::map<std::string, cmSourceFile*>::const_iterator
  262. sit=cFiles.begin();
  263. sit!=cFiles.end();
  264. ++sit)
  265. {
  266. std::string relativePath =
  267. cmSystemTools::RelativePath(projectPath.c_str(), sit->first.c_str());
  268. fout<< " <File Name=\"" << relativePath << "\"/>\n";
  269. }
  270. fout<< " </VirtualDirectory>\n";
  271. fout<< " <VirtualDirectory Name=\"include\">\n";
  272. for (std::set<std::string>::const_iterator
  273. sit=otherFiles.begin();
  274. sit!=otherFiles.end();
  275. ++sit)
  276. {
  277. std::string relativePath =
  278. cmSystemTools::RelativePath(projectPath.c_str(), sit->c_str());
  279. fout << " <File Name=\"" << relativePath << "\"/>\n";
  280. }
  281. fout << " </VirtualDirectory>\n";
  282. // Get the number of CPUs. We use this information for the make -jN
  283. // command
  284. cmsys::SystemInformation info;
  285. info.RunCPUCheck();
  286. this->CpuCount = info.GetNumberOfLogicalCPU() *
  287. info.GetNumberOfPhysicalCPU();
  288. std::string cleanCommand = GetCleanCommand(mf);
  289. std::string buildCommand = GetBuildCommand(mf);
  290. std::string rebuildCommand = GetRebuildCommand(mf);
  291. std::string singleFileCommand = GetSingleFileBuildCommand(mf);
  292. std::string codeliteCompilerName = this->GetCodeLiteCompilerName(mf);
  293. fout << "\n"
  294. " <Settings Type=\"" << projectType << "\">\n"
  295. " <Configuration Name=\"" << this->ConfigName << "\" CompilerType=\""
  296. << codeliteCompilerName << "\" DebuggerType=\"GNU gdb debugger\" "
  297. "Type=\""
  298. << projectType << "\" BuildCmpWithGlobalSettings=\"append\" "
  299. "BuildLnkWithGlobalSettings=\"append\" "
  300. "BuildResWithGlobalSettings=\"append\">\n"
  301. " <Compiler Options=\"-g\" "
  302. "Required=\"yes\" PreCompiledHeader=\"\">\n"
  303. " <IncludePath Value=\".\"/>\n"
  304. " </Compiler>\n"
  305. " <Linker Options=\"\" Required=\"yes\"/>\n"
  306. " <ResourceCompiler Options=\"\" Required=\"no\"/>\n"
  307. " <General OutputFile=\"$(IntermediateDirectory)/$(ProjectName)\" "
  308. "IntermediateDirectory=\"./\" Command=\"./$(ProjectName)\" "
  309. "CommandArguments=\"\" WorkingDirectory=\"$(IntermediateDirectory)\" "
  310. "PauseExecWhenProcTerminates=\"yes\"/>\n"
  311. " <Debugger IsRemote=\"no\" RemoteHostName=\"\" "
  312. "RemoteHostPort=\"\" DebuggerPath=\"\">\n"
  313. " <PostConnectCommands/>\n"
  314. " <StartupCommands/>\n"
  315. " </Debugger>\n"
  316. " <PreBuild/>\n"
  317. " <PostBuild/>\n"
  318. " <CustomBuild Enabled=\"yes\">\n"
  319. " <RebuildCommand>" << rebuildCommand << "</RebuildCommand>\n"
  320. " <CleanCommand>" << cleanCommand << "</CleanCommand>\n"
  321. " <BuildCommand>" << buildCommand << "</BuildCommand>\n"
  322. " <SingleFileCommand>" << singleFileCommand
  323. << "</SingleFileCommand>\n"
  324. " <PreprocessFileCommand/>\n"
  325. " <WorkingDirectory>$(WorkspacePath)</WorkingDirectory>\n"
  326. " </CustomBuild>\n"
  327. " <AdditionalRules>\n"
  328. " <CustomPostBuild/>\n"
  329. " <CustomPreBuild/>\n"
  330. " </AdditionalRules>\n"
  331. " </Configuration>\n"
  332. " <GlobalSettings>\n"
  333. " <Compiler Options=\"\">\n"
  334. " <IncludePath Value=\".\"/>\n"
  335. " </Compiler>\n"
  336. " <Linker Options=\"\">\n"
  337. " <LibraryPath Value=\".\"/>\n"
  338. " </Linker>\n"
  339. " <ResourceCompiler Options=\"\"/>\n"
  340. " </GlobalSettings>\n"
  341. " </Settings>\n"
  342. "</CodeLite_Project>\n";
  343. }
  344. std::string
  345. cmExtraCodeLiteGenerator::GetCodeLiteCompilerName(const cmMakefile* mf) const
  346. {
  347. // figure out which language to use
  348. // for now care only for C and C++
  349. std::string compilerIdVar = "CMAKE_CXX_COMPILER_ID";
  350. if (this->GlobalGenerator->GetLanguageEnabled("CXX") == false)
  351. {
  352. compilerIdVar = "CMAKE_C_COMPILER_ID";
  353. }
  354. std::string compilerId = mf->GetSafeDefinition(compilerIdVar);
  355. std::string compiler = "gnu g++"; // default to g++
  356. // Since we need the compiler for parsing purposes only
  357. // it does not matter if we use clang or clang++, same as
  358. // "gnu gcc" vs "gnu g++"
  359. if (compilerId == "MSVC")
  360. {
  361. compiler = "VC++";
  362. }
  363. else if (compilerId == "Clang")
  364. {
  365. compiler = "clang++";
  366. }
  367. else if (compilerId == "GNU")
  368. {
  369. compiler = "gnu g++";
  370. }
  371. return compiler;
  372. }
  373. std::string
  374. cmExtraCodeLiteGenerator::GetConfigurationName(const cmMakefile* mf) const
  375. {
  376. std::string confName = mf->GetSafeDefinition("CMAKE_BUILD_TYPE");
  377. // Trim the configuration name from whitespaces (left and right)
  378. confName.erase(0, confName.find_first_not_of(" \t\r\v\n"));
  379. confName.erase(confName.find_last_not_of(" \t\r\v\n")+1);
  380. if ( confName.empty() )
  381. {
  382. confName = "NoConfig";
  383. }
  384. return confName;
  385. }
  386. std::string
  387. cmExtraCodeLiteGenerator::GetBuildCommand(const cmMakefile* mf) const
  388. {
  389. std::string generator = mf->GetSafeDefinition("CMAKE_GENERATOR");
  390. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  391. std::string buildCommand = make; // Default
  392. if ( generator == "NMake Makefiles" ||
  393. generator == "Ninja" )
  394. {
  395. buildCommand = make;
  396. }
  397. else if ( generator == "MinGW Makefiles" ||
  398. generator == "Unix Makefiles" )
  399. {
  400. std::ostringstream ss;
  401. ss << make << " -j " << this->CpuCount;
  402. buildCommand = ss.str();
  403. }
  404. return buildCommand;
  405. }
  406. std::string
  407. cmExtraCodeLiteGenerator::GetCleanCommand(const cmMakefile* mf) const
  408. {
  409. return GetBuildCommand(mf) + " clean";
  410. }
  411. std::string
  412. cmExtraCodeLiteGenerator::GetRebuildCommand(const cmMakefile* mf) const
  413. {
  414. return GetCleanCommand(mf) + cmXMLSafe(" && ").str() + GetBuildCommand(mf);
  415. }
  416. std::string
  417. cmExtraCodeLiteGenerator::GetSingleFileBuildCommand
  418. (const cmMakefile* mf) const
  419. {
  420. std::string buildCommand;
  421. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  422. std::string generator = mf->GetSafeDefinition("CMAKE_GENERATOR");
  423. if ( generator == "Unix Makefiles" || generator == "MinGW Makefiles" )
  424. {
  425. std::ostringstream ss;
  426. ss << make << " -f$(ProjectPath)/Makefile $(CurrentFileName).cpp.o";
  427. buildCommand = ss.str();
  428. }
  429. return buildCommand;
  430. }