CMake: Use Utils::FileName where appropriate

Change-Id: I3ab0a68920e27ebcf4e1dd58180a72ded58b892e
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Tobias Hunger
2016-10-13 12:02:52 +02:00
parent 567db27e57
commit 9ff2bd16d3
9 changed files with 59 additions and 59 deletions

View File

@@ -269,13 +269,13 @@ QSet<Core::Id> BuildDirManager::updateCodeModel(CppTools::ProjectPartBuilder &pp
// So remove the toolchain include paths, so that at least those end up in the correct // So remove the toolchain include paths, so that at least those end up in the correct
// place. // place.
QStringList cxxflags = getCXXFlagsFor(cbt, targetDataCache); QStringList cxxflags = getCXXFlagsFor(cbt, targetDataCache);
QSet<QString> tcIncludes; QSet<Utils::FileName> tcIncludes;
foreach (const HeaderPath &hp, tc->systemHeaderPaths(cxxflags, sysroot)) foreach (const HeaderPath &hp, tc->systemHeaderPaths(cxxflags, sysroot))
tcIncludes.insert(hp.path()); tcIncludes.insert(Utils::FileName::fromString(hp.path()));
QStringList includePaths; QStringList includePaths;
foreach (const QString &i, cbt.includeFiles) { foreach (const Utils::FileName &i, cbt.includeFiles) {
if (!tcIncludes.contains(i)) if (!tcIncludes.contains(i))
includePaths.append(i); includePaths.append(i.toString());
} }
includePaths += buildDirectory().toString(); includePaths += buildDirectory().toString();
ppBuilder.setIncludePaths(includePaths); ppBuilder.setIncludePaths(includePaths);
@@ -285,7 +285,8 @@ QSet<Core::Id> BuildDirManager::updateCodeModel(CppTools::ProjectPartBuilder &pp
ppBuilder.setDisplayName(cbt.title); ppBuilder.setDisplayName(cbt.title);
const QSet<Core::Id> partLanguages const QSet<Core::Id> partLanguages
= QSet<Core::Id>::fromList(ppBuilder.createProjectPartsForFiles(cbt.files)); = QSet<Core::Id>::fromList(ppBuilder.createProjectPartsForFiles(
Utils::transform(cbt.files, [](const Utils::FileName &fn) { return fn.toString(); })));
languages.unite(partLanguages); languages.unite(partLanguages);
} }
@@ -424,10 +425,10 @@ void BuildDirManager::extractData()
// Do not insert topCMake into m_cmakeFiles: The project already watches that! // Do not insert topCMake into m_cmakeFiles: The project already watches that!
// Find cbp file // Find cbp file
QString cbpFile = CMakeManager::findCbpFile(workDirectory().toString()); Utils::FileName cbpFile = Utils::FileName::fromString(CMakeManager::findCbpFile(workDirectory().toString()));
if (cbpFile.isEmpty()) if (cbpFile.isEmpty())
return; return;
m_cmakeFiles.insert(Utils::FileName::fromString(cbpFile)); m_cmakeFiles.insert(cbpFile);
// Add CMakeCache.txt file: // Add CMakeCache.txt file:
Utils::FileName cacheFile = workDirectory(); Utils::FileName cacheFile = workDirectory();
@@ -439,7 +440,7 @@ void BuildDirManager::extractData()
CMakeCbpParser cbpparser; CMakeCbpParser cbpparser;
CMakeTool *cmake = CMakeKitInformation::cmakeTool(kit()); CMakeTool *cmake = CMakeKitInformation::cmakeTool(kit());
// Parsing // Parsing
if (!cbpparser.parseCbpFile(cmake->pathMapper(), cbpFile, sourceDirectory().toString())) if (!cbpparser.parseCbpFile(cmake->pathMapper(), cbpFile, sourceDirectory()))
return; return;
m_projectName = cbpparser.projectName(); m_projectName = cbpparser.projectName();
@@ -612,7 +613,7 @@ QStringList BuildDirManager::getCXXFlagsFor(const CMakeBuildTarget &buildTarget,
bool BuildDirManager::extractCXXFlagsFromMake(const CMakeBuildTarget &buildTarget, bool BuildDirManager::extractCXXFlagsFromMake(const CMakeBuildTarget &buildTarget,
QHash<QString, QStringList> &cache) QHash<QString, QStringList> &cache)
{ {
QString makeCommand = QDir::fromNativeSeparators(buildTarget.makeCommand); QString makeCommand = buildTarget.makeCommand.toString();
int startIndex = makeCommand.indexOf('\"'); int startIndex = makeCommand.indexOf('\"');
int endIndex = makeCommand.indexOf('\"', startIndex + 1); int endIndex = makeCommand.indexOf('\"', startIndex + 1);
if (startIndex != -1 && endIndex != -1) { if (startIndex != -1 && endIndex != -1) {
@@ -650,7 +651,7 @@ bool BuildDirManager::extractCXXFlagsFromNinja(const CMakeBuildTarget &buildTarg
// found // found
// Get "all" target's working directory // Get "all" target's working directory
QByteArray ninjaFile; QByteArray ninjaFile;
QString buildNinjaFile = QDir::fromNativeSeparators(buildTargets().at(0).workingDirectory); QString buildNinjaFile = buildTargets().at(0).workingDirectory.toString();
buildNinjaFile += "/build.ninja"; buildNinjaFile += "/build.ninja";
QFile buildNinja(buildNinjaFile); QFile buildNinja(buildNinjaFile);
if (buildNinja.exists()) { if (buildNinja.exists()) {

View File

@@ -45,10 +45,10 @@ namespace Internal {
//// ////
namespace { namespace {
int distance(const QString &targetDirectory, const FileName &fileName) int distance(const FileName &targetDirectory, const FileName &fileName)
{ {
const QString commonParent = commonPath(QStringList() << targetDirectory << fileName.toString()); const QString commonParent = commonPath(QStringList({ targetDirectory.toString(), fileName.toString() }));
return targetDirectory.mid(commonParent.size()).count('/') return targetDirectory.toString().mid(commonParent.size()).count('/')
+ fileName.toString().mid(commonParent.size()).count('/'); + fileName.toString().mid(commonParent.size()).count('/');
} }
} // namespace } // namespace
@@ -102,7 +102,7 @@ void CMakeCbpParser::sortFiles()
foreach (const QString &unitTarget, unitTargets) { foreach (const QString &unitTarget, unitTargets) {
int index = indexOf(m_buildTargets, equal(&CMakeBuildTarget::title, unitTarget)); int index = indexOf(m_buildTargets, equal(&CMakeBuildTarget::title, unitTarget));
if (index != -1) { if (index != -1) {
m_buildTargets[index].files.append(fileName.toString()); m_buildTargets[index].files.append(fileName);
qCDebug(log) << " into" << m_buildTargets[index].title << "(target attribute)"; qCDebug(log) << " into" << m_buildTargets[index].title << "(target attribute)";
continue; continue;
} }
@@ -113,7 +113,7 @@ void CMakeCbpParser::sortFiles()
// fallback for cmake < 3.3: // fallback for cmake < 3.3:
if (fileName.parentDir() == parentDirectory && last) { if (fileName.parentDir() == parentDirectory && last) {
// easy case, same parent directory as last file // easy case, same parent directory as last file
last->files.append(fileName.toString()); last->files.append(fileName);
qCDebug(log) << " into" << last->title << "(same parent)"; qCDebug(log) << " into" << last->title << "(same parent)";
} else { } else {
int bestDistance = std::numeric_limits<int>::max(); int bestDistance = std::numeric_limits<int>::max();
@@ -141,7 +141,7 @@ void CMakeCbpParser::sortFiles()
} }
if (bestIndex != -1) { if (bestIndex != -1) {
m_buildTargets[bestIndex].files.append(fileName.toString()); m_buildTargets[bestIndex].files.append(fileName);
last = &m_buildTargets[bestIndex]; last = &m_buildTargets[bestIndex];
parentDirectory = fileName.parentDir(); parentDirectory = fileName.parentDir();
qCDebug(log) << " into" << last->title; qCDebug(log) << " into" << last->title;
@@ -156,14 +156,15 @@ void CMakeCbpParser::sortFiles()
qCDebug(log) << target.title << target.sourceDirectory << target.includeFiles << target.defines << target.files << "\n"; qCDebug(log) << target.title << target.sourceDirectory << target.includeFiles << target.defines << target.files << "\n";
} }
bool CMakeCbpParser::parseCbpFile(CMakeTool::PathMapper mapper, const QString &fileName, bool CMakeCbpParser::parseCbpFile(CMakeTool::PathMapper mapper, const FileName &fileName,
const QString &sourceDirectory) const FileName &sourceDirectory)
{ {
m_pathMapper = mapper; m_pathMapper = mapper;
m_buildDirectory = QFileInfo(fileName).absolutePath(); m_buildDirectory = FileName::fromString(fileName.toFileInfo().absolutePath());
m_sourceDirectory = sourceDirectory; m_sourceDirectory = sourceDirectory;
QFile fi(fileName); QFile fi(fileName.toString());
if (fi.exists() && fi.open(QFile::ReadOnly)) { if (fi.exists() && fi.open(QFile::ReadOnly)) {
setDevice(&fi); setDevice(&fi);
@@ -267,7 +268,7 @@ void CMakeCbpParser::parseBuildTarget()
void CMakeCbpParser::parseBuildTargetOption() void CMakeCbpParser::parseBuildTargetOption()
{ {
if (attributes().hasAttribute("output")) { if (attributes().hasAttribute("output")) {
m_buildTarget.executable = m_pathMapper(attributes().value("output").toString()); m_buildTarget.executable = m_pathMapper(FileName::fromString(attributes().value("output").toString()));
} else if (attributes().hasAttribute("type")) { } else if (attributes().hasAttribute("type")) {
const QStringRef value = attributes().value("type"); const QStringRef value = attributes().value("type");
if (value == "0" || value == "1") if (value == "0" || value == "1")
@@ -279,9 +280,9 @@ void CMakeCbpParser::parseBuildTargetOption()
else else
m_buildTarget.targetType = UtilityType; m_buildTarget.targetType = UtilityType;
} else if (attributes().hasAttribute("working_dir")) { } else if (attributes().hasAttribute("working_dir")) {
m_buildTarget.workingDirectory = attributes().value("working_dir").toString(); m_buildTarget.workingDirectory = FileName::fromUserInput(attributes().value("working_dir").toString());
QFile cmakeSourceInfoFile(m_buildTarget.workingDirectory QFile cmakeSourceInfoFile(m_buildTarget.workingDirectory.toString()
+ QStringLiteral("/CMakeFiles/CMakeDirectoryInformation.cmake")); + QStringLiteral("/CMakeFiles/CMakeDirectoryInformation.cmake"));
if (cmakeSourceInfoFile.open(QIODevice::ReadOnly | QIODevice::Text)) { if (cmakeSourceInfoFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&cmakeSourceInfoFile); QTextStream stream(&cmakeSourceInfoFile);
@@ -289,18 +290,19 @@ void CMakeCbpParser::parseBuildTargetOption()
while (!stream.atEnd()) { while (!stream.atEnd()) {
const QString lineTopSource = stream.readLine().trimmed(); const QString lineTopSource = stream.readLine().trimmed();
if (lineTopSource.startsWith(searchSource, Qt::CaseInsensitive)) { if (lineTopSource.startsWith(searchSource, Qt::CaseInsensitive)) {
m_buildTarget.sourceDirectory = lineTopSource.mid(searchSource.size()); QString src = lineTopSource.mid(searchSource.size());
m_buildTarget.sourceDirectory.chop(2); // cut off ") src.chop(2);
m_buildTarget.sourceDirectory = FileName::fromString(src);
break; break;
} }
} }
} }
if (m_buildTarget.sourceDirectory.isEmpty()) { if (m_buildTarget.sourceDirectory.isEmpty()) {
QDir dir(m_buildDirectory); QDir dir(m_buildDirectory.toString());
const QString relative = dir.relativeFilePath(m_buildTarget.workingDirectory); const QString relative = dir.relativeFilePath(m_buildTarget.workingDirectory.toString());
m_buildTarget.sourceDirectory m_buildTarget.sourceDirectory = m_sourceDirectory;
= FileName::fromString(m_sourceDirectory).appendPath(relative).toString(); m_buildTarget.sourceDirectory.appendPath(relative).toString();
} }
} }
while (!atEnd()) { while (!atEnd()) {
@@ -352,7 +354,7 @@ void CMakeCbpParser::parseMakeCommands()
void CMakeCbpParser::parseBuildTargetBuild() void CMakeCbpParser::parseBuildTargetBuild()
{ {
if (attributes().hasAttribute("command")) if (attributes().hasAttribute("command"))
m_buildTarget.makeCommand = m_pathMapper(attributes().value("command").toString()); m_buildTarget.makeCommand = m_pathMapper(FileName::fromUserInput(attributes().value("command").toString()));
while (!atEnd()) { while (!atEnd()) {
readNext(); readNext();
if (isEndElement()) if (isEndElement())
@@ -391,7 +393,8 @@ void CMakeCbpParser::parseAdd()
// CMake only supports <Add option=\> and <Add directory=\> // CMake only supports <Add option=\> and <Add directory=\>
const QXmlStreamAttributes addAttributes = attributes(); const QXmlStreamAttributes addAttributes = attributes();
QString includeDirectory = m_pathMapper(addAttributes.value("directory").toString()); FileName includeDirectory
= m_pathMapper(FileName::fromString(addAttributes.value("directory").toString()));
// allow adding multiple times because order happens // allow adding multiple times because order happens
if (!includeDirectory.isEmpty()) if (!includeDirectory.isEmpty())
@@ -424,8 +427,7 @@ void CMakeCbpParser::parseAdd()
void CMakeCbpParser::parseUnit() void CMakeCbpParser::parseUnit()
{ {
FileName fileName = FileName fileName =
FileName::fromString(m_pathMapper(FileName::fromUserInput(attributes().value("filename") m_pathMapper(FileName::fromUserInput(attributes().value("filename").toString()));
.toString()).toString()));
m_parsingCMakeUnit = false; m_parsingCMakeUnit = false;
m_unitTargets.clear(); m_unitTargets.clear();

View File

@@ -47,8 +47,8 @@ namespace Internal {
class CMakeCbpParser : public QXmlStreamReader class CMakeCbpParser : public QXmlStreamReader
{ {
public: public:
bool parseCbpFile(CMakeTool::PathMapper mapper, const QString &fileName, bool parseCbpFile(CMakeTool::PathMapper mapper, const Utils::FileName &fileName,
const QString &sourceDirectory); const Utils::FileName &sourceDirectory);
QList<ProjectExplorer::FileNode *> fileList(); QList<ProjectExplorer::FileNode *> fileList();
QList<ProjectExplorer::FileNode *> cmakeFileList(); QList<ProjectExplorer::FileNode *> cmakeFileList();
QList<CMakeBuildTarget> buildTargets(); QList<CMakeBuildTarget> buildTargets();
@@ -84,8 +84,8 @@ private:
QList<CMakeBuildTarget> m_buildTargets; QList<CMakeBuildTarget> m_buildTargets;
QString m_projectName; QString m_projectName;
QString m_compiler; QString m_compiler;
QString m_sourceDirectory; Utils::FileName m_sourceDirectory;
QString m_buildDirectory; Utils::FileName m_buildDirectory;
QStringList m_unitTargets; QStringList m_unitTargets;
}; };

View File

@@ -393,7 +393,7 @@ void CMakeProject::updateTargetRunConfigurations(Target *t)
auto btIt = buildTargetHash.constFind(cmakeRc->title()); auto btIt = buildTargetHash.constFind(cmakeRc->title());
cmakeRc->setEnabled(btIt != buildTargetHash.constEnd()); cmakeRc->setEnabled(btIt != buildTargetHash.constEnd());
if (btIt != buildTargetHash.constEnd()) { if (btIt != buildTargetHash.constEnd()) {
cmakeRc->setExecutable(btIt.value()->executable); cmakeRc->setExecutable(btIt.value()->executable.toString());
cmakeRc->setBaseWorkingDirectory(btIt.value()->workingDirectory); cmakeRc->setBaseWorkingDirectory(btIt.value()->workingDirectory);
} }
} }
@@ -434,12 +434,10 @@ void CMakeProject::updateApplicationAndDeploymentTargets()
continue; continue;
if (ct.targetType == ExecutableType || ct.targetType == DynamicLibraryType) if (ct.targetType == ExecutableType || ct.targetType == DynamicLibraryType)
deploymentData.addFile(ct.executable, deploymentPrefix + buildDir.relativeFilePath(QFileInfo(ct.executable).dir().path()), DeployableFile::TypeExecutable); deploymentData.addFile(ct.executable.toString(), deploymentPrefix + buildDir.relativeFilePath(ct.executable.toFileInfo().dir().path()), DeployableFile::TypeExecutable);
if (ct.targetType == ExecutableType) { if (ct.targetType == ExecutableType) {
// TODO: Put a path to corresponding .cbp file into projectFilePath? // TODO: Put a path to corresponding .cbp file into projectFilePath?
appTargetList.list << BuildTargetInfo(ct.title, appTargetList.list << BuildTargetInfo(ct.title, ct.executable, ct.executable);
FileName::fromString(ct.executable),
FileName::fromString(ct.executable));
} }
} }

View File

@@ -58,17 +58,17 @@ class CMAKE_EXPORT CMakeBuildTarget
{ {
public: public:
QString title; QString title;
QString executable; // TODO: rename to output? Utils::FileName executable; // TODO: rename to output?
TargetType targetType = UtilityType; TargetType targetType = UtilityType;
QString workingDirectory; Utils::FileName workingDirectory;
QString sourceDirectory; Utils::FileName sourceDirectory;
QString makeCommand; Utils::FileName makeCommand;
// code model // code model
QStringList includeFiles; QList<Utils::FileName> includeFiles;
QStringList compilerOptions; QStringList compilerOptions;
QByteArray defines; QByteArray defines;
QStringList files; QList<Utils::FileName> files;
void clear(); void clear();
}; };

View File

@@ -60,7 +60,7 @@ const char TITLE_KEY[] = "CMakeProjectManager.CMakeRunConfiguation.Title";
} // namespace } // namespace
CMakeRunConfiguration::CMakeRunConfiguration(Target *parent, Core::Id id, const QString &target, CMakeRunConfiguration::CMakeRunConfiguration(Target *parent, Core::Id id, const QString &target,
const QString &workingDirectory, const QString &title) : const Utils::FileName &workingDirectory, const QString &title) :
RunConfiguration(parent, id), RunConfiguration(parent, id),
m_buildTarget(target), m_buildTarget(target),
m_title(title) m_title(title)
@@ -70,7 +70,7 @@ CMakeRunConfiguration::CMakeRunConfiguration(Target *parent, Core::Id id, const
addExtraAspect(new TerminalAspect(this, QStringLiteral("CMakeProjectManager.CMakeRunConfiguration.UseTerminal"))); addExtraAspect(new TerminalAspect(this, QStringLiteral("CMakeProjectManager.CMakeRunConfiguration.UseTerminal")));
auto wd = new WorkingDirectoryAspect(this, QStringLiteral("CMakeProjectManager.CMakeRunConfiguration.UserWorkingDirectory")); auto wd = new WorkingDirectoryAspect(this, QStringLiteral("CMakeProjectManager.CMakeRunConfiguration.UserWorkingDirectory"));
wd->setDefaultWorkingDirectory(Utils::FileName::fromString(workingDirectory)); wd->setDefaultWorkingDirectory(workingDirectory);
addExtraAspect(wd); addExtraAspect(wd);
ctor(); ctor();
@@ -119,10 +119,9 @@ void CMakeRunConfiguration::setExecutable(const QString &executable)
m_buildTarget = executable; m_buildTarget = executable;
} }
void CMakeRunConfiguration::setBaseWorkingDirectory(const QString &wd) void CMakeRunConfiguration::setBaseWorkingDirectory(const Utils::FileName &wd)
{ {
extraAspect<WorkingDirectoryAspect>() extraAspect<WorkingDirectoryAspect>()->setDefaultWorkingDirectory(wd);
->setDefaultWorkingDirectory(Utils::FileName::fromString(wd));
} }
QVariantMap CMakeRunConfiguration::toMap() const QVariantMap CMakeRunConfiguration::toMap() const
@@ -246,7 +245,7 @@ RunConfiguration *CMakeRunConfigurationFactory::doCreate(Target *parent, Core::I
CMakeProject *project = static_cast<CMakeProject *>(parent->project()); CMakeProject *project = static_cast<CMakeProject *>(parent->project());
const QString title(buildTargetFromId(id)); const QString title(buildTargetFromId(id));
const CMakeBuildTarget &ct = project->buildTargetForTitle(title); const CMakeBuildTarget &ct = project->buildTargetForTitle(title);
return new CMakeRunConfiguration(parent, id, ct.executable, ct.workingDirectory, ct.title); return new CMakeRunConfiguration(parent, id, ct.executable.toString(), ct.workingDirectory, ct.title);
} }
bool CMakeRunConfigurationFactory::canClone(Target *parent, RunConfiguration *source) const bool CMakeRunConfigurationFactory::canClone(Target *parent, RunConfiguration *source) const
@@ -273,7 +272,7 @@ bool CMakeRunConfigurationFactory::canRestore(Target *parent, const QVariantMap
RunConfiguration *CMakeRunConfigurationFactory::doRestore(Target *parent, const QVariantMap &map) RunConfiguration *CMakeRunConfigurationFactory::doRestore(Target *parent, const QVariantMap &map)
{ {
return new CMakeRunConfiguration(parent, idFromMap(map), QString(), QString(), QString()); return new CMakeRunConfiguration(parent, idFromMap(map), QString(), Utils::FileName(), QString());
} }
QString CMakeRunConfigurationFactory::buildTargetFromId(Core::Id id) QString CMakeRunConfigurationFactory::buildTargetFromId(Core::Id id)

View File

@@ -39,13 +39,13 @@ class CMakeRunConfiguration : public ProjectExplorer::RunConfiguration
public: public:
CMakeRunConfiguration(ProjectExplorer::Target *parent, Core::Id id, const QString &target, CMakeRunConfiguration(ProjectExplorer::Target *parent, Core::Id id, const QString &target,
const QString &workingDirectory, const QString &title); const Utils::FileName &workingDirectory, const QString &title);
ProjectExplorer::Runnable runnable() const override; ProjectExplorer::Runnable runnable() const override;
QWidget *createConfigurationWidget() override; QWidget *createConfigurationWidget() override;
void setExecutable(const QString &executable); void setExecutable(const QString &executable);
void setBaseWorkingDirectory(const QString &workingDirectory); void setBaseWorkingDirectory(const Utils::FileName &workingDirectory);
QString title() const; QString title() const;

View File

@@ -232,7 +232,7 @@ CMakeTool::PathMapper CMakeTool::pathMapper() const
{ {
if (m_pathMapper) if (m_pathMapper)
return m_pathMapper; return m_pathMapper;
return [](const QString &s) { return s; }; return [](const Utils::FileName &fn) { return fn; };
} }
void CMakeTool::readInformation(CMakeTool::QueryType type) const void CMakeTool::readInformation(CMakeTool::QueryType type) const

View File

@@ -75,7 +75,7 @@ public:
bool matches(const QString &n, const QString &ex) const; bool matches(const QString &n, const QString &ex) const;
}; };
typedef std::function<QString (const QString &)> PathMapper; typedef std::function<Utils::FileName (const Utils::FileName &)> PathMapper;
explicit CMakeTool(Detection d, const Core::Id &id); explicit CMakeTool(Detection d, const Core::Id &id);
explicit CMakeTool(const QVariantMap &map, bool fromSdk); explicit CMakeTool(const QVariantMap &map, bool fromSdk);