1 /****************************************************************************
2 ** $Id: qt/unixmake.cpp   3.3.8   edited Jan 11 14:37 $
3 **
4 ** Implementation of UnixMakefileGenerator class.
5 **
6 ** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
7 **
8 ** This file is part of qmake.
9 **
10 ** This file may be distributed under the terms of the Q Public License
11 ** as defined by Trolltech ASA of Norway and appearing in the file
12 ** LICENSE.QPL included in the packaging of this file.
13 **
14 ** This file may be distributed and/or modified under the terms of the
15 ** GNU General Public License version 2 as published by the Free Software
16 ** Foundation and appearing in the file LICENSE.GPL included in the
17 ** packaging of this file.
18 **
19 ** Licensees holding valid Qt Enterprise Edition licenses may use this
20 ** file in accordance with the Qt Commercial License Agreement provided
21 ** with the Software.
22 **
23 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
24 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25 **
26 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
27 **   information about Qt Commercial License Agreements.
28 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
29 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
30 **
31 ** Contact info@trolltech.com if any conditions of this licensing are
32 ** not clear to you.
33 **
34 **********************************************************************/
35 
36 #include "unixmake.h"
37 #include "option.h"
38 #include <qregexp.h>
39 #include <qfile.h>
40 #include <qdict.h>
41 #include <qdir.h>
42 #include <time.h>
43 
44 
45 void
init()46 UnixMakefileGenerator::init()
47 {
48     if(init_flag)
49 	return;
50     init_flag = TRUE;
51 
52     if(!project->isEmpty("QMAKE_FAILED_REQUIREMENTS")) /* no point */
53 	return;
54 
55     QStringList &configs = project->variables()["CONFIG"];
56     /* this should probably not be here, but I'm using it to wrap the .t files */
57     if(project->first("TEMPLATE") == "app")
58 	project->variables()["QMAKE_APP_FLAG"].append("1");
59     else if(project->first("TEMPLATE") == "lib")
60 	project->variables()["QMAKE_LIB_FLAG"].append("1");
61     else if(project->first("TEMPLATE") == "subdirs") {
62 	MakefileGenerator::init();
63 	if(project->isEmpty("MAKEFILE"))
64 	    project->variables()["MAKEFILE"].append("Makefile");
65 	if(project->isEmpty("QMAKE"))
66 	    project->variables()["QMAKE"].append("qmake");
67 	if(project->variables()["QMAKE_INTERNAL_QMAKE_DEPS"].findIndex("qmake_all") == -1)
68 	    project->variables()["QMAKE_INTERNAL_QMAKE_DEPS"].append("qmake_all");
69 	return; /* subdirs is done */
70     }
71 
72     if( project->isEmpty("QMAKE_EXTENSION_SHLIB") ) {
73 	if ( project->isEmpty("QMAKE_CYGWIN_SHLIB") ) {
74 	    project->variables()["QMAKE_EXTENSION_SHLIB"].append( "so" );
75 	} else {
76 	    project->variables()["QMAKE_EXTENSION_SHLIB"].append( "dll" );
77 	}
78     }
79     if( project->isEmpty("QMAKE_CFLAGS_PRECOMPILE"))
80 	project->variables()["QMAKE_CFLAGS_PRECOMPILE"].append("-x c-header -c");
81     if( project->isEmpty("QMAKE_CXXFLAGS_PRECOMPILE"))
82 	project->variables()["QMAKE_CXXFLAGS_PRECOMPILE"].append("-x c++-header -c");
83     if( project->isEmpty("QMAKE_CFLAGS_USE_PRECOMPILE"))
84 	project->variables()["QMAKE_CFLAGS_USE_PRECOMPILE"].append("-include");
85     if( project->isEmpty("QMAKE_EXTENSION_PLUGIN") )
86 	project->variables()["QMAKE_EXTENSION_PLUGIN"].append(project->first("QMAKE_EXTENSION_SHLIB"));
87     if( project->isEmpty("QMAKE_COPY_FILE") )
88 	project->variables()["QMAKE_COPY_FILE"].append( "$(COPY)" );
89     if( project->isEmpty("QMAKE_COPY_DIR") )
90 	project->variables()["QMAKE_COPY_DIR"].append( "$(COPY) -R" );
91     if( project->isEmpty("QMAKE_INSTALL_FILE") )
92 	project->variables()["QMAKE_INSTALL_FILE"].append( "$(COPY_FILE)" );
93     if( project->isEmpty("QMAKE_INSTALL_DIR") )
94 	project->variables()["QMAKE_INSTALL_DIR"].append( "$(COPY_DIR)" );
95     if( project->isEmpty("QMAKE_LIBTOOL") )
96 	project->variables()["QMAKE_LIBTOOL"].append( "libtool --silent" );
97     //If the TARGET looks like a path split it into DESTDIR and the resulting TARGET
98     if(!project->isEmpty("TARGET")) {
99 	QString targ = project->first("TARGET");
100 	int slsh = QMAX(targ.findRev('/'), targ.findRev(Option::dir_sep));
101 	if(slsh != -1) {
102 	    if(project->isEmpty("DESTDIR"))
103 		project->values("DESTDIR").append("");
104 	    else if(project->first("DESTDIR").right(1) != Option::dir_sep)
105 		project->variables()["DESTDIR"] = project->first("DESTDIR") + Option::dir_sep;
106 	    project->variables()["DESTDIR"] = project->first("DESTDIR") + targ.left(slsh+1);
107 	    project->variables()["TARGET"] = targ.mid(slsh+1);
108 	}
109     }
110 
111     project->variables()["QMAKE_ORIG_TARGET"] = project->variables()["TARGET"];
112     project->variables()["QMAKE_ORIG_DESTDIR"] = project->variables()["DESTDIR"];
113 
114     bool is_qt = (project->first("TARGET") == "qt" || project->first("TARGET") == "qte" ||
115 		  project->first("TARGET") == "qt-mt" || project->first("TARGET") == "qte-mt");
116     bool extern_libs = !project->isEmpty("QMAKE_APP_FLAG") ||
117 		       (!project->isEmpty("QMAKE_LIB_FLAG") &&
118 			project->isActiveConfig("dll")) || is_qt;
119     project->variables()["QMAKE_LIBS"] += project->variables()["LIBS"];
120     if ( (!project->isEmpty("QMAKE_LIB_FLAG") && !project->isActiveConfig("staticlib") ) ||
121 	 (project->isActiveConfig("qt") &&  project->isActiveConfig( "plugin" ) )) {
122 	if(configs.findIndex("dll") == -1) configs.append("dll");
123     } else if ( !project->isEmpty("QMAKE_APP_FLAG") || project->isActiveConfig("dll") ) {
124 	configs.remove("staticlib");
125     }
126     if ( project->isActiveConfig("warn_off") ) {
127 	project->variables()["QMAKE_CFLAGS"] += project->variables()["QMAKE_CFLAGS_WARN_OFF"];
128 	project->variables()["QMAKE_CXXFLAGS"] += project->variables()["QMAKE_CXXFLAGS_WARN_OFF"];
129     } else if ( project->isActiveConfig("warn_on") ) {
130 	project->variables()["QMAKE_CFLAGS"] += project->variables()["QMAKE_CFLAGS_WARN_ON"];
131 	project->variables()["QMAKE_CXXFLAGS"] += project->variables()["QMAKE_CXXFLAGS_WARN_ON"];
132     }
133     if ( project->isActiveConfig("debug") ) {
134 	project->variables()["QMAKE_CFLAGS"] += project->variables()["QMAKE_CFLAGS_DEBUG"];
135 	project->variables()["QMAKE_CXXFLAGS"] += project->variables()["QMAKE_CXXFLAGS_DEBUG"];
136 	project->variables()["QMAKE_LFLAGS"] += project->variables()["QMAKE_LFLAGS_DEBUG"];
137     } else {
138 	project->variables()["QMAKE_CFLAGS"] += project->variables()["QMAKE_CFLAGS_RELEASE"];
139 	project->variables()["QMAKE_CXXFLAGS"] += project->variables()["QMAKE_CXXFLAGS_RELEASE"];
140 	project->variables()["QMAKE_LFLAGS"] += project->variables()["QMAKE_LFLAGS_RELEASE"];
141     }
142     if(!project->isEmpty("QMAKE_INCREMENTAL"))
143 	project->variables()["QMAKE_LFLAGS"] += project->variables()["QMAKE_LFLAGS_INCREMENTAL"];
144     else if(!project->isEmpty("QMAKE_LFLAGS_PREBIND") &&
145 	    !project->variables()["QMAKE_LIB_FLAG"].isEmpty() &&
146 	    project->isActiveConfig("dll"))
147 	project->variables()["QMAKE_LFLAGS"] += project->variables()["QMAKE_LFLAGS_PREBIND"];
148     if(!project->isEmpty("QMAKE_INCDIR"))
149 	project->variables()["INCLUDEPATH"] += project->variables()["QMAKE_INCDIR"];
150     if(!project->isEmpty("QMAKE_LIBDIR")) {
151 	if ( !project->isEmpty("QMAKE_RPATH") )
152 	    project->variables()["QMAKE_LFLAGS"] += varGlue("QMAKE_LIBDIR", " " + var("QMAKE_RPATH"),
153 							    " " + var("QMAKE_RPATH"), "");
154 	project->variables()["QMAKE_LIBDIR_FLAGS"] += varGlue( "QMAKE_LIBDIR", "-L", " -L", "" );
155     }
156     if ( project->isActiveConfig("qtopia") ) {
157 	if(configs.findIndex("qtopialib") == -1)
158 	    configs.append("qtopialib");
159 	if(configs.findIndex("qtopiainc") == -1)
160 	    configs.append("qtopiainc");
161     }
162     if ( project->isActiveConfig("qtopiainc") )
163 	project->variables()["INCLUDEPATH"] += project->variables()["QMAKE_INCDIR_QTOPIA"];
164     if ( project->isActiveConfig("qtopialib") ) {
165 	if(!project->isEmpty("QMAKE_LIBDIR_QTOPIA"))
166 	    project->variables()["QMAKE_LIBDIR_FLAGS"] += varGlue("QMAKE_LIBDIR_QTOPIA", "-L", " -L", "");
167 	project->variables()["QMAKE_LIBS"] += project->variables()["QMAKE_LIBS_QTOPIA"];
168     }
169     if ( project->isActiveConfig("qt") ) {
170 	if ( project->isActiveConfig("accessibility" ) )
171 	    project->variables()[is_qt ? "PRL_EXPORT_DEFINES" : "DEFINES"].append("QT_ACCESSIBILITY_SUPPORT");
172 	if ( project->isActiveConfig("tablet") )
173 	    project->variables()[is_qt ? "PRL_EXPORT_DEFINES" : "DEFINES"].append("QT_TABLET_SUPPORT");
174 	if(configs.findIndex("moc")) configs.append("moc");
175 	project->variables()["INCLUDEPATH"] += project->variables()["QMAKE_INCDIR_QT"];
176 	if ( !project->isActiveConfig("debug") )
177 	    project->variables()[is_qt ? "PRL_EXPORT_DEFINES" : "DEFINES"].append("QT_NO_DEBUG");
178 	if ( !is_qt ) {
179 	    if ( !project->isEmpty("QMAKE_RPATH") ) {
180 		if ( !project->isEmpty("QMAKE_RTLDIR_QT") )
181 		    project->variables()["QMAKE_LFLAGS"] += varGlue("QMAKE_RTLDIR_QT", " " + var("QMAKE_RPATH"),
182 								    " " + var("QMAKE_RPATH"), "");
183 		else if ( !project->isEmpty("QMAKE_LIBDIR_QT") )
184 		    project->variables()["QMAKE_LFLAGS"] += varGlue("QMAKE_LIBDIR_QT", " " + var("QMAKE_RPATH"),
185 								    " " + var("QMAKE_RPATH"), "");
186 	    }
187 	    if ( !project->isEmpty("QMAKE_LIBDIR_QT") )
188 		project->variables()["QMAKE_LIBDIR_FLAGS"] += varGlue("QMAKE_LIBDIR_QT", "-L", " -L", "");
189 	    if ( project->isActiveConfig("thread") && !project->isEmpty("QMAKE_LIBS_QT_THREAD") )
190 		project->variables()["QMAKE_LIBS"] += project->variables()["QMAKE_LIBS_QT_THREAD"];
191 	    else
192 		project->variables()["QMAKE_LIBS"] += project->variables()["QMAKE_LIBS_QT"];
193 	}
194     }
195     if ( project->isActiveConfig("opengl") && !project->isActiveConfig("dlopen_opengl")) {
196 	project->variables()["INCLUDEPATH"] += project->variables()["QMAKE_INCDIR_OPENGL"];
197 	if(!project->isEmpty("QMAKE_LIBDIR_OPENGL"))
198 	    project->variables()["QMAKE_LIBDIR_FLAGS"] += varGlue("QMAKE_LIBDIR_OPENGL", "-L", " -L", "");
199 	if ( is_qt )
200 	    project->variables()["QMAKE_LIBS"] += project->variables()["QMAKE_LIBS_OPENGL_QT"];
201 	else
202 	    project->variables()["QMAKE_LIBS"] += project->variables()["QMAKE_LIBS_OPENGL"];
203     }
204     if ( extern_libs && (project->isActiveConfig("qt") || project->isActiveConfig("opengl")) ) {
205 	if(configs.findIndex("x11lib") == -1)
206 	    configs.append("x11lib");
207 	if ( project->isActiveConfig("opengl") && configs.findIndex("x11inc") == -1 )
208 	    configs.append("x11inc");
209     }
210     if ( project->isActiveConfig("x11") ) {
211 	if(configs.findIndex("x11lib") == -1)
212 	    configs.append("x11lib");
213 	if(configs.findIndex("x11inc") == -1)
214 	    configs.append("x11inc");
215     }
216     if ( project->isActiveConfig("x11inc") )
217 	project->variables()["INCLUDEPATH"] += project->variables()["QMAKE_INCDIR_X11"];
218     if ( project->isActiveConfig("x11lib") ) {
219 	if(!project->isEmpty("QMAKE_LIBDIR_X11"))
220 	    project->variables()["QMAKE_LIBDIR_FLAGS"] += varGlue("QMAKE_LIBDIR_X11", "-L", " -L", "");
221 	project->variables()["QMAKE_LIBS"] += project->variables()["QMAKE_LIBS_X11"];
222     }
223     if ( project->isActiveConfig("x11sm") )
224 	project->variables()["QMAKE_LIBS"] += project->variables()["QMAKE_LIBS_X11SM"];
225     if ( project->isActiveConfig("dylib") )
226 	project->variables()["QMAKE_LIBS"] += project->variables()["QMAKE_LIBS_DYNLOAD"];
227     if ( project->isActiveConfig("thread") ) {
228 	if(project->isActiveConfig("qt"))
229 	    project->variables()[is_qt ? "PRL_EXPORT_DEFINES" : "DEFINES"].append("QT_THREAD_SUPPORT");
230 	if ( !project->isEmpty("QMAKE_CFLAGS_THREAD")) {
231 	    project->variables()["QMAKE_CFLAGS"] += project->variables()["QMAKE_CFLAGS_THREAD"];
232 	    project->variables()["PRL_EXPORT_CFLAGS"] += project->variables()["QMAKE_CFLAGS_THREAD"];
233 	}
234 	if( !project->isEmpty("QMAKE_CXXFLAGS_THREAD")) {
235 	    project->variables()["QMAKE_CXXFLAGS"] += project->variables()["QMAKE_CXXFLAGS_THREAD"];
236 	    project->variables()["PRL_EXPORT_CXXFLAGS"] += project->variables()["QMAKE_CXXFLAGS_THREAD"];
237 	}
238 	project->variables()["INCLUDEPATH"] += project->variables()["QMAKE_INCDIR_THREAD"];
239 	project->variables()["QMAKE_LIBS"] += project->variables()["QMAKE_LIBS_THREAD"];
240 	if(!project->isEmpty("QMAKE_LFLAGS_THREAD"))
241 	    project->variables()["QMAKE_LFLAGS"] += project->variables()["QMAKE_LFLAGS_THREAD"];
242     }
243     if ( project->isActiveConfig("moc") )
244 	setMocAware(TRUE);
245     QString compile_flag = var("QMAKE_COMPILE_FLAG");
246     if(compile_flag.isEmpty())
247 	compile_flag = "-c";
248     if(doPrecompiledHeaders() && !project->isEmpty("PRECOMPILED_HEADER")) {
249 	QString prefix_flags = project->first("QMAKE_CFLAGS_PREFIX_INCLUDE");
250 	if(prefix_flags.isEmpty())
251 	    prefix_flags = "-include";
252 	compile_flag += " " + prefix_flags + " " + project->first("QMAKE_ORIG_TARGET");
253     }
254     if(!project->isEmpty("ALLMOC_HEADER")) {
255 	initOutPaths(); 	// Need to fix outdirs since we do this before init() (because we could add to SOURCES et al)
256 	QString allmoc = fileFixify(project->first("MOC_DIR") + "/allmoc.cpp", QDir::currentDirPath(), Option::output_dir);
257 	project->variables()["SOURCES"].prepend(allmoc);
258 	project->variables()["HEADERS_ORIG"] = project->variables()["HEADERS"];
259 	project->variables()["HEADERS"].clear();
260     }
261     if ( project->isEmpty("QMAKE_RUN_CC") )
262 	project->variables()["QMAKE_RUN_CC"].append("$(CC) " + compile_flag + " $(CFLAGS) $(INCPATH) -o $obj $src");
263     if ( project->isEmpty("QMAKE_RUN_CC_IMP") )
264 	project->variables()["QMAKE_RUN_CC_IMP"].append("$(CC) " + compile_flag + " $(CFLAGS) $(INCPATH) -o $@ $<");
265     if ( project->isEmpty("QMAKE_RUN_CXX") )
266 	project->variables()["QMAKE_RUN_CXX"].append("$(CXX) " + compile_flag + " $(CXXFLAGS) $(INCPATH) -o $obj $src");
267     if ( project->isEmpty("QMAKE_RUN_CXX_IMP") )
268 	project->variables()["QMAKE_RUN_CXX_IMP"].append("$(CXX) " + compile_flag + " $(CXXFLAGS) $(INCPATH) -o $@ $<");
269     project->variables()["QMAKE_FILETAGS"] += QStringList::split("HEADERS SOURCES TARGET DESTDIR", " ");
270     if( project->isActiveConfig("GNUmake") && !project->isEmpty("QMAKE_CFLAGS_DEPS"))
271 	include_deps = TRUE; //do not generate deps
272     if(project->isActiveConfig("compile_libtool"))
273 	Option::obj_ext = ".lo"; //override the .o
274 
275     MakefileGenerator::init();
276     if ( project->isActiveConfig("resource_fork") && !project->isActiveConfig("console")) {
277 	if(!project->isEmpty("QMAKE_APP_FLAG")) {
278 	    if(project->isEmpty("DESTDIR"))
279 		project->values("DESTDIR").append("");
280 	    project->variables()["DESTDIR"].first() += project->variables()["TARGET"].first() +
281 						       ".app/Contents/MacOS/";
282 	    project->variables()["QMAKE_PKGINFO"].append(project->first("DESTDIR") + "../PkgInfo");
283 	    project->variables()["ALL_DEPS"] += project->first("QMAKE_PKGINFO");
284 
285 	    QString plist = fileFixify(project->first("QMAKE_INFO_PLIST"));
286 	    if(plist.isEmpty())
287 		plist = specdir() + QDir::separator() + "Info.plist." + project->first("TEMPLATE");
288 	    if(QFile::exists(Option::fixPathToLocalOS(plist))) {
289 		if(project->isEmpty("QMAKE_INFO_PLIST"))
290 		    project->variables()["QMAKE_INFO_PLIST"].append(plist);
291 		project->variables()["QMAKE_INFO_PLIST_OUT"].append(project->first("DESTDIR") +
292 								    "../Info.plist");
293 		project->variables()["ALL_DEPS"] += project->first("QMAKE_INFO_PLIST_OUT");
294 		if(!project->isEmpty("RC_FILE"))
295 		    project->variables()["ALL_DEPS"] += project->first("DESTDIR") +
296 							"../Resources/application.icns";
297 	    }
298 	}
299     }
300 
301     if(!project->isEmpty("QMAKE_INTERNAL_INCLUDED_FILES"))
302 	project->variables()["DISTFILES"] += project->variables()["QMAKE_INTERNAL_INCLUDED_FILES"];
303     project->variables()["DISTFILES"] += Option::mkfile::project_files;
304 
305     init2();
306     project->variables()["QMAKE_INTERNAL_PRL_LIBS"] << "QMAKE_LIBDIR_FLAGS" << "QMAKE_LIBS";
307     if(!project->isEmpty("QMAKE_MAX_FILES_PER_AR")) {
308 	bool ok;
309 	int max_files = project->first("QMAKE_MAX_FILES_PER_AR").toInt(&ok);
310 	QStringList ar_sublibs, objs = project->variables()["OBJECTS"] + project->variables()["OBJMOC"];
311 	if(ok && max_files > 5 && max_files < (int)objs.count()) {
312 	    int obj_cnt = 0, lib_cnt = 0;
313 	    QString lib;
314 	    for(QStringList::Iterator objit = objs.begin(); objit != objs.end(); ++objit) {
315 		if((++obj_cnt) >= max_files) {
316 		    if(lib_cnt) {
317 			lib.sprintf("lib%s-tmp%d.a", project->first("QMAKE_ORIG_TARGET").latin1(), lib_cnt);
318 			ar_sublibs << lib;
319 			obj_cnt = 0;
320 		    }
321 		    lib_cnt++;
322 		}
323 	    }
324 	}
325 	if(!ar_sublibs.isEmpty()) {
326 	    project->variables()["QMAKE_AR_SUBLIBS"] = ar_sublibs;
327 	    project->variables()["QMAKE_INTERNAL_PRL_LIBS"] << "QMAKE_AR_SUBLIBS";
328 	}
329     }
330 
331     if(project->isActiveConfig("compile_libtool")) {
332 	const QString libtoolify[] = { "QMAKE_RUN_CC", "QMAKE_RUN_CC_IMP",
333 				       "QMAKE_RUN_CXX", "QMAKE_RUN_CXX_IMP",
334 				       "QMAKE_LINK_THREAD", "QMAKE_LINK", "QMAKE_AR_CMD", "QMAKE_LINK_SHLIB_CMD",
335 				       QString::null };
336 	for(int i = 0; !libtoolify[i].isNull(); i++) {
337 	    QStringList &l = project->variables()[libtoolify[i]];
338 	    if(!l.isEmpty()) {
339 		QString libtool_flags, comp_flags;
340 		if(libtoolify[i].startsWith("QMAKE_LINK") || libtoolify[i] == "QMAKE_AR_CMD") {
341 		    libtool_flags += " --mode=link";
342 		    if(project->isActiveConfig("staticlib")) {
343 			libtool_flags += " -static";
344 		    } else {
345 			if(!project->isEmpty("QMAKE_LIB_FLAG")) {
346 			    int maj = project->first("VER_MAJ").toInt();
347 			    int min = project->first("VER_MIN").toInt();
348 			    int pat = project->first("VER_PAT").toInt();
349 			    comp_flags += " -version-info " + QString::number(10*maj + min) +
350 					  ":" + QString::number(pat) + ":0";
351 			    if(libtoolify[i] != "QMAKE_AR_CMD") {
352 				QString rpath = Option::output_dir;
353 				if(!project->isEmpty("DESTDIR")) {
354 				    rpath = project->first("DESTDIR");
355 				    if(QDir::isRelativePath(rpath))
356 					rpath.prepend(Option::output_dir + Option::dir_sep);
357 				}
358 				comp_flags += " -rpath " + Option::fixPathToTargetOS(rpath, FALSE);
359 			    }
360 			}
361 		    }
362 		    if(project->isActiveConfig("plugin"))
363 			libtool_flags += " -module";
364 		} else {
365 		    libtool_flags += " --mode=compile";
366 		}
367 		l.first().prepend("$(LIBTOOL)" + libtool_flags + " ");
368 		if(!comp_flags.isEmpty())
369 		    l.first() += comp_flags;
370 	    }
371 	}
372     }
373 }
374 
375 QStringList
combineSetLFlags(const QStringList & list1,const QStringList & list2)376 UnixMakefileGenerator::combineSetLFlags(const QStringList &list1, const QStringList &list2)
377 {
378     if(project->isActiveConfig("no_smart_library_merge"))
379 	return list1 + list2;
380 
381     QStringList ret;
382     for(int i = 0; i < 2; i++) {
383 	const QStringList *lst = i ? &list2 : &list1;
384 	for(QStringList::ConstIterator it = lst->begin(); it != lst->end(); ++it) {
385 	    if((*it).startsWith("-")) {
386 		if((*it).startsWith("-L")) {
387 		    if(ret.findIndex((*it)) == -1)
388 			ret.append((*it));
389 		} else if((*it).startsWith("-l")) {
390 		    while(1) {
391 			QStringList::Iterator idx = ret.find((*it));
392 			if(idx == ret.end())
393 			    break;
394 			ret.remove(idx);
395 		    }
396 		    ret.append((*it));
397 		} else if(project->isActiveConfig("macx") && (*it).startsWith("-framework")) {
398 		    int as_one = TRUE;
399 		    QString framework_in;
400 		    if((*it).length() > 11) {
401 			framework_in = (*it).mid(11);
402 		    } else {
403 			if(it != lst->end()) {
404 			    ++it;
405 			    as_one = FALSE;
406 			    framework_in = (*it);
407 			}
408 		    }
409 		    if(!framework_in.isEmpty()) {
410 			for(QStringList::Iterator outit = ret.begin(); outit != ret.end(); ++outit) {
411 			    if((*outit).startsWith("-framework")) {
412 				int found = 0;
413 				if((*outit).length() > 11) {
414 				    if(framework_in == (*outit).mid(11))
415 					found = 1;
416 				} else {
417 				    if(it != lst->end()) {
418 					++outit;
419 					if(framework_in == (*outit)) {
420 					    --outit;
421 					    found = 2;
422 					}
423 				    }
424 				}
425 				for(int i = 0; i < found; i++)
426 				    outit = ret.remove(outit);
427 			    }
428 			}
429 			if(as_one) {
430 			    ret.append("-framework " + framework_in);
431 			} else {
432 			    ret.append("-framework");
433 			    ret.append(framework_in);
434 			}
435 		    }
436 		} else {
437 #if 1
438 		    while(1) {
439 			QStringList::Iterator idx = ret.find((*it));
440 			if(idx == ret.end())
441 			    break;
442 			ret.remove(idx);
443 		    }
444 #endif
445 		    ret.append((*it));
446 		}
447 	    } else /*if(QFile::exists((*it)))*/ {
448 		while(1) {
449 		    QStringList::Iterator idx = ret.find((*it));
450 		    if(idx == ret.end())
451 			break;
452 		    ret.remove(idx);
453 		}
454 		ret.append((*it));
455 	    }
456 	}
457     }
458     return ret;
459 }
460 
461 void
processPrlVariable(const QString & var,const QStringList & l)462 UnixMakefileGenerator::processPrlVariable(const QString &var, const QStringList &l)
463 {
464     if(var == "QMAKE_PRL_LIBS")
465 	project->variables()["QMAKE_CURRENT_PRL_LIBS"] = combineSetLFlags(project->variables()["QMAKE_CURRENT_PRL_LIBS"] +
466 									  project->variables()["QMAKE_LIBS"], l);
467     else
468 	MakefileGenerator::processPrlVariable(var, l);
469 }
470 
471 QString
findDependency(const QString & dep)472 UnixMakefileGenerator::findDependency(const QString &dep)
473 {
474     QStringList::Iterator it;
475     {
476 	QStringList &qut = project->variables()["QMAKE_EXTRA_UNIX_TARGETS"];
477 	for(it = qut.begin(); it != qut.end(); ++it) {
478 	    QString targ = var((*it) + ".target");
479 	    if(targ.isEmpty())
480 		targ = (*it);
481 	    if(targ.endsWith(dep))
482 		return targ;
483 	}
484     }
485     {
486 	QStringList &quc = project->variables()["QMAKE_EXTRA_UNIX_COMPILERS"];
487 	for(it = quc.begin(); it != quc.end(); ++it) {
488 	    QString tmp_out = project->variables()[(*it) + ".output"].first();
489 	    QString tmp_cmd = project->variables()[(*it) + ".commands"].join(" ");
490 	    if(tmp_out.isEmpty() || tmp_cmd.isEmpty())
491 		continue;
492 	    QStringList &tmp = project->variables()[(*it) + ".input"];
493 	    for(QStringList::Iterator it2 = tmp.begin(); it2 != tmp.end(); ++it2) {
494 		QStringList &inputs = project->variables()[(*it2)];
495 		for(QStringList::Iterator input = inputs.begin(); input != inputs.end(); ++input) {
496 		    QString out = tmp_out;
497 		    QFileInfo fi(Option::fixPathToLocalOS((*input)));
498 		    out.replace("${QMAKE_FILE_BASE}", fi.baseName());
499 		    out.replace("${QMAKE_FILE_NAME}", fi.filePath());
500 		    if(out.endsWith(dep))
501 			return out;
502 		}
503 	    }
504 	}
505     }
506     return MakefileGenerator::findDependency(dep);
507 }
508 
509 QStringList
findDependencies(const QString & file)510 &UnixMakefileGenerator::findDependencies(const QString &file)
511 {
512     QStringList &ret = MakefileGenerator::findDependencies(file);
513     // Note: The QMAKE_IMAGE_COLLECTION file have all images
514     // as dependency, so don't add precompiled header then
515     if(doPrecompiledHeaders() && !project->isEmpty("PRECOMPILED_HEADER")
516        && file != project->first("QMAKE_IMAGE_COLLECTION")) {
517 	QString header_prefix = project->first("QMAKE_ORIG_TARGET") + ".gch" + Option::dir_sep;
518 	header_prefix += project->first("QMAKE_PRECOMP_PREFIX");
519 	if(file.endsWith(".c")) {
520 	    QString precomp_h = header_prefix + "c";
521 	    if(!ret.contains(precomp_h))
522 		ret += precomp_h;
523 	} else {
524 	    for(QStringList::Iterator it = Option::cpp_ext.begin(); it != Option::cpp_ext.end(); ++it) {
525 		if(file.endsWith(*it)) {
526 		    QString precomp_h = header_prefix + "c++";
527 		    if(!ret.contains(precomp_h))
528 			ret += precomp_h;
529 		    break;
530 		}
531 	    }
532 	}
533     }
534     return ret;
535 }
536 
537 bool
findLibraries()538 UnixMakefileGenerator::findLibraries()
539 {
540     QPtrList<MakefileDependDir> libdirs;
541     libdirs.setAutoDelete(TRUE);
542     const QString lflags[] = { "QMAKE_LIBDIR_FLAGS", "QMAKE_LIBS", QString::null };
543     for(int i = 0; !lflags[i].isNull(); i++) {
544 	QStringList &l = project->variables()[lflags[i]];
545 	for(QStringList::Iterator it = l.begin(); it != l.end(); ++it) {
546 	    QString stub, dir, extn, opt = (*it).stripWhiteSpace();
547 	    if(opt.startsWith("-")) {
548 		if(opt.startsWith("-L")) {
549 		    QString r = opt.right(opt.length() - 2), l = r;
550 		    fixEnvVariables(l);
551 		    libdirs.append(new MakefileDependDir(r.replace("\"",""),
552 							 l.replace("\"","")));
553 		} else if(opt.startsWith("-l")) {
554 		    stub = opt.mid(2);
555 		} else if(project->isActiveConfig("macx") && opt.startsWith("-framework")) {
556 		    if(opt.length() > 11) {
557 			opt = opt.mid(11);
558 		    } else {
559 			++it;
560 			opt = (*it);
561 		    }
562 		    extn = "";
563 		    dir = "/System/Library/Frameworks/" + opt + ".framework/";
564 		    stub = opt;
565 		}
566 	    } else {
567 		extn = dir = "";
568 		stub = opt;
569 		int slsh = opt.findRev(Option::dir_sep);
570 		if(slsh != -1) {
571 		    dir = opt.left(slsh);
572 		    stub = opt.mid(slsh+1);
573 		}
574 		QRegExp stub_reg("^.*lib(" + stub + "[^./=]*)\\.(.*)$");
575 		if(stub_reg.exactMatch(stub)) {
576 		    stub = stub_reg.cap(1);
577 		    extn = stub_reg.cap(2);
578 		}
579 	    }
580 	    if(!stub.isEmpty()) {
581 		const QString modifs[] = { "", "-mt", QString::null };
582 		for(int modif = 0; !modifs[modif].isNull(); modif++) {
583 		    bool found = FALSE;
584 		    QStringList extens;
585 		    if(!extn.isNull())
586 			extens << extn;
587 		    else
588 			extens << project->variables()["QMAKE_EXTENSION_SHLIB"].first() << "a";
589 		    for(QStringList::Iterator extit = extens.begin(); extit != extens.end(); ++extit) {
590 			if(dir.isNull()) {
591 			    QString lib_stub;
592 			    for(MakefileDependDir *mdd = libdirs.first(); mdd; mdd = libdirs.next() ) {
593 				if(QFile::exists(mdd->local_dir + Option::dir_sep + "lib" + stub +
594 						 modifs[modif] + "." + (*extit))) {
595 				    lib_stub = stub + modifs[modif];
596 				    break;
597 				}
598 			    }
599 			    if(!lib_stub.isNull()) {
600 				(*it) = "-l" + lib_stub;
601 				found = TRUE;
602 				break;
603 			    }
604 			} else {
605 			    if(QFile::exists("lib" + stub + modifs[modif] + "." + (*extit))) {
606 				(*it) = "lib" + stub + modifs[modif] + "." + (*extit);
607 				found = TRUE;
608 				break;
609 			    }
610 			}
611 		    }
612 		    if(!found && project->isActiveConfig("compile_libtool")) {
613 			for(MakefileDependDir *mdd = libdirs.first(); mdd; mdd = libdirs.next() ) {
614 			    if(QFile::exists(mdd->local_dir + Option::dir_sep + "lib" + stub + modifs[modif] + Option::libtool_ext)) {
615 				(*it) = mdd->real_dir + Option::dir_sep + "lib" + stub + modifs[modif] + Option::libtool_ext;
616 				found = TRUE;
617 				break;
618 			    }
619 			}
620 		    }
621 		    if(found)
622 			break;
623 
624 		}
625 	    }
626 	}
627     }
628     return FALSE;
629 }
630 
linkLib(const QString & file,const QString & libName)631 QString linkLib(const QString &file, const QString &libName) {
632   QString ret;
633   QRegExp reg("^.*lib(" + libName + "[^./=]*).*$");
634   if(reg.exactMatch(file))
635     ret = "-l" + reg.cap(1);
636   return ret;
637 }
638 
639 void
processPrlFiles()640 UnixMakefileGenerator::processPrlFiles()
641 {
642     QDict<void> processed;
643     QPtrList<MakefileDependDir> libdirs;
644     libdirs.setAutoDelete(TRUE);
645     const QString lflags[] = { "QMAKE_LIBDIR_FLAGS", "QMAKE_LIBS", QString::null };
646     for(int i = 0; !lflags[i].isNull(); i++) {
647 	for(bool ret = FALSE; TRUE; ret = FALSE) {
648 	    QStringList l_out;
649 	    QStringList &l = project->variables()[lflags[i]];
650 	    for(QStringList::Iterator it = l.begin(); it != l.end(); ++it) {
651 		project->variables()["QMAKE_CURRENT_PRL_LIBS"].clear();
652 		QString opt = (*it).stripWhiteSpace();
653 		if(opt.startsWith("-")) {
654 		    if(opt.startsWith("-L")) {
655 			QString r = opt.right(opt.length() - 2), l = r;
656 			fixEnvVariables(l);
657 			libdirs.append(new MakefileDependDir(r.replace("\"",""),
658 							     l.replace("\"","")));
659 		    } else if(opt.startsWith("-l") && !processed[opt]) {
660 			QString lib = opt.right(opt.length() - 2);
661 			for(MakefileDependDir *mdd = libdirs.first(); mdd; mdd = libdirs.next() ) {
662  			    if(!project->isActiveConfig("compile_libtool")) { //give them the .libs..
663  				QString la = mdd->local_dir + Option::dir_sep + "lib" + lib + Option::libtool_ext;
664  				if(QFile::exists(la) && QFile::exists(mdd->local_dir + Option::dir_sep + ".libs")) {
665  				    l_out.append("-L" + mdd->real_dir + Option::dir_sep + ".libs");
666  				    libdirs.append(new MakefileDependDir(mdd->real_dir +  Option::dir_sep + ".libs",
667  									 mdd->local_dir + Option::dir_sep + ".libs"));
668  				}
669  			    }
670 
671 			    QString prl = mdd->local_dir + Option::dir_sep + "lib" + lib;
672 			    if(processPrlFile(prl)) {
673 				if(prl.startsWith(mdd->local_dir))
674 				    prl.replace(0, mdd->local_dir.length(), mdd->real_dir);
675 				opt = linkLib(prl, lib);
676 				processed.insert(opt, (void*)1);
677 				ret = TRUE;
678 				break;
679 			    }
680 			}
681 		    } else if(project->isActiveConfig("macx") && opt.startsWith("-framework")) {
682 			if(opt.length() > 11) {
683 			    opt = opt.mid(11);
684 			} else {
685 			    ++it;
686 			    opt = (*it);
687 			}
688 			QString prl = "/System/Library/Frameworks/" + opt +
689 				      ".framework/" + opt;
690 			if(processPrlFile(prl))
691 			    ret = TRUE;
692 			l_out.append("-framework");
693 		    }
694 		    if(!opt.isEmpty())
695 			l_out.append(opt);
696 		    l_out = combineSetLFlags(l_out, project->variables()["QMAKE_CURRENT_PRL_LIBS"]);
697 		} else {
698 		    QString lib = opt;
699 		    if(!processed[lib] && processPrlFile(lib)) {
700 		      processed.insert(lib, (void*)1);
701 		      ret = TRUE;
702 		    }
703 #if 0
704 		    if(ret)
705 		      opt = linkLib(lib, "");
706 #endif
707 		    if(!opt.isEmpty())
708 		      l_out.append(opt);
709 		    l_out = combineSetLFlags(l_out, project->variables()["QMAKE_CURRENT_PRL_LIBS"]);
710 		}
711 	    }
712 	    if(ret && l != l_out)
713 		l = l_out;
714 	    else
715 		break;
716 	}
717     }
718 }
719 
720 QString
defaultInstall(const QString & t)721 UnixMakefileGenerator::defaultInstall(const QString &t)
722 {
723     if(t != "target" || project->first("TEMPLATE") == "subdirs")
724 	return QString();
725 
726     bool resource = FALSE;
727     const QString root = "$(INSTALL_ROOT)";
728     QStringList &uninst = project->variables()[t + ".uninstall"];
729     QString ret, destdir=project->first("DESTDIR");
730     QString targetdir = Option::fixPathToTargetOS(project->first("target.path"), FALSE);
731     if(!destdir.isEmpty() && destdir.right(1) != Option::dir_sep)
732 	destdir += Option::dir_sep;
733     targetdir = fileFixify(targetdir);
734     if(targetdir.right(1) != Option::dir_sep)
735 	targetdir += Option::dir_sep;
736 
737     QStringList links;
738     QString target="$(TARGET)";
739     if(project->first("TEMPLATE") == "app") {
740 	target = "$(QMAKE_TARGET)";
741 	if(project->isActiveConfig("resource_fork") && !project->isActiveConfig("console")) {
742 	    destdir += "../../../";
743 	    target += ".app";
744 	    resource = TRUE;
745 	}
746     } else if(project->first("TEMPLATE") == "lib") {
747 	if(project->isActiveConfig("create_prl") && !project->isActiveConfig("no_install_prl") &&
748 	   !project->isEmpty("QMAKE_INTERNAL_PRL_FILE")) {
749 	    QString dst_prl = project->first("QMAKE_INTERNAL_PRL_FILE");
750 	    int slsh = dst_prl.findRev('/');
751 	    if(slsh != -1)
752 		dst_prl = dst_prl.right(dst_prl.length() - slsh - 1);
753 	    dst_prl = root + targetdir + dst_prl;
754 	    ret += "-$(INSTALL_FILE) \"" + project->first("QMAKE_INTERNAL_PRL_FILE") + "\" \"" + dst_prl + "\"";
755 	    if(!uninst.isEmpty())
756 		uninst.append("\n\t");
757 	    uninst.append("-$(DEL_FILE) \"" + dst_prl + "\"");
758 	}
759 	if(project->isActiveConfig("create_libtool") && !project->isActiveConfig("compile_libtool")) {
760 	    QString src_lt = var("QMAKE_ORIG_TARGET");
761 	    int slsh = src_lt.findRev(Option::dir_sep);
762 	    if(slsh != -1)
763 		src_lt = src_lt.right(src_lt.length() - slsh);
764 	    int dot = src_lt.find('.');
765 	    if(dot != -1)
766 		src_lt = src_lt.left(dot);
767 	    src_lt += Option::libtool_ext;
768 	    src_lt.prepend("lib");
769 	    QString dst_lt = root + targetdir + src_lt;
770 	    if(!project->isEmpty("DESTDIR")) {
771 		src_lt.prepend(var("DESTDIR"));
772 		src_lt = Option::fixPathToLocalOS(fileFixify(src_lt,
773 							     QDir::currentDirPath(), Option::output_dir));
774 	    }
775 	    if(!ret.isEmpty())
776 		ret += "\n\t";
777 	    ret += "-$(INSTALL_FILE) \"" + src_lt + "\" \"" + dst_lt + "\"";
778 	    if(!uninst.isEmpty())
779 		uninst.append("\n\t");
780 	    uninst.append("-$(DEL_FILE) \"" + dst_lt + "\"");
781 	}
782 	if(project->isActiveConfig("create_pc")) {
783 	    QString src_pc = var("QMAKE_ORIG_TARGET");
784 	    int slsh = src_pc.findRev(Option::dir_sep);
785 	    if(slsh != -1)
786 		src_pc = src_pc.right(src_pc.length() - slsh);
787 	    int dot = src_pc.find('.');
788 	    if(dot != -1)
789 		src_pc = src_pc.left(dot);
790 	    src_pc += ".pc";
791 	    QString d = root + targetdir + "pkgconfig" + Option::dir_sep;
792 	    QString dst_pc = d + src_pc;
793 	    if(!project->isEmpty("DESTDIR")) {
794 		src_pc.prepend(var("DESTDIR"));
795 		src_pc = Option::fixPathToLocalOS(fileFixify(src_pc,
796 							     QDir::currentDirPath(), Option::output_dir));
797 	    }
798 	    if(!ret.isEmpty())
799 		ret += "\n\t";
800 	    ret += mkdir_p_asstring(d) + "\n\t";
801 	    ret += "-$(INSTALL_FILE) \"" + src_pc + "\" \"" + dst_pc + "\"";
802 	    if(!uninst.isEmpty())
803 		uninst.append("\n\t");
804 	    uninst.append("-$(DEL_FILE) \"" + dst_pc + "\"");
805 	}
806 	if ( project->isEmpty("QMAKE_CYGWIN_SHLIB") ) {
807 	    if ( !project->isActiveConfig("staticlib") && !project->isActiveConfig("plugin") ) {
808 		if ( project->isEmpty("QMAKE_HPUX_SHLIB") ) {
809 		    links << "$(TARGET0)" << "$(TARGET1)" << "$(TARGET2)";
810 		} else {
811 		    links << "$(TARGET0)";
812 	        }
813 	    }
814 	}
815     }
816 
817     if(!resource && project->isActiveConfig("compile_libtool")) {
818 	QString src_targ = target;
819 	if(src_targ == "$(TARGET)")
820 	    src_targ = "$(TARGETL)";
821 	QString dst_dir = fileFixify(targetdir);
822 	if(QDir::isRelativePath(dst_dir))
823 	    dst_dir = Option::fixPathToTargetOS(Option::output_dir + Option::dir_sep + dst_dir);
824 	ret = "-$(LIBTOOL) --mode=install cp \"" + src_targ + "\" \"" + root + dst_dir + "\"";
825 	uninst.append("-$(LIBTOOL) --mode=uninstall \"" + src_targ + "\"");
826     } else {
827 	QString src_targ = target;
828 	if(!destdir.isEmpty())
829 	    src_targ = Option::fixPathToTargetOS(destdir + target, FALSE);
830 	QString dst_targ = root + targetdir + target;
831 	if(!ret.isEmpty())
832 	    ret += "\n\t";
833 	if(resource)
834 	    ret += "$(DEL_FILE) -r \"" + dst_targ + "\"" + "\n\t";
835 	if(!ret.isEmpty())
836 	    ret += "\n\t";
837 	ret += QString(resource ? "-$(INSTALL_DIR)" : "-$(INSTALL_FILE)") + " \"" +
838 	       src_targ + "\" \"" + dst_targ + "\"";
839 	if(!project->isActiveConfig("debug") && !project->isEmpty("QMAKE_STRIP") &&
840 	   (project->first("TEMPLATE") != "lib" || !project->isActiveConfig("staticlib"))) {
841 	    ret += "\n\t-" + var("QMAKE_STRIP");
842 	    if(project->first("TEMPLATE") == "lib" && !project->isEmpty("QMAKE_STRIPFLAGS_LIB"))
843 		ret += " " + var("QMAKE_STRIPFLAGS_LIB");
844 	    else if(project->first("TEMPLATE") == "app" && !project->isEmpty("QMAKE_STRIPFLAGS_APP"))
845 		ret += " " + var("QMAKE_STRIPFLAGS_APP");
846 	    if(resource)
847 		ret = " \"" + dst_targ + "/Contents/MacOS/$(QMAKE_TARGET)\"";
848 	    else
849 		ret += " \"" + dst_targ + "\"";
850 	}
851 	if(!uninst.isEmpty())
852 	    uninst.append("\n\t");
853 	if(resource)
854 	    uninst.append("-$(DEL_FILE) -r \"" + dst_targ + "\"");
855 	else
856 	    uninst.append("-$(DEL_FILE) \"" + dst_targ + "\"");
857 	if(!links.isEmpty()) {
858 	    for(QStringList::Iterator it = links.begin(); it != links.end(); it++) {
859 		if(Option::target_mode == Option::TARG_WIN_MODE ||
860 		   Option::target_mode == Option::TARG_MAC9_MODE) {
861 		} else if(Option::target_mode == Option::TARG_UNIX_MODE ||
862 			  Option::target_mode == Option::TARG_MACX_MODE) {
863 		    QString link = Option::fixPathToTargetOS(destdir + (*it), FALSE);
864 		    int lslash = link.findRev(Option::dir_sep);
865 		    if(lslash != -1)
866 			link = link.right(link.length() - (lslash + 1));
867 		    QString dst_link = root + targetdir + link;
868 		    ret += "\n\t-$(SYMLINK) \"$(TARGET)\" \"" + dst_link + "\"";
869 		    if(!uninst.isEmpty())
870 			uninst.append("\n\t");
871 		    uninst.append("-$(DEL_FILE) \"" + dst_link + "\"");
872 		}
873 	    }
874 	}
875     }
876     return ret;
877 }
878