1 /*
2     SPDX-FileCopyrightText: 2010 Patrick Spendrin <ps_ml@gmx.de>
3     SPDX-FileCopyrightText: 2013 Kevin Funk <kfunk@kde.org>
4     SPDX-FileCopyrightText: 2014 Sergey Kalinichev <kalinichev.so.0@gmail.com>
5 
6     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
7 */
8 
9 #include "msvccompiler.h"
10 
11 #include <QDir>
12 #include <QProcessEnvironment>
13 
14 #include <KProcess>
15 
16 #include <debug.h>
17 
18 using namespace KDevelop;
19 
defines(Utils::LanguageType,const QString &) const20 Defines MsvcCompiler::defines(Utils::LanguageType, const QString&) const
21 {
22     Defines ret;
23     //Get standard macros from kdevmsvcdefinehelpers
24     KProcess proc;
25     proc.setOutputChannelMode( KProcess::MergedChannels );
26     proc.setTextModeEnabled( true );
27 
28     // we want to use kdevmsvcdefinehelper as a pseudo compiler backend which
29     // returns the defines used in msvc. there is no such thing as -dM with cl.exe
30     proc << path() << QStringLiteral("/nologo") << QStringLiteral("/Bxkdevmsvcdefinehelper") << QStringLiteral("empty.cpp");
31 
32     // this will fail, so check on that as well
33     if ( proc.execute( 5000 ) == 2 ) {
34         QString line;
35         proc.readLine(); // read the filename
36 
37         while ( proc.canReadLine() ) {
38             QByteArray buff = proc.readLine();
39             qCDebug(DEFINESANDINCLUDES) << "msvcstandardmacros:" << buff;
40             if ( !buff.isEmpty() ) {
41                 line = QString::fromUtf8(buff);
42                 if ( line.startsWith( QLatin1String("#define ") ) ) {
43                     line = line.midRef(8).trimmed().toString();
44                     int pos = line.indexOf(QLatin1Char(' '));
45 
46                     if ( pos != -1 ) {
47                         ret[line.left(pos)] = line.mid(pos + 1);
48                     } else {
49                         ret[line] = QLatin1String("");
50                     }
51                 }
52             }
53         }
54     } else {
55         qCDebug(DEFINESANDINCLUDES) << QLatin1String("Unable to read standard c++ macro definitions from ") + path();
56         while ( proc.canReadLine() ){
57             qCDebug(DEFINESANDINCLUDES)  << proc.readLine();
58         }
59         qCDebug(DEFINESANDINCLUDES)  << proc.exitCode();
60     }
61 
62     // MSVC builtin attributes
63     {
64         ret[QStringLiteral("__cdecl")] = QLatin1String("");
65         ret[QStringLiteral("__fastcall")] = QLatin1String("");
66         ret[QStringLiteral("__stdcall")] = QLatin1String("");
67         ret[QStringLiteral("__thiscall")] = QLatin1String("");
68     }
69 
70     // MSVC builtin types
71     // see http://msdn.microsoft.com/en-us/library/cc953fe1.aspx
72     {
73         ret[QStringLiteral("__int8")] = QStringLiteral("char");
74         ret[QStringLiteral("__int16")] = QStringLiteral("short");
75         ret[QStringLiteral("__int32")] = QStringLiteral("int");
76         ret[QStringLiteral("__int64")] = QStringLiteral("long long");
77         ret[QStringLiteral("__int16")] = QStringLiteral("short");
78         ret[QStringLiteral("__ptr32")] = QLatin1String("");
79         ret[QStringLiteral("__ptr64")] = QLatin1String("");
80     }
81 
82     // MSVC specific modifiers
83     // see http://msdn.microsoft.com/en-us/library/vstudio/s04b5w00.aspx
84     {
85         ret[QStringLiteral("__sptr")] = QLatin1String("");
86         ret[QStringLiteral("__uptr")] = QLatin1String("");
87         ret[QStringLiteral("__unaligned")] = QLatin1String("");
88         ret[QStringLiteral("__w64")] = QLatin1String("");
89     }
90 
91     // MSVC function specifiers
92     // see http://msdn.microsoft.com/de-de/library/z8y1yy88.aspx
93     {
94         ret[QStringLiteral("__inline")] = QLatin1String("");
95         ret[QStringLiteral("__forceinline")] = QLatin1String("");
96     }
97 
98     return ret;
99 }
100 
includes(Utils::LanguageType,const QString &) const101 Path::List MsvcCompiler::includes(Utils::LanguageType, const QString&) const
102 {
103 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
104     const QStringList _includePaths = QProcessEnvironment::systemEnvironment().value(QStringLiteral("INCLUDE")).split(QLatin1Char(';'), Qt::SkipEmptyParts);
105 #else
106     const QStringList _includePaths = QProcessEnvironment::systemEnvironment().value(QStringLiteral("INCLUDE")).split(QLatin1Char(';'), QString::SkipEmptyParts);
107 #endif
108     Path::List includePaths;
109     includePaths.reserve(_includePaths.size());
110     for (const QString& include : _includePaths) {
111         includePaths.append( Path( QDir::fromNativeSeparators( include ) ) );
112     }
113     return includePaths;
114 }
115 
MsvcCompiler(const QString & name,const QString & path,bool editable,const QString & factoryName)116 MsvcCompiler::MsvcCompiler(const QString& name, const QString& path, bool editable, const QString& factoryName):
117     ICompiler(name, path, factoryName, editable)
118 {}
119