File: mesonnewbuilddir.cpp

package info (click to toggle)
kdevelop 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 73,508 kB
  • sloc: cpp: 291,803; python: 4,322; javascript: 3,518; sh: 1,316; ansic: 703; xml: 414; php: 95; lisp: 66; makefile: 31; sed: 12
file content (216 lines) | stat: -rw-r--r-- 6,477 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 2018 Daniel Mensinger <daniel@mensinger-ka.de>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "mesonnewbuilddir.h"

#include "mesonbuilder.h"
#include "mesonmanager.h"
#include "ui_mesonnewbuilddir.h"
#include <debug.h>

#include <interfaces/icore.h>
#include <interfaces/iproject.h>
#include <interfaces/iruncontroller.h>
#include <interfaces/iruntime.h>
#include <interfaces/iruntimecontroller.h>
#include <project/helper.h>

#include <KColorScheme>

#include <QDialogButtonBox>
#include <QFileInfo>

#include <algorithm>

using namespace KDevelop;

MesonNewBuildDir::MesonNewBuildDir(IProject* project, QWidget* parent)
    : QDialog(parent)
    , m_project(project)
{
    Q_ASSERT(project); // Just in case
    auto* mgr = dynamic_cast<MesonManager*>(m_project->buildSystemManager());
    Q_ASSERT(mgr); // This dialog only works with the MesonManager

    setWindowTitle(
        i18nc("@title:window", "Configure a Build Directory - %1", ICore::self()->runtimeController()->currentRuntime()->name()));

    m_ui = new Ui::MesonNewBuildDir;
    m_ui->setupUi(this);

    m_ui->advanced->setSupportedBackends(mgr->supportedMesonBackends());

    connect(m_ui->b_buttonBox, &QDialogButtonBox::clicked, this, [this](QAbstractButton* b) {
        if (m_ui->b_buttonBox->buttonRole(b) == QDialogButtonBox::ResetRole) {
            resetFields();
        }
    });

    m_ui->i_buildDir->setAcceptMode(QFileDialog::AcceptSave);

    resetFields();
}

MesonNewBuildDir::~MesonNewBuildDir()
{
    delete m_ui;
}

void MesonNewBuildDir::resetFields()
{
    Meson::MesonConfig cfg = Meson::getMesonConfig(m_project);
    Path projectPath = m_project->path();
    auto* mgr = dynamic_cast<MesonManager*>(m_project->buildSystemManager());
    Q_ASSERT(mgr); // This dialog only works with the MesonManager

    auto aConf = m_ui->advanced->getConfig();

    // Find a build dir that is not already configured
    Path buildDirPath = projectPath;
    buildDirPath.addPath(QStringLiteral("build"));

    auto checkInCfg = [](const Meson::MesonConfig& cfg, const Path& p) -> bool {
        for (const auto& i : cfg.buildDirs) {
            if (i.buildDir == p) {
                return true;
            }
        }
        return false;
    };

    for (int i = 2; checkInCfg(cfg, buildDirPath); ++i) {
        buildDirPath = projectPath;
        buildDirPath.addPath(QStringLiteral("build%1").arg(i));
    }

    m_ui->i_buildDir->setUrl(buildDirPath.toUrl());

    // Extra args
    aConf.args.clear();

    // Backend
    aConf.backend = mgr->defaultMesonBackend();

    // Meson exe
    aConf.meson = mgr->findMeson();

    m_ui->advanced->setConfig(aConf);
    updated();
}

void MesonNewBuildDir::setStatus(const QString& str, bool validConfig)
{
    m_configIsValid = validConfig;

    KColorScheme scheme(QPalette::Normal);
    KColorScheme::ForegroundRole role;
    if (validConfig) {
        role = KColorScheme::PositiveText;
    } else {
        role = KColorScheme::NegativeText;
    }

    QPalette pal = m_ui->l_statusMessage->palette();
    pal.setColor(QPalette::WindowText, scheme.foreground(role).color());
    m_ui->l_statusMessage->setPalette(pal);
    m_ui->l_statusMessage->setText(str);

    auto okButton = m_ui->b_buttonBox->button(QDialogButtonBox::Ok);
    okButton->setEnabled(m_configIsValid);
    if (m_configIsValid) {
        auto cancelButton = m_ui->b_buttonBox->button(QDialogButtonBox::Cancel);
        cancelButton->clearFocus();
    }
}

void MesonNewBuildDir::updated()
{
    auto advanced = m_ui->advanced->getConfig();
    Path buildDir = Path(m_ui->i_buildDir->url());
    QFileInfo mesonExe(advanced.meson.toLocalFile());

    if (!mesonExe.exists() || !mesonExe.isExecutable() || !mesonExe.isFile()
        || !mesonExe.permission(QFileDevice::ReadUser | QFileDevice::ExeUser)) {
        setStatus(i18n("Specified meson executable does not exist"), false);
        return;
    }

    MesonBuilder::DirectoryStatus status = MesonBuilder::evaluateBuildDirectory(buildDir, advanced.backend);
    switch (status) {
    case MesonBuilder::CLEAN:
    case MesonBuilder::DOES_NOT_EXIST:
        setStatus(i18n("Creating new build directory"), true);
        break;
    case MesonBuilder::MESON_CONFIGURED:
        setStatus(i18n("Using an already configured build directory"), true);
        break;
    case MesonBuilder::MESON_FAILED_CONFIGURATION:
        setStatus(i18n("Using a broken meson build directory (this should be fine)"), true);
        break;
    case MesonBuilder::INVALID_BUILD_DIR:
        setStatus(i18n("Cannot use specified directory"), false);
        break;
    case MesonBuilder::DIR_NOT_EMPTY:
        setStatus(i18n("There are already files in the build directory"), false);
        break;
    case MesonBuilder::EMPTY_STRING:
        setStatus(i18n("The build directory field must not be empty"), false);
        break;
    case MesonBuilder::___UNDEFINED___:
        setStatus(i18n("You have reached unreachable code. This is a bug"), false);
        break;
    }

    bool buildDirChanged = false;
    if (m_oldBuildDir != buildDir.toLocalFile()) {
        m_oldBuildDir = buildDir.toLocalFile();
        buildDirChanged = true;
    }

    bool mesonHasChanged = m_ui->advanced->hasMesonChanged(); // Outside if to prevent lazy evaluation
    if (!m_ui->options->options() || mesonHasChanged || buildDirChanged) {
        if (status == MesonBuilder::MESON_CONFIGURED) {
            m_ui->options->repopulateFromBuildDir(m_project, currentConfig())->start();
        } else {
            m_ui->options->repopulateFromMesonFile(m_project, advanced.meson)->start();
        }
    }
}

Meson::BuildDir MesonNewBuildDir::currentConfig() const
{
    Meson::BuildDir buildDir;
    if (!m_configIsValid) {
        qCDebug(KDEV_Meson) << "Cannot generate build dir config from invalid config";
        return buildDir;
    }

    auto advanced = m_ui->advanced->getConfig();

    buildDir.buildDir = Path(m_ui->i_buildDir->url());
    buildDir.mesonArgs = advanced.args;
    buildDir.mesonBackend = advanced.backend;
    buildDir.mesonExecutable = advanced.meson;

    return buildDir;
}

QStringList MesonNewBuildDir::mesonArgs() const
{
    auto options = m_ui->options->options();
    if (!options) {
        return {};
    }

    return options->getMesonArgs();
}

bool MesonNewBuildDir::isConfigValid() const
{
    return m_configIsValid;
}

#include "moc_mesonnewbuilddir.cpp"