Refactor: reduce cmToCStr usage

This commit is contained in:
Marc Chevrier 2021-08-13 15:57:23 +02:00
parent 2984df9100
commit 5a2a275bb4
17 changed files with 62 additions and 69 deletions

View File

@ -278,13 +278,11 @@ cmComputeLinkInformation::cmComputeLinkInformation(
// On platforms without import libraries there may be a special flag
// to use when creating a plugin (module) that obtains symbols from
// the program that will load it.
this->LoaderFlag = nullptr;
if (!this->Target->IsDLLPlatform() &&
this->Target->GetType() == cmStateEnums::MODULE_LIBRARY) {
std::string loader_flag_var =
cmStrCat("CMAKE_SHARED_MODULE_LOADER_", this->LinkLanguage, "_FLAG");
this->LoaderFlag =
cmToCStr(this->Makefile->GetDefinition(loader_flag_var));
this->LoaderFlag = this->Makefile->GetDefinition(loader_flag_var);
}
// Get options needed to link libraries.
@ -660,8 +658,7 @@ void cmComputeLinkInformation::AddItem(BT<std::string> const& item,
// This link item is an executable that may provide symbols
// used by this target. A special flag is needed on this
// platform. Add it now.
std::string linkItem;
linkItem = this->LoaderFlag;
std::string linkItem = this->LoaderFlag;
cmStateEnums::ArtifactType artifact = tgt->HasImportLibrary(config)
? cmStateEnums::ImportLibraryArtifact
: cmStateEnums::RuntimeBinaryArtifact;

View File

@ -14,6 +14,7 @@
#include "cmsys/RegularExpression.hxx"
#include "cmListFileCache.h"
#include "cmProperty.h"
class cmGeneratorTarget;
class cmGlobalGenerator;
@ -137,7 +138,7 @@ private:
SharedDepModeLink // List file on link line
};
const char* LoaderFlag;
cmProp LoaderFlag;
std::string LibLinkFlag;
std::string LibLinkFileFlag;
std::string ObjLinkFileFlag;

View File

@ -4962,11 +4962,11 @@ cmGeneratorTarget::Names cmGeneratorTarget::GetLibraryNames(
// The library's soname.
this->ComputeVersionedName(targetNames.SharedObject, prefix,
targetNames.Base, suffix, targetNames.Output,
cmToCStr(soversion));
soversion);
// The library's real name on disk.
this->ComputeVersionedName(targetNames.Real, prefix, targetNames.Base,
suffix, targetNames.Output, cmToCStr(version));
suffix, targetNames.Output, version);
}
// The import library name.
@ -4999,10 +4999,10 @@ cmGeneratorTarget::Names cmGeneratorTarget::GetExecutableNames(
// This versioning is supported only for executables and then only
// when the platform supports symbolic links.
#if defined(_WIN32) && !defined(__CYGWIN__)
const char* version = nullptr;
cmProp version;
#else
// Check for executable version properties.
const char* version = cmToCStr(this->GetProperty("VERSION"));
cmProp version = this->GetProperty("VERSION");
if (this->GetType() != cmStateEnums::EXECUTABLE ||
this->Makefile->IsOn("XCODE")) {
version = nullptr;
@ -5026,7 +5026,7 @@ cmGeneratorTarget::Names cmGeneratorTarget::GetExecutableNames(
#endif
if (version) {
targetNames.Real += "-";
targetNames.Real += version;
targetNames.Real += *version;
}
#if defined(__CYGWIN__)
targetNames.Real += suffix;
@ -6290,17 +6290,14 @@ std::string cmGeneratorTarget::GetFrameworkVersion() const
return "A";
}
void cmGeneratorTarget::ComputeVersionedName(std::string& vName,
std::string const& prefix,
std::string const& base,
std::string const& suffix,
std::string const& name,
const char* version) const
void cmGeneratorTarget::ComputeVersionedName(
std::string& vName, std::string const& prefix, std::string const& base,
std::string const& suffix, std::string const& name, cmProp version) const
{
vName = this->Makefile->IsOn("APPLE") ? (prefix + base) : name;
if (version) {
vName += ".";
vName += version;
vName += *version;
}
vName += this->Makefile->IsOn("APPLE") ? suffix : std::string();
}

View File

@ -905,8 +905,7 @@ private:
void ComputeVersionedName(std::string& vName, std::string const& prefix,
std::string const& base, std::string const& suffix,
std::string const& name,
const char* version) const;
std::string const& name, cmProp version) const;
struct CompatibleInterfacesBase
{

View File

@ -13,6 +13,8 @@
namespace {
void StoreResult(cmMakefile& makefile, std::string const& variable,
const char* prop);
void StoreResult(cmMakefile& makefile, std::string const& variable,
cmProp prop);
}
// cmGetDirectoryPropertyCommand
@ -76,7 +78,6 @@ bool cmGetDirectoryPropertyCommand(std::vector<std::string> const& args,
return false;
}
const char* prop = nullptr;
if (*i == "DEFINITIONS") {
switch (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0059)) {
case cmPolicies::WARN:
@ -94,8 +95,7 @@ bool cmGetDirectoryPropertyCommand(std::vector<std::string> const& args,
break;
}
}
prop = cmToCStr(dir->GetProperty(*i));
StoreResult(status.GetMakefile(), variable, prop);
StoreResult(status.GetMakefile(), variable, dir->GetProperty(*i));
return true;
}
@ -105,4 +105,9 @@ void StoreResult(cmMakefile& makefile, std::string const& variable,
{
makefile.AddDefinition(variable, prop ? prop : "");
}
void StoreResult(cmMakefile& makefile, std::string const& variable,
cmProp prop)
{
makefile.AddDefinition(variable, prop);
}
}

View File

@ -431,9 +431,8 @@ bool HandleVariableMode(cmExecutionStatus& status, const std::string& name,
return false;
}
return StoreResult(
infoType, status.GetMakefile(), variable,
cmToCStr(status.GetMakefile().GetDefinition(propertyName)));
return StoreResult(infoType, status.GetMakefile(), variable,
status.GetMakefile().GetDefinition(propertyName));
}
bool HandleCacheMode(cmExecutionStatus& status, const std::string& name,

View File

@ -157,11 +157,11 @@ void cmGlobalVisualStudio71Generator::WriteProjectDepends(
// executables to the libraries it uses are also done here
void cmGlobalVisualStudio71Generator::WriteExternalProject(
std::ostream& fout, const std::string& name, const std::string& location,
const char* typeGuid,
const std::set<BT<std::pair<std::string, bool>>>& depends)
cmProp typeGuid, const std::set<BT<std::pair<std::string, bool>>>& depends)
{
fout << "Project(\"{"
<< (typeGuid ? typeGuid : this->ExternalProjectType(location))
<< (typeGuid ? typeGuid
: std::string(this->ExternalProjectType(location)))
<< "}\") = \"" << name << "\", \""
<< this->ConvertToSolutionPath(location) << "\", \"{"
<< this->GetGUID(name) << "}\"\n";

View File

@ -33,7 +33,7 @@ protected:
const std::string& platformMapping = "") override;
void WriteExternalProject(
std::ostream& fout, const std::string& name, const std::string& path,
const char* typeGuid,
cmProp typeGuid,
const std::set<BT<std::pair<std::string, bool>>>& depends) override;
// Folders are not supported by VS 7.1.

View File

@ -391,10 +391,9 @@ void cmGlobalVisualStudio7Generator::WriteTargetsToSolution(
std::string project = target->GetName();
std::string location = *expath;
this->WriteExternalProject(
fout, project, location,
cmToCStr(target->GetProperty("VS_PROJECT_TYPE")),
target->GetUtilities());
this->WriteExternalProject(fout, project, location,
target->GetProperty("VS_PROJECT_TYPE"),
target->GetUtilities());
written = true;
} else {
cmProp vcprojName = target->GetProperty("GENERATOR_FILE_NAME");

View File

@ -6,6 +6,7 @@
#include "cmGlobalGeneratorFactory.h"
#include "cmGlobalVisualStudioGenerator.h"
#include "cmProperty.h"
class cmTarget;
struct cmIDEFlagTable;
@ -142,7 +143,7 @@ protected:
virtual void WriteExternalProject(
std::ostream& fout, const std::string& name, const std::string& path,
const char* typeGuid,
cmProp typeGuid,
const std::set<BT<std::pair<std::string, bool>>>& dependencies) = 0;
std::string ConvertToSolutionPath(const std::string& path);

View File

@ -14,7 +14,7 @@ bool cmIncludeRegularExpressionCommand(std::vector<std::string> const& args,
}
cmMakefile& mf = status.GetMakefile();
mf.SetIncludeRegularExpression(args[0].c_str());
mf.SetIncludeRegularExpression(args[0]);
if (args.size() > 1) {
mf.SetComplainRegularExpression(args[1]);

View File

@ -425,27 +425,25 @@ void cmLocalGenerator::ProcessEvaluationFiles(
void cmLocalGenerator::GenerateInstallRules()
{
// Compute the install prefix.
const char* prefix =
cmToCStr(this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX"));
cmProp installPrefix = this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
std::string prefix = installPrefix;
#if defined(_WIN32) && !defined(__CYGWIN__)
std::string prefix_win32;
if (!prefix) {
if (!cmSystemTools::GetEnv("SystemDrive", prefix_win32)) {
prefix_win32 = "C:";
if (!installPrefix) {
if (!cmSystemTools::GetEnv("SystemDrive", prefix)) {
prefix = "C:";
}
cmProp project_name = this->Makefile->GetDefinition("PROJECT_NAME");
if (cmNonempty(project_name)) {
prefix_win32 += "/Program Files/";
prefix_win32 += *project_name;
prefix += "/Program Files/";
prefix += *project_name;
} else {
prefix_win32 += "/InstalledCMakeProject";
prefix += "/InstalledCMakeProject";
}
prefix = prefix_win32.c_str();
}
#elif defined(__HAIKU__)
char dir[B_PATH_NAME_LENGTH];
if (!prefix) {
if (!installPrefix) {
if (find_directory(B_SYSTEM_DIRECTORY, -1, false, dir, sizeof(dir)) ==
B_OK) {
prefix = dir;
@ -454,13 +452,13 @@ void cmLocalGenerator::GenerateInstallRules()
}
}
#else
if (!prefix) {
if (!installPrefix) {
prefix = "/usr/local";
}
#endif
if (cmProp stagingPrefix =
this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX")) {
prefix = stagingPrefix->c_str();
prefix = *stagingPrefix;
}
// Compute the set of configurations.
@ -1869,17 +1867,17 @@ std::string cmLocalGenerator::GetLinkLibsCMP0065(
}
bool cmLocalGenerator::AllAppleArchSysrootsAreTheSame(
const std::vector<std::string>& archs, const char* sysroot)
const std::vector<std::string>& archs, cmProp sysroot)
{
if (!sysroot) {
return false;
}
return std::all_of(archs.begin(), archs.end(),
[this, &sysroot](std::string const& arch) -> bool {
[this, sysroot](std::string const& arch) -> bool {
std::string const& archSysroot =
this->AppleArchSysroots[arch];
return cmIsOff(archSysroot) || archSysroot == sysroot;
return cmIsOff(archSysroot) || sysroot == archSysroot;
});
}
@ -1912,7 +1910,7 @@ void cmLocalGenerator::AddArchitectureFlags(std::string& flags,
cmProp sysrootFlag = this->Makefile->GetDefinition(sysrootFlagVar);
if (cmNonempty(sysrootFlag)) {
if (!this->AppleArchSysroots.empty() &&
!this->AllAppleArchSysrootsAreTheSame(archs, cmToCStr(sysroot))) {
!this->AllAppleArchSysrootsAreTheSame(archs, sysroot)) {
for (std::string const& arch : archs) {
std::string const& archSysroot = this->AppleArchSysroots[arch];
if (cmIsOff(archSysroot)) {

View File

@ -651,7 +651,7 @@ private:
void ComputeObjectMaxPath();
bool AllAppleArchSysrootsAreTheSame(const std::vector<std::string>& archs,
const char* sysroot);
cmProp sysroot);
void CopyPchCompilePdb(const std::string& config, cmGeneratorTarget* target,
const std::string& ReuseFrom,

View File

@ -399,13 +399,13 @@ public:
* Set a regular expression that include files must match
* in order to be considered as part of the depend information.
*/
void SetIncludeRegularExpression(const char* regex)
void SetIncludeRegularExpression(const std::string& regex)
{
this->SetProperty("INCLUDE_REGULAR_EXPRESSION", regex);
this->SetProperty("INCLUDE_REGULAR_EXPRESSION", regex.c_str());
}
const char* GetIncludeRegularExpression() const
const std::string& GetIncludeRegularExpression() const
{
return cmToCStr(this->GetProperty("INCLUDE_REGULAR_EXPRESSION"));
return this->GetProperty("INCLUDE_REGULAR_EXPRESSION");
}
/**

View File

@ -293,8 +293,7 @@ void cmMakefileTargetGenerator::WriteTargetBuildRules()
this->GetConfigName());
for (cmSourceFile const* sf : externalObjects) {
auto const& objectFileName = sf->GetFullPath();
if (!cmSystemTools::StringEndsWith(objectFileName,
cmToCStr(pchExtension))) {
if (!cmHasSuffix(objectFileName, pchExtension)) {
this->ExternalObjects.push_back(objectFileName);
}
}
@ -1732,7 +1731,7 @@ void cmMakefileTargetGenerator::WriteObjectsVariable(
cmProp pchExtension = this->Makefile->GetDefinition("CMAKE_PCH_EXTENSION");
for (std::string const& obj : this->Objects) {
if (cmSystemTools::StringEndsWith(obj, cmToCStr(pchExtension))) {
if (cmHasSuffix(obj, pchExtension)) {
continue;
}
*this->BuildFileStream << " " << lineContinue;
@ -1822,7 +1821,7 @@ void cmMakefileTargetGenerator::WriteObjectsStrings(
objStrings, this->LocalGenerator,
this->LocalGenerator->GetStateSnapshot().GetDirectory(), limit);
for (std::string const& obj : this->Objects) {
if (cmSystemTools::StringEndsWith(obj, cmToCStr(pchExtension))) {
if (cmHasSuffix(obj, pchExtension)) {
continue;
}
helper.Feed(obj);

View File

@ -994,8 +994,7 @@ void cmNinjaTargetGenerator::WriteObjectBuildStatements(
for (cmSourceFile const* sf : externalObjects) {
auto objectFileName = this->GetGlobalGenerator()->ExpandCFGIntDir(
this->ConvertToNinjaPath(sf->GetFullPath()), config);
if (!cmSystemTools::StringEndsWith(objectFileName,
cmToCStr(pchExtension))) {
if (!cmHasSuffix(objectFileName, pchExtension)) {
this->Configs[config].Objects.push_back(objectFileName);
}
}
@ -1260,8 +1259,7 @@ void cmNinjaTargetGenerator::WriteObjectBuildStatement(
if (firstForConfig) {
cmProp pchExtension =
this->GetMakefile()->GetDefinition("CMAKE_PCH_EXTENSION");
if (!cmSystemTools::StringEndsWith(objectFileName,
cmToCStr(pchExtension))) {
if (!cmHasSuffix(objectFileName, pchExtension)) {
// Add this object to the list of object files.
this->Configs[config].Objects.push_back(objectFileName);
}

View File

@ -508,15 +508,15 @@ void cmVisualStudio10TargetGenerator::Generate()
p = this->GeneratorTarget->GetProperty(
"DOTNET_TARGET_FRAMEWORK_VERSION");
}
const char* targetFrameworkVersion = cmToCStr(p);
if (!targetFrameworkVersion && this->ProjectType == csproj &&
std::string targetFrameworkVersion = p;
if (targetFrameworkVersion.empty() && this->ProjectType == csproj &&
this->GlobalGenerator->TargetsWindowsCE() &&
this->GlobalGenerator->GetVersion() ==
cmGlobalVisualStudioGenerator::VS12) {
// VS12 .NETCF default to .NET framework 3.9
targetFrameworkVersion = "v3.9";
}
if (targetFrameworkVersion) {
if (!targetFrameworkVersion.empty()) {
e1.Element("TargetFrameworkVersion", targetFrameworkVersion);
}
}