123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767 |
- #include "cmExtraCodeBlocksGenerator.h"
- #include <map>
- #include <ostream>
- #include <set>
- #include <utility>
- #include "cmAlgorithms.h"
- #include "cmGeneratedFileStream.h"
- #include "cmGeneratorTarget.h"
- #include "cmGlobalGenerator.h"
- #include "cmLocalGenerator.h"
- #include "cmMakefile.h"
- #include "cmSourceFile.h"
- #include "cmStateTypes.h"
- #include "cmSystemTools.h"
- #include "cmXMLWriter.h"
- #include "cmake.h"
- cmExtraCodeBlocksGenerator::cmExtraCodeBlocksGenerator()
- : cmExternalMakefileProjectGenerator()
- {
- }
- cmExternalMakefileProjectGeneratorFactory*
- cmExtraCodeBlocksGenerator::GetFactory()
- {
- static cmExternalMakefileProjectGeneratorSimpleFactory<
- cmExtraCodeBlocksGenerator>
- factory("CodeBlocks", "Generates CodeBlocks project files.");
- if (factory.GetSupportedGlobalGenerators().empty()) {
- #if defined(_WIN32)
- factory.AddSupportedGlobalGenerator("MinGW Makefiles");
- factory.AddSupportedGlobalGenerator("NMake Makefiles");
- factory.AddSupportedGlobalGenerator("NMake Makefiles JOM");
- #endif
- factory.AddSupportedGlobalGenerator("Ninja");
- factory.AddSupportedGlobalGenerator("Unix Makefiles");
- }
- return &factory;
- }
- void cmExtraCodeBlocksGenerator::Generate()
- {
-
- for (auto const& it : this->GlobalGenerator->GetProjectMap()) {
-
- this->CreateProjectFile(it.second);
- }
- }
- void cmExtraCodeBlocksGenerator::CreateProjectFile(
- const std::vector<cmLocalGenerator*>& lgs)
- {
- std::string outputDir = lgs[0]->GetCurrentBinaryDirectory();
- std::string projectName = lgs[0]->GetProjectName();
- std::string filename = outputDir + "/";
- filename += projectName + ".cbp";
- std::string sessionFilename = outputDir + "/";
- sessionFilename += projectName + ".layout";
- this->CreateNewProjectFile(lgs, filename);
- }
- struct Tree
- {
- std::string path;
- std::vector<Tree> folders;
- std::set<std::string> files;
- void InsertPath(const std::vector<std::string>& splitted,
- std::vector<std::string>::size_type start,
- const std::string& fileName);
- void BuildVirtualFolder(cmXMLWriter& xml) const;
- void BuildVirtualFolderImpl(std::string& virtualFolders,
- const std::string& prefix) const;
- void BuildUnit(cmXMLWriter& xml, const std::string& fsPath) const;
- void BuildUnitImpl(cmXMLWriter& xml, const std::string& virtualFolderPath,
- const std::string& fsPath) const;
- };
- void Tree::InsertPath(const std::vector<std::string>& splitted,
- std::vector<std::string>::size_type start,
- const std::string& fileName)
- {
- if (start == splitted.size()) {
- files.insert(fileName);
- return;
- }
- for (Tree& folder : folders) {
- if (folder.path == splitted[start]) {
- if (start + 1 < splitted.size()) {
- folder.InsertPath(splitted, start + 1, fileName);
- return;
- }
-
- folder.files.insert(fileName);
- return;
- }
- }
-
- Tree newFolder;
- newFolder.path = splitted[start];
- if (start + 1 < splitted.size()) {
- newFolder.InsertPath(splitted, start + 1, fileName);
- folders.push_back(newFolder);
- return;
- }
-
- newFolder.files.insert(fileName);
- folders.push_back(newFolder);
- }
- void Tree::BuildVirtualFolder(cmXMLWriter& xml) const
- {
- xml.StartElement("Option");
- std::string virtualFolders = "CMake Files\\;";
- for (Tree const& folder : folders) {
- folder.BuildVirtualFolderImpl(virtualFolders, "");
- }
- xml.Attribute("virtualFolders", virtualFolders);
- xml.EndElement();
- }
- void Tree::BuildVirtualFolderImpl(std::string& virtualFolders,
- const std::string& prefix) const
- {
- virtualFolders += "CMake Files\\" + prefix + path + "\\;";
- for (Tree const& folder : folders) {
- folder.BuildVirtualFolderImpl(virtualFolders, prefix + path + "\\");
- }
- }
- void Tree::BuildUnit(cmXMLWriter& xml, const std::string& fsPath) const
- {
- for (std::string const& f : files) {
- xml.StartElement("Unit");
- xml.Attribute("filename", fsPath + f);
- xml.StartElement("Option");
- xml.Attribute("virtualFolder", "CMake Files\\");
- xml.EndElement();
- xml.EndElement();
- }
- for (Tree const& folder : folders) {
- folder.BuildUnitImpl(xml, "", fsPath);
- }
- }
- void Tree::BuildUnitImpl(cmXMLWriter& xml,
- const std::string& virtualFolderPath,
- const std::string& fsPath) const
- {
- for (std::string const& f : files) {
- xml.StartElement("Unit");
- xml.Attribute("filename", fsPath + path + "/" + f);
- xml.StartElement("Option");
- xml.Attribute("virtualFolder",
- "CMake Files\\" + virtualFolderPath + path + "\\");
- xml.EndElement();
- xml.EndElement();
- }
- for (Tree const& folder : folders) {
- folder.BuildUnitImpl(xml, virtualFolderPath + path + "\\",
- fsPath + path + "/");
- }
- }
- void cmExtraCodeBlocksGenerator::CreateNewProjectFile(
- const std::vector<cmLocalGenerator*>& lgs, const std::string& filename)
- {
- const cmMakefile* mf = lgs[0]->GetMakefile();
- cmGeneratedFileStream fout(filename.c_str());
- if (!fout) {
- return;
- }
- Tree tree;
-
- for (auto const& it : this->GlobalGenerator->GetProjectMap()) {
-
- std::vector<std::string> listFiles;
- for (cmLocalGenerator* lg : it.second) {
- const std::vector<std::string>& files =
- lg->GetMakefile()->GetListFiles();
- listFiles.insert(listFiles.end(), files.begin(), files.end());
- }
-
- for (std::string const& listFile : listFiles) {
-
- if (listFile.find(cmSystemTools::GetCMakeRoot()) == 0) {
- continue;
- }
- const std::string& relative = cmSystemTools::RelativePath(
- it.second[0]->GetSourceDirectory(), listFile);
- std::vector<std::string> splitted;
- cmSystemTools::SplitPath(relative, splitted, false);
-
- std::string fileName = *(splitted.end() - 1);
- splitted.erase(splitted.end() - 1, splitted.end());
-
-
-
-
-
-
- const bool excludeExternal =
- cmSystemTools::IsOn(it.second[0]->GetMakefile()->GetSafeDefinition(
- "CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES"));
- if (!splitted.empty() &&
- (!excludeExternal || (relative.find("..") == std::string::npos)) &&
- relative.find("CMakeFiles") == std::string::npos) {
- tree.InsertPath(splitted, 1, fileName);
- }
- }
- }
-
- std::string compiler = this->GetCBCompilerId(mf);
- std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
- const std::string makeArgs =
- mf->GetSafeDefinition("CMAKE_CODEBLOCKS_MAKE_ARGUMENTS");
- cmXMLWriter xml(fout);
- xml.StartDocument();
- xml.StartElement("CodeBlocks_project_file");
- xml.StartElement("FileVersion");
- xml.Attribute("major", 1);
- xml.Attribute("minor", 6);
- xml.EndElement();
- xml.StartElement("Project");
- xml.StartElement("Option");
- xml.Attribute("title", lgs[0]->GetProjectName());
- xml.EndElement();
- xml.StartElement("Option");
- xml.Attribute("makefile_is_custom", 1);
- xml.EndElement();
- xml.StartElement("Option");
- xml.Attribute("compiler", compiler);
- xml.EndElement();
-
- tree.BuildVirtualFolder(xml);
- xml.StartElement("Build");
- this->AppendTarget(xml, "all", nullptr, make.c_str(), lgs[0],
- compiler.c_str(), makeArgs);
-
-
- for (cmLocalGenerator* lg : lgs) {
- const std::vector<cmGeneratorTarget*>& targets = lg->GetGeneratorTargets();
- for (cmGeneratorTarget* target : targets) {
- std::string targetName = target->GetName();
- switch (target->GetType()) {
- case cmStateEnums::GLOBAL_TARGET: {
-
-
- if (lg->GetCurrentBinaryDirectory() == lg->GetBinaryDirectory()) {
- this->AppendTarget(xml, targetName, nullptr, make.c_str(), lg,
- compiler.c_str(), makeArgs);
- }
- } break;
- case cmStateEnums::UTILITY:
-
-
- if (((targetName.find("Nightly") == 0) &&
- (targetName != "Nightly")) ||
- ((targetName.find("Continuous") == 0) &&
- (targetName != "Continuous")) ||
- ((targetName.find("Experimental") == 0) &&
- (targetName != "Experimental"))) {
- break;
- }
- this->AppendTarget(xml, targetName, nullptr, make.c_str(), lg,
- compiler.c_str(), makeArgs);
- break;
- case cmStateEnums::EXECUTABLE:
- case cmStateEnums::STATIC_LIBRARY:
- case cmStateEnums::SHARED_LIBRARY:
- case cmStateEnums::MODULE_LIBRARY:
- case cmStateEnums::OBJECT_LIBRARY: {
- cmGeneratorTarget* gt = target;
- this->AppendTarget(xml, targetName, gt, make.c_str(), lg,
- compiler.c_str(), makeArgs);
- std::string fastTarget = targetName;
- fastTarget += "/fast";
- this->AppendTarget(xml, fastTarget, gt, make.c_str(), lg,
- compiler.c_str(), makeArgs);
- } break;
- default:
- break;
- }
- }
- }
- xml.EndElement();
-
-
-
- typedef std::map<std::string, CbpUnit> all_files_map_t;
- all_files_map_t allFiles;
- std::vector<std::string> cFiles;
- auto cm = this->GlobalGenerator->GetCMakeInstance();
- for (cmLocalGenerator* lg : lgs) {
- cmMakefile* makefile = lg->GetMakefile();
- const std::vector<cmGeneratorTarget*>& targets = lg->GetGeneratorTargets();
- for (cmGeneratorTarget* target : targets) {
- switch (target->GetType()) {
- case cmStateEnums::EXECUTABLE:
- case cmStateEnums::STATIC_LIBRARY:
- case cmStateEnums::SHARED_LIBRARY:
- case cmStateEnums::MODULE_LIBRARY:
- case cmStateEnums::OBJECT_LIBRARY:
- case cmStateEnums::UTILITY:
- {
- std::vector<cmSourceFile*> sources;
- cmGeneratorTarget* gt = target;
- gt->GetSourceFiles(sources,
- makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
- for (cmSourceFile* s : sources) {
-
-
- if (gt->GetType() == cmStateEnums::UTILITY &&
- s->GetPropertyAsBool("GENERATED")) {
- continue;
- }
-
- bool isCFile = false;
- std::string lang = s->GetLanguage();
- if (lang == "C" || lang == "CXX" || lang == "CUDA") {
- std::string const& srcext = s->GetExtension();
- isCFile = cm->IsSourceExtension(srcext);
- }
- std::string const& fullPath = s->GetFullPath();
-
- const std::string& relative =
- cmSystemTools::RelativePath(lg->GetSourceDirectory(), fullPath);
-
-
- const bool excludeExternal =
- cmSystemTools::IsOn(lg->GetMakefile()->GetSafeDefinition(
- "CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES"));
- if (excludeExternal &&
- (relative.find("..") != std::string::npos)) {
- continue;
- }
- if (isCFile) {
- cFiles.push_back(fullPath);
- }
- CbpUnit& cbpUnit = allFiles[fullPath];
- cbpUnit.Targets.push_back(target);
- }
- }
- default:
- break;
- }
- }
- }
- std::vector<std::string> const& headerExts =
- this->GlobalGenerator->GetCMakeInstance()->GetHeaderExtensions();
-
-
-
-
-
-
-
- for (std::string const& fileName : cFiles) {
- std::string headerBasename = cmSystemTools::GetFilenamePath(fileName);
- headerBasename += "/";
- headerBasename += cmSystemTools::GetFilenameWithoutExtension(fileName);
-
- for (std::string const& ext : headerExts) {
- std::string hname = headerBasename;
- hname += ".";
- hname += ext;
-
- if (allFiles.find(hname) != allFiles.end()) {
- break;
- }
- if (cmSystemTools::FileExists(hname)) {
- allFiles[hname].Targets = allFiles[fileName].Targets;
- break;
- }
- }
- }
-
- for (auto const& s : allFiles) {
- std::string const& unitFilename = s.first;
- CbpUnit const& unit = s.second;
- xml.StartElement("Unit");
- xml.Attribute("filename", unitFilename);
- for (cmGeneratorTarget const* tgt : unit.Targets) {
- xml.StartElement("Option");
- xml.Attribute("target", tgt->GetName());
- xml.EndElement();
- }
- xml.EndElement();
- }
-
- tree.BuildUnit(xml, std::string(mf->GetHomeDirectory()) + "/");
- xml.EndElement();
- xml.EndElement();
- xml.EndDocument();
- }
- std::string cmExtraCodeBlocksGenerator::CreateDummyTargetFile(
- cmLocalGenerator* lg, cmGeneratorTarget* target) const
- {
-
-
-
- std::string filename = lg->GetCurrentBinaryDirectory();
- filename += "/";
- filename += lg->GetTargetDirectory(target);
- filename += "/";
- filename += target->GetName();
- filename += ".objlib";
- cmGeneratedFileStream fout(filename.c_str());
- if (fout) {
-
- fout << "# This is a dummy file for the OBJECT library "
- << target->GetName()
- << " for the CMake CodeBlocks project generator.\n"
- << "# Don't edit, this file will be overwritten.\n";
-
- }
- return filename;
- }
- void cmExtraCodeBlocksGenerator::AppendTarget(
- cmXMLWriter& xml, const std::string& targetName, cmGeneratorTarget* target,
- const char* make, const cmLocalGenerator* lg, const char* compiler,
- const std::string& makeFlags)
- {
- cmMakefile const* makefile = lg->GetMakefile();
- std::string makefileName = lg->GetCurrentBinaryDirectory();
- makefileName += "/Makefile";
- xml.StartElement("Target");
- xml.Attribute("title", targetName);
- if (target != nullptr) {
- int cbTargetType = this->GetCBTargetType(target);
- std::string workingDir = lg->GetCurrentBinaryDirectory();
- if (target->GetType() == cmStateEnums::EXECUTABLE) {
-
-
- const char* runtimeOutputDir =
- makefile->GetDefinition("CMAKE_RUNTIME_OUTPUT_DIRECTORY");
- if (runtimeOutputDir != nullptr) {
- workingDir = runtimeOutputDir;
- } else {
- const char* executableOutputDir =
- makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
- if (executableOutputDir != nullptr) {
- workingDir = executableOutputDir;
- }
- }
- }
- std::string buildType = makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
- std::string location;
- if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
- location =
- this->CreateDummyTargetFile(const_cast<cmLocalGenerator*>(lg), target);
- } else {
- location = target->GetLocation(buildType);
- }
- xml.StartElement("Option");
- xml.Attribute("output", location);
- xml.Attribute("prefix_auto", 0);
- xml.Attribute("extension_auto", 0);
- xml.EndElement();
- xml.StartElement("Option");
- xml.Attribute("working_dir", workingDir);
- xml.EndElement();
- xml.StartElement("Option");
- xml.Attribute("object_output", "./");
- xml.EndElement();
- xml.StartElement("Option");
- xml.Attribute("type", cbTargetType);
- xml.EndElement();
- xml.StartElement("Option");
- xml.Attribute("compiler", compiler);
- xml.EndElement();
- xml.StartElement("Compiler");
-
- std::vector<std::string> cdefs;
- target->GetCompileDefinitions(cdefs, buildType, "C");
-
- for (std::string const& d : cdefs) {
- xml.StartElement("Add");
- xml.Attribute("option", "-D" + d);
- xml.EndElement();
- }
-
- std::vector<std::string> allIncludeDirs;
- std::vector<std::string> includes;
- lg->GetIncludeDirectories(includes, target, "C", buildType);
- allIncludeDirs.insert(allIncludeDirs.end(), includes.begin(),
- includes.end());
- std::string systemIncludeDirs = makefile->GetSafeDefinition(
- "CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS");
- if (!systemIncludeDirs.empty()) {
- std::vector<std::string> dirs;
- cmSystemTools::ExpandListArgument(systemIncludeDirs, dirs);
- allIncludeDirs.insert(allIncludeDirs.end(), dirs.begin(), dirs.end());
- }
- systemIncludeDirs = makefile->GetSafeDefinition(
- "CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS");
- if (!systemIncludeDirs.empty()) {
- std::vector<std::string> dirs;
- cmSystemTools::ExpandListArgument(systemIncludeDirs, dirs);
- allIncludeDirs.insert(allIncludeDirs.end(), dirs.begin(), dirs.end());
- }
- std::vector<std::string>::const_iterator end =
- cmRemoveDuplicates(allIncludeDirs);
- for (std::vector<std::string>::const_iterator i = allIncludeDirs.begin();
- i != end; ++i) {
- xml.StartElement("Add");
- xml.Attribute("directory", *i);
- xml.EndElement();
- }
- xml.EndElement();
- } else
- {
- xml.StartElement("Option");
- xml.Attribute("working_dir", lg->GetCurrentBinaryDirectory());
- xml.EndElement();
- xml.StartElement("Option");
- xml.Attribute("type", 4);
- xml.EndElement();
- }
- xml.StartElement("MakeCommands");
- xml.StartElement("Build");
- xml.Attribute("command", this->BuildMakeCommand(make, makefileName.c_str(),
- targetName, makeFlags));
- xml.EndElement();
- xml.StartElement("CompileFile");
- xml.Attribute("command", this->BuildMakeCommand(make, makefileName.c_str(),
- "\"$file\"", makeFlags));
- xml.EndElement();
- xml.StartElement("Clean");
- xml.Attribute("command", this->BuildMakeCommand(make, makefileName.c_str(),
- "clean", makeFlags));
- xml.EndElement();
- xml.StartElement("DistClean");
- xml.Attribute("command", this->BuildMakeCommand(make, makefileName.c_str(),
- "clean", makeFlags));
- xml.EndElement();
- xml.EndElement();
- xml.EndElement();
- }
- std::string cmExtraCodeBlocksGenerator::GetCBCompilerId(const cmMakefile* mf)
- {
-
- std::string userCompiler =
- mf->GetSafeDefinition("CMAKE_CODEBLOCKS_COMPILER_ID");
- if (!userCompiler.empty()) {
- return userCompiler;
- }
-
-
-
- bool pureFortran = false;
- std::string compilerIdVar;
- if (this->GlobalGenerator->GetLanguageEnabled("CXX")) {
- compilerIdVar = "CMAKE_CXX_COMPILER_ID";
- } else if (this->GlobalGenerator->GetLanguageEnabled("C")) {
- compilerIdVar = "CMAKE_C_COMPILER_ID";
- } else if (this->GlobalGenerator->GetLanguageEnabled("Fortran")) {
- compilerIdVar = "CMAKE_Fortran_COMPILER_ID";
- pureFortran = true;
- }
- std::string compilerId = mf->GetSafeDefinition(compilerIdVar);
- std::string compiler = "gcc";
- if (compilerId == "MSVC") {
- if (mf->IsDefinitionSet("MSVC10")) {
- compiler = "msvc10";
- } else {
- compiler = "msvc8";
- }
- } else if (compilerId == "Borland") {
- compiler = "bcc";
- } else if (compilerId == "SDCC") {
- compiler = "sdcc";
- } else if (compilerId == "Intel") {
- if (pureFortran && mf->IsDefinitionSet("WIN32")) {
- compiler = "ifcwin";
- } else {
- compiler = "icc";
- }
- } else if (compilerId == "Watcom" || compilerId == "OpenWatcom") {
- compiler = "ow";
- } else if (compilerId == "Clang") {
- compiler = "clang";
- } else if (compilerId == "PGI") {
- if (pureFortran) {
- compiler = "pgifortran";
- } else {
- compiler = "pgi";
- }
- } else if (compilerId == "GNU") {
- if (pureFortran) {
- compiler = "gfortran";
- } else {
- compiler = "gcc";
- }
- }
- return compiler;
- }
- int cmExtraCodeBlocksGenerator::GetCBTargetType(cmGeneratorTarget* target)
- {
- switch (target->GetType()) {
- case cmStateEnums::EXECUTABLE:
- if ((target->GetPropertyAsBool("WIN32_EXECUTABLE")) ||
- (target->GetPropertyAsBool("MACOSX_BUNDLE"))) {
- return 0;
- }
- return 1;
- case cmStateEnums::STATIC_LIBRARY:
- case cmStateEnums::OBJECT_LIBRARY:
- return 2;
- case cmStateEnums::SHARED_LIBRARY:
- case cmStateEnums::MODULE_LIBRARY:
- return 3;
- default:
- return 4;
- }
- }
- std::string cmExtraCodeBlocksGenerator::BuildMakeCommand(
- const std::string& make, const char* makefile, const std::string& target,
- const std::string& makeFlags)
- {
- std::string command = make;
- if (!makeFlags.empty()) {
- command += " ";
- command += makeFlags;
- }
- std::string generator = this->GlobalGenerator->GetName();
- if (generator == "NMake Makefiles" || generator == "NMake Makefiles JOM") {
-
-
-
- std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
- command += " /NOLOGO /f ";
- command += makefileName;
- command += " VERBOSE=1 ";
- command += target;
- } else if (generator == "MinGW Makefiles") {
-
-
- std::string makefileName = makefile;
- command += " -f \"";
- command += makefileName;
- command += "\" ";
- command += " VERBOSE=1 ";
- command += target;
- } else if (generator == "Ninja") {
- command += " -v ";
- command += target;
- } else {
- std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
- command += " -f \"";
- command += makefileName;
- command += "\" ";
- command += " VERBOSE=1 ";
- command += target;
- }
- return command;
- }
|