repurpose deprecated -E switch
[profile/ivi/qtbase.git] / qmake / meta.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the qmake application of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "meta.h"
43 #include "project.h"
44 #include "option.h"
45 #include <qdir.h>
46
47 QT_BEGIN_NAMESPACE
48
49 QHash<QString, QHash<QString, QStringList> > QMakeMetaInfo::cache_vars;
50
51 QMakeMetaInfo::QMakeMetaInfo()
52 {
53
54 }
55
56
57 bool
58 QMakeMetaInfo::readLib(QString lib)
59 {
60     clear();
61     QString meta_file = findLib(lib);
62
63     if(cache_vars.contains(meta_file)) {
64         vars = cache_vars[meta_file];
65         return true;
66     }
67
68     bool ret = false;
69     if(!meta_file.isNull()) {
70         if(meta_file.endsWith(Option::pkgcfg_ext)) {
71             if((ret=readPkgCfgFile(meta_file)))
72                 meta_type = "pkgcfg";
73         } else if(meta_file.endsWith(Option::libtool_ext)) {
74             if((ret=readLibtoolFile(meta_file)))
75                 meta_type = "libtool";
76         } else if(meta_file.endsWith(Option::prl_ext)) {
77             QMakeProject proj;
78             if(!proj.read(Option::normalizePath(meta_file), QMakeProject::ReadProFile))
79                 return false;
80             meta_type = "qmake";
81             vars = proj.variables();
82             ret = true;
83         } else {
84             warn_msg(WarnLogic, "QMakeMetaInfo: unknown file format for %s",
85                      QDir::toNativeSeparators(meta_file).toLatin1().constData());
86         }
87     }
88     if(ret)
89         cache_vars.insert(meta_file, vars);
90     return ret;
91 }
92
93
94 void
95 QMakeMetaInfo::clear()
96 {
97     vars.clear();
98 }
99
100
101 QString
102 QMakeMetaInfo::findLib(QString lib)
103 {
104     if((lib[0] == '\'' || lib[0] == '"') &&
105        lib[lib.length()-1] == lib[0])
106     lib = lib.mid(1, lib.length()-2);
107     lib = Option::normalizePath(lib);
108
109     QString ret;
110     QString extns[] = { Option::prl_ext, /*Option::pkgcfg_ext, Option::libtool_ext,*/ QString() };
111     for(int extn = 0; !extns[extn].isNull(); extn++) {
112         if(lib.endsWith(extns[extn]))
113             ret = QFile::exists(lib) ? lib : QString();
114     }
115     if(ret.isNull()) {
116         for(int extn = 0; !extns[extn].isNull(); extn++) {
117             if(QFile::exists(lib + extns[extn])) {
118                 ret = lib + extns[extn];
119                 break;
120             }
121         }
122     }
123     if(ret.isNull()) {
124         debug_msg(2, "QMakeMetaInfo: Cannot find info file for %s", lib.toLatin1().constData());
125     } else {
126         debug_msg(2, "QMakeMetaInfo: Found info file %s for %s", ret.toLatin1().constData(), lib.toLatin1().constData());
127     }
128     return ret;
129 }
130
131
132 bool
133 QMakeMetaInfo::readLibtoolFile(const QString &f)
134 {
135     /* I can just run the .la through the .pro parser since they are compatible.. */
136     QMakeProject proj;
137     QString nf = Option::normalizePath(f);
138     if(!proj.read(nf, QMakeProject::ReadProFile))
139         return false;
140     QString dirf = nf.section(QLatin1Char('/'), 0, -2);
141     if(dirf == nf)
142         dirf = "";
143     else if(!dirf.isEmpty() && !dirf.endsWith(Option::output_dir))
144         dirf += QLatin1Char('/');
145     const QHash<QString, QStringList> &v = proj.variables();
146     for (QHash<QString, QStringList>::ConstIterator it = v.begin(); it != v.end(); ++it) {
147         QStringList lst = it.value();
148         if(lst.count() == 1 && (lst.first().startsWith("'") || lst.first().startsWith("\"")) &&
149            lst.first().endsWith(QString(lst.first()[0])))
150             lst = QStringList(lst.first().mid(1, lst.first().length() - 2));
151         if(!vars.contains("QMAKE_PRL_TARGET") &&
152            (it.key() == "dlname" || it.key() == "library_names" || it.key() == "old_library")) {
153             QString dir = v["libdir"].first();
154             if((dir.startsWith("'") || dir.startsWith("\"")) && dir.endsWith(QString(dir[0])))
155                 dir = dir.mid(1, dir.length() - 2);
156             dir = dir.trimmed();
157             if(!dir.isEmpty() && !dir.endsWith(QLatin1Char('/')))
158                 dir += QLatin1Char('/');
159             if(lst.count() == 1)
160                 lst = lst.first().split(" ");
161             for(QStringList::Iterator lst_it = lst.begin(); lst_it != lst.end(); ++lst_it) {
162                 bool found = false;
163                 QString dirs[] = { "", dir, dirf, dirf + ".libs/", "(term)" };
164                 for(int i = 0; !found && dirs[i] != "(term)"; i++) {
165                     if(QFile::exists(dirs[i] + (*lst_it))) {
166                         QString targ = dirs[i] + (*lst_it);
167                         if(QDir::isRelativePath(targ))
168                             targ.prepend(qmake_getpwd() + QLatin1Char('/'));
169                         vars["QMAKE_PRL_TARGET"] << targ;
170                         found = true;
171                     }
172                 }
173                 if(found)
174                     break;
175             }
176         } else if(it.key() == "dependency_libs") {
177             if(lst.count() == 1) {
178                 QString dep = lst.first();
179                 if((dep.startsWith("'") || dep.startsWith("\"")) && dep.endsWith(QString(dep[0])))
180                     dep = dep.mid(1, dep.length() - 2);
181                 lst = dep.trimmed().split(" ");
182             }
183             QMakeProject *conf = NULL;
184             for(QStringList::Iterator lit = lst.begin(); lit != lst.end(); ++lit) {
185                 if((*lit).startsWith("-R")) {
186                     if(!conf) {
187                         conf = new QMakeProject;
188                         conf->read(QMakeProject::ReadAll ^ QMakeProject::ReadProFile);
189                     }
190                     if(!conf->isEmpty("QMAKE_LFLAGS_RPATH"))
191                         (*lit) = conf->first("QMAKE_LFLAGS_RPATH") + (*lit).mid(2);
192                 }
193             }
194             if(conf)
195                 delete conf;
196             vars["QMAKE_PRL_LIBS"] += lst;
197         }
198     }
199     return true;
200 }
201
202 bool
203 QMakeMetaInfo::readPkgCfgFile(const QString &f)
204 {
205     fprintf(stderr, "Must implement reading in pkg-config files (%s)!!!\n", f.toLatin1().constData());
206     return false;
207 }
208
209 QT_END_NAMESPACE