1 /****************************************************************************
2 **
3 ** Copyright (C) Filippo Cucchetto <filippocucchetto@gmail.com>
4 ** Contact: http://www.qt.io/licensing
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "nimtoolchain.h"
27 #include "nimconstants.h"
28 #include "nimtoolchainfactory.h"
29 
30 #include <projectexplorer/abi.h>
31 #include <utils/environment.h>
32 
33 #include <QFileInfo>
34 #include <QProcess>
35 #include <QRegularExpression>
36 
37 using namespace ProjectExplorer;
38 using namespace Utils;
39 
40 namespace Nim {
41 
NimToolChain()42 NimToolChain::NimToolChain()
43     : NimToolChain(Constants::C_NIMTOOLCHAIN_TYPEID)
44 {}
45 
NimToolChain(Utils::Id typeId)46 NimToolChain::NimToolChain(Utils::Id typeId)
47     : ToolChain(typeId)
48     , m_version(std::make_tuple(-1,-1,-1))
49 {
50     setLanguage(Constants::C_NIMLANGUAGE_ID);
51     setTypeDisplayName(tr("Nim"));
52     setTargetAbiNoSignal(Abi::hostAbi());
53     setCompilerCommandKey("Nim.NimToolChain.CompilerCommand");
54 }
55 
createMacroInspectionRunner() const56 ToolChain::MacroInspectionRunner NimToolChain::createMacroInspectionRunner() const
57 {
58     return ToolChain::MacroInspectionRunner();
59 }
60 
languageExtensions(const QStringList &) const61 LanguageExtensions NimToolChain::languageExtensions(const QStringList &) const
62 {
63     return LanguageExtension::None;
64 }
65 
warningFlags(const QStringList &) const66 WarningFlags NimToolChain::warningFlags(const QStringList &) const
67 {
68     return WarningFlags::NoWarnings;
69 }
70 
createBuiltInHeaderPathsRunner(const Environment &) const71 ToolChain::BuiltInHeaderPathsRunner NimToolChain::createBuiltInHeaderPathsRunner(
72         const Environment &) const
73 {
74     return ToolChain::BuiltInHeaderPathsRunner();
75 }
76 
addToEnvironment(Environment & env) const77 void NimToolChain::addToEnvironment(Environment &env) const
78 {
79     if (isValid())
80         env.prependOrSetPath(compilerCommand().parentDir().toString());
81 }
82 
makeCommand(const Environment & env) const83 FilePath NimToolChain::makeCommand(const Environment &env) const
84 {
85     const FilePath tmp = env.searchInPath("make");
86     return tmp.isEmpty() ? FilePath::fromString("make") : tmp;
87 }
88 
createOutputParsers() const89 QList<Utils::OutputLineParser *> NimToolChain::createOutputParsers() const
90 {
91     return {};
92 }
93 
createConfigurationWidget()94 std::unique_ptr<ProjectExplorer::ToolChainConfigWidget> NimToolChain::createConfigurationWidget()
95 {
96     return std::make_unique<NimToolChainConfigWidget>(this);
97 }
98 
compilerVersion() const99 QString NimToolChain::compilerVersion() const
100 {
101     return compilerCommand().isEmpty() || m_version == std::make_tuple(-1,-1,-1)
102             ? QString()
103             : QString::asprintf("%d.%d.%d",
104                                 std::get<0>(m_version),
105                                 std::get<1>(m_version),
106                                 std::get<2>(m_version));
107 }
108 
fromMap(const QVariantMap & data)109 bool NimToolChain::fromMap(const QVariantMap &data)
110 {
111     if (!ToolChain::fromMap(data))
112         return false;
113     parseVersion(compilerCommand(), m_version);
114     return true;
115 }
116 
parseVersion(const FilePath & path,std::tuple<int,int,int> & result)117 bool NimToolChain::parseVersion(const FilePath &path, std::tuple<int, int, int> &result)
118 {
119     QProcess process;
120     process.start(path.toString(), {"--version"});
121     if (!process.waitForFinished())
122         return false;
123     const QString version = QString::fromUtf8(process.readLine());
124     if (version.isEmpty())
125         return false;
126     const QRegularExpression regex("(\\d+)\\.(\\d+)\\.(\\d+)");
127     const QRegularExpressionMatch match = regex.match(version);
128     if (!match.hasMatch())
129         return false;
130     const QStringList text = match.capturedTexts();
131     if (text.length() != 4)
132         return false;
133     result = std::make_tuple(text[1].toInt(), text[2].toInt(), text[3].toInt());
134     return true;
135 }
136 
137 } // Nim
138