diff options
author | Marc Chevrier <marc.chevrier@gmail.com> | 2019-12-09 17:13:42 (GMT) |
---|---|---|
committer | Marc Chevrier <marc.chevrier@gmail.com> | 2019-12-09 23:24:06 (GMT) |
commit | 36c8cae2e8c23228d3292e77b9bfc65b009bb263 (patch) | |
tree | d2dae86bea8247398e6edef358a72cd661e588a7 /Source/cmGlobalUnixMakefileGenerator3.cxx | |
parent | 352999ac4647d6c5b0d81890df4531d677532843 (diff) | |
download | CMake-36c8cae2e8c23228d3292e77b9bfc65b009bb263.zip CMake-36c8cae2e8c23228d3292e77b9bfc65b009bb263.tar.gz CMake-36c8cae2e8c23228d3292e77b9bfc65b009bb263.tar.bz2 |
cmLocalGenerator: modernize memory management
Diffstat (limited to 'Source/cmGlobalUnixMakefileGenerator3.cxx')
-rw-r--r-- | Source/cmGlobalUnixMakefileGenerator3.cxx | 263 |
1 files changed, 134 insertions, 129 deletions
diff --git a/Source/cmGlobalUnixMakefileGenerator3.cxx b/Source/cmGlobalUnixMakefileGenerator3.cxx index dfc495e..fe8c3f4 100644 --- a/Source/cmGlobalUnixMakefileGenerator3.cxx +++ b/Source/cmGlobalUnixMakefileGenerator3.cxx @@ -8,6 +8,7 @@ #include <utility> #include <cm/memory> +#include <cmext/memory> #include "cmAlgorithms.h" #include "cmDocumentationEntry.h" @@ -61,10 +62,11 @@ void cmGlobalUnixMakefileGenerator3::EnableLanguage( } //! Create a local generator appropriate to this Global Generator -cmLocalGenerator* cmGlobalUnixMakefileGenerator3::CreateLocalGenerator( - cmMakefile* mf) +std::unique_ptr<cmLocalGenerator> +cmGlobalUnixMakefileGenerator3::CreateLocalGenerator(cmMakefile* mf) { - return new cmLocalUnixMakefileGenerator3(this, mf); + return std::unique_ptr<cmLocalGenerator>( + cm::make_unique<cmLocalUnixMakefileGenerator3>(this, mf)); } void cmGlobalUnixMakefileGenerator3::GetDocumentation( @@ -144,11 +146,11 @@ void cmGlobalUnixMakefileGenerator3::Generate() for (auto& pmi : this->ProgressMap) { pmi.second.WriteProgressVariables(total, current); } - for (cmLocalGenerator* lg : this->LocalGenerators) { + for (const auto& lg : this->LocalGenerators) { std::string markFileName = cmStrCat(lg->GetCurrentBinaryDirectory(), "/CMakeFiles/progress.marks"); cmGeneratedFileStream markFile(markFileName); - markFile << this->CountProgressMarksInAll(lg) << "\n"; + markFile << this->CountProgressMarksInAll(*lg) << "\n"; } // write the main makefile @@ -203,11 +205,11 @@ void cmGlobalUnixMakefileGenerator3::WriteMainMakefile2() } // get a local generator for some useful methods - cmLocalUnixMakefileGenerator3* lg = - static_cast<cmLocalUnixMakefileGenerator3*>(this->LocalGenerators[0]); + auto& lg = cm::static_reference_cast<cmLocalUnixMakefileGenerator3>( + this->LocalGenerators[0]); // Write the do not edit header. - lg->WriteDisclaimer(makefileStream); + lg.WriteDisclaimer(makefileStream); // Write the main entry point target. This must be the VERY first // target so that make with no arguments will run it. @@ -217,10 +219,10 @@ void cmGlobalUnixMakefileGenerator3::WriteMainMakefile2() depends.emplace_back("all"); // Write the rule. - lg->WriteMakeRule(makefileStream, - "Default target executed when no arguments are " - "given to make.", - "default_target", depends, no_commands, true); + lg.WriteMakeRule(makefileStream, + "Default target executed when no arguments are " + "given to make.", + "default_target", depends, no_commands, true); depends.clear(); @@ -231,7 +233,7 @@ void cmGlobalUnixMakefileGenerator3::WriteMainMakefile2() } // Write out the "special" stuff - lg->WriteSpecialTargetsTop(makefileStream); + lg.WriteSpecialTargetsTop(makefileStream); // Write the directory level rules. for (auto const& it : this->ComputeDirectoryTargets()) { @@ -239,13 +241,14 @@ void cmGlobalUnixMakefileGenerator3::WriteMainMakefile2() } // Write the target convenience rules - for (cmLocalGenerator* localGen : this->LocalGenerators) { + for (const auto& localGen : this->LocalGenerators) { this->WriteConvenienceRules2( - makefileStream, static_cast<cmLocalUnixMakefileGenerator3*>(localGen)); + makefileStream, + cm::static_reference_cast<cmLocalUnixMakefileGenerator3>(localGen)); } // Write special bottom targets - lg->WriteSpecialTargetsBottom(makefileStream); + lg.WriteSpecialTargetsBottom(makefileStream); } void cmGlobalUnixMakefileGenerator3::WriteMainCMakefile() @@ -268,12 +271,14 @@ void cmGlobalUnixMakefileGenerator3::WriteMainCMakefile() std::string makefileName = cmStrCat(this->GetCMakeInstance()->GetHomeOutputDirectory(), "/Makefile"); - // get a local generator for some useful methods - cmLocalUnixMakefileGenerator3* lg = - static_cast<cmLocalUnixMakefileGenerator3*>(this->LocalGenerators[0]); + { + // get a local generator for some useful methods + auto& lg = cm::static_reference_cast<cmLocalUnixMakefileGenerator3>( + this->LocalGenerators[0]); - // Write the do not edit header. - lg->WriteDisclaimer(cmakefileStream); + // Write the do not edit header. + lg.WriteDisclaimer(cmakefileStream); + } // Save the generator name cmakefileStream << "# The generator used is:\n" @@ -282,7 +287,7 @@ void cmGlobalUnixMakefileGenerator3::WriteMainCMakefile() // for each cmMakefile get its list of dependencies std::vector<std::string> lfiles; - for (cmLocalGenerator* localGen : this->LocalGenerators) { + for (const auto& localGen : this->LocalGenerators) { // Get the list of files contributing to this generation step. cmAppend(lfiles, localGen->GetMakefile()->GetListFiles()); } @@ -300,59 +305,61 @@ void cmGlobalUnixMakefileGenerator3::WriteMainCMakefile() lfiles.erase(new_end, lfiles.end()); #endif - // reset lg to the first makefile - lg = static_cast<cmLocalUnixMakefileGenerator3*>(this->LocalGenerators[0]); - - std::string currentBinDir = lg->GetCurrentBinaryDirectory(); - // Save the list to the cmake file. - cmakefileStream - << "# The top level Makefile was generated from the following files:\n" - << "set(CMAKE_MAKEFILE_DEPENDS\n" - << " \"CMakeCache.txt\"\n"; - for (std::string const& f : lfiles) { - cmakefileStream << " \"" - << lg->MaybeConvertToRelativePath(currentBinDir, f) - << "\"\n"; - } - cmakefileStream << " )\n\n"; - - // Build the path to the cache check file. - std::string check = - cmStrCat(this->GetCMakeInstance()->GetHomeOutputDirectory(), - "/CMakeFiles/cmake.check_cache"); - - // Set the corresponding makefile in the cmake file. - cmakefileStream << "# The corresponding makefile is:\n" - << "set(CMAKE_MAKEFILE_OUTPUTS\n" - << " \"" - << lg->MaybeConvertToRelativePath(currentBinDir, - makefileName) - << "\"\n" - << " \"" - << lg->MaybeConvertToRelativePath(currentBinDir, check) - << "\"\n"; - cmakefileStream << " )\n\n"; - - const std::string binDir = lg->GetBinaryDirectory(); - - // CMake must rerun if a byproduct is missing. { - cmakefileStream << "# Byproducts of CMake generate step:\n" - << "set(CMAKE_MAKEFILE_PRODUCTS\n"; - for (std::string const& outfile : lg->GetMakefile()->GetOutputFiles()) { + // reset lg to the first makefile + const auto& lg = cm::static_reference_cast<cmLocalUnixMakefileGenerator3>( + this->LocalGenerators[0]); + + const std::string& currentBinDir = lg.GetCurrentBinaryDirectory(); + // Save the list to the cmake file. + cmakefileStream + << "# The top level Makefile was generated from the following files:\n" + << "set(CMAKE_MAKEFILE_DEPENDS\n" + << " \"CMakeCache.txt\"\n"; + for (std::string const& f : lfiles) { cmakefileStream << " \"" - << lg->MaybeConvertToRelativePath(binDir, outfile) + << lg.MaybeConvertToRelativePath(currentBinDir, f) << "\"\n"; } + cmakefileStream << " )\n\n"; + + // Build the path to the cache check file. + std::string check = + cmStrCat(this->GetCMakeInstance()->GetHomeOutputDirectory(), + "/CMakeFiles/cmake.check_cache"); + + // Set the corresponding makefile in the cmake file. + cmakefileStream << "# The corresponding makefile is:\n" + << "set(CMAKE_MAKEFILE_OUTPUTS\n" + << " \"" + << lg.MaybeConvertToRelativePath(currentBinDir, + makefileName) + << "\"\n" + << " \"" + << lg.MaybeConvertToRelativePath(currentBinDir, check) + << "\"\n"; + cmakefileStream << " )\n\n"; + + const std::string& binDir = lg.GetBinaryDirectory(); + + // CMake must rerun if a byproduct is missing. + { + cmakefileStream << "# Byproducts of CMake generate step:\n" + << "set(CMAKE_MAKEFILE_PRODUCTS\n"; + for (std::string const& outfile : lg.GetMakefile()->GetOutputFiles()) { + cmakefileStream << " \"" + << lg.MaybeConvertToRelativePath(binDir, outfile) + << "\"\n"; + } + } // add in all the directory information files std::string tmpStr; - for (cmLocalGenerator* localGen : this->LocalGenerators) { - lg = static_cast<cmLocalUnixMakefileGenerator3*>(localGen); - tmpStr = cmStrCat(lg->GetCurrentBinaryDirectory(), + for (const auto& localGen : this->LocalGenerators) { + tmpStr = cmStrCat(localGen->GetCurrentBinaryDirectory(), "/CMakeFiles/CMakeDirectoryInformation.cmake"); cmakefileStream << " \"" - << lg->MaybeConvertToRelativePath(binDir, tmpStr) + << localGen->MaybeConvertToRelativePath(binDir, tmpStr) << "\"\n"; } cmakefileStream << " )\n\n"; @@ -364,24 +371,23 @@ void cmGlobalUnixMakefileGenerator3::WriteMainCMakefile() void cmGlobalUnixMakefileGenerator3::WriteMainCMakefileLanguageRules( cmGeneratedFileStream& cmakefileStream, - std::vector<cmLocalGenerator*>& lGenerators) + std::vector<std::unique_ptr<cmLocalGenerator>>& lGenerators) { - cmLocalUnixMakefileGenerator3* lg; - // now list all the target info files cmakefileStream << "# Dependency information for all targets:\n"; cmakefileStream << "set(CMAKE_DEPEND_INFO_FILES\n"; - for (cmLocalGenerator* lGenerator : lGenerators) { - lg = static_cast<cmLocalUnixMakefileGenerator3*>(lGenerator); + for (const auto& lGenerator : lGenerators) { + const auto& lg = + cm::static_reference_cast<cmLocalUnixMakefileGenerator3>(lGenerator); // for all of out targets - for (const auto& tgt : lg->GetGeneratorTargets()) { + for (const auto& tgt : lg.GetGeneratorTargets()) { if ((tgt->GetType() == cmStateEnums::EXECUTABLE) || (tgt->GetType() == cmStateEnums::STATIC_LIBRARY) || (tgt->GetType() == cmStateEnums::SHARED_LIBRARY) || (tgt->GetType() == cmStateEnums::MODULE_LIBRARY) || (tgt->GetType() == cmStateEnums::OBJECT_LIBRARY) || (tgt->GetType() == cmStateEnums::UTILITY)) { - std::string tname = cmStrCat(lg->GetRelativeTargetDirectory(tgt.get()), + std::string tname = cmStrCat(lg.GetRelativeTargetDirectory(tgt.get()), "/DependInfo.cmake"); cmSystemTools::ConvertToUnixSlashes(tname); cmakefileStream << " \"" << tname << "\"\n"; @@ -544,11 +550,11 @@ void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules( } // write the target convenience rules - for (cmLocalGenerator* localGen : this->LocalGenerators) { - cmLocalUnixMakefileGenerator3* lg = - static_cast<cmLocalUnixMakefileGenerator3*>(localGen); + for (const auto& localGen : this->LocalGenerators) { + auto& lg = + cm::static_reference_cast<cmLocalUnixMakefileGenerator3>(localGen); // for each target Generate the rule files for each target. - for (const auto& gtarget : lg->GetGeneratorTargets()) { + for (const auto& gtarget : lg.GetGeneratorTargets()) { // Don't emit the same rule twice (e.g. two targets with the same // simple name) int type = gtarget->GetType(); @@ -563,23 +569,23 @@ void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules( (type == cmStateEnums::OBJECT_LIBRARY) || (type == cmStateEnums::UTILITY))) { // Add a rule to build the target by name. - lg->WriteDivider(ruleFileStream); + lg.WriteDivider(ruleFileStream); ruleFileStream << "# Target rules for targets named " << name << "\n\n"; // Write the rule. commands.clear(); std::string tmp = "CMakeFiles/Makefile2"; - commands.push_back(lg->GetRecursiveMakeCall(tmp, name)); + commands.push_back(lg.GetRecursiveMakeCall(tmp, name)); depends.clear(); if (regenerate) { depends.emplace_back("cmake_check_build_system"); } - lg->WriteMakeRule(ruleFileStream, "Build rule for target.", name, - depends, commands, true); + lg.WriteMakeRule(ruleFileStream, "Build rule for target.", name, + depends, commands, true); // Add a fast rule to build the target - std::string localName = lg->GetRelativeTargetDirectory(gtarget.get()); + std::string localName = lg.GetRelativeTargetDirectory(gtarget.get()); std::string makefileName; makefileName = cmStrCat(localName, "/build.make"); depends.clear(); @@ -587,23 +593,23 @@ void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules( std::string makeTargetName = cmStrCat(localName, "/build"); localName = cmStrCat(name, "/fast"); commands.push_back( - lg->GetRecursiveMakeCall(makefileName, makeTargetName)); - lg->WriteMakeRule(ruleFileStream, "fast build rule for target.", - localName, depends, commands, true); + lg.GetRecursiveMakeCall(makefileName, makeTargetName)); + lg.WriteMakeRule(ruleFileStream, "fast build rule for target.", + localName, depends, commands, true); // Add a local name for the rule to relink the target before // installation. - if (gtarget->NeedRelinkBeforeInstall(lg->GetConfigName())) { + if (gtarget->NeedRelinkBeforeInstall(lg.GetConfigName())) { makeTargetName = cmStrCat( - lg->GetRelativeTargetDirectory(gtarget.get()), "/preinstall"); + lg.GetRelativeTargetDirectory(gtarget.get()), "/preinstall"); localName = cmStrCat(name, "/preinstall"); depends.clear(); commands.clear(); commands.push_back( - lg->GetRecursiveMakeCall(makefileName, makeTargetName)); - lg->WriteMakeRule(ruleFileStream, - "Manual pre-install relink rule for target.", - localName, depends, commands, true); + lg.GetRecursiveMakeCall(makefileName, makeTargetName)); + lg.WriteMakeRule(ruleFileStream, + "Manual pre-install relink rule for target.", + localName, depends, commands, true); } } } @@ -611,7 +617,7 @@ void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules( } void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules2( - std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3* lg) + std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3& lg) { std::vector<std::string> depends; std::vector<std::string> commands; @@ -624,7 +630,7 @@ void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules2( } // for each target Generate the rule files for each target. - for (const auto& gtarget : lg->GetGeneratorTargets()) { + for (const auto& gtarget : lg.GetGeneratorTargets()) { int type = gtarget->GetType(); std::string name = gtarget->GetName(); if (!name.empty() && @@ -636,27 +642,27 @@ void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules2( (type == cmStateEnums::UTILITY))) { std::string makefileName; // Add a rule to build the target by name. - localName = lg->GetRelativeTargetDirectory(gtarget.get()); + localName = lg.GetRelativeTargetDirectory(gtarget.get()); makefileName = cmStrCat(localName, "/build.make"); - lg->WriteDivider(ruleFileStream); + lg.WriteDivider(ruleFileStream); ruleFileStream << "# Target rules for target " << localName << "\n\n"; commands.clear(); makeTargetName = cmStrCat(localName, "/depend"); commands.push_back( - lg->GetRecursiveMakeCall(makefileName, makeTargetName)); + lg.GetRecursiveMakeCall(makefileName, makeTargetName)); makeTargetName = cmStrCat(localName, "/build"); commands.push_back( - lg->GetRecursiveMakeCall(makefileName, makeTargetName)); + lg.GetRecursiveMakeCall(makefileName, makeTargetName)); // Write the rule. localName += "/all"; depends.clear(); cmLocalUnixMakefileGenerator3::EchoProgress progress; - progress.Dir = cmStrCat(lg->GetBinaryDirectory(), "/CMakeFiles"); + progress.Dir = cmStrCat(lg.GetBinaryDirectory(), "/CMakeFiles"); { std::ostringstream progressArg; const char* sep = ""; @@ -675,13 +681,13 @@ void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules2( } if (targetMessages) { - lg->AppendEcho(commands, "Built target " + name, - cmLocalUnixMakefileGenerator3::EchoNormal, &progress); + lg.AppendEcho(commands, "Built target " + name, + cmLocalUnixMakefileGenerator3::EchoNormal, &progress); } this->AppendGlobalTargetDepends(depends, gtarget.get()); - lg->WriteMakeRule(ruleFileStream, "All Build rule for target.", - localName, depends, commands, true); + lg.WriteMakeRule(ruleFileStream, "All Build rule for target.", localName, + depends, commands, true); // Write the rule. commands.clear(); @@ -691,7 +697,7 @@ void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules2( std::ostringstream progCmd; progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # in target - progCmd << lg->ConvertToOutputFormat( + progCmd << lg.ConvertToOutputFormat( cmSystemTools::CollapseFullPath(progress.Dir), cmOutputConverter::SHELL); // @@ -701,11 +707,11 @@ void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules2( commands.push_back(progCmd.str()); } std::string tmp = "CMakeFiles/Makefile2"; - commands.push_back(lg->GetRecursiveMakeCall(tmp, localName)); + commands.push_back(lg.GetRecursiveMakeCall(tmp, localName)); { std::ostringstream progCmd; progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0 - progCmd << lg->ConvertToOutputFormat( + progCmd << lg.ConvertToOutputFormat( cmSystemTools::CollapseFullPath(progress.Dir), cmOutputConverter::SHELL); progCmd << " 0"; @@ -716,39 +722,38 @@ void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules2( depends.emplace_back("cmake_check_build_system"); } localName = - cmStrCat(lg->GetRelativeTargetDirectory(gtarget.get()), "/rule"); - lg->WriteMakeRule(ruleFileStream, - "Build rule for subdir invocation for target.", - localName, depends, commands, true); + cmStrCat(lg.GetRelativeTargetDirectory(gtarget.get()), "/rule"); + lg.WriteMakeRule(ruleFileStream, + "Build rule for subdir invocation for target.", + localName, depends, commands, true); // Add a target with the canonical name (no prefix, suffix or path). commands.clear(); depends.clear(); depends.push_back(localName); - lg->WriteMakeRule(ruleFileStream, "Convenience name for target.", name, - depends, commands, true); + lg.WriteMakeRule(ruleFileStream, "Convenience name for target.", name, + depends, commands, true); // Add rules to prepare the target for installation. - if (gtarget->NeedRelinkBeforeInstall(lg->GetConfigName())) { - localName = cmStrCat(lg->GetRelativeTargetDirectory(gtarget.get()), + if (gtarget->NeedRelinkBeforeInstall(lg.GetConfigName())) { + localName = cmStrCat(lg.GetRelativeTargetDirectory(gtarget.get()), "/preinstall"); depends.clear(); commands.clear(); - commands.push_back(lg->GetRecursiveMakeCall(makefileName, localName)); - lg->WriteMakeRule(ruleFileStream, - "Pre-install relink rule for target.", localName, - depends, commands, true); + commands.push_back(lg.GetRecursiveMakeCall(makefileName, localName)); + lg.WriteMakeRule(ruleFileStream, "Pre-install relink rule for target.", + localName, depends, commands, true); } // add the clean rule - localName = lg->GetRelativeTargetDirectory(gtarget.get()); + localName = lg.GetRelativeTargetDirectory(gtarget.get()); makeTargetName = cmStrCat(localName, "/clean"); depends.clear(); commands.clear(); commands.push_back( - lg->GetRecursiveMakeCall(makefileName, makeTargetName)); - lg->WriteMakeRule(ruleFileStream, "clean rule for target.", - makeTargetName, depends, commands, true); + lg.GetRecursiveMakeCall(makefileName, makeTargetName)); + lg.WriteMakeRule(ruleFileStream, "clean rule for target.", + makeTargetName, depends, commands, true); commands.clear(); } } @@ -760,7 +765,7 @@ void cmGlobalUnixMakefileGenerator3::InitializeProgressMarks() { this->DirectoryTargetsMap.clear(); // Loop over all targets in all local generators. - for (cmLocalGenerator* lg : this->LocalGenerators) { + for (const auto& lg : this->LocalGenerators) { for (const auto& gt : lg->GetGeneratorTargets()) { cmLocalGenerator* tlg = gt->GetLocalGenerator(); @@ -810,12 +815,12 @@ size_t cmGlobalUnixMakefileGenerator3::CountProgressMarksInTarget( } size_t cmGlobalUnixMakefileGenerator3::CountProgressMarksInAll( - cmLocalGenerator* lg) + const cmLocalGenerator& lg) { size_t count = 0; std::set<cmGeneratorTarget const*> emitted; for (cmGeneratorTarget const* target : - this->DirectoryTargetsMap[lg->GetStateSnapshot()]) { + this->DirectoryTargetsMap[lg.GetStateSnapshot()]) { count += this->CountProgressMarksInTarget(target, emitted); } return count; @@ -889,14 +894,14 @@ void cmGlobalUnixMakefileGenerator3::WriteHelpRule( std::set<std::string> emittedTargets; // for each local generator - for (cmLocalGenerator* localGen : this->LocalGenerators) { - cmLocalUnixMakefileGenerator3* lg2 = - static_cast<cmLocalUnixMakefileGenerator3*>(localGen); + for (const auto& localGen : this->LocalGenerators) { + const auto& lg2 = + cm::static_reference_cast<cmLocalUnixMakefileGenerator3>(localGen); // for the passed in makefile or if this is the top Makefile wripte out // the targets - if (lg2 == lg || lg->IsRootMakefile()) { + if (&lg2 == lg || lg->IsRootMakefile()) { // for each target Generate the rule files for each target. - for (const auto& target : lg2->GetGeneratorTargets()) { + for (const auto& target : lg2.GetGeneratorTargets()) { cmStateEnums::TargetType type = target->GetType(); if ((type == cmStateEnums::EXECUTABLE) || (type == cmStateEnums::STATIC_LIBRARY) || |