CMake: Make CMakeConfig a proper class

Looks a bit more "object oriented" in my book.

Change-Id: I6a3b1b4691ec1c7465f652608678e8f31e7e52a7
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
hjk
2021-07-06 10:29:29 +02:00
parent 7aef03134d
commit 3aa324ef1f
8 changed files with 77 additions and 71 deletions

View File

@@ -515,7 +515,7 @@ void CMakeBuildSettingsWidget::batchEditConfiguration()
[expander](const QString &s) {
return expander->expand(s);
});
const CMakeConfig config = CMakeConfigItem::itemsFromArguments(expandedLines);
const CMakeConfig config = CMakeConfig::fromArguments(expandedLines);
m_configModel->setBatchEditConfiguration(config);
});
@@ -1048,7 +1048,7 @@ bool CMakeBuildConfiguration::fromMap(const QVariantMap &map)
}();
if (initialCMakeArguments().isEmpty()) {
QStringList initialArgs = defaultInitialCMakeArguments(kit(), buildTypeName)
+ Utils::transform(conf, [this](const CMakeConfigItem &i) {
+ Utils::transform(conf.toList(), [this](const CMakeConfigItem &i) {
return i.toArgument(macroExpander());
});
@@ -1110,7 +1110,8 @@ CMakeConfig CMakeBuildConfiguration::configurationChanges() const
QStringList CMakeBuildConfiguration::configurationChangesArguments() const
{
return Utils::transform(m_configurationChanges, [](const CMakeConfigItem &i) { return i.toArgument(); });
return Utils::transform(m_configurationChanges.toList(),
[](const CMakeConfigItem &i) { return i.toArgument(); });
}
QStringList CMakeBuildConfiguration::initialCMakeArguments() const
@@ -1296,9 +1297,9 @@ BuildInfo CMakeBuildConfigurationFactory::createBuildInfo(BuildType buildType)
BuildConfiguration::BuildType CMakeBuildConfiguration::buildType() const
{
QByteArray cmakeBuildTypeName = CMakeConfigItem::valueOf("CMAKE_BUILD_TYPE", m_configurationFromCMake);
QByteArray cmakeBuildTypeName = m_configurationFromCMake.valueOf("CMAKE_BUILD_TYPE");
if (cmakeBuildTypeName.isEmpty()) {
QByteArray cmakeCfgTypes = CMakeConfigItem::valueOf("CMAKE_CONFIGURATION_TYPES", m_configurationFromCMake);
QByteArray cmakeCfgTypes = m_configurationFromCMake.valueOf("CMAKE_CONFIGURATION_TYPES");
if (!cmakeCfgTypes.isEmpty())
cmakeBuildTypeName = cmakeBuildType().toUtf8();
}
@@ -1352,14 +1353,14 @@ QString CMakeBuildConfiguration::cmakeBuildType() const
QString errorMessage;
config = CMakeBuildSystem::parseCMakeCacheDotTxt(cmakeCacheTxt, &errorMessage);
} else {
config = CMakeConfigItem::itemsFromArguments(initialCMakeArguments());
config = CMakeConfig::fromArguments(initialCMakeArguments());
}
} else if (!hasCMakeCache) {
config = CMakeConfigItem::itemsFromArguments(initialCMakeArguments());
config = CMakeConfig::fromArguments(initialCMakeArguments());
}
if (!config.isEmpty() && !isMultiConfig()) {
cmakeBuildType = CMakeConfigItem::stringValueOf("CMAKE_BUILD_TYPE", config);
cmakeBuildType = config.stringValueOf("CMAKE_BUILD_TYPE");
const_cast<CMakeBuildConfiguration*>(this)->setCMakeBuildType(cmakeBuildType);
}

View File

@@ -838,7 +838,7 @@ void CMakeBuildSystem::wireUpConnections()
QString errorMessage;
const CMakeConfig config = CMakeBuildSystem::parseCMakeCacheDotTxt(cmakeCacheTxt, &errorMessage);
if (!config.isEmpty() && errorMessage.isEmpty()) {
QString cmakeBuildTypeName = CMakeConfigItem::stringValueOf("CMAKE_BUILD_TYPE", config);
QString cmakeBuildTypeName = config.stringValueOf("CMAKE_BUILD_TYPE");
cmakeBuildConfiguration()->setCMakeBuildType(cmakeBuildTypeName, true);
}
}
@@ -1057,7 +1057,7 @@ CMakeConfig CMakeBuildSystem::parseCMakeCacheDotTxt(const Utils::FilePath &cache
*errorMessage = tr("CMakeCache.txt file not found.");
return {};
}
CMakeConfig result = CMakeConfigItem::itemsFromFile(cacheFile, errorMessage);
CMakeConfig result = CMakeConfig::fromFile(cacheFile, errorMessage);
if (!errorMessage->isEmpty())
return {};
return result;
@@ -1210,7 +1210,7 @@ void CMakeBuildSystem::updateQmlJSCodeModel(const QStringList &extraHeaderPaths,
};
const CMakeConfig &cm = cmakeBuildConfiguration()->configurationFromCMake();
addImports(CMakeConfigItem::stringValueOf("QML_IMPORT_PATH", cm));
addImports(cm.stringValueOf("QML_IMPORT_PATH"));
addImports(kit()->value(QtSupport::KitQmlImportPath::id()).toString());
for (const QString &extraHeaderPath : extraHeaderPaths)
@@ -1244,7 +1244,7 @@ void CMakeBuildSystem::updateInitialCMakeExpandableVars()
{
const CMakeConfig &cm = cmakeBuildConfiguration()->configurationFromCMake();
const CMakeConfig &initialConfig =
CMakeConfigItem::itemsFromArguments(cmakeBuildConfiguration()->initialCMakeArguments());
CMakeConfig::fromArguments(cmakeBuildConfiguration()->initialCMakeArguments());
CMakeConfig config;
@@ -1275,7 +1275,7 @@ void CMakeBuildSystem::updateInitialCMakeExpandableVars()
});
if (it != cm.cend()) {
const QByteArray initialValue = CMakeConfigItem::expandedValueOf(kit(), var, initialConfig).toUtf8();
const QByteArray initialValue = initialConfig.expandedValueOf(kit(), var).toUtf8();
const FilePath initialPath = FilePath::fromString(QString::fromUtf8(initialValue));
const FilePath path = FilePath::fromString(QString::fromUtf8(it->value));
@@ -1299,7 +1299,7 @@ void CMakeBuildSystem::updateInitialCMakeExpandableVars()
});
if (it != cm.cend()) {
const QByteArrayList initialValueList = CMakeConfigItem::expandedValueOf(kit(), var, initialConfig).toUtf8().split(';');
const QByteArrayList initialValueList = initialConfig.expandedValueOf(kit(), var).toUtf8().split(';');
for (const auto &initialValue: initialValueList) {
const FilePath initialPath = FilePath::fromString(QString::fromUtf8(initialValue));

View File

@@ -55,29 +55,28 @@ CMakeConfigItem::CMakeConfigItem(const QByteArray &k, const QByteArray &v) :
key(k), value(v)
{ }
QByteArray CMakeConfigItem::valueOf(const QByteArray &key, const QList<CMakeConfigItem> &input)
QByteArray CMakeConfig::valueOf(const QByteArray &key) const
{
for (auto it = input.constBegin(); it != input.constEnd(); ++it) {
for (auto it = constBegin(); it != constEnd(); ++it) {
if (it->key == key)
return it->value;
}
return QByteArray();
}
QString CMakeConfigItem::stringValueOf(const QByteArray &key, const QList<CMakeConfigItem> &input)
QString CMakeConfig::stringValueOf(const QByteArray &key) const
{
return QString::fromUtf8(valueOf(key, input));
return QString::fromUtf8(valueOf(key));
}
FilePath CMakeConfigItem::filePathValueOf(const QByteArray &key, const QList<CMakeConfigItem> &input)
FilePath CMakeConfig::filePathValueOf(const QByteArray &key) const
{
return FilePath::fromUtf8(valueOf(key, input));
return FilePath::fromUtf8(valueOf(key));
}
QString CMakeConfigItem::expandedValueOf(const ProjectExplorer::Kit *k, const QByteArray &key,
const QList<CMakeConfigItem> &input)
QString CMakeConfig::expandedValueOf(const ProjectExplorer::Kit *k, const QByteArray &key) const
{
for (auto it = input.constBegin(); it != input.constEnd(); ++it) {
for (auto it = constBegin(); it != constEnd(); ++it) {
if (it->key == key)
return it->expandedValue(k);
}
@@ -312,7 +311,7 @@ static CMakeConfigItem unsetItemFromString(const QString &input)
return item;
}
QList<CMakeConfigItem> CMakeConfigItem::itemsFromArguments(const QStringList &list)
CMakeConfig CMakeConfig::fromArguments(const QStringList &list)
{
CMakeConfig result;
bool inSet = false;
@@ -348,7 +347,7 @@ QList<CMakeConfigItem> CMakeConfigItem::itemsFromArguments(const QStringList &li
return result;
}
QList<CMakeConfigItem> CMakeConfigItem::itemsFromFile(const Utils::FilePath &cacheFile, QString *errorMessage)
CMakeConfig CMakeConfig::fromFile(const Utils::FilePath &cacheFile, QString *errorMessage)
{
CMakeConfig result;
QFile cache(cacheFile.toString());

View File

@@ -43,18 +43,16 @@ class Kit;
namespace CMakeProjectManager {
class CMAKE_EXPORT CMakeConfigItem {
class CMakeConfig;
class CMAKE_EXPORT CMakeConfigItem
{
public:
enum Type { FILEPATH, PATH, BOOL, STRING, INTERNAL, STATIC, UNINITIALIZED };
CMakeConfigItem();
CMakeConfigItem(const QByteArray &k, Type t, const QByteArray &d, const QByteArray &v, const QStringList &s = {});
CMakeConfigItem(const QByteArray &k, const QByteArray &v);
static QByteArray valueOf(const QByteArray &key, const QList<CMakeConfigItem> &input);
static QString stringValueOf(const QByteArray &key, const QList<CMakeConfigItem> &input);
static Utils::FilePath filePathValueOf(const QByteArray &key, const QList<CMakeConfigItem> &input);
static QString expandedValueOf(const ProjectExplorer::Kit *k, const QByteArray &key,
const QList<CMakeConfigItem> &input);
static QStringList cmakeSplitValue(const QString &in, bool keepEmpty = false);
static Type typeStringToType(const QByteArray &typeString);
static QString typeToTypeString(const Type t);
@@ -66,8 +64,6 @@ public:
static bool less(const CMakeConfigItem &a, const CMakeConfigItem &b);
static CMakeConfigItem fromString(const QString &s);
static QList<CMakeConfigItem> itemsFromArguments(const QStringList &list);
static QList<CMakeConfigItem> itemsFromFile(const Utils::FilePath &input, QString *errorMessage);
QString toString(const Utils::MacroExpander *expander = nullptr) const;
QString toArgument(const Utils::MacroExpander *expander = nullptr) const;
QString toCMakeSetLine(const Utils::MacroExpander *expander = nullptr) const;
@@ -83,6 +79,23 @@ public:
QByteArray documentation;
QStringList values;
};
using CMakeConfig = QList<CMakeConfigItem>;
class CMAKE_EXPORT CMakeConfig : public QList<CMakeConfigItem>
{
public:
CMakeConfig() = default;
CMakeConfig(const QList<CMakeConfigItem> &items) : QList<CMakeConfigItem>(items) {}
CMakeConfig(std::initializer_list<CMakeConfigItem> items) : QList<CMakeConfigItem>(items) {}
const QList<CMakeConfigItem> &toList() const { return *this; }
static CMakeConfig fromArguments(const QStringList &list);
static CMakeConfig fromFile(const Utils::FilePath &input, QString *errorMessage);
QByteArray valueOf(const QByteArray &key) const;
QString stringValueOf(const QByteArray &key) const;
Utils::FilePath filePathValueOf(const QByteArray &key) const;
QString expandedValueOf(const ProjectExplorer::Kit *k, const QByteArray &key) const;
};
} // namespace CMakeProjectManager

View File

@@ -996,15 +996,15 @@ void CMakeConfigurationKitAspect::setConfiguration(Kit *k, const CMakeConfig &co
{
if (!k)
return;
const QStringList tmp = Utils::transform(config, [](const CMakeConfigItem &i) { return i.toString(); });
const QStringList tmp = Utils::transform(config.toList(),
[](const CMakeConfigItem &i) { return i.toString(); });
k->setValue(CONFIGURATION_ID, tmp);
}
QStringList CMakeConfigurationKitAspect::toStringList(const Kit *k)
{
QStringList current
= Utils::transform(CMakeConfigurationKitAspect::configuration(k),
[](const CMakeConfigItem &i) { return i.toString(); });
QStringList current = Utils::transform(CMakeConfigurationKitAspect::configuration(k).toList(),
[](const CMakeConfigItem &i) { return i.toString(); });
current = Utils::filtered(current, [](const QString &s) { return !s.isEmpty(); });
Utils::sort(current);
return current;
@@ -1023,7 +1023,7 @@ void CMakeConfigurationKitAspect::fromStringList(Kit *k, const QStringList &in)
QStringList CMakeConfigurationKitAspect::toArgumentsList(const Kit *k)
{
return Utils::transform(CMakeConfigurationKitAspect::configuration(k),
return Utils::transform(CMakeConfigurationKitAspect::configuration(k).toList(),
[](const CMakeConfigItem &i) { return i.toArgument(nullptr); });
}
@@ -1046,8 +1046,8 @@ QVariant CMakeConfigurationKitAspect::defaultValue(const Kit *k) const
{
// FIXME: Convert preload scripts
CMakeConfig config = defaultConfiguration(k);
const QStringList tmp
= Utils::transform(config, [](const CMakeConfigItem &i) { return i.toString(); });
const QStringList tmp = Utils::transform(config.toList(),
[](const CMakeConfigItem &i) { return i.toString(); });
return tmp;
}

View File

@@ -135,20 +135,16 @@ QStringList CMakeProjectImporter::importCandidates()
static FilePath qmakeFromCMakeCache(const CMakeConfig &config)
{
// Qt4 way to define things (more convenient for us, so try this first;-)
FilePath qmake
= FilePath::fromUtf8(CMakeConfigItem::valueOf(QByteArray("QT_QMAKE_EXECUTABLE"), config));
const FilePath qmake = config.filePathValueOf("QT_QMAKE_EXECUTABLE");
qCDebug(cmInputLog) << "QT_QMAKE_EXECUTABLE=" << qmake.toUserOutput();
if (!qmake.isEmpty())
return qmake;
// Check Qt5 settings: oh, the horror!
const FilePath qtCMakeDir = [config] {
FilePath tmp = FilePath::fromUtf8(
CMakeConfigItem::valueOf(QByteArray("Qt5Core_DIR"), config));
if (tmp.isEmpty()) {
tmp = FilePath::fromUtf8(
CMakeConfigItem::valueOf(QByteArray("Qt6Core_DIR"), config));
}
FilePath tmp = config.filePathValueOf("Qt5Core_DIR");
if (tmp.isEmpty())
tmp = config.filePathValueOf("Qt6Core_DIR");
return tmp;
}();
qCDebug(cmInputLog) << "QtXCore_DIR=" << qtCMakeDir.toUserOutput();
@@ -206,11 +202,11 @@ static FilePath qmakeFromCMakeCache(const CMakeConfig &config)
cmake.setEnvironment(env);
cmake.setTimeOutMessageBoxEnabled(false);
QString cmakeGenerator = CMakeConfigItem::stringValueOf(QByteArray("CMAKE_GENERATOR"), config);
FilePath cmakeExecutable = CMakeConfigItem::filePathValueOf(QByteArray("CMAKE_COMMAND"), config);
FilePath cmakeMakeProgram =CMakeConfigItem::filePathValueOf(QByteArray("CMAKE_MAKE_PROGRAM"), config);
FilePath toolchainFile = CMakeConfigItem::filePathValueOf(QByteArray("CMAKE_TOOLCHAIN_FILE"), config);
FilePath hostPath = CMakeConfigItem::filePathValueOf(QByteArray("QT_HOST_PATH"), config);
QString cmakeGenerator = config.stringValueOf(QByteArray("CMAKE_GENERATOR"));
FilePath cmakeExecutable = config.filePathValueOf(QByteArray("CMAKE_COMMAND"));
FilePath cmakeMakeProgram = config.filePathValueOf(QByteArray("CMAKE_MAKE_PROGRAM"));
FilePath toolchainFile = config.filePathValueOf(QByteArray("CMAKE_TOOLCHAIN_FILE"));
FilePath hostPath = config.filePathValueOf(QByteArray("QT_HOST_PATH"));
QStringList args;
args.push_back("-S");
@@ -270,7 +266,7 @@ static QVector<ToolChainDescription> extractToolChainsFromCache(const CMakeConfi
}
if (!haveCCxxCompiler) {
const QByteArray generator = CMakeConfigItem::valueOf(QByteArray("CMAKE_GENERATOR"), config);
const QByteArray generator = config.valueOf("CMAKE_GENERATOR");
QString cCompilerName;
QString cxxCompilerName;
if (generator.contains("Visual Studio")) {
@@ -282,8 +278,7 @@ static QVector<ToolChainDescription> extractToolChainsFromCache(const CMakeConfi
}
if (!cCompilerName.isEmpty() && !cxxCompilerName.isEmpty()) {
const FilePath linker = FilePath::fromUtf8(
CMakeConfigItem::valueOf(QByteArray("CMAKE_LINKER"), config));
const FilePath linker = config.filePathValueOf("CMAKE_LINKER");
if (!linker.isEmpty()) {
const FilePath compilerPath = linker.parentDir();
result.append({compilerPath.pathAppended(cCompilerName),
@@ -315,13 +310,11 @@ QList<void *> CMakeProjectImporter::examineDirectory(const FilePath &importPath,
return { };
}
QByteArrayList buildConfigurationTypes = {CMakeConfigItem::valueOf("CMAKE_BUILD_TYPE", config)};
QByteArrayList buildConfigurationTypes = {config.valueOf("CMAKE_BUILD_TYPE")};
if (buildConfigurationTypes.front().isEmpty()) {
QByteArray buildConfigurationTypesString =
CMakeConfigItem::valueOf("CMAKE_CONFIGURATION_TYPES", config);
if (!buildConfigurationTypesString.isEmpty()) {
QByteArray buildConfigurationTypesString = config.valueOf("CMAKE_CONFIGURATION_TYPES");
if (!buildConfigurationTypesString.isEmpty())
buildConfigurationTypes = buildConfigurationTypesString.split(';');
}
}
QList<void *> result;
@@ -329,7 +322,7 @@ QList<void *> CMakeProjectImporter::examineDirectory(const FilePath &importPath,
auto data = std::make_unique<DirectoryData>();
data->cmakeHomeDirectory =
FilePath::fromUserInput(CMakeConfigItem::stringValueOf("CMAKE_HOME_DIRECTORY", config))
FilePath::fromUserInput(config.stringValueOf("CMAKE_HOME_DIRECTORY"))
.canonicalPath();
const FilePath canonicalProjectDirectory = projectDirectory().canonicalPath();
if (data->cmakeHomeDirectory != canonicalProjectDirectory) {
@@ -344,12 +337,12 @@ QList<void *> CMakeProjectImporter::examineDirectory(const FilePath &importPath,
data->buildDirectory = importPath;
data->cmakeBuildType = buildType;
data->cmakeBinary = CMakeConfigItem::filePathValueOf("CMAKE_COMMAND", config);
data->generator = CMakeConfigItem::stringValueOf("CMAKE_GENERATOR", config);
data->extraGenerator = CMakeConfigItem::stringValueOf("CMAKE_EXTRA_GENERATOR", config);
data->platform = CMakeConfigItem::stringValueOf("CMAKE_GENERATOR_PLATFORM", config);
data->toolset = CMakeConfigItem::stringValueOf("CMAKE_GENERATOR_TOOLSET", config);
data->sysroot = CMakeConfigItem::filePathValueOf("CMAKE_SYSROOT", config);
data->cmakeBinary = config.filePathValueOf("CMAKE_COMMAND");
data->generator = config.stringValueOf("CMAKE_GENERATOR");
data->extraGenerator = config.stringValueOf("CMAKE_EXTRA_GENERATOR");
data->platform = config.stringValueOf("CMAKE_GENERATOR_PLATFORM");
data->toolset = config.stringValueOf("CMAKE_GENERATOR_TOOLSET");
data->sysroot = config.filePathValueOf("CMAKE_SYSROOT");
// Qt:
const FilePath qmake = qmakeFromCMakeCache(config);

View File

@@ -203,7 +203,7 @@ QList<ConfigModel::DataItem> ConfigModel::configurationForCMake() const
void ConfigModel::setConfiguration(const CMakeConfig &config)
{
setConfiguration(Utils::transform(config, [](const CMakeConfigItem &i) {
setConfiguration(Utils::transform(config.toList(), [](const CMakeConfigItem &i) {
return DataItem(i);
}));
}

View File

@@ -337,7 +337,7 @@ void FileApiReader::writeConfigurationIntoBuildDirectory(const QStringList &conf
QByteArray contents;
contents.append("# This file is managed by Qt Creator, do not edit!\n\n");
contents.append(
transform(CMakeConfigItem::itemsFromArguments(configurationArguments),
transform(CMakeConfig::fromArguments(configurationArguments).toList(),
[](const CMakeConfigItem &item) {
return item.toCMakeSetLine(nullptr);
})