summaryrefslogtreecommitdiff
path: root/src/app/qbs-setup-toolchains
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2021-07-07 17:29:42 +0300
committerDenis Shienkov <denis.shienkov@gmail.com>2021-07-27 08:18:11 +0000
commitae1368529849bcad0d7d524ae9396c4205209322 (patch)
treeb520209a486da0b2f03e3f8fb170a0934f21a37c /src/app/qbs-setup-toolchains
parent08782fcc1002e8a6722cdc3d1b9912d01413805d (diff)
downloadqbs-ae1368529849bcad0d7d524ae9396c4205209322.tar.gz
Long live Digital Mars toolchain
This patch adds an experimental support for the Digital Mars C/C++ compiler for Windows: * https://www.digitalmars.com/ This compiler is only distributed for Windows host platform, but allows to create the binaries for Win32, Win16, DOS32 and DOS targets. This patch implements only the Win32 targets and has the following limitations: * Support for precompiled headers is not implemented yet (but which is supported by the compiler). * There is no support for the CI autotests due to the fact that the compiler has the following restrictions: - it needs a separate masm386 assembler. - it needs a special format of DLL library sources (required the DLL main function). - it needs the `kernel32.lib` to be always explicitly linked. - and, maybe other unexpected stuff. The following features are currently supported: * Compiling a source files in the C or the C++ mode. * Compiling the source resource (RC) files. * Creation of an applications (both console and GUI). * Creation of an dynamic (DLL) libraries with its import files. * Creation of a static libraries. Task-number: QBS-1598 Change-Id: I5a6ac05a7e7513a1c6d2403db037c309b0f12b90 Reviewed-by: Ivan Komissarov <ABBAPOH@gmail.com>
Diffstat (limited to 'src/app/qbs-setup-toolchains')
-rw-r--r--src/app/qbs-setup-toolchains/CMakeLists.txt2
-rw-r--r--src/app/qbs-setup-toolchains/dmcprobe.cpp221
-rw-r--r--src/app/qbs-setup-toolchains/dmcprobe.h61
-rw-r--r--src/app/qbs-setup-toolchains/probe.cpp24
-rw-r--r--src/app/qbs-setup-toolchains/probe.h6
-rw-r--r--src/app/qbs-setup-toolchains/qbs-setup-toolchains.pro2
-rw-r--r--src/app/qbs-setup-toolchains/qbs-setup-toolchains.qbs2
7 files changed, 318 insertions, 0 deletions
diff --git a/src/app/qbs-setup-toolchains/CMakeLists.txt b/src/app/qbs-setup-toolchains/CMakeLists.txt
index 212c2b5dc..bb287be52 100644
--- a/src/app/qbs-setup-toolchains/CMakeLists.txt
+++ b/src/app/qbs-setup-toolchains/CMakeLists.txt
@@ -5,6 +5,8 @@ set(SOURCES
commandlineparser.h
cosmicprobe.cpp
cosmicprobe.h
+ dmcprobe.cpp
+ dmcprobe.h
gccprobe.cpp
gccprobe.h
iarewprobe.cpp
diff --git a/src/app/qbs-setup-toolchains/dmcprobe.cpp b/src/app/qbs-setup-toolchains/dmcprobe.cpp
new file mode 100644
index 000000000..3ac12eba5
--- /dev/null
+++ b/src/app/qbs-setup-toolchains/dmcprobe.cpp
@@ -0,0 +1,221 @@
+/****************************************************************************
+**
+** Copyright (C) 2021 Denis Shienkov <denis.shienkov@gmail.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of Qbs.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** 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
+** 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.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** 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-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "probe.h"
+#include "dmcprobe.h"
+
+#include "../shared/logging/consolelogger.h"
+
+#include <logging/translator.h>
+
+#include <tools/hostosinfo.h>
+#include <tools/profile.h>
+
+#include <QtCore/qdir.h>
+#include <QtCore/qfile.h>
+#include <QtCore/qprocess.h>
+#include <QtCore/qregularexpression.h>
+#include <QtCore/qsettings.h>
+#include <QtCore/qstandardpaths.h>
+
+using namespace qbs;
+using Internal::Tr;
+using Internal::HostOsInfo;
+
+static QStringList knownDmcCompilerNames()
+{
+ return {QStringLiteral("dmc")};
+}
+
+static QString guessDmcArchitecture(const QFileInfo &compiler)
+{
+ const QStringList keys = {QStringLiteral("__I86__")};
+ const auto macros = dumpMacros([&compiler, &keys]() {
+ const QString filePath = QDir(QDir::tempPath())
+ .absoluteFilePath(QLatin1String("dmc-dump.c"));
+ QFile fakeIn(filePath);
+ if (!fakeIn.open(QIODevice::Truncate | QIODevice::WriteOnly | QIODevice::Text)) {
+ qbsWarning() << Tr::tr("Unable to open temporary file %1 for output: %2")
+ .arg(fakeIn.fileName(), fakeIn.errorString());
+ return QStringList{};
+ }
+ fakeIn.write("#define VALUE_TO_STRING(x) #x\n");
+ fakeIn.write("#define VALUE(x) VALUE_TO_STRING(x)\n");
+ fakeIn.write("#define VAR_NAME_VALUE(var) \"#define \" #var\" \"VALUE(var)\n");
+ for (const QString &key : keys) {
+ fakeIn.write("#if defined(" + key.toLatin1() + ")\n");
+ fakeIn.write("#pragma message (VAR_NAME_VALUE(" + key.toLatin1() + "))\n");
+ fakeIn.write("#endif\n");
+ }
+ fakeIn.close();
+ QProcess p;
+ p.start(compiler.absoluteFilePath(), {QStringLiteral("-e"), filePath});
+ p.waitForFinished(3000);
+ fakeIn.remove();
+ const QStringList lines = QString::fromUtf8(p.readAllStandardOutput())
+ .split(QRegularExpression(QLatin1String("\\r?\\n")));
+ return lines;
+ });
+
+ for (const QString &key : keys) {
+ if (macros.contains(key)) {
+ bool ok = false;
+ const auto value = macros.value(key).toInt(&ok);
+ if (ok) {
+ switch (value) {
+ case 0: // 8088
+ case 2: // 286
+ case 3: // 386
+ case 4: // 486
+ break;
+ case 5: // P5
+ case 6: // P6
+ return QLatin1String("x86");
+ default:
+ break;
+ }
+ }
+ }
+ }
+
+ return QLatin1String("unknown");
+}
+
+static Profile createDmcProfileHelper(const ToolchainInstallInfo &info,
+ Settings *settings,
+ QString profileName = QString())
+{
+ const QFileInfo compiler = info.compilerPath;
+ const QString architecture = guessDmcArchitecture(compiler);
+
+ // In case the profile is auto-detected.
+ if (profileName.isEmpty()) {
+ if (!info.compilerVersion.isValid()) {
+ profileName = QStringLiteral("dmc-unknown-%1").arg(architecture);
+ } else {
+ const QString version = info.compilerVersion.toString(QLatin1Char('_'),
+ QLatin1Char('_'));
+ profileName = QStringLiteral("dmc-%1-%2").arg(
+ version, architecture);
+ }
+ }
+
+ Profile profile(profileName, settings);
+ profile.setValue(QLatin1String("cpp.toolchainInstallPath"), compiler.absolutePath());
+ profile.setValue(QLatin1String("qbs.toolchainType"), QLatin1String("dmc"));
+ if (!architecture.isEmpty())
+ profile.setValue(QLatin1String("qbs.architecture"), architecture);
+
+ qbsInfo() << Tr::tr("Profile '%1' created for '%2'.").arg(
+ profile.name(), compiler.absoluteFilePath());
+ return profile;
+}
+
+static Version dumpDmcCompilerVersion(const QFileInfo &compiler)
+{
+ QProcess p;
+ QStringList args;
+ p.start(compiler.absoluteFilePath(), args);
+ p.waitForFinished(3000);
+ const auto es = p.exitStatus();
+ if (es != QProcess::NormalExit) {
+ const QByteArray out = p.readAll();
+ qbsWarning() << Tr::tr("Compiler dumping failed:\n%1")
+ .arg(QString::fromUtf8(out));
+ return Version{};
+ }
+
+ const QByteArray output = p.readAllStandardOutput();
+ const QRegularExpression re(QLatin1String(
+ "^Digital Mars Compiler Version (\\d+)\\.?(\\d+)\\.?(\\*|\\d+)?"));
+ const QRegularExpressionMatch match = re.match(QString::fromLatin1(output));
+ if (!match.hasMatch())
+ return Version{};
+
+ const auto major = match.captured(1).toInt();
+ const auto minor = match.captured(2).toInt();
+ const auto patch = match.captured(3).toInt();
+ return Version{major, minor, patch};
+}
+
+static std::vector<ToolchainInstallInfo> installedDmcsFromPath()
+{
+ std::vector<ToolchainInstallInfo> infos;
+ const auto compilerNames = knownDmcCompilerNames();
+ for (const QString &compilerName : compilerNames) {
+ const QFileInfo dmcPath(
+ findExecutable(
+ HostOsInfo::appendExecutableSuffix(compilerName)));
+ if (!dmcPath.exists())
+ continue;
+ const Version version = dumpDmcCompilerVersion(dmcPath);
+ infos.push_back({dmcPath, version});
+ }
+ std::sort(infos.begin(), infos.end());
+ return infos;
+}
+
+bool isDmcCompiler(const QString &compilerName)
+{
+ return Internal::any_of(knownDmcCompilerNames(),
+ [compilerName](const QString &knownName) {
+ return compilerName.contains(knownName);
+ });
+}
+
+void createDmcProfile(const QFileInfo &compiler, Settings *settings,
+ QString profileName)
+{
+ const ToolchainInstallInfo info = {compiler, Version{}};
+ createDmcProfileHelper(info, settings, std::move(profileName));
+}
+
+void dmcProbe(Settings *settings, std::vector<Profile> &profiles)
+{
+ qbsInfo() << Tr::tr("Trying to detect DMC toolchains...");
+
+ const std::vector<ToolchainInstallInfo> allInfos = installedDmcsFromPath();
+ for (const ToolchainInstallInfo &info : allInfos) {
+ const auto profile = createDmcProfileHelper(info, settings);
+ profiles.push_back(profile);
+ }
+
+ if (allInfos.empty())
+ qbsInfo() << Tr::tr("No DMC toolchains found.");
+}
diff --git a/src/app/qbs-setup-toolchains/dmcprobe.h b/src/app/qbs-setup-toolchains/dmcprobe.h
new file mode 100644
index 000000000..a2fbc74cb
--- /dev/null
+++ b/src/app/qbs-setup-toolchains/dmcprobe.h
@@ -0,0 +1,61 @@
+/****************************************************************************
+**
+** Copyright (C) 2021 Denis Shienkov <denis.shienkov@gmail.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of Qbs.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** 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
+** 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.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** 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-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QBS_SETUPTOOLCHAINS_DMCPROBE_H
+#define QBS_SETUPTOOLCHAINS_DMCPROBE_H
+
+#include <QtCore/qlist.h>
+
+QT_BEGIN_NAMESPACE
+class QFileInfo;
+QT_END_NAMESPACE
+
+namespace qbs {
+class Profile;
+class Settings;
+}
+
+bool isDmcCompiler(const QString &compilerName);
+
+void createDmcProfile(const QFileInfo &compiler, qbs::Settings *settings,
+ QString profileName);
+
+void dmcProbe(qbs::Settings *settings, std::vector<qbs::Profile> &profiles);
+
+#endif // Header guard
diff --git a/src/app/qbs-setup-toolchains/probe.cpp b/src/app/qbs-setup-toolchains/probe.cpp
index 27c96ec89..5a04232e1 100644
--- a/src/app/qbs-setup-toolchains/probe.cpp
+++ b/src/app/qbs-setup-toolchains/probe.cpp
@@ -40,6 +40,7 @@
#include "clangclprobe.h"
#include "cosmicprobe.h"
+#include "dmcprobe.h"
#include "gccprobe.h"
#include "iarewprobe.h"
#include "keilprobe.h"
@@ -120,6 +121,8 @@ QString toolchainTypeFromCompilerName(const QString &compilerName)
return QStringLiteral("sdcc");
if (isCosmicCompiler(compilerName))
return QStringLiteral("cosmic");
+ if (isDmcCompiler(compilerName))
+ return QStringLiteral("dmc");
return {};
}
@@ -140,6 +143,7 @@ void probe(Settings *settings)
keilProbe(settings, profiles);
sdccProbe(settings, profiles);
cosmicProbe(settings, profiles);
+ dmcProbe(settings, profiles);
if (profiles.empty()) {
qStderr << Tr::tr("Could not detect any toolchains. No profile created.") << Qt::endl;
@@ -181,6 +185,8 @@ void createProfile(const QString &profileName, const QString &toolchainType,
createSdccProfile(compiler, settings, profileName);
else if (toolchain.contains(QLatin1String("cosmic")))
createCosmicProfile(compiler, settings, profileName);
+ else if (toolchain.contains(QLatin1String("dmc")))
+ createDmcProfile(compiler, settings, profileName);
else
throw qbs::ErrorInfo(Tr::tr("Cannot create profile: Unknown toolchain type."));
}
@@ -307,3 +313,21 @@ bool isSameExecutable(const QString &filePath1, const QString &filePath2)
return false;
}
+
+MacrosMap dumpMacros(const std::function<QStringList()> &func)
+{
+ MacrosMap macros;
+ const QStringList lines = func();
+ for (const QString &line : lines) {
+ const QString prefix = QLatin1String("#define ");
+ if (!line.startsWith(prefix))
+ return macros;
+ const auto index = line.indexOf(QLatin1String(" "), prefix.length());
+ if (index != -1) {
+ const auto key = line.mid(prefix.length(), index - prefix.length());
+ const auto value = line.mid(index + 1);
+ macros.insert(key, value);
+ }
+ }
+ return macros;
+}
diff --git a/src/app/qbs-setup-toolchains/probe.h b/src/app/qbs-setup-toolchains/probe.h
index 63b4c6555..e97530285 100644
--- a/src/app/qbs-setup-toolchains/probe.h
+++ b/src/app/qbs-setup-toolchains/probe.h
@@ -46,6 +46,7 @@
#include <QtCore/qstring.h>
#include <QtCore/qstringlist.h>
+#include <functional> // for std::function
#include <tuple> // for std::tie
namespace qbs { class Settings; }
@@ -78,4 +79,9 @@ int extractVersion(const QByteArray &macroDump, const QByteArray &keyToken);
bool isSameExecutable(const QString &exe1, const QString &exe2);
+using MacrosMap = QMap<QString, QString>;
+using DefinesList = QVector<QByteArray>;
+
+MacrosMap dumpMacros(const std::function<QStringList()> &func);
+
#endif // Header guard
diff --git a/src/app/qbs-setup-toolchains/qbs-setup-toolchains.pro b/src/app/qbs-setup-toolchains/qbs-setup-toolchains.pro
index de7f55eb1..1ae1c710d 100644
--- a/src/app/qbs-setup-toolchains/qbs-setup-toolchains.pro
+++ b/src/app/qbs-setup-toolchains/qbs-setup-toolchains.pro
@@ -6,6 +6,7 @@ HEADERS += \
clangclprobe.h \
commandlineparser.h \
cosmicprobe.h \
+ dmcprobe.h \
gccprobe.h \
iarewprobe.h \
keilprobe.h \
@@ -18,6 +19,7 @@ SOURCES += \
clangclprobe.cpp \
commandlineparser.cpp \
cosmicprobe.cpp \
+ dmcprobe.cpp \
gccprobe.cpp \
iarewprobe.cpp \
keilprobe.cpp \
diff --git a/src/app/qbs-setup-toolchains/qbs-setup-toolchains.qbs b/src/app/qbs-setup-toolchains/qbs-setup-toolchains.qbs
index 3b4f2d737..d33b4c8fb 100644
--- a/src/app/qbs-setup-toolchains/qbs-setup-toolchains.qbs
+++ b/src/app/qbs-setup-toolchains/qbs-setup-toolchains.qbs
@@ -10,6 +10,8 @@ QbsApp {
"commandlineparser.h",
"cosmicprobe.cpp",
"cosmicprobe.h",
+ "dmcprobe.cpp",
+ "dmcprobe.h",
"gccprobe.cpp",
"gccprobe.h",
"iarewprobe.cpp",