CMakePM: Add installDir CMakePresets support

Is part of CMakePrests v3, and slipped at the implementation time.

Task-number: QTCREATORBUG-24555
Change-Id: Id3ce90c0a979d44287fc03ae1dd49a64e964cdf2
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Cristian Adam
2022-10-20 16:22:28 +02:00
parent 430ff621aa
commit bf83f63a13
6 changed files with 53 additions and 0 deletions

View File

@@ -1241,6 +1241,7 @@ static void addCMakeConfigurePresetToInitialArguments(QStringList &initialArgume
env,
project->projectDirectory(),
buildDirectory);
CMakePresets::Macros::updateInstallDir(configurePreset, env, project->projectDirectory());
// Merge the presets cache variables
CMakeConfig cache;

View File

@@ -253,6 +253,44 @@ void updateToolchainFile(
configurePreset.cacheVariables = cache;
}
void updateInstallDir(PresetsDetails::ConfigurePreset &configurePreset,
const Utils::Environment &env,
const Utils::FilePath &sourceDirectory)
{
if (!configurePreset.installDir)
return;
QString installDirString = configurePreset.installDir.value();
CMakePresets::Macros::expand(configurePreset, env, sourceDirectory, installDirString);
// Resolve the relative path first to source and afterwards to build directory
Utils::FilePath installDir = Utils::FilePath::fromString(installDirString);
if (installDir.isRelativePath()) {
Utils::FilePath probePath = sourceDirectory.resolvePath(installDir);
if (probePath != sourceDirectory) {
installDir = probePath;
}
}
installDirString = installDir.cleanPath().toString();
// installDir takes precedence to CMAKE_INSTALL_PREFIX
CMakeConfig cache = configurePreset.cacheVariables ? configurePreset.cacheVariables.value()
: CMakeConfig();
auto it = std::find_if(cache.begin(), cache.end(), [](const CMakeConfigItem &item) {
return item.key == "CMAKE_INSTALL_PREFIX";
});
if (it != cache.end())
it->value = installDirString.toUtf8();
else
cache << CMakeConfigItem("CMAKE_INSTALL_PREFIX",
CMakeConfigItem::PATH,
installDirString.toUtf8());
configurePreset.cacheVariables = cache;
}
template<class PresetType>
void expandConditionValues(const PresetType &preset,
const Utils::Environment &env,

View File

@@ -53,6 +53,13 @@ void updateToolchainFile(PresetsDetails::ConfigurePreset &configurePreset,
const Utils::FilePath &sourceDirectory,
const Utils::FilePath &buildDirectory);
/**
* Updates the cacheVariables parameter of the configurePreset with the expanded installDir parameter.
* Including macro expansion and relative paths resolving.
*/
void updateInstallDir(PresetsDetails::ConfigurePreset &configurePreset,
const Utils::Environment &env,
const Utils::FilePath &sourceDirectory);
/**
* Expands the condition values and then evaluates the condition object of the preset and returns
* the boolean result.

View File

@@ -177,6 +177,8 @@ bool parseConfigurePresets(const QJsonValue &jsonValue,
preset.generator = object.value("generator").toString();
if (object.contains("binaryDir"))
preset.binaryDir = object.value("binaryDir").toString();
if (object.contains("installDir"))
preset.installDir = object.value("installDir").toString();
if (object.contains("toolchainFile"))
preset.toolchainFile = object.value("toolchainFile").toString();
if (object.contains("cmakeExecutable"))
@@ -462,6 +464,9 @@ void PresetsDetails::ConfigurePreset::inheritFrom(const ConfigurePreset &other)
if (!binaryDir && other.binaryDir)
binaryDir = other.binaryDir;
if (!installDir && other.installDir)
installDir = other.installDir;
if (!cmakeExecutable && other.cmakeExecutable)
cmakeExecutable = other.cmakeExecutable;

View File

@@ -100,6 +100,7 @@ public:
std::optional<ValueStrategyPair> toolset;
std::optional<QString> toolchainFile;
std::optional<QString> binaryDir;
std::optional<QString> installDir;
std::optional<QString> cmakeExecutable;
std::optional<CMakeConfig> cacheVariables;
std::optional<QHash<QString, QString>> environment;

View File

@@ -10,6 +10,7 @@
"name": "mingw",
"displayName": "MinGW 11.2.0",
"generator": "Ninja",
"installDir": "../inst-${presetName}",
"cacheVariables": {
"CMAKE_PREFIX_PATH": "c:/Qt/6.3.2/mingw_64"
},