1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/* KDevelop CMake Support
*
* Copyright 2007 Aleix Pol <aleixpol@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "cmakebuilddirchooser.h"
#include "ui_cmakebuilddirchooser.h"
#include "cmakeutils.h"
#include "debug.h"
#include <project/helper.h>
#include <interfaces/iproject.h>
#include <interfaces/icore.h>
#include <interfaces/iruntime.h>
#include <interfaces/iruntimecontroller.h>
#include <KLocalizedString>
#include <QDir>
#include <QDialogButtonBox>
#include <QVBoxLayout>
using namespace KDevelop;
CMakeBuildDirChooser::CMakeBuildDirChooser(QWidget* parent)
: QDialog(parent)
{
setWindowTitle(i18nc("@title:window", "Configure a Build Directory - %1", ICore::self()->runtimeController()->currentRuntime()->name()));
m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
m_buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
auto mainWidget = new QWidget(this);
auto mainLayout = new QVBoxLayout;
setLayout(mainLayout);
mainLayout->addWidget(mainWidget);
m_chooserUi = new Ui::CMakeBuildDirChooser;
m_chooserUi->setupUi(mainWidget);
setShowAvailableBuildDirs(false);
mainLayout->addWidget(m_buttonBox);
m_chooserUi->buildFolder->setMode(KFile::Directory|KFile::ExistingOnly);
m_chooserUi->installPrefix->setMode(KFile::Directory|KFile::ExistingOnly);
// configure the extraArguments widget to span the widget width but not
// expand the dialog to the width of the longest element in the argument history.
// static_cast<QComboBox*> needed because KComboBox::minimumSizeHint() override by mistake made it protected
m_chooserUi->extraArguments->setMinimumWidth(static_cast<QComboBox*>(m_chooserUi->extraArguments)->minimumSizeHint().width());
m_extraArgumentsHistory = new CMakeExtraArgumentsHistory(m_chooserUi->extraArguments);
connect(m_chooserUi->buildFolder, &KUrlRequester::textChanged, this, &CMakeBuildDirChooser::updated);
connect(m_chooserUi->buildType, &QComboBox::currentTextChanged,
this, &CMakeBuildDirChooser::updated);
connect(m_chooserUi->extraArguments, &KComboBox::editTextChanged, this, &CMakeBuildDirChooser::updated);
connect(m_chooserUi->availableBuildDirs, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &CMakeBuildDirChooser::adoptPreviousBuildDirectory);
const auto defaultInstallPrefix = ICore::self()->runtimeController()->currentRuntime()->getenv("KDEV_DEFAULT_INSTALL_PREFIX");
if (!defaultInstallPrefix.isEmpty()) {
m_chooserUi->installPrefix->setUrl(QUrl::fromLocalFile(QFile::decodeName(defaultInstallPrefix)));
}
updated();
}
CMakeBuildDirChooser::~CMakeBuildDirChooser()
{
delete m_extraArgumentsHistory;
delete m_chooserUi;
}
void CMakeBuildDirChooser::setProject( IProject* project )
{
m_project = project;
KDevelop::Path folder = m_project->path();
QString relative=CMake::projectRootRelative(m_project);
folder.cd(relative);
m_srcFolder = folder;
m_chooserUi->buildFolder->setUrl(KDevelop::proposedBuildFolder(m_srcFolder).toUrl());
setWindowTitle(i18nc("@title:window", "Configure a Build Directory for %1", project->name()));
update();
}
void CMakeBuildDirChooser::buildDirSettings(
const KDevelop::Path& buildDir,
QString& srcDir,
QString& installDir,
QString& buildType)
{
const QByteArray srcLine = QByteArrayLiteral("CMAKE_HOME_DIRECTORY:INTERNAL=");
const QByteArray installLine = QByteArrayLiteral("CMAKE_INSTALL_PREFIX:PATH=");
const QByteArray buildLine = QByteArrayLiteral("CMAKE_BUILD_TYPE:STRING=");
const Path cachePath(buildDir, QStringLiteral("CMakeCache.txt"));
QFile file(cachePath.toLocalFile());
srcDir.clear();
installDir.clear();
buildType.clear();
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qCWarning(CMAKE) << "Something really strange happened reading" << cachePath;
return;
}
int cnt = 0;
while (cnt != 3 && !file.atEnd())
{
auto rawLine = file.readLine();
if (rawLine.endsWith('\n'))
rawLine.chop(1);
if (rawLine.endsWith('\r'))
rawLine.chop(1);
auto match = [&rawLine](const QByteArray& prefix, QString* target) -> bool
{
if (rawLine.startsWith(prefix)) {
// note: CMakeCache.txt is UTF8-encoded, also see bug 329305
*target = QString::fromUtf8(rawLine.constData() + prefix.size(), rawLine.size() - prefix.size());
return true;
}
return false;
};
if (match(srcLine, &srcDir) || match(installLine, &installDir) || match(buildLine, &buildType)) {
++cnt;
}
}
qCDebug(CMAKE) << "The source directory for " << file.fileName() << "is" << srcDir;
qCDebug(CMAKE) << "The install directory for " << file.fileName() << "is" << installDir;
qCDebug(CMAKE) << "The build type for " << file.fileName() << "is" << buildType;
}
void CMakeBuildDirChooser::updated()
{
StatusTypes st;
Path chosenBuildFolder(m_chooserUi->buildFolder->url());
bool emptyUrl = chosenBuildFolder.isEmpty();
if( emptyUrl ) st |= BuildFolderEmpty;
bool dirEmpty = false, dirExists= false, dirRelative = false;
QString srcDir;
QString installDir;
QString buildType;
if(!emptyUrl)
{
QDir d(chosenBuildFolder.toLocalFile());
dirExists = d.exists();
dirEmpty = dirExists && d.count()<=2;
dirRelative = d.isRelative();
if(!dirEmpty && dirExists && !dirRelative)
{
bool hasCache=QFile::exists(Path(chosenBuildFolder, QStringLiteral("CMakeCache.txt")).toLocalFile());
if(hasCache)
{
QString proposed=m_srcFolder.toLocalFile();
buildDirSettings(chosenBuildFolder, srcDir, installDir, buildType);
if(!srcDir.isEmpty())
{
auto rt = ICore::self()->runtimeController()->currentRuntime();
if(QDir(rt->pathInHost(Path(srcDir)).toLocalFile()).canonicalPath() == QDir(proposed).canonicalPath())
{
st |= CorrectBuildDir | BuildDirCreated;
}
}
else
{
qCWarning(CMAKE) << "maybe you are trying a damaged CMakeCache.txt file. Proper: ";
}
if(!installDir.isEmpty() && QDir(installDir).exists())
{
m_chooserUi->installPrefix->setUrl(QUrl::fromLocalFile(installDir));
}
m_chooserUi->buildType->setCurrentText(buildType);
}
}
if(m_alreadyUsed.contains(chosenBuildFolder.toLocalFile()) && !m_chooserUi->availableBuildDirs->isEnabled()) {
st=DirAlreadyCreated;
}
}
else
{
setStatus(i18n("You need to specify a build directory."), false);
return;
}
if(st & (BuildDirCreated | CorrectBuildDir))
{
setStatus(i18n("Using an already created build directory."), true);
m_chooserUi->installPrefix->setEnabled(false);
m_chooserUi->buildType->setEnabled(false);
}
else
{
bool correct = (dirEmpty || !dirExists) && !(st & DirAlreadyCreated) && !dirRelative;
if(correct)
{
st |= CorrectBuildDir;
setStatus(i18n("Creating a new build directory."), true);
}
else
{
//Useful to explain what's going wrong
if(st & DirAlreadyCreated)
setStatus(i18n("Build directory already configured."), false);
else if (!srcDir.isEmpty())
setStatus(i18n("This build directory is for %1, "
"but the project directory is %2.", srcDir, m_srcFolder.toLocalFile()), false);
else if(dirRelative)
setStatus(i18n("You may not select a relative build directory."), false);
else if(!dirEmpty)
setStatus(i18n("The selected build directory is not empty."), false);
}
m_chooserUi->installPrefix->setEnabled(correct);
m_chooserUi->buildType->setEnabled(correct);
}
}
void CMakeBuildDirChooser::setCMakeExecutable(const Path& path)
{
m_chooserUi->cmakeExecutable->setUrl(path.toUrl());
updated();
}
void CMakeBuildDirChooser::setInstallPrefix(const Path& path)
{
m_chooserUi->installPrefix->setUrl(path.toUrl());
updated();
}
void CMakeBuildDirChooser::setBuildFolder(const Path& path)
{
m_chooserUi->buildFolder->setUrl(path.toUrl());
updated();
}
void CMakeBuildDirChooser::setBuildType(const QString& s)
{
m_chooserUi->buildType->addItem(s);
m_chooserUi->buildType->setCurrentIndex(m_chooserUi->buildType->findText(s));
updated();
}
void CMakeBuildDirChooser::setAlreadyUsed (const QStringList & used)
{
m_chooserUi->availableBuildDirs->addItems(used);
m_alreadyUsed = used;
updated();
}
void CMakeBuildDirChooser::setExtraArguments(const QString& args)
{
m_chooserUi->extraArguments->setEditText(args);
updated();
}
void CMakeBuildDirChooser::setStatus(const QString& message, bool canApply)
{
m_chooserUi->status->setMessageType(canApply ? KMessageWidget::Positive : KMessageWidget::Warning);
m_chooserUi->status->setText(message);
auto okButton = m_buttonBox->button(QDialogButtonBox::Ok);
okButton->setEnabled(canApply);
if (canApply) {
auto cancelButton = m_buttonBox->button(QDialogButtonBox::Cancel);
cancelButton->clearFocus();
}
}
void CMakeBuildDirChooser::adoptPreviousBuildDirectory(int index)
{
if (index > 0) {
Q_ASSERT(m_project);
m_chooserUi->cmakeExecutable->setUrl(CMake::currentCMakeExecutable(m_project, index -1).toUrl());
m_chooserUi->buildFolder->setUrl(CMake::currentBuildDir(m_project, index -1).toUrl());
m_chooserUi->installPrefix->setUrl(CMake::currentInstallDir(m_project, index -1).toUrl());
m_chooserUi->buildType->setCurrentText(CMake::currentBuildType(m_project, index -1));
m_chooserUi->extraArguments->setCurrentText(CMake::currentExtraArguments(m_project, index -1));
}
m_chooserUi->label_5->setEnabled(index == 0);
m_chooserUi->cmakeExecutable->setEnabled(index == 0);
m_chooserUi->label_3->setEnabled(index == 0);
m_chooserUi->buildFolder->setEnabled(index == 0);
m_chooserUi->label->setEnabled(index == 0);
m_chooserUi->installPrefix->setEnabled(index == 0);
m_chooserUi->label_2->setEnabled(index == 0);
m_chooserUi->buildType->setEnabled(index == 0);
m_chooserUi->status->setEnabled(index == 0);
m_chooserUi->extraArguments->setEnabled(index == 0);
m_chooserUi->label_4->setEnabled(index == 0);
}
bool CMakeBuildDirChooser::reuseBuilddir()
{
return m_chooserUi->availableBuildDirs->currentIndex() > 0;
}
int CMakeBuildDirChooser::alreadyUsedIndex() const
{
return m_chooserUi->availableBuildDirs->currentIndex() - 1;
}
void CMakeBuildDirChooser::setShowAvailableBuildDirs(bool show)
{
m_chooserUi->availableLabel->setVisible(show);
m_chooserUi->availableBuildDirs->setVisible(show);
}
Path CMakeBuildDirChooser::cmakeExecutable() const { return Path(m_chooserUi->cmakeExecutable->url()); }
Path CMakeBuildDirChooser::installPrefix() const { return Path(m_chooserUi->installPrefix->url()); }
Path CMakeBuildDirChooser::buildFolder() const { return Path(m_chooserUi->buildFolder->url()); }
QString CMakeBuildDirChooser::buildType() const { return m_chooserUi->buildType->currentText(); }
QString CMakeBuildDirChooser::extraArguments() const { return m_chooserUi->extraArguments->currentText(); }
|