1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the qmake application of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
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 QMap<QString, QMap<QString, QStringList> > QMakeMetaInfo::cache_vars;
50 
QMakeMetaInfo()51 QMakeMetaInfo::QMakeMetaInfo()
52 {
53 
54 }
55 
56 
57 bool
readLib(QString lib)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::fixPathToLocalOS(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", meta_file.toLatin1().constData());
85         }
86     }
87     if(ret)
88         cache_vars.insert(meta_file, vars);
89     return ret;
90 }
91 
92 
93 void
clear()94 QMakeMetaInfo::clear()
95 {
96     vars.clear();
97 }
98 
99 
100 QString
findLib(QString lib)101 QMakeMetaInfo::findLib(QString lib)
102 {
103     if((lib[0] == '\'' || lib[0] == '"') &&
104        lib[lib.length()-1] == lib[0])
105 	lib = lib.mid(1, lib.length()-2);
106     lib = Option::fixPathToLocalOS(lib);
107 
108     QString ret;
109     QString extns[] = { Option::prl_ext, /*Option::pkgcfg_ext, Option::libtool_ext,*/ QString() };
110     for(int extn = 0; !extns[extn].isNull(); extn++) {
111         if(lib.endsWith(extns[extn]))
112             ret = QFile::exists(lib) ? lib : QString();
113     }
114     if(ret.isNull()) {
115         for(int extn = 0; !extns[extn].isNull(); extn++) {
116             if(QFile::exists(lib + extns[extn])) {
117                 ret = lib + extns[extn];
118                 break;
119             }
120         }
121     }
122     if(ret.isNull()) {
123         debug_msg(2, "QMakeMetaInfo: Cannot find info file for %s", lib.toLatin1().constData());
124     } else {
125         debug_msg(2, "QMakeMetaInfo: Found info file %s for %s", ret.toLatin1().constData(), lib.toLatin1().constData());
126     }
127     return ret;
128 }
129 
130 
131 bool
readLibtoolFile(const QString & f)132 QMakeMetaInfo::readLibtoolFile(const QString &f)
133 {
134     /* I can just run the .la through the .pro parser since they are compatible.. */
135     QMakeProject proj;
136     if(!proj.read(Option::fixPathToLocalOS(f), QMakeProject::ReadProFile))
137         return false;
138     QString dirf = Option::fixPathToTargetOS(f).section(Option::dir_sep, 0, -2);
139     if(dirf == f)
140         dirf = "";
141     else if(!dirf.isEmpty() && !dirf.endsWith(Option::output_dir))
142         dirf += Option::dir_sep;
143     QMap<QString, QStringList> &v = proj.variables();
144     for(QMap<QString, QStringList>::Iterator it = v.begin(); it != v.end(); ++it) {
145         QStringList lst = it.value();
146         if(lst.count() == 1 && (lst.first().startsWith("'") || lst.first().startsWith("\"")) &&
147            lst.first().endsWith(QString(lst.first()[0])))
148             lst = QStringList(lst.first().mid(1, lst.first().length() - 2));
149         if(!vars.contains("QMAKE_PRL_TARGET") &&
150            (it.key() == "dlname" || it.key() == "library_names" || it.key() == "old_library")) {
151             QString dir = v["libdir"].first();
152             if((dir.startsWith("'") || dir.startsWith("\"")) && dir.endsWith(QString(dir[0])))
153                 dir = dir.mid(1, dir.length() - 2);
154             dir = dir.trimmed();
155             if(!dir.isEmpty() && !dir.endsWith(Option::dir_sep))
156                 dir += Option::dir_sep;
157             if(lst.count() == 1)
158                 lst = lst.first().split(" ");
159             for(QStringList::Iterator lst_it = lst.begin(); lst_it != lst.end(); ++lst_it) {
160                 bool found = false;
161                 QString dirs[] = { "", dir, dirf, dirf + ".libs" + QDir::separator(), "(term)" };
162                 for(int i = 0; !found && dirs[i] != "(term)"; i++) {
163                     if(QFile::exists(dirs[i] + (*lst_it))) {
164                         QString targ = dirs[i] + (*lst_it);
165                         if(QDir::isRelativePath(targ))
166                             targ.prepend(qmake_getpwd() + QDir::separator());
167                         vars["QMAKE_PRL_TARGET"] << targ;
168                         found = true;
169                     }
170                 }
171                 if(found)
172                     break;
173             }
174         } else if(it.key() == "dependency_libs") {
175             if(lst.count() == 1) {
176                 QString dep = lst.first();
177                 if((dep.startsWith("'") || dep.startsWith("\"")) && dep.endsWith(QString(dep[0])))
178                     dep = dep.mid(1, dep.length() - 2);
179                 lst = dep.trimmed().split(" ");
180             }
181             QMakeProject *conf = NULL;
182             for(QStringList::Iterator lit = lst.begin(); lit != lst.end(); ++lit) {
183                 if((*lit).startsWith("-R")) {
184                     if(!conf) {
185                         conf = new QMakeProject;
186                         conf->read(QMakeProject::ReadAll ^ QMakeProject::ReadProFile);
187                     }
188                     if(!conf->isEmpty("QMAKE_LFLAGS_RPATH"))
189                         (*lit) = conf->first("QMAKE_LFLAGS_RPATH") + (*lit).mid(2);
190                 }
191             }
192             if(conf)
193                 delete conf;
194             vars["QMAKE_PRL_LIBS"] += lst;
195         }
196     }
197     return true;
198 }
199 
200 bool
readPkgCfgFile(const QString & f)201 QMakeMetaInfo::readPkgCfgFile(const QString &f)
202 {
203     fprintf(stderr, "Must implement reading in pkg-config files (%s)!!!\n", f.toLatin1().constData());
204     return false;
205 }
206 
207 QT_END_NAMESPACE
208