diff options
Diffstat (limited to 'Source')
30 files changed, 295 insertions, 136 deletions
diff --git a/Source/CMakeVersion.cmake b/Source/CMakeVersion.cmake index 23a44e7..fc80b1f 100644 --- a/Source/CMakeVersion.cmake +++ b/Source/CMakeVersion.cmake @@ -1,7 +1,7 @@ # CMake version number components. set(CMake_VERSION_MAJOR 3) set(CMake_VERSION_MINOR 21) -set(CMake_VERSION_PATCH 20210702) +set(CMake_VERSION_PATCH 20210713) #set(CMake_VERSION_RC 0) set(CMake_VERSION_IS_DIRTY 0) diff --git a/Source/CTest/cmCTestTestHandler.cxx b/Source/CTest/cmCTestTestHandler.cxx index c29c124..1157d10 100644 --- a/Source/CTest/cmCTestTestHandler.cxx +++ b/Source/CTest/cmCTestTestHandler.cxx @@ -2165,7 +2165,7 @@ bool cmCTestTestHandler::SetTestsProperties( // Ensure we have complete triples otherwise the data is corrupt. if (triples.size() % 3 == 0) { - cmState state; + cmState state(cmState::Unknown); rt.Backtrace = cmListFileBacktrace(state.CreateBaseSnapshot()); // the first entry represents the top of the trace so we need to diff --git a/Source/cmBinUtilsLinuxELFLinker.cxx b/Source/cmBinUtilsLinuxELFLinker.cxx index 9ce403d..a69c00d 100644 --- a/Source/cmBinUtilsLinuxELFLinker.cxx +++ b/Source/cmBinUtilsLinuxELFLinker.cxx @@ -11,6 +11,7 @@ #include <cmsys/RegularExpression.hxx> #include "cmBinUtilsLinuxELFObjdumpGetRuntimeDependenciesTool.h" +#include "cmELF.h" #include "cmLDConfigLDConfigTool.h" #include "cmMakefile.h" #include "cmMessageType.h" @@ -86,6 +87,22 @@ bool cmBinUtilsLinuxELFLinker::ScanDependencies( std::string const& file, cmStateEnums::TargetType /* unused */) { std::vector<std::string> parentRpaths; + + cmELF elf(file.c_str()); + if (!elf) { + return false; + } + if (elf.GetMachine() != 0) { + if (this->Machine != 0) { + if (elf.GetMachine() != this->Machine) { + this->SetError("All files must have the same architecture."); + return false; + } + } else { + this->Machine = elf.GetMachine(); + } + } + return this->ScanDependencies(file, parentRpaths); } @@ -150,13 +167,25 @@ bool cmBinUtilsLinuxELFLinker::ScanDependencies( return true; } +namespace { +bool FileHasArchitecture(const char* filename, std::uint16_t machine) +{ + cmELF elf(filename); + if (!elf) { + return false; + } + return machine == 0 || machine == elf.GetMachine(); +} +} + bool cmBinUtilsLinuxELFLinker::ResolveDependency( std::string const& name, std::vector<std::string> const& searchPaths, std::string& path, bool& resolved) { for (auto const& searchPath : searchPaths) { path = cmStrCat(searchPath, '/', name); - if (cmSystemTools::PathExists(path)) { + if (cmSystemTools::PathExists(path) && + FileHasArchitecture(path.c_str(), this->Machine)) { resolved = true; return true; } @@ -164,7 +193,8 @@ bool cmBinUtilsLinuxELFLinker::ResolveDependency( for (auto const& searchPath : this->Archive->GetSearchDirectories()) { path = cmStrCat(searchPath, '/', name); - if (cmSystemTools::PathExists(path)) { + if (cmSystemTools::PathExists(path) && + FileHasArchitecture(path.c_str(), this->Machine)) { std::ostringstream warning; warning << "Dependency " << name << " found in search directory:\n " << searchPath diff --git a/Source/cmBinUtilsLinuxELFLinker.h b/Source/cmBinUtilsLinuxELFLinker.h index 4e7e36d..395ed56 100644 --- a/Source/cmBinUtilsLinuxELFLinker.h +++ b/Source/cmBinUtilsLinuxELFLinker.h @@ -3,6 +3,9 @@ #pragma once +#include "cmConfigure.h" // IWYU pragma: keep + +#include <cstdint> #include <memory> #include <string> #include <vector> @@ -29,6 +32,7 @@ private: std::unique_ptr<cmLDConfigTool> LDConfigTool; bool HaveLDConfigPaths = false; std::vector<std::string> LDConfigPaths; + std::uint16_t Machine = 0; bool ScanDependencies(std::string const& file, std::vector<std::string> const& parentRpaths); diff --git a/Source/cmCommonTargetGenerator.h b/Source/cmCommonTargetGenerator.h index a156a41..e1a4f8b 100644 --- a/Source/cmCommonTargetGenerator.h +++ b/Source/cmCommonTargetGenerator.h @@ -40,6 +40,7 @@ protected: cmLocalCommonGenerator* LocalCommonGenerator; cmGlobalCommonGenerator* GlobalCommonGenerator; std::vector<std::string> ConfigNames; + bool UseLWYU = false; void AppendFortranFormatFlags(std::string& flags, cmSourceFile const& source); diff --git a/Source/cmDependsCompiler.cxx b/Source/cmDependsCompiler.cxx index 800725f..bf599ff 100644 --- a/Source/cmDependsCompiler.cxx +++ b/Source/cmDependsCompiler.cxx @@ -131,7 +131,9 @@ bool cmDependsCompiler::CheckDependencies( depends.emplace_back(std::move(line)); } } else if (format == "gcc"_s) { - auto deps = cmReadGccDepfile(depFile.c_str()); + auto deps = cmReadGccDepfile( + depFile.c_str(), this->LocalGenerator->GetCurrentBinaryDirectory(), + GccDepfilePrependPaths::Deps); if (!deps) { continue; } diff --git a/Source/cmELF.cxx b/Source/cmELF.cxx index cde7cdf..1678ce8 100644 --- a/Source/cmELF.cxx +++ b/Source/cmELF.cxx @@ -137,6 +137,9 @@ public: // Return the recorded ELF type. cmELF::FileType GetFileType() const { return this->ELFType; } + // Return the recorded machine. + std::uint16_t GetMachine() const { return this->Machine; } + protected: // Data common to all ELF class implementations. @@ -152,6 +155,9 @@ protected: // The ELF file type. cmELF::FileType ELFType; + // The ELF architecture. + std::uint16_t Machine; + // Whether we need to byte-swap structures read from the stream. bool NeedSwap; @@ -439,6 +445,8 @@ cmELFInternalImpl<Types>::cmELFInternalImpl(cmELF* external, } } + this->Machine = this->ELFHeader.e_machine; + // Load the section headers. this->SectionHeaders.resize(this->ELFHeader.e_shnum); for (ELF_Half i = 0; i < this->ELFHeader.e_shnum; ++i) { @@ -709,6 +717,14 @@ cmELF::FileType cmELF::GetFileType() const return FileTypeInvalid; } +std::uint16_t cmELF::GetMachine() const +{ + if (this->Valid()) { + return this->Internal->GetMachine(); + } + return 0; +} + unsigned int cmELF::GetNumberOfSections() const { if (this->Valid()) { diff --git a/Source/cmELF.h b/Source/cmELF.h index 5807c16..ce8bd7f 100644 --- a/Source/cmELF.h +++ b/Source/cmELF.h @@ -4,6 +4,7 @@ #include "cmConfigure.h" // IWYU pragma: keep +#include <cstdint> #include <iosfwd> #include <memory> #include <string> @@ -68,6 +69,9 @@ public: /** Get the type of the file opened. */ FileType GetFileType() const; + /** Get the machine of the file opened. */ + std::uint16_t GetMachine() const; + /** Get the number of ELF sections present. */ unsigned int GetNumberOfSections() const; diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index fba736e..a0de74c 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -478,17 +478,35 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args) this->VersionMaxPatch, this->VersionMaxTweak); } + const std::string makePackageRequiredVar = + cmStrCat("CMAKE_REQUIRE_FIND_PACKAGE_", this->Name); + const bool makePackageRequiredSet = + this->Makefile->IsOn(makePackageRequiredVar); + if (makePackageRequiredSet) { + if (this->Required) { + this->Makefile->IssueMessage( + MessageType::WARNING, + cmStrCat("for module ", this->Name, + " already called with REQUIRED, thus ", + makePackageRequiredVar, " has no effect.")); + } else { + this->Required = true; + } + } + std::string disableFindPackageVar = cmStrCat("CMAKE_DISABLE_FIND_PACKAGE_", this->Name); if (this->Makefile->IsOn(disableFindPackageVar)) { if (this->Required) { this->SetError( - cmStrCat("for module ", this->Name, " called with REQUIRED, but ", - disableFindPackageVar, + cmStrCat("for module ", this->Name, + (makePackageRequiredSet + ? " was made REQUIRED with " + makePackageRequiredVar + : " called with REQUIRED, "), + " but ", disableFindPackageVar, " is enabled. A REQUIRED package cannot be disabled.")); return false; } - return true; } diff --git a/Source/cmGccDepfileReader.cxx b/Source/cmGccDepfileReader.cxx index 6436baa..d30dbc3 100644 --- a/Source/cmGccDepfileReader.cxx +++ b/Source/cmGccDepfileReader.cxx @@ -12,8 +12,9 @@ #include "cmStringAlgorithms.h" #include "cmSystemTools.h" -cm::optional<cmGccDepfileContent> cmReadGccDepfile(const char* filePath, - const std::string& prefix) +cm::optional<cmGccDepfileContent> cmReadGccDepfile( + const char* filePath, const std::string& prefix, + GccDepfilePrependPaths prependPaths) { cmGccDepfileLexerHelper helper; if (!helper.readFile(filePath)) { @@ -23,7 +24,8 @@ cm::optional<cmGccDepfileContent> cmReadGccDepfile(const char* filePath, for (auto& dep : *deps) { for (auto& rule : dep.rules) { - if (!prefix.empty() && !cmSystemTools::FileIsFullPath(rule)) { + if (prependPaths == GccDepfilePrependPaths::All && !prefix.empty() && + !cmSystemTools::FileIsFullPath(rule)) { rule = cmStrCat(prefix, '/', rule); } if (cmSystemTools::FileIsFullPath(rule)) { diff --git a/Source/cmGccDepfileReader.h b/Source/cmGccDepfileReader.h index c8a3748..2433492 100644 --- a/Source/cmGccDepfileReader.h +++ b/Source/cmGccDepfileReader.h @@ -8,8 +8,15 @@ #include "cmGccDepfileReaderTypes.h" +enum class GccDepfilePrependPaths +{ + All, + Deps, +}; + /* - * Read dependencies file and append prefix to all relative paths + * Read dependencies file and prepend prefix to all relative paths */ cm::optional<cmGccDepfileContent> cmReadGccDepfile( - const char* filePath, const std::string& prefix = {}); + const char* filePath, const std::string& prefix = {}, + GccDepfilePrependPaths prependPaths = GccDepfilePrependPaths::All); diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index f035bd0..300c13b 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -4461,6 +4461,13 @@ std::vector<BT<std::string>> cmGeneratorTarget::GetLinkOptions( // Last step: replace "LINKER:" prefixed elements by // actual linker wrapper + return this->ResolveLinkerWrapper(result, language); +} + +std::vector<BT<std::string>>& cmGeneratorTarget::ResolveLinkerWrapper( + std::vector<BT<std::string>>& result, const std::string& language) const +{ + // replace "LINKER:" prefixed elements by actual linker wrapper const std::string wrapper(this->Makefile->GetSafeDefinition( "CMAKE_" + language + (this->IsDeviceLink() ? "_DEVICE_LINKER_WRAPPER_FLAG" diff --git a/Source/cmGeneratorTarget.h b/Source/cmGeneratorTarget.h index e1909a4..09f4167 100644 --- a/Source/cmGeneratorTarget.h +++ b/Source/cmGeneratorTarget.h @@ -498,6 +498,9 @@ public: std::vector<BT<std::string>> GetLinkOptions( std::string const& config, std::string const& language) const; + std::vector<BT<std::string>>& ResolveLinkerWrapper( + std::vector<BT<std::string>>& result, const std::string& language) const; + void GetStaticLibraryLinkOptions(std::vector<std::string>& result, const std::string& config, const std::string& language) const; diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx index cad7855..15a7304 100644 --- a/Source/cmGlobalGenerator.cxx +++ b/Source/cmGlobalGenerator.cxx @@ -1263,10 +1263,8 @@ void cmGlobalGenerator::Configure() this->CreateDefaultGlobalTargets(globalTargets); for (const auto& mf : this->Makefiles) { - auto& targets = mf->GetTargets(); for (GlobalTargetInfo const& globalTarget : globalTargets) { - targets.emplace(globalTarget.Name, - this->CreateGlobalTarget(globalTarget, mf.get())); + this->CreateGlobalTarget(globalTarget, mf.get()); } } } @@ -1783,9 +1781,8 @@ void cmGlobalGenerator::CreateGeneratorTargets( std::map<cmTarget*, cmGeneratorTarget*> const& importedMap) { if (targetTypes == AllTargets) { - for (auto& target : mf->GetTargets()) { - cmTarget* t = &target.second; - lg->AddGeneratorTarget(cm::make_unique<cmGeneratorTarget>(t, lg)); + for (cmTarget* target : mf->GetOrderedTargets()) { + lg->AddGeneratorTarget(cm::make_unique<cmGeneratorTarget>(target, lg)); } } @@ -2824,12 +2821,19 @@ bool cmGlobalGenerator::UseFolderProperty() const return false; } -cmTarget cmGlobalGenerator::CreateGlobalTarget(GlobalTargetInfo const& gti, - cmMakefile* mf) +void cmGlobalGenerator::CreateGlobalTarget(GlobalTargetInfo const& gti, + cmMakefile* mf) { // Package - cmTarget target(gti.Name, cmStateEnums::GLOBAL_TARGET, - cmTarget::VisibilityNormal, mf, gti.PerConfig); + auto tb = + mf->CreateNewTarget(gti.Name, cmStateEnums::GLOBAL_TARGET, gti.PerConfig); + + // Do nothing if gti.Name is already used + if (!tb.second) { + return; + } + + cmTarget& target = tb.first; target.SetProperty("EXCLUDE_FROM_ALL", "TRUE"); std::vector<std::string> no_outputs; @@ -2853,8 +2857,6 @@ cmTarget cmGlobalGenerator::CreateGlobalTarget(GlobalTargetInfo const& gti, if (this->UseFolderProperty()) { target.SetProperty("FOLDER", this->GetPredefinedTargetsFolder()); } - - return target; } std::string cmGlobalGenerator::GenerateRuleFile( diff --git a/Source/cmGlobalGenerator.h b/Source/cmGlobalGenerator.h index 147146e..f0b59bf 100644 --- a/Source/cmGlobalGenerator.h +++ b/Source/cmGlobalGenerator.h @@ -596,7 +596,7 @@ protected: void AddGlobalTarget_RebuildCache( std::vector<GlobalTargetInfo>& targets) const; void AddGlobalTarget_Install(std::vector<GlobalTargetInfo>& targets); - cmTarget CreateGlobalTarget(GlobalTargetInfo const& gti, cmMakefile* mf); + void CreateGlobalTarget(GlobalTargetInfo const& gti, cmMakefile* mf); std::string FindMakeProgramFile; std::string ConfiguredFilesPath; diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx index f513942..3994816 100644 --- a/Source/cmGlobalXCodeGenerator.cxx +++ b/Source/cmGlobalXCodeGenerator.cxx @@ -4610,7 +4610,11 @@ void cmGlobalXCodeGenerator::OutputXCodeWorkspaceSettings( switch (this->XcodeBuildSystem) { case BuildSystem::One: xout.Element("string", "Original"); - xout.Element("key", "DisableBuildSystemDeprecationWarning"); + if (this->XcodeVersion >= 130) { + xout.Element("key", "DisableBuildSystemDeprecationDiagnostic"); + } else { + xout.Element("key", "DisableBuildSystemDeprecationWarning"); + } xout.Element("true"); break; case BuildSystem::Twelve: diff --git a/Source/cmListFileCache.cxx b/Source/cmListFileCache.cxx index 4f7c959..2e444f2 100644 --- a/Source/cmListFileCache.cxx +++ b/Source/cmListFileCache.cxx @@ -548,7 +548,7 @@ void cmListFileBacktrace::PrintTitle(std::ostream& out) const } cmListFileContext lfc = this->TopEntry->Context; cmStateSnapshot bottom = this->GetBottom(); - if (!bottom.GetState()->GetIsInTryCompile()) { + if (bottom.GetState()->GetProjectKind() == cmState::ProjectKind::Normal) { lfc.FilePath = cmSystemTools::RelativeIfUnder( bottom.GetState()->GetSourceDirectory(), lfc.FilePath); } @@ -579,7 +579,7 @@ void cmListFileBacktrace::PrintCallStack(std::ostream& out) const out << "Call Stack (most recent call first):\n"; } cmListFileContext lfc = cur->Context; - if (!bottom.GetState()->GetIsInTryCompile()) { + if (bottom.GetState()->GetProjectKind() == cmState::ProjectKind::Normal) { lfc.FilePath = cmSystemTools::RelativeIfUnder( bottom.GetState()->GetSourceDirectory(), lfc.FilePath); } diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index a14f085..028952a 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -107,10 +107,9 @@ cmLocalGenerator::cmLocalGenerator(cmGlobalGenerator* gg, cmMakefile* makefile) { std::vector<std::string> cpath; cmSystemTools::GetPath(cpath, "CPATH"); - for (std::string& cp : cpath) { + for (std::string const& cp : cpath) { if (cmSystemTools::FileIsFullPath(cp)) { - cp = cmSystemTools::CollapseFullPath(cp); - this->EnvCPATH.emplace(std::move(cp)); + this->EnvCPATH.emplace_back(cmSystemTools::CollapseFullPath(cp)); } } } @@ -1247,19 +1246,31 @@ std::vector<BT<std::string>> cmLocalGenerator::GetIncludeDirectoriesImplicit( } } + bool const isCorCxx = (lang == "C" || lang == "CXX"); + + // Resolve symlinks in CPATH for comparison with resolved include paths. + // We do this here instead of when EnvCPATH is populated in case symlinks + // on disk have changed in the meantime. + std::set<std::string> resolvedEnvCPATH; + if (isCorCxx) { + for (std::string const& i : this->EnvCPATH) { + resolvedEnvCPATH.emplace(this->GlobalGenerator->GetRealPath(i)); + } + } + // Checks if this is not an excluded (implicit) include directory. - auto notExcluded = [this, &implicitSet, &implicitExclude, - &lang](std::string const& dir) { - return ( - // Do not exclude directories that are not in an excluded set. - ((!cm::contains(implicitSet, this->GlobalGenerator->GetRealPath(dir))) && - (!cm::contains(implicitExclude, dir))) + auto notExcluded = [this, &implicitSet, &implicitExclude, &resolvedEnvCPATH, + isCorCxx](std::string const& dir) -> bool { + std::string const& real_dir = this->GlobalGenerator->GetRealPath(dir); + return + // Do not exclude directories that are not in any excluded set. + !(cm::contains(implicitSet, real_dir) || + cm::contains(implicitExclude, dir)) // Do not exclude entries of the CPATH environment variable even though // they are implicitly searched by the compiler. They are meant to be // user-specified directories that can be re-ordered or converted to // -isystem without breaking real compiler builtin headers. - || - ((lang == "C" || lang == "CXX") && cm::contains(this->EnvCPATH, dir))); + || (isCorCxx && cm::contains(resolvedEnvCPATH, real_dir)); }; // Get the target-specific include directories. @@ -3047,6 +3058,30 @@ void cmLocalGenerator::AppendPositionIndependentLinkerFlags( } } +bool cmLocalGenerator::AppendLWYUFlags(std::string& flags, + const cmGeneratorTarget* target, + const std::string& lang) +{ + auto useLWYU = target->GetPropertyAsBool("LINK_WHAT_YOU_USE") && + (target->GetType() == cmStateEnums::TargetType::EXECUTABLE || + target->GetType() == cmStateEnums::TargetType::SHARED_LIBRARY || + target->GetType() == cmStateEnums::TargetType::MODULE_LIBRARY); + + if (useLWYU) { + const auto& lwyuFlag = this->GetMakefile()->GetSafeDefinition( + cmStrCat("CMAKE_", lang, "_LINK_WHAT_YOU_USE_FLAG")); + useLWYU = !lwyuFlag.empty(); + + if (useLWYU) { + std::vector<BT<std::string>> lwyuOpts; + lwyuOpts.emplace_back(lwyuFlag); + this->AppendFlags(flags, target->ResolveLinkerWrapper(lwyuOpts, lang)); + } + } + + return useLWYU; +} + void cmLocalGenerator::AppendCompileOptions(std::string& options, std::string const& options_list, const char* regex) const diff --git a/Source/cmLocalGenerator.h b/Source/cmLocalGenerator.h index 993280a..1e09b23 100644 --- a/Source/cmLocalGenerator.h +++ b/Source/cmLocalGenerator.h @@ -171,6 +171,8 @@ public: cmGeneratorTarget* target, const std::string& config, const std::string& lang); + bool AppendLWYUFlags(std::string& flags, const cmGeneratorTarget* target, + const std::string& lang); enum class IncludePathStyle { @@ -587,7 +589,7 @@ protected: std::string::size_type ObjectPathMax; std::set<std::string> ObjectMaxPathViolations; - std::set<std::string> EnvCPATH; + std::vector<std::string> EnvCPATH; using GeneratorTargetMap = std::unordered_map<std::string, cmGeneratorTarget*>; diff --git a/Source/cmLocalVisualStudioGenerator.cxx b/Source/cmLocalVisualStudioGenerator.cxx index 002f484..46f9d31 100644 --- a/Source/cmLocalVisualStudioGenerator.cxx +++ b/Source/cmLocalVisualStudioGenerator.cxx @@ -171,7 +171,7 @@ std::string cmLocalVisualStudioGenerator::ConstructScript( // for visual studio IDE add extra stuff to the PATH // if CMAKE_MSVCIDE_RUN_PATH is set. - if (this->Makefile->GetDefinition("MSVC_IDE")) { + if (this->GetGlobalGenerator()->IsVisualStudio()) { cmProp extraPath = this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH"); if (extraPath) { script += newline; diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index f6ecf8e..6d477a7 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -2118,15 +2118,23 @@ cmTarget* cmMakefile::AddExecutable(const std::string& exeName, cmTarget* cmMakefile::AddNewTarget(cmStateEnums::TargetType type, const std::string& name) { - auto it = this->Targets - .emplace(name, - cmTarget(name, type, cmTarget::VisibilityNormal, this, - cmTarget::PerConfig::Yes)) - .first; + return &this->CreateNewTarget(name, type).first; +} + +std::pair<cmTarget&, bool> cmMakefile::CreateNewTarget( + const std::string& name, cmStateEnums::TargetType type, + cmTarget::PerConfig perConfig) +{ + auto ib = this->Targets.emplace( + name, cmTarget(name, type, cmTarget::VisibilityNormal, this, perConfig)); + auto it = ib.first; + if (!ib.second) { + return std::make_pair(std::ref(it->second), false); + } this->OrderedTargets.push_back(&it->second); this->GetGlobalGenerator()->IndexTarget(&it->second); this->GetStateSnapshot().GetDirectory().AddNormalTargetName(name); - return &it->second; + return std::make_pair(std::ref(it->second), true); } cmTarget* cmMakefile::AddNewUtilityTarget(const std::string& utilityName, @@ -3559,8 +3567,8 @@ int cmMakefile::TryCompile(const std::string& srcdir, // make sure the same generator is used // use this program as the cmake to be run, it should not // be run that way but the cmake object requires a vailid path - cmake cm(cmake::RoleProject, cmState::Project); - cm.SetIsInTryCompile(true); + cmake cm(cmake::RoleProject, cmState::Project, + cmState::ProjectKind::TryCompile); auto gg = cm.CreateGlobalGenerator(this->GetGlobalGenerator()->GetName()); if (!gg) { this->IssueMessage(MessageType::INTERNAL_ERROR, diff --git a/Source/cmMakefile.h b/Source/cmMakefile.h index 14c1a0f..5886c86 100644 --- a/Source/cmMakefile.h +++ b/Source/cmMakefile.h @@ -13,6 +13,7 @@ #include <stack> #include <string> #include <unordered_map> +#include <utility> #include <vector> #include <cm/optional> @@ -230,6 +231,10 @@ public: cmTarget* AddImportedTarget(const std::string& name, cmStateEnums::TargetType type, bool global); + std::pair<cmTarget&, bool> CreateNewTarget( + const std::string& name, cmStateEnums::TargetType type, + cmTarget::PerConfig perConfig = cmTarget::PerConfig::Yes); + cmTarget* AddNewTarget(cmStateEnums::TargetType type, const std::string& name); diff --git a/Source/cmMakefileExecutableTargetGenerator.cxx b/Source/cmMakefileExecutableTargetGenerator.cxx index 3a2744e..306b38f 100644 --- a/Source/cmMakefileExecutableTargetGenerator.cxx +++ b/Source/cmMakefileExecutableTargetGenerator.cxx @@ -397,9 +397,8 @@ void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink) this->LocalGenerator->GetLinkLibsCMP0065( linkLanguage, *this->GeneratorTarget)); - if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE")) { - this->LocalGenerator->AppendFlags(linkFlags, " -Wl,--no-as-needed"); - } + this->UseLWYU = this->LocalGenerator->AppendLWYUFlags( + linkFlags, this->GeneratorTarget, linkLanguage); // Add language feature flags. this->LocalGenerator->AddLanguageFlagsForLinking( @@ -577,12 +576,18 @@ void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink) vars.Launcher = linkerLauncher.c_str(); } - if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE")) { - std::string cmakeCommand = - cmStrCat(this->LocalGenerator->ConvertToOutputFormat( - cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL), - " -E __run_co_compile --lwyu=", targetOutPathReal); - real_link_commands.push_back(std::move(cmakeCommand)); + if (this->UseLWYU) { + cmProp lwyuCheck = + this->Makefile->GetDefinition("CMAKE_LINK_WHAT_YOU_USE_CHECK"); + if (lwyuCheck) { + std::string cmakeCommand = cmStrCat( + this->LocalGenerator->ConvertToOutputFormat( + cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL), + " -E __run_co_compile --lwyu="); + cmakeCommand += this->LocalGenerator->EscapeForShell(*lwyuCheck); + cmakeCommand += cmStrCat(" --source=", targetOutPathReal); + real_link_commands.push_back(std::move(cmakeCommand)); + } } std::string launcher; diff --git a/Source/cmMakefileLibraryTargetGenerator.cxx b/Source/cmMakefileLibraryTargetGenerator.cxx index d0e3837..64992f2 100644 --- a/Source/cmMakefileLibraryTargetGenerator.cxx +++ b/Source/cmMakefileLibraryTargetGenerator.cxx @@ -178,9 +178,9 @@ void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink) this->AddModuleDefinitionFlag(linkLineComputer.get(), extraFlags, this->GetConfigName()); - if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE")) { - this->LocalGenerator->AppendFlags(extraFlags, " -Wl,--no-as-needed"); - } + this->UseLWYU = this->LocalGenerator->AppendLWYUFlags( + extraFlags, this->GeneratorTarget, linkLanguage); + this->WriteLibraryRules(linkRuleVar, extraFlags, relink); } @@ -871,13 +871,18 @@ void cmMakefileLibraryTargetGenerator::WriteLibraryRules( // Get the set of commands. std::string linkRule = this->GetLinkRule(linkRuleVar); cmExpandList(linkRule, real_link_commands); - if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE") && - (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY)) { - std::string cmakeCommand = cmStrCat( - this->LocalGenerator->ConvertToOutputFormat( - cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL), - " -E __run_co_compile --lwyu=", targetOutPathReal); - real_link_commands.push_back(std::move(cmakeCommand)); + if (this->UseLWYU) { + cmProp lwyuCheck = + this->Makefile->GetDefinition("CMAKE_LINK_WHAT_YOU_USE_CHECK"); + if (lwyuCheck) { + std::string cmakeCommand = cmStrCat( + this->LocalGenerator->ConvertToOutputFormat( + cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL), + " -E __run_co_compile --lwyu="); + cmakeCommand += this->LocalGenerator->EscapeForShell(*lwyuCheck); + cmakeCommand += cmStrCat(" --source=", targetOutPathReal); + real_link_commands.push_back(std::move(cmakeCommand)); + } } // Expand placeholders. diff --git a/Source/cmNinjaNormalTargetGenerator.cxx b/Source/cmNinjaNormalTargetGenerator.cxx index 5a4c652..93a54c2 100644 --- a/Source/cmNinjaNormalTargetGenerator.cxx +++ b/Source/cmNinjaNormalTargetGenerator.cxx @@ -581,17 +581,23 @@ std::vector<std::string> cmNinjaNormalTargetGenerator::ComputeLinkCmd( } } cmExpandList(linkCmdStr, linkCmds); - if (this->GetGeneratorTarget()->GetPropertyAsBool("LINK_WHAT_YOU_USE")) { - std::string cmakeCommand = cmStrCat( - this->GetLocalGenerator()->ConvertToOutputFormat( - cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL), - " -E __run_co_compile --lwyu="); - cmGeneratorTarget& gt = *this->GetGeneratorTarget(); - std::string targetOutputReal = this->ConvertToNinjaPath( - gt.GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact, - /*realname=*/true)); - cmakeCommand += targetOutputReal; - linkCmds.push_back(std::move(cmakeCommand)); + if (this->UseLWYU) { + cmProp lwyuCheck = mf->GetDefinition("CMAKE_LINK_WHAT_YOU_USE_CHECK"); + if (lwyuCheck) { + std::string cmakeCommand = cmStrCat( + this->GetLocalGenerator()->ConvertToOutputFormat( + cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL), + " -E __run_co_compile --lwyu="); + cmakeCommand += + this->GetLocalGenerator()->EscapeForShell(*lwyuCheck); + + std::string targetOutputReal = + this->ConvertToNinjaPath(this->GetGeneratorTarget()->GetFullPath( + config, cmStateEnums::RuntimeBinaryArtifact, + /*realname=*/true)); + cmakeCommand += cmStrCat(" --source=", targetOutputReal); + linkCmds.push_back(std::move(cmakeCommand)); + } } return linkCmds; } @@ -1156,9 +1162,11 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement( this->AddModuleDefinitionFlag(linkLineComputer.get(), vars["LINK_FLAGS"], config); - if (gt->GetPropertyAsBool("LINK_WHAT_YOU_USE")) { - vars["LINK_FLAGS"] += " -Wl,--no-as-needed"; - } + + this->UseLWYU = this->GetLocalGenerator()->AppendLWYUFlags( + vars["LINK_FLAGS"], this->GetGeneratorTarget(), + this->TargetLinkLanguage(config)); + vars["LINK_FLAGS"] = globalGen->EncodeLiteral(vars["LINK_FLAGS"]); vars["MANIFESTS"] = this->GetManifests(config); diff --git a/Source/cmState.cxx b/Source/cmState.cxx index ce6eb31..bdb5bda 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -26,7 +26,9 @@ #include "cmSystemTools.h" #include "cmake.h" -cmState::cmState() +cmState::cmState(Mode mode, ProjectKind projectKind) + : StateMode(mode) + , StateProjectKind(projectKind) { this->CacheManager = cm::make_unique<cmCacheManager>(); this->GlobVerificationManager = cm::make_unique<cmGlobVerificationManager>(); @@ -381,16 +383,6 @@ void cmState::ClearEnabledLanguages() this->EnabledLanguages.clear(); } -bool cmState::GetIsInTryCompile() const -{ - return this->IsInTryCompile; -} - -void cmState::SetIsInTryCompile(bool b) -{ - this->IsInTryCompile = b; -} - bool cmState::GetIsGeneratorMultiConfig() const { return this->IsGeneratorMultiConfig; @@ -593,8 +585,9 @@ cmProp cmState::GetGlobalProperty(const std::string& prop) std::vector<std::string> commands = this->GetCommandNames(); this->SetGlobalProperty("COMMANDS", cmJoin(commands, ";").c_str()); } else if (prop == "IN_TRY_COMPILE") { - this->SetGlobalProperty("IN_TRY_COMPILE", - this->IsInTryCompile ? "1" : "0"); + this->SetGlobalProperty( + "IN_TRY_COMPILE", + this->StateProjectKind == ProjectKind::TryCompile ? "1" : "0"); } else if (prop == "GENERATOR_IS_MULTI_CONFIG") { this->SetGlobalProperty("GENERATOR_IS_MULTI_CONFIG", this->IsGeneratorMultiConfig ? "1" : "0"); @@ -771,17 +764,12 @@ unsigned int cmState::GetCacheMinorVersion() const cmState::Mode cmState::GetMode() const { - return this->CurrentMode; + return this->StateMode; } std::string cmState::GetModeString() const { - return ModeToString(this->CurrentMode); -} - -void cmState::SetMode(cmState::Mode mode) -{ - this->CurrentMode = mode; + return ModeToString(this->StateMode); } std::string cmState::ModeToString(cmState::Mode mode) @@ -803,6 +791,11 @@ std::string cmState::ModeToString(cmState::Mode mode) return "UNKNOWN"; } +cmState::ProjectKind cmState::GetProjectKind() const +{ + return this->StateProjectKind; +} + std::string const& cmState::GetBinaryDirectory() const { return this->BinaryDirectory; diff --git a/Source/cmState.h b/Source/cmState.h index 9951b9a..8561fc0 100644 --- a/Source/cmState.h +++ b/Source/cmState.h @@ -35,12 +35,6 @@ class cmState friend class cmStateSnapshot; public: - cmState(); - ~cmState(); - - cmState(const cmState&) = delete; - cmState& operator=(const cmState&) = delete; - enum Mode { Unknown, @@ -51,6 +45,18 @@ public: CPack, }; + enum class ProjectKind + { + Normal, + TryCompile, + }; + + cmState(Mode mode, ProjectKind projectKind = ProjectKind::Normal); + ~cmState(); + + cmState(const cmState&) = delete; + cmState& operator=(const cmState&) = delete; + static const std::string& GetTargetTypeName( cmStateEnums::TargetType targetType); @@ -141,9 +147,6 @@ public: void SetEnabledLanguages(std::vector<std::string> const& langs); void ClearEnabledLanguages(); - bool GetIsInTryCompile() const; - void SetIsInTryCompile(bool b); - bool GetIsGeneratorMultiConfig() const; void SetIsGeneratorMultiConfig(bool b); @@ -207,10 +210,11 @@ public: Mode GetMode() const; std::string GetModeString() const; - void SetMode(Mode mode); static std::string ModeToString(Mode mode); + ProjectKind GetProjectKind() const; + private: friend class cmake; void AddCacheEntry(const std::string& key, const char* value, @@ -248,7 +252,6 @@ private: std::string SourceDirectory; std::string BinaryDirectory; - bool IsInTryCompile = false; bool IsGeneratorMultiConfig = false; bool WindowsShell = false; bool WindowsVSIDE = false; @@ -258,5 +261,6 @@ private: bool NMake = false; bool MSYSShell = false; bool NinjaMulti = false; - Mode CurrentMode = Unknown; + Mode StateMode = Unknown; + ProjectKind StateProjectKind = ProjectKind::Normal; }; diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 14f66d9..ab8309d 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -156,17 +156,16 @@ static void cmWarnUnusedCliWarning(const std::string& variable, int /*unused*/, } #endif -cmake::cmake(Role role, cmState::Mode mode) +cmake::cmake(Role role, cmState::Mode mode, cmState::ProjectKind projectKind) : CMakeWorkingDirectory(cmSystemTools::GetCurrentWorkingDirectory()) , FileTimeCache(cm::make_unique<cmFileTimeCache>()) #ifndef CMAKE_BOOTSTRAP , VariableWatch(cm::make_unique<cmVariableWatch>()) #endif - , State(cm::make_unique<cmState>()) + , State(cm::make_unique<cmState>(mode, projectKind)) , Messenger(cm::make_unique<cmMessenger>()) { this->TraceFile.close(); - this->State->SetMode(mode); this->CurrentSnapshot = this->State->CreateBaseSnapshot(); #ifdef __APPLE__ @@ -1687,6 +1686,9 @@ void cmake::PrintPresetList(const cmCMakePresetsFile& file) const this->GetRegisteredGenerators(generators, false); auto filter = [&generators](const cmCMakePresetsFile::ConfigurePreset& preset) -> bool { + if (preset.Generator.empty()) { + return true; + } auto condition = [&preset](const GeneratorInfo& info) -> bool { return info.name == preset.Generator; }; @@ -1840,7 +1842,7 @@ int cmake::HandleDeleteCacheVariables(const std::string& var) std::vector<std::string> argsSplit = cmExpandedList(var, true); // erase the property to avoid infinite recursion this->State->SetGlobalProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_", ""); - if (this->State->GetIsInTryCompile()) { + if (this->GetIsInTryCompile()) { return 0; } std::vector<SaveCacheEntry> saved; @@ -2108,7 +2110,7 @@ int cmake::ActualConfigure() // reset any system configuration information, except for when we are // InTryCompile. With TryCompile the system info is taken from the parent's // info to save time - if (!this->State->GetIsInTryCompile()) { + if (!this->GetIsInTryCompile()) { this->GlobalGenerator->ClearEnabledLanguages(); this->TruncateOutputLog("CMakeOutput.log"); @@ -2619,19 +2621,14 @@ void cmake::SetProgressCallback(ProgressCallbackType f) void cmake::UpdateProgress(const std::string& msg, float prog) { - if (this->ProgressCallback && !this->State->GetIsInTryCompile()) { + if (this->ProgressCallback && !this->GetIsInTryCompile()) { this->ProgressCallback(msg, prog); } } bool cmake::GetIsInTryCompile() const { - return this->State->GetIsInTryCompile(); -} - -void cmake::SetIsInTryCompile(bool b) -{ - this->State->SetIsInTryCompile(b); + return this->State->GetProjectKind() == cmState::ProjectKind::TryCompile; } void cmake::AppendGlobalGeneratorsDocumentation( diff --git a/Source/cmake.h b/Source/cmake.h index 5a2a88f..12cce7e 100644 --- a/Source/cmake.h +++ b/Source/cmake.h @@ -168,7 +168,8 @@ public: static const int DEFAULT_BUILD_PARALLEL_LEVEL = 0; /// Default constructor - cmake(Role role, cmState::Mode mode); + cmake(Role role, cmState::Mode mode, + cmState::ProjectKind projectKind = cmState::ProjectKind::Normal); /// Destructor ~cmake(); @@ -356,7 +357,6 @@ public: //! Is this cmake running as a result of a TRY_COMPILE command bool GetIsInTryCompile() const; - void SetIsInTryCompile(bool b); #ifndef CMAKE_BOOTSTRAP void SetWarningFromPreset(const std::string& name, diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx index 1f4c0b8..1e0f497 100644 --- a/Source/cmcmd.cxx +++ b/Source/cmcmd.cxx @@ -385,18 +385,15 @@ int HandleTidy(const std::string& runCmd, const std::string& sourceFile, return ret; } -int HandleLWYU(const std::string& runCmd, const std::string& /* sourceFile */, +int HandleLWYU(const std::string& runCmd, const std::string& sourceFile, const std::vector<std::string>&) { // Construct the ldd -r -u (link what you use lwyu) command line // ldd -u -r lwuy target - std::vector<std::string> lwyu_cmd; - lwyu_cmd.emplace_back("ldd"); - lwyu_cmd.emplace_back("-u"); - lwyu_cmd.emplace_back("-r"); - lwyu_cmd.push_back(runCmd); + std::vector<std::string> lwyu_cmd = cmExpandedList(runCmd, true); + lwyu_cmd.push_back(sourceFile); - // Run the ldd -u -r command line. + // Run the lwyu check command line, currently ldd is expected. // Capture its stdout and hide its stderr. // Ignore its return code because the tool always returns non-zero // if there are any warnings, but we just want to warn. |