baremetalrunconfigurationfactory.cpp 5.2 KB
Newer Older
Tim Sander's avatar
Tim Sander committed
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 Tim Sander <tim@krieglstein.org>
** Contact: https://www.qt.io/licensing/
Tim Sander's avatar
Tim Sander committed
5 6 7 8 9 10 11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
Tim Sander's avatar
Tim Sander committed
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
Tim Sander's avatar
Tim Sander committed
23 24 25 26 27
**
****************************************************************************/

#include "baremetalrunconfigurationfactory.h"
#include "baremetalconstants.h"
28
#include "baremetalcustomrunconfiguration.h"
Tim Sander's avatar
Tim Sander committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include "baremetalrunconfiguration.h"

#include <projectexplorer/buildtargetinfo.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/project.h>
#include <projectexplorer/target.h>
#include <utils/qtcassert.h>

#include <QFileInfo>
#include <QString>

using namespace ProjectExplorer;

namespace BareMetal {
namespace Internal {

static QString pathFromId(Core::Id id)
{
    QByteArray idStr = id.name();
    if (!idStr.startsWith(BareMetalRunConfiguration::IdPrefix))
        return QString();
50
    return QString::fromUtf8(idStr.mid(int(strlen(BareMetalRunConfiguration::IdPrefix))));
Tim Sander's avatar
Tim Sander committed
51 52 53
}

BareMetalRunConfigurationFactory::BareMetalRunConfigurationFactory(QObject *parent) :
54
    IRunConfigurationFactory(parent)
Tim Sander's avatar
Tim Sander committed
55 56 57 58
{
    setObjectName(QLatin1String("BareMetalRunConfigurationFactory"));
}

59
bool BareMetalRunConfigurationFactory::canCreate(Target *parent, Core::Id id) const
Tim Sander's avatar
Tim Sander committed
60 61 62
{
    if (!canHandle(parent))
        return false;
63 64
    return id == BareMetalCustomRunConfiguration::runConfigId()
            || !parent->applicationTargets().targetForProject(pathFromId(id)).isEmpty();
Tim Sander's avatar
Tim Sander committed
65 66 67 68 69 70
}

bool BareMetalRunConfigurationFactory::canRestore(Target *parent, const QVariantMap &map) const
{
    if (!canHandle(parent))
        return false;
71 72 73
    const Core::Id id = idFromMap(map);
    return id == BareMetalCustomRunConfiguration::runConfigId()
            || idFromMap(map).name().startsWith(BareMetalRunConfiguration::IdPrefix);
Tim Sander's avatar
Tim Sander committed
74 75 76 77 78 79 80 81 82
}

bool BareMetalRunConfigurationFactory::canClone(Target *parent, RunConfiguration *source) const
{
    const BareMetalRunConfiguration * const bmrc
            = qobject_cast<BareMetalRunConfiguration *>(source);
    return bmrc && canCreate(parent, source->id());
}

83
QList<Core::Id> BareMetalRunConfigurationFactory::availableCreationIds(Target *parent, CreationMode mode) const
Tim Sander's avatar
Tim Sander committed
84
{
85
    Q_UNUSED(mode)
Tim Sander's avatar
Tim Sander committed
86 87 88 89 90 91 92
    QList<Core::Id> result;
    if (!canHandle(parent))
        return result;

    const Core::Id base = Core::Id(BareMetalRunConfiguration::IdPrefix);
    foreach (const BuildTargetInfo &bti, parent->applicationTargets().list)
        result << base.withSuffix(bti.projectFilePath.toString());
93
    result << BareMetalCustomRunConfiguration::runConfigId();
Tim Sander's avatar
Tim Sander committed
94 95 96
    return result;
}

97
QString BareMetalRunConfigurationFactory::displayNameForId(Core::Id id) const
Tim Sander's avatar
Tim Sander committed
98
{
99 100
    if (id == BareMetalCustomRunConfiguration::runConfigId())
        return BareMetalCustomRunConfiguration::runConfigDefaultDisplayName();
101
    return tr("%1 (on GDB server or hardware debugger)")
Tim Sander's avatar
Tim Sander committed
102 103 104
        .arg(QFileInfo(pathFromId(id)).completeBaseName());
}

105
RunConfiguration *BareMetalRunConfigurationFactory::doCreate(Target *parent, Core::Id id)
Tim Sander's avatar
Tim Sander committed
106
{
107 108
    if (id == BareMetalCustomRunConfiguration::runConfigId())
        return new BareMetalCustomRunConfiguration(parent);
Tim Sander's avatar
Tim Sander committed
109 110 111 112 113
    return new BareMetalRunConfiguration(parent, id, pathFromId(id));
}

RunConfiguration *BareMetalRunConfigurationFactory::doRestore(Target *parent, const QVariantMap &map)
{
114 115
    if (idFromMap(map) == BareMetalCustomRunConfiguration::runConfigId())
        return new BareMetalCustomRunConfiguration(parent);
Tim Sander's avatar
Tim Sander committed
116 117 118 119 120 121
    return doCreate(parent,Core::Id(BareMetalRunConfiguration::IdPrefix));
}

RunConfiguration *BareMetalRunConfigurationFactory::clone(Target *parent, RunConfiguration *source)
{
    QTC_ASSERT(canClone(parent, source), return 0);
122 123
    if (BareMetalCustomRunConfiguration *old = qobject_cast<BareMetalCustomRunConfiguration *>(source))
        return new BareMetalCustomRunConfiguration(parent, old);
Tim Sander's avatar
Tim Sander committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137
    BareMetalRunConfiguration *old = static_cast<BareMetalRunConfiguration*>(source);
    return new BareMetalRunConfiguration(parent,old);
}

bool BareMetalRunConfigurationFactory::canHandle(const Target *target) const
{
    if (!target->project()->supportsKit(target->kit()))
        return false;
    const Core::Id deviceType = DeviceTypeKitInformation::deviceTypeId(target->kit());
    return deviceType == BareMetal::Constants::BareMetalOsType;
}

} // namespace Internal
} // namespace BareMetal