tools/shared/symbian/epocroot.cpp
branchRCL_3
changeset 8 3f74d0d4af4c
child 30 5dc02b23752f
equal deleted inserted replaced
6:dee5afe5301f 8:3f74d0d4af4c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the qmake application of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    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 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <iostream>
       
    43 
       
    44 #include <QtCore/qdir.h>
       
    45 #include <QtCore/qxmlstream.h>
       
    46 
       
    47 #include "epocroot.h"
       
    48 #include "../windows/registry.h"
       
    49 
       
    50 // Registry key under which the location of the Symbian devices.xml file is
       
    51 // stored.
       
    52 // Note that, on 64-bit machines, this key is located under the 32-bit
       
    53 // compatibility key:
       
    54 //     HKEY_LOCAL_MACHINE\Software\Wow6432Node
       
    55 #define SYMBIAN_SDKS_REG_SUBKEY "Software\\Symbian\\EPOC SDKs\\CommonPath"
       
    56 
       
    57 #ifdef Q_OS_WIN32
       
    58 #   define SYMBIAN_SDKS_REG_HANDLE HKEY_LOCAL_MACHINE
       
    59 #else
       
    60 #   define SYMBIAN_SDKS_REG_HANDLE 0
       
    61 #endif
       
    62 
       
    63 // Value which is populated and returned by the epocRoot() function.
       
    64 // Stored as a static value in order to avoid unnecessary re-evaluation.
       
    65 static QString epocRootValue;
       
    66 
       
    67 #ifdef QT_BUILD_QMAKE
       
    68 std::ostream &operator<<(std::ostream &s, const QString &val) {
       
    69     s << val.toLocal8Bit().data();
       
    70     return s;
       
    71 }
       
    72 #else
       
    73 // Operator implemented in configureapp.cpp
       
    74 std::ostream &operator<<(std::ostream &s, const QString &val);
       
    75 #endif
       
    76 
       
    77 QString getDevicesXmlPath()
       
    78     {
       
    79     // Note that the following call will return a null string on platforms other
       
    80     // than Windows.  If support is required on other platforms for devices.xml,
       
    81     // an alternative mechanism for retrieving the location of this file will
       
    82     // be required.
       
    83     return readRegistryKey(SYMBIAN_SDKS_REG_HANDLE, SYMBIAN_SDKS_REG_SUBKEY);
       
    84     }
       
    85 
       
    86 /**
       
    87  * Checks whether epocRootValue points to an existent directory.
       
    88  * If not, epocRootValue is set to an empty string and an error message is printed.
       
    89  */
       
    90 void checkEpocRootExists(const QString &source)
       
    91 {
       
    92     if (!epocRootValue.isEmpty()) {
       
    93         QDir dir(epocRootValue);
       
    94         if (!dir.exists()) {
       
    95             std::cerr << "Warning: " << source << " is set to an invalid path: " << epocRootValue << std::endl;
       
    96             epocRootValue = QString();
       
    97         }
       
    98     }
       
    99 }
       
   100 
       
   101 /**
       
   102  * Translate path from Windows to Qt format.
       
   103  */
       
   104 static void fixEpocRoot(QString &path)
       
   105 {
       
   106     path.replace("\\", "/");
       
   107 
       
   108     if (path.size() > 1 && path[1] == QChar(':')) {
       
   109         path = path.mid(2);
       
   110     }
       
   111 
       
   112     if (!path.size() || path[path.size()-1] != QChar('/')) {
       
   113         path += QChar('/');
       
   114     }
       
   115 }
       
   116 
       
   117 /**
       
   118  * Determine the epoc root for the currently active SDK.
       
   119  */
       
   120 QString epocRoot()
       
   121 {
       
   122     if (epocRootValue.isEmpty()) {
       
   123         // 1. If environment variable EPOCROOT is set and points to an existent
       
   124         //    directory, this is returned.
       
   125         epocRootValue = qgetenv("EPOCROOT");
       
   126         checkEpocRootExists("EPOCROOT");
       
   127 
       
   128         if (epocRootValue.isEmpty()) {
       
   129             // 2. The location of devices.xml is specified by a registry key.  If this
       
   130             //    file exists, it is parsed.
       
   131             QString devicesXmlPath = getDevicesXmlPath();
       
   132             if (devicesXmlPath.isEmpty()) {
       
   133                 std::cerr << "Error: Symbian SDK registry key not found" << std::endl;
       
   134             } else {
       
   135                 devicesXmlPath += "/devices.xml";
       
   136                 QFile devicesFile(devicesXmlPath);
       
   137                 if (devicesFile.open(QIODevice::ReadOnly)) {
       
   138 
       
   139                     // 3. If the EPOCDEVICE environment variable is set and a corresponding
       
   140                     //    entry is found in devices.xml, and its epocroot value points to an
       
   141                     //    existent directory, it is returned.
       
   142                     // 4. If a device element marked as default is found in devices.xml and its
       
   143                     //    epocroot value points to an existent directory, this is returned.
       
   144 
       
   145                     const QString epocDeviceValue = qgetenv("EPOCDEVICE");
       
   146                     bool epocDeviceFound = false;
       
   147 
       
   148                     QXmlStreamReader xml(&devicesFile);
       
   149                     while (!xml.atEnd()) {
       
   150                         xml.readNext();
       
   151                         if (xml.isStartElement() && xml.name() == "devices") {
       
   152                             if (xml.attributes().value("version") == "1.0") {
       
   153                                 while (!(xml.isEndElement() && xml.name() == "devices") && !xml.atEnd()) {
       
   154                                     xml.readNext();
       
   155                                     if (xml.isStartElement() && xml.name() == "device") {
       
   156                                         const bool isDefault =  xml.attributes().value("default") == "yes";
       
   157                                         const QString id = xml.attributes().value("id").toString();
       
   158                                         const QString name =  xml.attributes().value("name").toString();
       
   159                                         const bool epocDeviceMatch = (id + ":" + name) == epocDeviceValue;
       
   160                                         epocDeviceFound |= epocDeviceMatch;
       
   161 
       
   162                                         if((epocDeviceValue.isEmpty() && isDefault) || epocDeviceMatch) {
       
   163                                             // Found a matching device
       
   164                                             while (!(xml.isEndElement() && xml.name() == "device") && !xml.atEnd()) {
       
   165                                                 xml.readNext();
       
   166                                                 if (xml.isStartElement() && xml.name() == "epocroot") {
       
   167                                                     epocRootValue = xml.readElementText();
       
   168                                                     const QString deviceSource = epocDeviceValue.isEmpty()
       
   169                                                         ? "default device"
       
   170                                                         : "EPOCDEVICE (" + epocDeviceValue + ")";
       
   171                                                     checkEpocRootExists(deviceSource);
       
   172                                                 }
       
   173                                             }
       
   174 
       
   175                                             if (epocRootValue.isEmpty())
       
   176                                                 xml.raiseError("No epocroot element found");
       
   177                                         }
       
   178                                     }
       
   179                                 }
       
   180                             } else {
       
   181                                 xml.raiseError("Invalid 'devices' element version");
       
   182                             }
       
   183                         }
       
   184                     }
       
   185                     if (xml.hasError()) {
       
   186                         std::cerr << "Error: \"" << xml.errorString() << "\" when parsing devices.xml" << std::endl;
       
   187                     } else {
       
   188                         if (epocRootValue.isEmpty()) {
       
   189                             if (!epocDeviceValue.isEmpty()) {
       
   190                                 if (epocDeviceFound) {
       
   191                                     std::cerr << "Error: missing or invalid epocroot attribute "
       
   192                                               << "in device '" << epocDeviceValue << "'";
       
   193                                 } else {
       
   194                                     std::cerr << "Error: no device matching EPOCDEVICE ("
       
   195                                               << epocDeviceValue << ")";
       
   196                                 }
       
   197                             } else {
       
   198                                 if (epocDeviceFound) {
       
   199                                     std::cerr << "Error: missing or invalid epocroot attribute "
       
   200                                               << "in default device";
       
   201                                 } else {
       
   202                                     std::cerr << "Error: no default device";
       
   203                                 }
       
   204                             }
       
   205                             std::cerr << " found in devices.xml file." << std::endl;
       
   206                         }
       
   207                     }
       
   208                 } else {
       
   209                     std::cerr << "Error: could not open file " << devicesXmlPath << std::endl;
       
   210                 }
       
   211             }
       
   212         }
       
   213 
       
   214         if (epocRootValue.isEmpty()) {
       
   215             // 5. An empty string is returned.
       
   216             std::cerr << "Error: failed to find epoc root" << std::endl
       
   217                  << "Either" << std::endl
       
   218                  << "    1. Set EPOCROOT environment variable to a valid value" << std::endl
       
   219                  << " or 2. Ensure that the HKEY_LOCAL_MACHINE\\" SYMBIAN_SDKS_REG_SUBKEY
       
   220                     " registry key is set, and then" << std::endl
       
   221                  << "       a. Set EPOCDEVICE environment variable to a valid device" << std::endl
       
   222                  << "    or b. Specify a default device in the devices.xml file." << std::endl;
       
   223         } else {
       
   224             fixEpocRoot(epocRootValue);
       
   225         }
       
   226     }
       
   227 
       
   228     return epocRootValue;
       
   229 }
       
   230