diff options
Diffstat (limited to 'Source/cmLocalGenerator.cxx')
-rw-r--r-- | Source/cmLocalGenerator.cxx | 585 |
1 files changed, 357 insertions, 228 deletions
diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index 2c923dd..22e5d32 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -20,7 +20,6 @@ #include "cmsys/RegularExpression.hxx" -#include "cmAlgorithms.h" #include "cmComputeLinkInformation.h" #include "cmCustomCommand.h" #include "cmCustomCommandGenerator.h" @@ -35,7 +34,6 @@ #include "cmInstallScriptGenerator.h" #include "cmInstallTargetGenerator.h" #include "cmLinkLineComputer.h" -#include "cmLinkLineDeviceComputer.h" #include "cmMakefile.h" #include "cmRulePlaceholderExpander.h" #include "cmSourceFile.h" @@ -310,29 +308,35 @@ void cmLocalGenerator::GenerateTestFiles() cmGeneratedFileStream fout(file); fout.SetCopyIfDifferent(true); - fout << "# CMake generated Testfile for " << std::endl - << "# Source directory: " - << this->StateSnapshot.GetDirectory().GetCurrentSource() << std::endl - << "# Build directory: " - << this->StateSnapshot.GetDirectory().GetCurrentBinary() << std::endl - << "# " << std::endl - << "# This file includes the relevant testing commands " - << "required for " << std::endl - << "# testing this directory and lists subdirectories to " - << "be tested as well." << std::endl; - - const char* testIncludeFile = - this->Makefile->GetProperty("TEST_INCLUDE_FILE"); + fout << "# CMake generated Testfile for \n" + "# Source directory: " + << this->StateSnapshot.GetDirectory().GetCurrentSource() + << "\n" + "# Build directory: " + << this->StateSnapshot.GetDirectory().GetCurrentBinary() + << "\n" + "# \n" + "# This file includes the relevant testing commands " + "required for \n" + "# testing this directory and lists subdirectories to " + "be tested as well.\n"; + + std::string resourceSpecFile = + this->Makefile->GetSafeDefinition("CTEST_RESOURCE_SPEC_FILE"); + if (!resourceSpecFile.empty()) { + fout << "set(CTEST_RESOURCE_SPEC_FILE \"" << resourceSpecFile << "\")\n"; + } + + cmProp testIncludeFile = this->Makefile->GetProperty("TEST_INCLUDE_FILE"); if (testIncludeFile) { - fout << "include(\"" << testIncludeFile << "\")" << std::endl; + fout << "include(\"" << *testIncludeFile << "\")\n"; } - const char* testIncludeFiles = - this->Makefile->GetProperty("TEST_INCLUDE_FILES"); + cmProp testIncludeFiles = this->Makefile->GetProperty("TEST_INCLUDE_FILES"); if (testIncludeFiles) { - std::vector<std::string> includesList = cmExpandedList(testIncludeFiles); + std::vector<std::string> includesList = cmExpandedList(*testIncludeFiles); for (std::string const& i : includesList) { - fout << "include(\"" << i << "\")" << std::endl; + fout << "include(\"" << i << "\")\n"; } } @@ -349,18 +353,18 @@ void cmLocalGenerator::GenerateTestFiles() std::string outP = i.GetDirectory().GetCurrentBinary(); outP = this->MaybeConvertToRelativePath(parentBinDir, outP); outP = cmOutputConverter::EscapeForCMake(outP); - fout << "subdirs(" << outP << ")" << std::endl; + fout << "subdirs(" << outP << ")\n"; } // Add directory labels property const char* directoryLabels = this->Makefile->GetDefinition("CMAKE_DIRECTORY_LABELS"); - const char* labels = this->Makefile->GetProperty("LABELS"); + cmProp labels = this->Makefile->GetProperty("LABELS"); if (labels || directoryLabels) { fout << "set_directory_properties(PROPERTIES LABELS "; if (labels) { - fout << cmOutputConverter::EscapeForCMake(labels); + fout << cmOutputConverter::EscapeForCMake(*labels); } if (labels && directoryLabels) { fout << ";"; @@ -368,7 +372,7 @@ void cmLocalGenerator::GenerateTestFiles() if (directoryLabels) { fout << cmOutputConverter::EscapeForCMake(directoryLabels); } - fout << ")" << std::endl; + fout << ")\n"; } } @@ -489,16 +493,17 @@ void cmLocalGenerator::GenerateInstallRules() fout.SetCopyIfDifferent(true); // Write the header. + /* clang-format off */ fout << "# Install script for directory: " - << this->StateSnapshot.GetDirectory().GetCurrentSource() << std::endl - << std::endl; - fout << "# Set the install prefix" << std::endl - << "if(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl - << " set(CMAKE_INSTALL_PREFIX \"" << prefix << "\")" << std::endl - << "endif()" << std::endl + << this->StateSnapshot.GetDirectory().GetCurrentSource() + << "\n\n" + "# Set the install prefix\n" + "if(NOT DEFINED CMAKE_INSTALL_PREFIX)\n" + " set(CMAKE_INSTALL_PREFIX \"" << prefix << "\")\n" + "endif()\n" << R"(string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX )" - << "\"${CMAKE_INSTALL_PREFIX}\")" << std::endl - << std::endl; + << "\"${CMAKE_INSTALL_PREFIX}\")\n\n"; + /* clang-format on */ // Write support code for generating per-configuration install rules. /* clang-format off */ @@ -572,6 +577,71 @@ void cmLocalGenerator::GenerateInstallRules() /* clang-format on */ } + // Write out CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM so that + // installed code that uses `file(GET_RUNTIME_DEPENDENCIES)` + // has same platform variable as when running cmake + if (const char* platform = this->Makefile->GetDefinition( + "CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM")) { + /* clang-format off */ + fout << + "# Set default install directory permissions.\n" + "if(NOT DEFINED CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM)\n" + " set(CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM \"" + << platform << "\")\n" + "endif()\n" + "\n"; + /* clang-format on */ + } + + // Write out CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL so that + // installed code that uses `file(GET_RUNTIME_DEPENDENCIES)` + // has same tool selected as when running cmake + if (const char* command = + this->Makefile->GetDefinition("CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL")) { + /* clang-format off */ + fout << + "# Set default install directory permissions.\n" + "if(NOT DEFINED CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL)\n" + " set(CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL \"" + << command << "\")\n" + "endif()\n" + "\n"; + /* clang-format on */ + } + + // Write out CMAKE_GET_RUNTIME_DEPENDENCIES_COMMAND so that + // installed code that uses `file(GET_RUNTIME_DEPENDENCIES)` + // has same path to the tool as when running cmake + if (const char* command = this->Makefile->GetDefinition( + "CMAKE_GET_RUNTIME_DEPENDENCIES_COMMAND")) { + /* clang-format off */ + fout << + "# Set default install directory permissions.\n" + "if(NOT DEFINED CMAKE_GET_RUNTIME_DEPENDENCIES_COMMAND)\n" + " set(CMAKE_GET_RUNTIME_DEPENDENCIES_COMMAND \"" + << command << "\")\n" + "endif()\n" + "\n"; + /* clang-format on */ + } + + // Write out CMAKE_OBJDUMP so that installed code that uses + // `file(GET_RUNTIME_DEPENDENCIES)` and hasn't specified + // CMAKE_GET_RUNTIME_DEPENDENCIES_COMMAND has consistent + // logic to fallback to CMAKE_OBJDUMP when `objdump` is + // not on the path + if (const char* command = this->Makefile->GetDefinition("CMAKE_OBJDUMP")) { + /* clang-format off */ + fout << + "# Set default install directory permissions.\n" + "if(NOT DEFINED CMAKE_OBJDUMP)\n" + " set(CMAKE_OBJDUMP \"" + << command << "\")\n" + "endif()\n" + "\n"; + /* clang-format on */ + } + // Ask each install generator to write its code. cmPolicies::PolicyStatus status = this->GetPolicyStatus(cmPolicies::CMP0082); auto const& installers = this->Makefile->GetInstallGenerators(); @@ -613,8 +683,7 @@ void cmLocalGenerator::GenerateInstallRules() if (!c.GetDirectory().GetPropertyAsBool("EXCLUDE_FROM_ALL")) { std::string odir = c.GetDirectory().GetCurrentBinary(); cmSystemTools::ConvertToUnixSlashes(odir); - fout << " include(\"" << odir << "/cmake_install.cmake\")" - << std::endl; + fout << " include(\"" << odir << "/cmake_install.cmake\")\n"; } } fout << "\n"; @@ -795,7 +864,8 @@ const char* cmLocalGenerator::GetRuleLauncher(cmGeneratorTarget* target, if (target) { return target->GetProperty(prop); } - return this->Makefile->GetProperty(prop); + cmProp p = this->Makefile->GetProperty(prop); + return p ? p->c_str() : nullptr; } std::string cmLocalGenerator::ConvertToIncludeReference( @@ -899,7 +969,7 @@ std::string cmLocalGenerator::GetIncludeFlags( if ((sep[0] != ' ') && !flags.empty() && flags.back() == sep[0]) { flags.back() = ' '; } - return flags; + return cmTrimWhitespace(flags); } void cmLocalGenerator::AddCompileOptions(std::string& flags, @@ -971,8 +1041,7 @@ void cmLocalGenerator::AddCompileOptions(std::vector<BT<std::string>>& flags, << "\". " "This is not permitted. The COMPILE_FEATURES may not both depend " "on " - "and be depended on by the link implementation." - << std::endl; + "and be depended on by the link implementation.\n"; this->IssueMessage(MessageType::FATAL_ERROR, e.str()); return; } @@ -1018,7 +1087,8 @@ cmTarget* cmLocalGenerator::AddCustomCommandToTarget( const cmCustomCommandLines& commandLines, cmCustomCommandType type, const char* comment, const char* workingDir, bool escapeOldStyle, bool uses_terminal, const std::string& depfile, const std::string& job_pool, - bool command_expand_lists, cmObjectLibraryCommands objLibCommands) + bool command_expand_lists, cmObjectLibraryCommands objLibCommands, + bool stdPipesUTF8) { cmTarget* t = this->Makefile->GetCustomCommandTarget( target, objLibCommands, this->DirectoryBacktrace); @@ -1029,7 +1099,7 @@ cmTarget* cmLocalGenerator::AddCustomCommandToTarget( detail::AddCustomCommandToTarget( *this, this->DirectoryBacktrace, cmCommandOrigin::Generator, t, byproducts, depends, commandLines, type, comment, workingDir, escapeOldStyle, - uses_terminal, depfile, job_pool, command_expand_lists); + uses_terminal, depfile, job_pool, command_expand_lists, stdPipesUTF8); return t; } @@ -1039,14 +1109,14 @@ cmSourceFile* cmLocalGenerator::AddCustomCommandToOutput( const std::string& main_dependency, const cmCustomCommandLines& commandLines, const char* comment, const char* workingDir, bool replace, bool escapeOldStyle, bool uses_terminal, bool command_expand_lists, - const std::string& depfile, const std::string& job_pool) + const std::string& depfile, const std::string& job_pool, bool stdPipesUTF8) { std::vector<std::string> no_byproducts; cmImplicitDependsList no_implicit_depends; return this->AddCustomCommandToOutput( { output }, no_byproducts, depends, main_dependency, no_implicit_depends, commandLines, comment, workingDir, replace, escapeOldStyle, uses_terminal, - command_expand_lists, depfile, job_pool); + command_expand_lists, depfile, job_pool, stdPipesUTF8); } cmSourceFile* cmLocalGenerator::AddCustomCommandToOutput( @@ -1057,7 +1127,7 @@ cmSourceFile* cmLocalGenerator::AddCustomCommandToOutput( const cmCustomCommandLines& commandLines, const char* comment, const char* workingDir, bool replace, bool escapeOldStyle, bool uses_terminal, bool command_expand_lists, const std::string& depfile, - const std::string& job_pool) + const std::string& job_pool, bool stdPipesUTF8) { // Make sure there is at least one output. if (outputs.empty()) { @@ -1069,7 +1139,7 @@ cmSourceFile* cmLocalGenerator::AddCustomCommandToOutput( *this, this->DirectoryBacktrace, cmCommandOrigin::Generator, outputs, byproducts, depends, main_dependency, implicit_depends, commandLines, comment, workingDir, replace, escapeOldStyle, uses_terminal, - command_expand_lists, depfile, job_pool); + command_expand_lists, depfile, job_pool, stdPipesUTF8); } cmTarget* cmLocalGenerator::AddUtilityCommand( @@ -1078,7 +1148,7 @@ cmTarget* cmLocalGenerator::AddUtilityCommand( const std::vector<std::string>& depends, const cmCustomCommandLines& commandLines, bool escapeOldStyle, const char* comment, bool uses_terminal, bool command_expand_lists, - const std::string& job_pool) + const std::string& job_pool, bool stdPipesUTF8) { cmTarget* target = this->Makefile->AddNewUtilityTarget(utilityName, excludeFromAll); @@ -1092,7 +1162,7 @@ cmTarget* cmLocalGenerator::AddUtilityCommand( *this, this->DirectoryBacktrace, cmCommandOrigin::Generator, target, this->Makefile->GetUtilityOutput(target), workingDir, byproducts, depends, commandLines, escapeOldStyle, comment, uses_terminal, command_expand_lists, - job_pool); + job_pool, stdPipesUTF8); return target; } @@ -1183,7 +1253,7 @@ std::vector<BT<std::string>> cmLocalGenerator::GetIncludeDirectoriesImplicit( // directly. In this case adding -I/usr/include can hide SDK headers so we // must still exclude it. if ((lang == "C" || lang == "CXX" || lang == "CUDA") && - !cmContains(impDirVec, "/usr/include") && + !cm::contains(impDirVec, "/usr/include") && std::find_if(impDirVec.begin(), impDirVec.end(), [](std::string const& d) { return cmHasLiteralSuffix(d, "/usr/include"); @@ -1204,13 +1274,14 @@ std::vector<BT<std::string>> cmLocalGenerator::GetIncludeDirectoriesImplicit( &lang](std::string const& dir) { return ( // Do not exclude directories that are not in an excluded set. - ((!cmContains(implicitSet, this->GlobalGenerator->GetRealPath(dir))) && - (!cmContains(implicitExclude, dir))) + ((!cm::contains(implicitSet, this->GlobalGenerator->GetRealPath(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") && cmContains(this->EnvCPATH, dir))); + || + ((lang == "C" || lang == "CXX") && cm::contains(this->EnvCPATH, dir))); }; // Get the target-specific include directories. @@ -1257,7 +1328,7 @@ std::vector<BT<std::string>> cmLocalGenerator::GetIncludeDirectoriesImplicit( if (!stripImplicitDirs) { // Append implicit directories that were requested by the user only for (BT<std::string> const& udr : userDirs) { - if (cmContains(implicitSet, cmSystemTools::GetRealPath(udr.Value))) { + if (cm::contains(implicitSet, cmSystemTools::GetRealPath(udr.Value))) { emitBT(udr); } } @@ -1314,14 +1385,15 @@ std::vector<BT<std::string>> cmLocalGenerator::GetStaticLibraryFlags( std::string const& config, std::string const& linkLanguage, cmGeneratorTarget* target) { + const std::string configUpper = cmSystemTools::UpperCase(config); std::vector<BT<std::string>> flags; if (linkLanguage != "Swift") { std::string staticLibFlags; this->AppendFlags( staticLibFlags, this->Makefile->GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS")); - if (!config.empty()) { - std::string name = "CMAKE_STATIC_LINKER_FLAGS_" + config; + if (!configUpper.empty()) { + std::string name = "CMAKE_STATIC_LINKER_FLAGS_" + configUpper; this->AppendFlags(staticLibFlags, this->Makefile->GetSafeDefinition(name)); } @@ -1333,8 +1405,8 @@ std::vector<BT<std::string>> cmLocalGenerator::GetStaticLibraryFlags( std::string staticLibFlags; this->AppendFlags(staticLibFlags, target->GetSafeProperty("STATIC_LIBRARY_FLAGS")); - if (!config.empty()) { - std::string name = "STATIC_LIBRARY_FLAGS_" + config; + if (!configUpper.empty()) { + std::string name = "STATIC_LIBRARY_FLAGS_" + configUpper; this->AppendFlags(staticLibFlags, target->GetSafeProperty(name)); } @@ -1350,6 +1422,30 @@ std::vector<BT<std::string>> cmLocalGenerator::GetStaticLibraryFlags( return flags; } +void cmLocalGenerator::GetDeviceLinkFlags( + cmLinkLineComputer* linkLineComputer, const std::string& config, + std::string& linkLibs, std::string& linkFlags, std::string& frameworkPath, + std::string& linkPath, cmGeneratorTarget* target) +{ + cmGeneratorTarget::DeviceLinkSetter setter(*target); + + cmComputeLinkInformation* pcli = target->GetLinkInformation(config); + const std::string linkLanguage = + linkLineComputer->GetLinkerLanguage(target, config); + + if (pcli) { + // Compute the required cuda device link libraries when + // resolving cuda device symbols + this->OutputLinkLibraries(pcli, linkLineComputer, linkLibs, frameworkPath, + linkPath); + } + + std::vector<std::string> linkOpts; + target->GetLinkOptions(linkOpts, config, linkLanguage); + // LINK_OPTIONS are escaped. + this->AppendCompileOptions(linkFlags, linkOpts); +} + void cmLocalGenerator::GetTargetFlags( cmLinkLineComputer* linkLineComputer, const std::string& config, std::string& linkLibs, std::string& flags, std::string& linkFlags, @@ -1371,23 +1467,17 @@ void cmLocalGenerator::GetTargetFlags( std::vector<BT<std::string>>& linkFlags, std::string& frameworkPath, std::vector<BT<std::string>>& linkPath, cmGeneratorTarget* target) { - const std::string buildType = cmSystemTools::UpperCase(config); + const std::string configUpper = cmSystemTools::UpperCase(config); cmComputeLinkInformation* pcli = target->GetLinkInformation(config); const char* libraryLinkVariable = "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library const std::string linkLanguage = - linkLineComputer->GetLinkerLanguage(target, buildType); + linkLineComputer->GetLinkerLanguage(target, config); switch (target->GetType()) { case cmStateEnums::STATIC_LIBRARY: - linkFlags = this->GetStaticLibraryFlags(buildType, linkLanguage, target); - if (pcli && dynamic_cast<cmLinkLineDeviceComputer*>(linkLineComputer)) { - // Compute the required cuda device link libraries when - // resolving cuda device symbols - this->OutputLinkLibraries(pcli, linkLineComputer, linkLibs, - frameworkPath, linkPath); - } + linkFlags = this->GetStaticLibraryFlags(config, linkLanguage, target); break; case cmStateEnums::MODULE_LIBRARY: libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS"; @@ -1397,8 +1487,8 @@ void cmLocalGenerator::GetTargetFlags( if (linkLanguage != "Swift") { sharedLibFlags = cmStrCat( this->Makefile->GetSafeDefinition(libraryLinkVariable), ' '); - if (!buildType.empty()) { - std::string build = cmStrCat(libraryLinkVariable, '_', buildType); + if (!configUpper.empty()) { + std::string build = cmStrCat(libraryLinkVariable, '_', configUpper); sharedLibFlags += this->Makefile->GetSafeDefinition(build); sharedLibFlags += " "; } @@ -1406,14 +1496,14 @@ void cmLocalGenerator::GetTargetFlags( !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW"))) { std::vector<cmSourceFile*> sources; - target->GetSourceFiles(sources, buildType); + target->GetSourceFiles(sources, config); std::string defFlag = this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG"); for (cmSourceFile* sf : sources) { if (sf->GetExtension() == "def") { sharedLibFlags += defFlag; - sharedLibFlags += this->ConvertToOutputFormat( - cmSystemTools::CollapseFullPath(sf->ResolveFullPath()), SHELL); + sharedLibFlags += + this->ConvertToOutputFormat(sf->ResolveFullPath(), SHELL); sharedLibFlags += " "; } } @@ -1425,9 +1515,9 @@ void cmLocalGenerator::GetTargetFlags( sharedLibFlags += targetLinkFlags; sharedLibFlags += " "; } - if (!buildType.empty()) { + if (!configUpper.empty()) { targetLinkFlags = - target->GetProperty(cmStrCat("LINK_FLAGS_", buildType)); + target->GetProperty(cmStrCat("LINK_FLAGS_", configUpper)); if (targetLinkFlags) { sharedLibFlags += targetLinkFlags; sharedLibFlags += " "; @@ -1452,9 +1542,9 @@ void cmLocalGenerator::GetTargetFlags( if (linkLanguage != "Swift") { exeFlags = this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS"); exeFlags += " "; - if (!buildType.empty()) { + if (!configUpper.empty()) { exeFlags += this->Makefile->GetSafeDefinition( - cmStrCat("CMAKE_EXE_LINKER_FLAGS_", buildType)); + cmStrCat("CMAKE_EXE_LINKER_FLAGS_", configUpper)); exeFlags += " "; } if (linkLanguage.empty()) { @@ -1481,7 +1571,7 @@ void cmLocalGenerator::GetTargetFlags( } } - this->AddLanguageFlagsForLinking(flags, target, linkLanguage, buildType); + this->AddLanguageFlagsForLinking(flags, target, linkLanguage, config); if (pcli) { this->OutputLinkLibraries(pcli, linkLineComputer, linkLibs, frameworkPath, linkPath); @@ -1506,9 +1596,9 @@ void cmLocalGenerator::GetTargetFlags( exeFlags += targetLinkFlags; exeFlags += " "; } - if (!buildType.empty()) { + if (!configUpper.empty()) { targetLinkFlags = - target->GetProperty(cmStrCat("LINK_FLAGS_", buildType)); + target->GetProperty(cmStrCat("LINK_FLAGS_", configUpper)); if (targetLinkFlags) { exeFlags += targetLinkFlags; exeFlags += " "; @@ -1541,16 +1631,17 @@ void cmLocalGenerator::GetTargetFlags( void cmLocalGenerator::GetTargetCompileFlags(cmGeneratorTarget* target, std::string const& config, std::string const& lang, - std::string& flags) + std::string& flags, + std::string const& arch) { std::vector<BT<std::string>> tmpFlags = - this->GetTargetCompileFlags(target, config, lang); + this->GetTargetCompileFlags(target, config, lang, arch); this->AppendFlags(flags, tmpFlags); } std::vector<BT<std::string>> cmLocalGenerator::GetTargetCompileFlags( cmGeneratorTarget* target, std::string const& config, - std::string const& lang) + std::string const& lang, std::string const& arch) { std::vector<BT<std::string>> flags; std::string compileFlags; @@ -1564,7 +1655,7 @@ std::vector<BT<std::string>> cmLocalGenerator::GetTargetCompileFlags( this->AppendFeatureOptions(compileFlags, lang, "IPO"); } - this->AddArchitectureFlags(compileFlags, target, lang, config); + this->AddArchitectureFlags(compileFlags, target, lang, config, arch); if (lang == "Fortran") { this->AppendFlags(compileFlags, @@ -1753,10 +1844,10 @@ std::string cmLocalGenerator::GetLinkLibsCMP0065( "CMAKE_POLICY_WARNING_CMP0065")) { std::ostringstream w; /* clang-format off */ - w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0065) << "\n" - "For compatibility with older versions of CMake, " - "additional flags may be added to export symbols on all " - "executables regardless of their ENABLE_EXPORTS property."; + w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0065) << "\n" + "For compatibility with older versions of CMake, " + "additional flags may be added to export symbols on all " + "executables regardless of their ENABLE_EXPORTS property."; /* clang-format on */ this->IssueMessage(MessageType::AUTHOR_WARNING, w.str()); } @@ -1792,7 +1883,8 @@ std::string cmLocalGenerator::GetLinkLibsCMP0065( void cmLocalGenerator::AddArchitectureFlags(std::string& flags, cmGeneratorTarget const* target, const std::string& lang, - const std::string& config) + const std::string& config, + const std::string& filterArch) { // Only add Apple specific flags on Apple platforms if (this->Makefile->IsOn("APPLE") && this->EmitUniversalBinaryFlags) { @@ -1801,8 +1893,10 @@ void cmLocalGenerator::AddArchitectureFlags(std::string& flags, if (!archs.empty() && !lang.empty() && (lang[0] == 'C' || lang[0] == 'F' || lang[0] == 'O')) { for (std::string const& arch : archs) { - flags += " -arch "; - flags += arch; + if (filterArch.empty() || filterArch == arch) { + flags += " -arch "; + flags += arch; + } } } @@ -1820,10 +1914,12 @@ void cmLocalGenerator::AddArchitectureFlags(std::string& flags, if (cmIsOff(archSysroot)) { continue; } - flags += " -Xarch_" + arch + " "; - // Combine sysroot flag and path to work with -Xarch - std::string arch_sysroot = sysrootFlag + archSysroot; - flags += this->ConvertToOutputFormat(arch_sysroot, SHELL); + if (filterArch.empty() || filterArch == arch) { + flags += " -Xarch_" + arch + " "; + // Combine sysroot flag and path to work with -Xarch + std::string arch_sysroot = sysrootFlag + archSysroot; + flags += this->ConvertToOutputFormat(arch_sysroot, SHELL); + } } } else if (sysroot && *sysroot) { flags += " "; @@ -1866,6 +1962,8 @@ void cmLocalGenerator::AddLanguageFlags(std::string& flags, this->AppendFlags(flags, "-swift-version " + std::string(v)); } } + } else if (lang == "CUDA") { + target->AddCUDAArchitectureFlags(flags); } // Add MSVC runtime library flags. This is activated by the presence @@ -1952,7 +2050,6 @@ bool cmLocalGenerator::GetRealDependency(const std::string& inName, if (name.empty()) { return false; } - if (cmSystemTools::GetFilenameLastExtension(name) == ".exe") { name = cmSystemTools::GetFilenameWithoutLastExtension(name); } @@ -1992,11 +2089,9 @@ bool cmLocalGenerator::GetRealDependency(const std::string& inName, case cmStateEnums::OBJECT_LIBRARY: // An object library has no single file on which to depend. // This was listed to get the target-level dependency. - return false; case cmStateEnums::INTERFACE_LIBRARY: // An interface library has no file on which to depend. // This was listed to get the target-level dependency. - return false; case cmStateEnums::UTILITY: case cmStateEnums::GLOBAL_TARGET: // A utility target has no file on which to depend. This was listed @@ -2413,7 +2508,9 @@ void cmLocalGenerator::AddConfigVariableFlags(std::string& flags, void cmLocalGenerator::AppendFlags(std::string& flags, const std::string& newFlags) const { - if (!newFlags.empty()) { + bool allSpaces = std::all_of(newFlags.begin(), newFlags.end(), cmIsSpace); + + if (!newFlags.empty() && !allSpaces) { if (!flags.empty()) { flags += " "; } @@ -2460,146 +2557,177 @@ void cmLocalGenerator::AddPchDependencies(cmGeneratorTarget* target) continue; } - const std::string pchSource = target->GetPchSource(config, lang); - const std::string pchHeader = target->GetPchHeader(config, lang); - - if (pchSource.empty() || pchHeader.empty()) { - continue; + std::vector<std::string> architectures; + if (!this->GetGlobalGenerator()->IsXcode()) { + target->GetAppleArchs(config, architectures); } + if (architectures.empty()) { + architectures.emplace_back(); + } else { + std::string useMultiArchPch; + for (const std::string& arch : architectures) { + const std::string pchHeader = + target->GetPchHeader(config, lang, arch); + if (!pchHeader.empty()) { + useMultiArchPch = cmStrCat(useMultiArchPch, ";-Xarch_", arch, + ";-include", pchHeader); + } + } - const std::string pchExtension = - this->Makefile->GetSafeDefinition("CMAKE_PCH_EXTENSION"); - - if (pchExtension.empty()) { - continue; + if (!useMultiArchPch.empty()) { + target->Target->SetProperty( + cmStrCat(lang, "_COMPILE_OPTIONS_USE_PCH"), useMultiArchPch); + } } - const char* pchReuseFrom = - target->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM"); + for (const std::string& arch : architectures) { + const std::string pchSource = target->GetPchSource(config, lang, arch); + const std::string pchHeader = target->GetPchHeader(config, lang, arch); - auto pch_sf = this->Makefile->GetOrCreateSource( - pchSource, false, cmSourceFileLocationKind::Known); + if (pchSource.empty() || pchHeader.empty()) { + continue; + } - if (!this->GetGlobalGenerator()->IsXcode()) { - if (!pchReuseFrom) { - target->AddSource(pchSource, true); + const std::string pchExtension = + this->Makefile->GetSafeDefinition("CMAKE_PCH_EXTENSION"); + + if (pchExtension.empty()) { + continue; } - const std::string pchFile = target->GetPchFile(config, lang); + const char* pchReuseFrom = + target->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM"); - // Exclude the pch files from linking - if (this->Makefile->IsOn("CMAKE_LINK_PCH")) { - if (!pchReuseFrom) { - pch_sf->SetProperty("OBJECT_OUTPUTS", pchFile.c_str()); - } else { - auto reuseTarget = - this->GlobalGenerator->FindGeneratorTarget(pchReuseFrom); + auto pch_sf = this->Makefile->GetOrCreateSource( + pchSource, false, cmSourceFileLocationKind::Known); - if (this->Makefile->IsOn("CMAKE_PCH_COPY_COMPILE_PDB")) { + if (!this->GetGlobalGenerator()->IsXcode()) { + if (!pchReuseFrom) { + target->AddSource(pchSource, true); + } - const std::string pdb_prefix = - this->GetGlobalGenerator()->IsMultiConfig() - ? cmStrCat(this->GlobalGenerator->GetCMakeCFGIntDir(), "/") - : ""; + const std::string pchFile = target->GetPchFile(config, lang, arch); - const std::string target_compile_pdb_dir = cmStrCat( - target->GetLocalGenerator()->GetCurrentBinaryDirectory(), "/", - target->GetName(), ".dir/"); + // Exclude the pch files from linking + if (this->Makefile->IsOn("CMAKE_LINK_PCH")) { + if (!pchReuseFrom) { + pch_sf->SetProperty("OBJECT_OUTPUTS", pchFile.c_str()); + } else { + auto reuseTarget = + this->GlobalGenerator->FindGeneratorTarget(pchReuseFrom); - const std::string copy_script = - cmStrCat(target_compile_pdb_dir, "copy_idb_pdb.cmake"); - cmGeneratedFileStream file(copy_script); + if (this->Makefile->IsOn("CMAKE_PCH_COPY_COMPILE_PDB")) { - file << "# CMake generated file\n"; - for (auto extension : { ".pdb", ".idb" }) { - const std::string from_file = - cmStrCat(reuseTarget->GetLocalGenerator() - ->GetCurrentBinaryDirectory(), - "/", pchReuseFrom, ".dir/${PDB_PREFIX}", - pchReuseFrom, extension); + const std::string pdb_prefix = + this->GetGlobalGenerator()->IsMultiConfig() + ? cmStrCat(this->GlobalGenerator->GetCMakeCFGIntDir(), "/") + : ""; - const std::string to_dir = cmStrCat( + const std::string target_compile_pdb_dir = cmStrCat( target->GetLocalGenerator()->GetCurrentBinaryDirectory(), - "/", target->GetName(), ".dir/${PDB_PREFIX}"); + "/", target->GetName(), ".dir/"); - const std::string to_file = - cmStrCat(to_dir, pchReuseFrom, extension); + const std::string copy_script = + cmStrCat(target_compile_pdb_dir, "copy_idb_pdb.cmake"); + cmGeneratedFileStream file(copy_script); - std::string dest_file = to_file; + file << "# CMake generated file\n"; + for (auto extension : { ".pdb", ".idb" }) { + const std::string from_file = + cmStrCat(reuseTarget->GetLocalGenerator() + ->GetCurrentBinaryDirectory(), + "/", pchReuseFrom, ".dir/${PDB_PREFIX}", + pchReuseFrom, extension); - const std::string prefix = target->GetSafeProperty("PREFIX"); - if (!prefix.empty()) { - dest_file = - cmStrCat(to_dir, prefix, pchReuseFrom, extension); - } + const std::string to_dir = cmStrCat( + target->GetLocalGenerator()->GetCurrentBinaryDirectory(), + "/", target->GetName(), ".dir/${PDB_PREFIX}"); + + const std::string to_file = + cmStrCat(to_dir, pchReuseFrom, extension); + + std::string dest_file = to_file; + + const std::string prefix = target->GetSafeProperty("PREFIX"); + if (!prefix.empty()) { + dest_file = + cmStrCat(to_dir, prefix, pchReuseFrom, extension); + } - file << "if (EXISTS \"" << from_file << "\" AND \"" - << from_file << "\" IS_NEWER_THAN \"" << dest_file - << "\")\n"; - file << " file(COPY \"" << from_file << "\"" - << " DESTINATION \"" << to_dir << "\")\n"; - if (!prefix.empty()) { - file << " file(REMOVE \"" << dest_file << "\")\n"; - file << " file(RENAME \"" << to_file << "\" \"" << dest_file + file << "if (EXISTS \"" << from_file << "\" AND \"" + << from_file << "\" IS_NEWER_THAN \"" << dest_file << "\")\n"; + file << " file(COPY \"" << from_file << "\"" + << " DESTINATION \"" << to_dir << "\")\n"; + if (!prefix.empty()) { + file << " file(REMOVE \"" << dest_file << "\")\n"; + file << " file(RENAME \"" << to_file << "\" \"" + << dest_file << "\")\n"; + } + file << "endif()\n"; } - file << "endif()\n"; - } - cmCustomCommandLines commandLines = cmMakeSingleCommandLine( - { cmSystemTools::GetCMakeCommand(), - cmStrCat("-DPDB_PREFIX=", pdb_prefix), "-P", copy_script }); - - const std::string no_main_dependency; - const std::vector<std::string> no_deps; - const char* no_message = ""; - const char* no_current_dir = nullptr; - std::vector<std::string> no_byproducts; - - std::vector<std::string> outputs; - outputs.push_back(cmStrCat(target_compile_pdb_dir, pdb_prefix, - pchReuseFrom, ".pdb")); - - if (this->GetGlobalGenerator()->IsVisualStudio()) { - this->AddCustomCommandToTarget( - target->GetName(), outputs, no_deps, commandLines, - cmCustomCommandType::PRE_BUILD, no_message, no_current_dir); - } else { - cmImplicitDependsList no_implicit_depends; - cmSourceFile* copy_rule = this->AddCustomCommandToOutput( - outputs, no_byproducts, no_deps, no_main_dependency, - no_implicit_depends, commandLines, no_message, - no_current_dir); - - if (copy_rule) { - target->AddSource(copy_rule->ResolveFullPath()); + bool stdPipesUTF8 = true; + cmCustomCommandLines commandLines = cmMakeSingleCommandLine( + { cmSystemTools::GetCMakeCommand(), + cmStrCat("-DPDB_PREFIX=", pdb_prefix), "-P", + copy_script }); + + const std::string no_main_dependency; + const std::vector<std::string> no_deps; + const char* no_message = ""; + const char* no_current_dir = nullptr; + std::vector<std::string> no_byproducts; + + std::vector<std::string> outputs; + outputs.push_back(cmStrCat(target_compile_pdb_dir, pdb_prefix, + pchReuseFrom, ".pdb")); + + if (this->GetGlobalGenerator()->IsVisualStudio()) { + this->AddCustomCommandToTarget( + target->GetName(), outputs, no_deps, commandLines, + cmCustomCommandType::PRE_BUILD, no_message, no_current_dir, + true, false, "", "", false, + cmObjectLibraryCommands::Reject, stdPipesUTF8); + } else { + cmImplicitDependsList no_implicit_depends; + cmSourceFile* copy_rule = this->AddCustomCommandToOutput( + outputs, no_byproducts, no_deps, no_main_dependency, + no_implicit_depends, commandLines, no_message, + no_current_dir, false, true, false, false, "", "", + stdPipesUTF8); + + if (copy_rule) { + target->AddSource(copy_rule->ResolveFullPath()); + } } - } - target->Target->SetProperty("COMPILE_PDB_OUTPUT_DIRECTORY", - target_compile_pdb_dir); - } + target->Target->SetProperty("COMPILE_PDB_OUTPUT_DIRECTORY", + target_compile_pdb_dir); + } - std::string pchSourceObj = - reuseTarget->GetPchFileObject(config, lang); + std::string pchSourceObj = + reuseTarget->GetPchFileObject(config, lang, arch); - // Link to the pch object file - target->Target->AppendProperty( - "LINK_FLAGS", - cmStrCat(" ", this->ConvertToOutputFormat(pchSourceObj, SHELL)), - true); + // Link to the pch object file + target->Target->AppendProperty( + "LINK_FLAGS", + cmStrCat(" ", + this->ConvertToOutputFormat(pchSourceObj, SHELL)), + true); + } + } else { + pch_sf->SetProperty("PCH_EXTENSION", pchExtension.c_str()); } - } else { - pch_sf->SetProperty("PCH_EXTENSION", pchExtension.c_str()); - } - // Add pchHeader to source files, which will - // be grouped as "Precompile Header File" - auto pchHeader_sf = this->Makefile->GetOrCreateSource( - pchHeader, false, cmSourceFileLocationKind::Known); - std::string err; - pchHeader_sf->ResolveFullPath(&err); - target->AddSource(pchHeader); + // Add pchHeader to source files, which will + // be grouped as "Precompile Header File" + auto pchHeader_sf = this->Makefile->GetOrCreateSource( + pchHeader, false, cmSourceFileLocationKind::Known); + std::string err; + pchHeader_sf->ResolveFullPath(&err); + target->AddSource(pchHeader); + } } } } @@ -2927,11 +3055,11 @@ void cmLocalGenerator::JoinDefines(const std::set<std::string>& defines, // command line without any escapes. However we still have to // get the '$' and '#' characters through WMake as '$$' and // '$#'. - for (const char* c = define.c_str(); *c; ++c) { - if (*c == '$' || *c == '#') { + for (char c : define) { + if (c == '$' || c == '#') { def += '$'; } - def += *c; + def += c; } } else { // Make the definition appear properly on the command line. Use @@ -2975,8 +3103,8 @@ const char* cmLocalGenerator::GetFeature(const std::string& feature, } cmStateSnapshot snp = this->StateSnapshot; while (snp.IsValid()) { - if (const char* value = snp.GetDirectory().GetProperty(featureName)) { - return value; + if (cmProp value = snp.GetDirectory().GetProperty(featureName)) { + return value->c_str(); } snp = snp.GetBuildsystemDirectoryParent(); } @@ -3281,11 +3409,12 @@ std::string cmLocalGenerator::GetObjectFileNameWithoutTarget( // Select a nice-looking reference to the source file to construct // the object file name. std::string objectName; + // XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165 + // NOLINTNEXTLINE(bugprone-branch-clone) if ((relSource && !relBinary) || (subSource && !subBinary)) { objectName = relFromSource; - } else if ((relBinary && !relSource) || (subBinary && !subSource)) { - objectName = relFromBinary; - } else if (relFromBinary.length() < relFromSource.length()) { + } else if ((relBinary && !relSource) || (subBinary && !subSource) || + relFromBinary.length() < relFromSource.length()) { objectName = relFromBinary; } else { objectName = relFromSource; @@ -3303,12 +3432,12 @@ std::string cmLocalGenerator::GetObjectFileNameWithoutTarget( // Ensure that for the CMakeFiles/<target>.dir/generated_source_file // we don't end up having: // CMakeFiles/<target>.dir/CMakeFiles/<target>.dir/generated_source_file.obj - const char* unitySourceFile = source.GetProperty("UNITY_SOURCE_FILE"); - const char* pchExtension = source.GetProperty("PCH_EXTENSION"); + cmProp unitySourceFile = source.GetProperty("UNITY_SOURCE_FILE"); + cmProp psExtension = source.GetProperty("PCH_EXTENSION"); const bool isPchObject = objectName.find("cmake_pch") != std::string::npos; - if (unitySourceFile || pchExtension || isPchObject) { - if (pchExtension) { - customOutputExtension = pchExtension; + if (unitySourceFile || psExtension || isPchObject) { + if (psExtension) { + customOutputExtension = psExtension->c_str(); } cmsys::RegularExpression var("(CMakeFiles/[^/]+.dir/)"); @@ -3444,7 +3573,6 @@ bool cmLocalGenerator::NeedBackwardsCompatibility_2_4() break; case cmPolicies::NEW: // New behavior is to ignore the variable. - return false; case cmPolicies::REQUIRED_IF_USED: case cmPolicies::REQUIRED_ALWAYS: // This will never be the case because the only way to require @@ -3624,7 +3752,7 @@ cmSourceFile* AddCustomCommand( const cmCustomCommandLines& commandLines, const char* comment, const char* workingDir, bool replace, bool escapeOldStyle, bool uses_terminal, bool command_expand_lists, const std::string& depfile, - const std::string& job_pool) + const std::string& job_pool, bool stdPipesUTF8) { cmMakefile* mf = lg.GetMakefile(); @@ -3686,7 +3814,8 @@ cmSourceFile* AddCustomCommand( } std::unique_ptr<cmCustomCommand> cc = cm::make_unique<cmCustomCommand>( - outputs, byproducts, depends2, commandLines, lfbt, comment, workingDir); + outputs, byproducts, depends2, commandLines, lfbt, comment, workingDir, + stdPipesUTF8); cc->SetEscapeOldStyle(escapeOldStyle); cc->SetEscapeAllowMakeVars(true); cc->SetImplicitDepends(implicit_depends); @@ -3713,7 +3842,7 @@ void AddCustomCommandToTarget(cmLocalGenerator& lg, const char* workingDir, bool escapeOldStyle, bool uses_terminal, const std::string& depfile, const std::string& job_pool, - bool command_expand_lists) + bool command_expand_lists, bool stdPipesUTF8) { cmMakefile* mf = lg.GetMakefile(); @@ -3723,7 +3852,7 @@ void AddCustomCommandToTarget(cmLocalGenerator& lg, // Add the command to the appropriate build step for the target. std::vector<std::string> no_output; cmCustomCommand cc(no_output, byproducts, depends, commandLines, lfbt, - comment, workingDir); + comment, workingDir, stdPipesUTF8); cc.SetEscapeOldStyle(escapeOldStyle); cc.SetEscapeAllowMakeVars(true); cc.SetUsesTerminal(uses_terminal); @@ -3754,7 +3883,7 @@ cmSourceFile* AddCustomCommandToOutput( const cmCustomCommandLines& commandLines, const char* comment, const char* workingDir, bool replace, bool escapeOldStyle, bool uses_terminal, bool command_expand_lists, const std::string& depfile, - const std::string& job_pool) + const std::string& job_pool, bool stdPipesUTF8) { // Always create the output sources and mark them generated. CreateGeneratedSources(lg, outputs, origin, lfbt); @@ -3763,7 +3892,7 @@ cmSourceFile* AddCustomCommandToOutput( return AddCustomCommand( lg, lfbt, outputs, byproducts, depends, main_dependency, implicit_depends, commandLines, comment, workingDir, replace, escapeOldStyle, uses_terminal, - command_expand_lists, depfile, job_pool); + command_expand_lists, depfile, job_pool, stdPipesUTF8); } void AppendCustomCommandToOutput(cmLocalGenerator& lg, @@ -3799,7 +3928,7 @@ void AddUtilityCommand(cmLocalGenerator& lg, const cmListFileBacktrace& lfbt, const cmCustomCommandLines& commandLines, bool escapeOldStyle, const char* comment, bool uses_terminal, bool command_expand_lists, - const std::string& job_pool) + const std::string& job_pool, bool stdPipesUTF8) { // Always create the byproduct sources and mark them generated. CreateGeneratedSource(lg, force.Name, origin, lfbt); @@ -3816,7 +3945,7 @@ void AddUtilityCommand(cmLocalGenerator& lg, const cmListFileBacktrace& lfbt, lg, lfbt, { force.Name }, byproducts, depends, no_main_dependency, no_implicit_depends, commandLines, comment, workingDir, /*replace=*/false, escapeOldStyle, uses_terminal, command_expand_lists, /*depfile=*/"", - job_pool); + job_pool, stdPipesUTF8); if (rule) { lg.GetMakefile()->AddTargetByproducts(target, byproducts); } |