OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / projectexplorer / debugginghelper.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "debugginghelper.h"
35
36 #include <coreplugin/icore.h>
37 #include <QtCore/QFileInfo>
38 #include <QtCore/QCoreApplication>
39 #include <QtCore/QHash>
40 #include <QtCore/QProcess>
41 #include <QtCore/QDir>
42 #include <QtCore/QDateTime>
43
44 #include <utils/synchronousprocess.h>
45
46 #include <QtGui/QDesktopServices>
47
48 using namespace ProjectExplorer;
49
50 static inline QStringList validBinaryFilenames()
51 {
52     return QStringList()
53             << QLatin1String("debug/gdbmacros.dll")
54             << QLatin1String("libgdbmacros.dylib")
55             << QLatin1String("libgdbmacros.so");
56 }
57
58 QStringList DebuggingHelperLibrary::debuggingHelperLibraryDirectories(const QString &qtInstallData)
59 {
60     const QChar slash = QLatin1Char('/');
61     const uint hash = qHash(qtInstallData);
62     QStringList directories;
63     directories
64             << (qtInstallData + QLatin1String("/qtc-debugging-helper/"))
65             << QDir::cleanPath((QCoreApplication::applicationDirPath() + QLatin1String("/../qtc-debugging-helper/") + QString::number(hash))) + slash
66             << (QDesktopServices::storageLocation(QDesktopServices::DataLocation) + QLatin1String("/qtc-debugging-helper/") + QString::number(hash)) + slash;
67     return directories;
68 }
69
70 QStringList DebuggingHelperLibrary::locationsByInstallData(const QString &qtInstallData)
71 {
72     QStringList result;
73     QFileInfo fileInfo;
74     const QStringList binFilenames = validBinaryFilenames();
75     foreach(const QString &directory, debuggingHelperLibraryDirectories(qtInstallData)) {
76         if (getHelperFileInfoFor(binFilenames, directory, &fileInfo))
77             result << fileInfo.filePath();
78     }
79     return result;
80 }
81
82 QString DebuggingHelperLibrary::debuggingHelperLibraryByInstallData(const QString &qtInstallData)
83 {
84     if (!Core::ICore::instance())
85         return QString();
86
87     const QString mainFilename = Core::ICore::instance()->resourcePath()
88             + QLatin1String("/gdbmacros/gdbmacros.cpp");
89     const QStringList directories = DebuggingHelperLibrary::debuggingHelperLibraryDirectories(qtInstallData);
90     const QStringList binFilenames = validBinaryFilenames();
91
92     return byInstallDataHelper(mainFilename, directories, binFilenames);
93 }
94
95 QString DebuggingHelperLibrary::copy(const QString &qtInstallData,
96                                      QString *errorMessage)
97 {
98     // Locations to try:
99     //    $QTDIR/qtc-debugging-helper
100     //    $APPLICATION-DIR/qtc-debugging-helper/$hash
101     //    $USERDIR/qtc-debugging-helper/$hash
102     const QStringList directories = DebuggingHelperLibrary::debuggingHelperLibraryDirectories(qtInstallData);
103
104     QStringList files;
105     files << QLatin1String("gdbmacros.cpp") << QLatin1String("gdbmacros_p.h")
106           << QLatin1String("gdbmacros.h") << QLatin1String("gdbmacros.pro")
107           << QLatin1String("LICENSE.LGPL") << QLatin1String("LGPL_EXCEPTION.TXT");
108
109     QString sourcePath = Core::ICore::instance()->resourcePath() + QLatin1String("/gdbmacros/");
110
111     // Try to find a writeable directory.
112     foreach(const QString &directory, directories)
113         if (copyFiles(sourcePath, files, directory, errorMessage)) {
114             errorMessage->clear();
115             return directory;
116         }
117     *errorMessage = QCoreApplication::translate("ProjectExplorer::DebuggingHelperLibrary",
118                                                 "The debugger helpers could not be built in any of the directories:\n- %1\n\nReason: %2")
119                     .arg(directories.join(QLatin1String("\n- ")), *errorMessage);
120     return QString();
121 }
122
123 bool DebuggingHelperLibrary::build(const QString &directory, const QString &makeCommand,
124                                       const QString &qmakeCommand, const QString &mkspec,
125                                       const Utils::Environment &env, const QString &targetMode,
126                                       QString *output, QString *errorMessage)
127 {
128     return buildHelper(QCoreApplication::translate("ProjectExplorer::DebuggingHelperLibrary",
129                                                    "GDB helper"), QLatin1String("gdbmacros.pro"), directory,
130                        makeCommand, qmakeCommand, mkspec, env, targetMode, output, errorMessage);
131 }