/****************************************************************************
**
** Implementation of NmakeMakefileGenerator class.
**
** Copyright (C) 2010 Timothy Pearson and (C) 1992-2008 Trolltech ASA.
**
** This file is part of qmake.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file.  Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/

#include "msvc_nmake.h"
#include "option.h"
#include <tqregexp.h>
#include <tqdict.h>
#include <tqdir.h>
#include <stdlib.h>
#include <time.h>

NmakeMakefileGenerator::NmakeMakefileGenerator(TQMakeProject *p) : Win32MakefileGenerator(p), init_flag(FALSE)
{

}

bool
NmakeMakefileGenerator::writeMakefile(TQTextStream &t)
{
    writeHeader(t);
    if(!project->variables()["TQMAKE_FAILED_RETQUIREMENTS"].isEmpty()) {
	{ //write the extra unix targets..
	    TQStringList &qut = project->variables()["TQMAKE_EXTRA_WIN_TARGETS"];
	    for(TQStringList::ConstIterator it = qut.begin(); it != qut.end(); ++it)
		t << *it << " ";
	}
	t << "all clean:" << "\n\t"
	  << "@echo \"Some of the required modules ("
	  << var("TQMAKE_FAILED_RETQUIREMENTS") << ") are not available.\"" << "\n\t"
	  << "@echo \"Skipped.\"" << endl << endl;
	writeMakeQmake(t);
	return TRUE;
    }

    if(project->first("TEMPLATE") == "app" ||
       project->first("TEMPLATE") == "lib") {
	writeNmakeParts(t);
	return MakefileGenerator::writeMakefile(t);
    }
    else if(project->first("TEMPLATE") == "subdirs") {
	writeSubDirs(t);
	return TRUE;
    }
    return FALSE;
}

TQStringList
&NmakeMakefileGenerator::findDependencies(const TQString &file)
{
    TQStringList &aList = MakefileGenerator::findDependencies(file);
    // Note: The TQMAKE_IMAGE_COLLECTION file have all images
    // as dependency, so don't add precompiled header then
    if (file == project->first("TQMAKE_IMAGE_COLLECTION"))
	return aList;
    for(TQStringList::Iterator it = Option::cpp_ext.begin(); it != Option::cpp_ext.end(); ++it) {
	if(file.endsWith(*it)) {
            if(!precompObj.isEmpty() && !aList.contains(precompObj))
                aList += precompObj;
	    break;
	}
    }
    return aList;
}

void
NmakeMakefileGenerator::writeNmakeParts(TQTextStream &t)
{
    t << "####### Compiler, tools and options" << endl << endl;
    t << "CC		=	" << var("TQMAKE_CC") << endl;
    t << "CXX		=	" << var("TQMAKE_CXX") << endl;
    t << "LEX		= " << var("TQMAKE_LEX") << endl;
    t << "YACC		= " << var("TQMAKE_YACC") << endl;
    t << "CFLAGS	=	" << var("TQMAKE_CFLAGS") << " "
      << varGlue("PRL_EXPORT_DEFINES","-D"," -D","") << " "
      <<  varGlue("DEFINES","-D"," -D","") << endl;
    t << "CXXFLAGS	=	" << var("TQMAKE_CXXFLAGS") << " "
      << varGlue("PRL_EXPORT_DEFINES","-D"," -D","") << " "
      << varGlue("DEFINES","-D"," -D","") << endl;
    t << "LEXFLAGS	=" << var("TQMAKE_LEXFLAGS") << endl;
    t << "YACCFLAGS	=" << var("TQMAKE_YACCFLAGS") << endl;

    t << "INCPATH	=	";
    TQStringList &incs = project->variables()["INCLUDEPATH"];
    for(TQStringList::Iterator incit = incs.begin(); incit != incs.end(); ++incit) {
	TQString inc = (*incit);
	if (inc.endsWith("\\"))
	    inc.truncate(inc.length()-1);
	if (inc.startsWith("\"") && inc.endsWith("\""))
	    inc = inc.mid(1, inc.length() - 2);
	t << " -I\"" << inc << "\"";
    }
    t << " -I\"" << specdir() << "\""
      << endl;
    if(!project->variables()["TQMAKE_APP_OR_DLL"].isEmpty()) {
	t << "LINK	=	" << var("TQMAKE_LINK") << endl;
	t << "LFLAGS	=	" << var("TQMAKE_LFLAGS");
	if ( !project->variables()["TQMAKE_LIBDIR"].isEmpty() )
	  t << " " << varGlue("TQMAKE_LIBDIR","/LIBPATH:\"","\" /LIBPATH:\"","\"");
	t << endl;
	t << "LIBS	=	";
	TQStringList &libs = project->variables()["TQMAKE_LIBS"];
	for(TQStringList::Iterator libit = libs.begin(); libit != libs.end(); ++libit) {
	    TQString lib = (*libit);
	    if (lib.endsWith("\\"))
		lib.truncate(lib.length()-1);
	    t << " \"" << lib << "\"";
	}
	t << endl;
    }
    else {
	t << "LIB	=	" << var("TQMAKE_LIB") << endl;
    }
    t << "TQMOC		=	" << (project->isEmpty("TQMAKE_TQMOC") ? TQString("tqmoc") :
			      Option::fixPathToTargetOS(var("TQMAKE_TQMOC"), FALSE)) << endl;
    t << "UIC		=	" << (project->isEmpty("TQMAKE_UIC") ? TQString("uic") :
			      Option::fixPathToTargetOS(var("TQMAKE_UIC"), FALSE)) << endl;
    t << "TQMAKE		=	" << (project->isEmpty("TQMAKE_TQMAKE") ? TQString("qmake") :
			      Option::fixPathToTargetOS(var("TQMAKE_TQMAKE"), FALSE)) << endl;
    t << "IDC		=	" << (project->isEmpty("TQMAKE_IDC") ? TQString("idc") :
			      Option::fixPathToTargetOS(var("TQMAKE_IDC"), FALSE)) << endl;
    t << "IDL		=	" << (project->isEmpty("TQMAKE_IDL") ? TQString("midl") :
			      Option::fixPathToTargetOS(var("TQMAKE_IDL"), FALSE)) << endl;
    t << "ZIP		=	" << var("TQMAKE_ZIP") << endl;
    t << "COPY_FILE	=       " << var("TQMAKE_COPY") << endl;
    t << "COPY_DIR	=       " << var("TQMAKE_COPY") << endl;
    t << "DEL_FILE	=       " << var("TQMAKE_DEL_FILE") << endl;
    t << "DEL_DIR	=       " << var("TQMAKE_DEL_DIR") << endl;
    t << "MOVE		=       " << var("TQMAKE_MOVE") << endl;
    t << "CHK_DIR_EXISTS =	" << var("TQMAKE_CHK_DIR_EXISTS") << endl;
    t << "MKDIR		=	" << var("TQMAKE_MKDIR") << endl;
    t << "INSTALL_FILE= " << var("TQMAKE_INSTALL_FILE") << endl;
    t << "INSTALL_DIR = " << var("TQMAKE_INSTALL_DIR") << endl;
    t << endl;

    t << "####### Files" << endl << endl;
    t << "HEADERS =	" << varList("HEADERS") << endl;
    t << "SOURCES =	" << varList("SOURCES") << endl;
    t << "OBJECTS =	" << varList("OBJECTS") << endl;
    t << "FORMS =	" << varList("FORMS") << endl;
    t << "UICDECLS =	" << varList("UICDECLS") << endl;
    t << "UICIMPLS =	" << varList("UICIMPLS") << endl;
    t << "SRCTQMOC	=	" << varList("SRCTQMOC") << endl;
    t << "OBJTQMOC	=	" << varList("OBJTQMOC") << endl;

    TQString extraCompilerDeps;
    if(!project->isEmpty("TQMAKE_EXTRA_WIN_COMPILERS")) {
	t << "OBJCOMP = " << varList("OBJCOMP") << endl;
	extraCompilerDeps += " $(OBJCOMP) ";

	TQStringList &comps = project->variables()["TQMAKE_EXTRA_WIN_COMPILERS"];
	for(TQStringList::Iterator compit = comps.begin(); compit != comps.end(); ++compit) {
	    TQStringList &vars = project->variables()[(*compit) + ".variables"];
	    for(TQStringList::Iterator varit = vars.begin(); varit != vars.end(); ++varit) {
		TQStringList vals = project->variables()[(*varit)];
		if(!vals.isEmpty())
		    t << "TQMAKE_COMP_" << (*varit) << " = " << valList(vals) << endl;
	    }
	}
    }

    t << "DIST	=	" << varList("DISTFILES") << endl;
    t << "TARGET	=	";
    if( !project->variables()[ "DESTDIR" ].isEmpty() )
	t << varGlue("TARGET",project->first("DESTDIR"),"",project->first("TARGET_EXT"));
    else
	t << project->variables()[ "TARGET" ].first() << project->variables()[ "TARGET_EXT" ].first();
    t << endl;
    t << endl;

    t << "####### Implicit rules" << endl << endl;
    t << ".SUFFIXES: .c";
    TQStringList::Iterator cppit;
    for(cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit)
	t << " " << (*cppit);
    t << endl << endl;

    if(!project->isActiveConfig("no_batch")) {
	// Batchmode doesn't use the non implicit rules TQMAKE_RUN_CXX & TQMAKE_RUN_CC
	project->variables().remove("TQMAKE_RUN_CXX");
	project->variables().remove("TQMAKE_RUN_CC");

	TQDict<void> source_directories;
	source_directories.insert(".", (void*)1);
	TQString directories[] = { TQString("TQMOC_DIR"), TQString("UI_SOURCES_DIR"), TQString("UI_DIR"), TQString::null };
	for(int y = 0; !directories[y].isNull(); y++) {
	    TQString dirTemp = project->first(directories[y]);
	    if (dirTemp.endsWith("\\"))
		dirTemp.truncate(dirTemp.length()-1);
	    if(!dirTemp.isEmpty())
		source_directories.insert(dirTemp, (void*)1);
	}
	TQString srcs[] = { TQString("SOURCES"), TQString("UICIMPLS"), TQString("SRCTQMOC"), TQString::null };
	for(int x = 0; !srcs[x].isNull(); x++) {
	    TQStringList &l = project->variables()[srcs[x]];
	    for(TQStringList::Iterator sit = l.begin(); sit != l.end(); ++sit) {
		TQString sep = "\\";
		if((*sit).find(sep) == -1)
		    sep = "/";
		TQString dir = (*sit).section(sep, 0, -2);
		if(!dir.isEmpty() && !source_directories[dir])
		    source_directories.insert(dir, (void*)1);
	    }
	}

	for(TQDictIterator<void> it(source_directories); it.current(); ++it) {
	    if(it.currentKey().isEmpty())
		continue;
	    for(cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit)
		t << "{" << it.currentKey() << "}" << (*cppit) << "{" << var("OBJECTS_DIR") << "}" << Option::obj_ext << "::\n\t"
		  << var("TQMAKE_RUN_CXX_IMP_BATCH").replace( TQRegExp( "\\$@" ), var("OBJECTS_DIR") ) << endl << "\t$<" << endl << "<<" << endl << endl;
	    t << "{" << it.currentKey() << "}" << ".c{" << var("OBJECTS_DIR") << "}" << Option::obj_ext << "::\n\t"
	      << var("TQMAKE_RUN_CC_IMP_BATCH").replace( TQRegExp( "\\$@" ), var("OBJECTS_DIR") ) << endl << "\t$<" << endl << "<<" << endl << endl;
	}
    } else {
	for(cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit)
	    t << (*cppit) << Option::obj_ext << ":\n\t" << var("TQMAKE_RUN_CXX_IMP") << endl << endl;
	t << ".c" << Option::obj_ext << ":\n\t" << var("TQMAKE_RUN_CC_IMP") << endl << endl;
    }

    t << "####### Build rules" << endl << endl;
    t << "all: " << fileFixify(Option::output.name()) << " " << varGlue("ALL_DEPS"," "," "," ") << "$(TARGET)" << endl << endl;
    t << "$(TARGET): " << var("PRE_TARGETDEPS") << " $(UICDECLS) $(OBJECTS) $(OBJTQMOC) "
      << extraCompilerDeps << var("POST_TARGETDEPS");
    if(!project->variables()["TQMAKE_APP_OR_DLL"].isEmpty()) {
	t << "\n\t" << "$(LINK) $(LFLAGS) /OUT:$(TARGET) @<< " << "\n\t  "
	  << "$(OBJECTS) $(OBJTQMOC) $(LIBS)";
    } else {
	t << "\n\t" << "$(LIB) /OUT:$(TARGET) @<<" << "\n\t  "
	  << "$(OBJECTS) $(OBJTQMOC)";
    }
    t << extraCompilerDeps;
    t << endl << "<<" << endl;
    if ( !project->variables()["TQMAKE_POST_LINK"].isEmpty() )
	t << "\t" << var( "TQMAKE_POST_LINK" ) << endl;
    if(project->isActiveConfig("dll") && !project->variables()["DLLDESTDIR"].isEmpty()) {
	TQStringList dlldirs = project->variables()["DLLDESTDIR"];
	for ( TQStringList::Iterator dlldir = dlldirs.begin(); dlldir != dlldirs.end(); ++dlldir ) {
	    t << "\n\t" << "-$(COPY_FILE) \"$(TARGET)\" " << *dlldir;
	}
    }
    TQString targetfilename = project->variables()["TARGET"].first();
    if(project->isActiveConfig("activeqt")) {
	TQString version = project->variables()["VERSION"].first();
	if ( version.isEmpty() )
	    version = "1.0";

	if ( project->isActiveConfig("dll")) {
	    t << "\n\t" << ("-$(IDC) $(TARGET) /idl " + var("OBJECTS_DIR") + targetfilename + ".idl -version " + version);
	    t << "\n\t" << ("-$(IDL) /nologo " + var("OBJECTS_DIR") + targetfilename + ".idl /tlb " + var("OBJECTS_DIR") + targetfilename + ".tlb");
	    t << "\n\t" << ("-$(IDC) $(TARGET) /tlb " + var("OBJECTS_DIR") + targetfilename + ".tlb");
	    t << "\n\t" << ("-$(IDC) $(TARGET) /regserver" );
	} else {
	    t << "\n\t" << ("-$(TARGET) -dumpidl " + var("OBJECTS_DIR") + targetfilename + ".idl -version " + version);
	    t << "\n\t" << ("-$(IDL) /nologo " + var("OBJECTS_DIR") + targetfilename + ".idl /tlb " + var("OBJECTS_DIR") + targetfilename + ".tlb");
	    t << "\n\t" << ("-$(IDC) $(TARGET) /tlb " + var("OBJECTS_DIR") + targetfilename + ".tlb");
	    t << "\n\t" << "-$(TARGET) -regserver";
	}
    }
    t << endl << endl;

    if(!project->variables()["RC_FILE"].isEmpty()) {
	t << var("RES_FILE") << ": " << var("RC_FILE") << "\n\t"
	  << var("TQMAKE_RC") << " " << var("RC_FILE") << endl << endl;
    }

    t << "tqmocables: $(SRCTQMOC)" << endl
      << "uicables: $(UICIMPLS) $(UICDECLS)" << endl << endl;

    writeMakeQmake(t);

    TQStringList dist_files = Option::mkfile::project_files;
    if(!project->isEmpty("TQMAKE_INTERNAL_INCLUDED_FILES"))
	dist_files += project->variables()["TQMAKE_INTERNAL_INCLUDED_FILES"];
    if(!project->isEmpty("TRANSLATIONS"))
	dist_files << var("TRANSLATIONS");
    if(!project->isEmpty("FORMS")) {
	TQStringList &forms = project->variables()["FORMS"];
	for(TQStringList::Iterator formit = forms.begin(); formit != forms.end(); ++formit) {
	    TQString ui_h = fileFixify((*formit) + Option::h_ext.first());
	    if(TQFile::exists(ui_h) )
		dist_files << ui_h;
	}
    }
    t << "dist:" << "\n\t"
      << "$(ZIP) " << var("TQMAKE_ORIG_TARGET") << ".zip " << "$(SOURCES) $(HEADERS) $(DIST) $(FORMS) "
      << dist_files.join(" ") << " " << var("TRANSLATIONS") << " " << var("IMAGES") << endl << endl;

    t << "uiclean:"
      << varGlue("UICDECLS" ,"\n\t-$(DEL_FILE) ","\n\t-$(DEL_FILE) ","")
      << varGlue("UICIMPLS" ,"\n\t-$(DEL_FILE) ","\n\t-$(DEL_FILE) ","") << endl;

    t << "tqmocclean:"
      << varGlue("SRCTQMOC" ,"\n\t-$(DEL_FILE) ","\n\t-$(DEL_FILE) ","")
      << varGlue("OBJTQMOC" ,"\n\t-$(DEL_FILE) ","\n\t-$(DEL_FILE) ","") << endl;

    t << "clean: uiclean tqmocclean"
      << varGlue("OBJECTS","\n\t-$(DEL_FILE) ","\n\t-$(DEL_FILE) ","")
      << varGlue("TQMAKE_CLEAN","\n\t-$(DEL_FILE) ","\n\t-$(DEL_FILE) ","\n")
      << varGlue("CLEAN_FILES","\n\t-$(DEL_FILE) ","\n\t-$(DEL_FILE) ","\n");
    if ( project->isActiveConfig("activeqt")) {
	t << ("\n\t-$(DEL_FILE) " + var("OBJECTS_DIR") + targetfilename + ".idl");
	t << ("\n\t-$(DEL_FILE) " + var("OBJECTS_DIR") + targetfilename + ".tlb");
    }
    if(!project->isEmpty("IMAGES"))
	t << varGlue("TQMAKE_IMAGE_COLLECTION", "\n\t-$(DEL_FILE) ", "\n\t-$(DEL_FILE) ", "");
    t << endl;

    // user defined targets

    TQStringList::Iterator it;
    TQStringList &qut = project->variables()["TQMAKE_EXTRA_WIN_TARGETS"];
    for(it = qut.begin(); it != qut.end(); ++it) {
	TQString targ = var((*it) + ".target"),
		 cmd = var((*it) + ".commands"), deps;
	if(targ.isEmpty())
	    targ = (*it);
	TQStringList &deplist = project->variables()[(*it) + ".depends"];
	for(TQStringList::Iterator dep_it = deplist.begin(); dep_it != deplist.end(); ++dep_it) {
	    TQString dep = var((*dep_it) + ".target");
	    if(dep.isEmpty())
		dep = (*dep_it);
	    deps += " " + dep;
	}
	if(!project->variables()["TQMAKE_NOFORCE"].isEmpty() &&
	   project->variables()[(*it) + ".CONFIG"].findIndex("phony") != -1)
	    deps += TQString(" ") + "FORCE";
	t << "\n\n" << targ << ":" << deps << "\n\t"
	  << cmd;
    }
    t << endl << endl;

    TQStringList &quc = project->variables()["TQMAKE_EXTRA_WIN_COMPILERS"];
    for(it = quc.begin(); it != quc.end(); ++it) {
	TQString tmp_out = project->variables()[(*it) + ".output"].first();
	TQString tmp_cmd = project->variables()[(*it) + ".commands"].join(" ");
	TQString tmp_dep = project->variables()[(*it) + ".depends"].join(" ");
	TQStringList &vars = project->variables()[(*it) + ".variables"];
	if(tmp_out.isEmpty() || tmp_cmd.isEmpty())
	    continue;
	TQStringList &tmp = project->variables()[(*it) + ".input"];
	for(TQStringList::Iterator it2 = tmp.begin(); it2 != tmp.end(); ++it2) {
	    TQStringList &inputs = project->variables()[(*it2)];
	    for(TQStringList::Iterator input = inputs.begin(); input != inputs.end(); ++input) {
		TQFileInfo fi(Option::fixPathToLocalOS((*input)));
		TQString in = Option::fixPathToTargetOS((*input), FALSE),
		       out = tmp_out, cmd = tmp_cmd, deps;
		out.replace("${TQMAKE_FILE_BASE}", fi.baseName());
		out.replace("${TQMAKE_FILE_NAME}", fi.filePath());
		cmd.replace("${TQMAKE_FILE_BASE}", fi.baseName());
		cmd.replace("${TQMAKE_FILE_OUT}", out);
		cmd.replace("${TQMAKE_FILE_NAME}", fi.filePath());
		for(TQStringList::Iterator it3 = vars.begin(); it3 != vars.end(); ++it3)
		    cmd.replace("$(" + (*it3) + ")", "$(TQMAKE_COMP_" + (*it3)+")");
		if(!tmp_dep.isEmpty()) {
		    char buff[256];
		    TQString dep_cmd = tmp_dep;
		    dep_cmd.replace("${TQMAKE_FILE_NAME}", fi.filePath());
		    if(FILE *proc = TQT_POPEN(dep_cmd.latin1(), "r")) {
			while(!feof(proc)) {
			    int read_in = int(fread(buff, 1, 255, proc));
			    if(!read_in)
				break;
			    int l = 0;
			    for(int i = 0; i < read_in; i++) {
				if(buff[i] == '\n' || buff[i] == ' ') {
				    deps += " " + TQCString(buff+l, (i - l) + 1);
				    l = i;
				}
			    }
			}
			fclose(proc);
		    }
		}
		t << out << ": " << in << deps << "\n\t"
		  << cmd << endl << endl;
	    }
	}
    }
    t << endl;

    if(project->variables()["TQMAKE_NOFORCE"].isEmpty())
	t << "FORCE:" << endl << endl;

    t << "distclean: clean"
      << "\n\t-$(DEL_FILE) $(TARGET)"
      << endl << endl;

    // precompiled header
    if(usePCH) {
	TQString precompRule = TQString("-c -Yc -Fp%1 -Fo%2").arg(precompPch).arg(precompObj);
	t << precompObj << ": " << precompH << " " << findDependencies(precompH).join(" \\\n\t\t")
	  << "\n\t" << ("$(CXX) " + precompRule + " $(CXXFLAGS) $(INCPATH) -TP ") << precompH << endl << endl;
    }
}

TQString
NmakeMakefileGenerator::var(const TQString &value)
{
    if (usePCH) {
    	if ((value == "TQMAKE_RUN_CXX_IMP_BATCH"
	    || value == "TQMAKE_RUN_CXX_IMP"
	    || value == "TQMAKE_RUN_CXX")) {
	    TQFileInfo precompHInfo(precompH);
	    TQString precompRule = TQString("-c -FI%1 -Yu%2 -Fp%3")
		.arg(precompHInfo.fileName())
		.arg(precompHInfo.fileName())
		.arg(precompPch);
	    TQString p = MakefileGenerator::var(value);
	    p.replace("-c", precompRule);
	    // Cannot use -Gm with -FI & -Yu, as this gives an
	    // internal compiler error, on the newer compilers
	    p.remove("-Gm");
	    return p;
	} else if (value == "TQMAKE_CXXFLAGS") {
	    // Remove internal compiler error option
	    return MakefileGenerator::var(value).remove("-Gm");
	}
    }

    // Normal val
    return MakefileGenerator::var(value);
}

void
NmakeMakefileGenerator::init()
{
    if(init_flag)
	return;
    init_flag = TRUE;

    /* this should probably not be here, but I'm using it to wrap the .t files */
    if(project->first("TEMPLATE") == "app")
	project->variables()["TQMAKE_APP_FLAG"].append("1");
    else if(project->first("TEMPLATE") == "lib")
	project->variables()["TQMAKE_LIB_FLAG"].append("1");
    else if(project->first("TEMPLATE") == "subdirs") {
	MakefileGenerator::init();
	if(project->variables()["MAKEFILE"].isEmpty())
	    project->variables()["MAKEFILE"].append("Makefile");
	if(project->variables()["TQMAKE"].isEmpty())
	    project->variables()["TQMAKE"].append("qmake");
	return;
    }

    if(project->isEmpty("TQMAKE_INSTALL_FILE"))
	project->variables()["TQMAKE_INSTALL_FILE"].append("$(COPY_FILE)");
    if(project->isEmpty("TQMAKE_INSTALL_DIR"))
	project->variables()["TQMAKE_INSTALL_DIR"].append("$(COPY_DIR)");

    bool is_qt = (project->first("TARGET") == "qt"TQTDLL_POSTFIX || project->first("TARGET") == "qt-mt"TQTDLL_POSTFIX);
    project->variables()["TQMAKE_ORIG_TARGET"] = project->variables()["TARGET"];

    TQString targetfilename = project->variables()["TARGET"].first();
    TQStringList &configs = project->variables()["CONFIG"];
    if (project->isActiveConfig("qt") && project->isActiveConfig("shared"))
	project->variables()["DEFINES"].append("TQT_DLL");
    if (project->isActiveConfig("qt_dll"))
	if(configs.findIndex("qt") == -1) configs.append("qt");
    if ( project->isActiveConfig("qtopia") ) {
	if(configs.findIndex("qtopialib") == -1)
	    configs.append("qtopialib");
	if(configs.findIndex("qtopiainc") == -1)
	    configs.append("qtopiainc");
    }
    if ( project->isActiveConfig("qt") ) {
	if ( project->isActiveConfig( "plugin" ) ) {
	    project->variables()["CONFIG"].append("dll");
	    if(project->isActiveConfig("qt"))
		project->variables()["DEFINES"].append("TQT_PLUGIN");
	}
	if ( (project->variables()["DEFINES"].findIndex("TQT_NODLL") == -1) &&
         ((project->variables()["DEFINES"].findIndex("TQT_MAKEDLL") != -1 ||
           project->variables()["DEFINES"].findIndex("TQT_DLL") != -1) ||
          (getenv("TQT_DLL") && !getenv("TQT_NODLL"))) ) {
	    project->variables()["TQMAKE_TQT_DLL"].append("1");
	    if ( is_qt && !project->variables()["TQMAKE_LIB_FLAG"].isEmpty() )
		project->variables()["CONFIG"].append("dll");
	}
	if ( project->isActiveConfig("thread") )
	    project->variables()[is_qt ? "PRL_EXPORT_DEFINES" : "DEFINES"].append("TQT_THREAD_SUPPORT");
	if ( project->isActiveConfig("accessibility" ) )
	    project->variables()[is_qt ? "PRL_EXPORT_DEFINES" : "DEFINES"].append("TQT_ACCESSIBILITY_SUPPORT");
	if ( project->isActiveConfig("tablet") )
	    project->variables()[is_qt ? "PRL_EXPORT_DEFINES" : "DEFINES"].append("TQT_TABLET_SUPPORT");
    }
    if ( project->isActiveConfig("dll") || !project->variables()["TQMAKE_APP_FLAG"].isEmpty() ) {
	project->variables()["CONFIG"].remove("staticlib");
	project->variables()["TQMAKE_APP_OR_DLL"].append("1");
    } else {
	project->variables()["CONFIG"].append("staticlib");
    }
    if ( project->isActiveConfig("warn_off") ) {
	project->variables()["TQMAKE_CFLAGS"] += project->variables()["TQMAKE_CFLAGS_WARN_OFF"];
	project->variables()["TQMAKE_CXXFLAGS"] += project->variables()["TQMAKE_CXXFLAGS_WARN_OFF"];
    } else if ( project->isActiveConfig("warn_on") ) {
	project->variables()["TQMAKE_CFLAGS"] += project->variables()["TQMAKE_CFLAGS_WARN_ON"];
	project->variables()["TQMAKE_CXXFLAGS"] += project->variables()["TQMAKE_CXXFLAGS_WARN_ON"];
    }
    if ( project->isActiveConfig("debug") ) {
        if ( project->isActiveConfig("thread") ) {
	    // use the DLL RT even here
	    if ( project->variables()["DEFINES"].contains("TQT_DLL") ) {
		project->variables()["TQMAKE_CFLAGS"] += project->variables()["TQMAKE_CFLAGS_MT_DLLDBG"];
		project->variables()["TQMAKE_CXXFLAGS"] += project->variables()["TQMAKE_CXXFLAGS_MT_DLLDBG"];
	    } else {
		project->variables()["TQMAKE_CFLAGS"] += project->variables()["TQMAKE_CFLAGS_MT_DBG"];
		project->variables()["TQMAKE_CXXFLAGS"] += project->variables()["TQMAKE_CXXFLAGS_MT_DBG"];
	    }
	}
	project->variables()["TQMAKE_CFLAGS"] += project->variables()["TQMAKE_CFLAGS_DEBUG"];
	project->variables()["TQMAKE_CXXFLAGS"] += project->variables()["TQMAKE_CXXFLAGS_DEBUG"];
	project->variables()["TQMAKE_LFLAGS"] += project->variables()["TQMAKE_LFLAGS_DEBUG"];
    } else {
	if ( project->isActiveConfig("thread") ) {
	    if ( project->variables()["DEFINES"].contains("TQT_DLL") ) {
		project->variables()["TQMAKE_CFLAGS"] += project->variables()["TQMAKE_CFLAGS_MT_DLL"];
		project->variables()["TQMAKE_CXXFLAGS"] += project->variables()["TQMAKE_CXXFLAGS_MT_DLL"];
	    } else {
		project->variables()["TQMAKE_CFLAGS"] += project->variables()["TQMAKE_CFLAGS_MT"];
		project->variables()["TQMAKE_CXXFLAGS"] += project->variables()["TQMAKE_CXXFLAGS_MT"];
	    }
	}
	project->variables()["TQMAKE_CFLAGS"] += project->variables()["TQMAKE_CFLAGS_RELEASE"];
	project->variables()["TQMAKE_CXXFLAGS"] += project->variables()["TQMAKE_CXXFLAGS_RELEASE"];
	project->variables()["TQMAKE_LFLAGS"] += project->variables()["TQMAKE_LFLAGS_RELEASE"];
    }
    if ( project->isActiveConfig("thread") && !project->variables()["DEFINES"].contains("TQT_DLL")
	&& !is_qt && project->first("TARGET") != "qtmain") {
	project->variables()["TQMAKE_LFLAGS"].append("/NODEFAULTLIB:\"libc\"");
    }

    if ( !project->variables()["TQMAKE_INCDIR"].isEmpty())
	project->variables()["INCLUDEPATH"] += project->variables()["TQMAKE_INCDIR"];
    if ( project->isActiveConfig("qt") || project->isActiveConfig("opengl") )
	project->variables()["CONFIG"].append("windows");
    if ( project->isActiveConfig("qtopiainc") )
	project->variables()["INCLUDEPATH"] += project->variables()["TQMAKE_INCDIR_TQTOPIA"];
    if ( project->isActiveConfig("qtopialib") ) {
	if(!project->isEmpty("TQMAKE_LIBDIR_TQTOPIA"))
	    project->variables()["TQMAKE_LIBDIR"] += project->variables()["TQMAKE_LIBDIR_TQTOPIA"];
	project->variables()["TQMAKE_LIBS"] += project->variables()["TQMAKE_LIBS_TQTOPIA"];
    }
    if ( project->isActiveConfig("qt") ) {
	project->variables()["CONFIG"].append("tqmoc");
	project->variables()["INCLUDEPATH"] +=	project->variables()["TQMAKE_INCDIR_QT"];
	project->variables()["TQMAKE_LIBDIR"] += project->variables()["TQMAKE_LIBDIR_QT"];
	if ( !project->isActiveConfig("debug") )
	    project->variables()[is_qt ? "PRL_EXPORT_DEFINES" : "DEFINES"].append("TQT_NO_DEBUG");
	if ( is_qt && !project->variables()["TQMAKE_LIB_FLAG"].isEmpty() ) {
	    if ( !project->variables()["TQMAKE_TQT_DLL"].isEmpty()) {
		project->variables()["DEFINES"].append("TQT_MAKEDLL");
		project->variables()["TQMAKE_LFLAGS"] += project->variables()["TQMAKE_LFLAGS_TQT_DLL"];
	    }
	} else {
	    if(project->isActiveConfig("thread"))
		project->variables()["TQMAKE_LIBS"] += project->variables()["TQMAKE_LIBS_TQT_THREAD"];
	    else
		project->variables()["TQMAKE_LIBS"] += project->variables()["TQMAKE_LIBS_QT"];
	    if ( !project->variables()["TQMAKE_TQT_DLL"].isEmpty() ) {
		int hver = findHighestVersion(project->first("TQMAKE_LIBDIR_QT"), "qt");
		if ( hver == -1 )
		    hver = findHighestVersion(project->first("TQMAKE_LIBDIR_QT"), "qt-mt");
		if(hver != -1) {
		    TQString ver;
		    ver.sprintf("qt%s" TQTDLL_POSTFIX "%d.lib", (project->isActiveConfig("thread") ? "-mt" : ""), hver);
		    TQStringList &libs = project->variables()["TQMAKE_LIBS"];
		    for(TQStringList::Iterator libit = libs.begin(); libit != libs.end(); ++libit)
			(*libit).replace(TQRegExp("qt(-mt)?\\.lib"), ver);
		}
	    }
	    if ( project->isActiveConfig( "activeqt" ) ) {
		project->variables().remove("TQMAKE_LIBS_TQT_ENTRY");
		project->variables()["TQMAKE_LIBS_TQT_ENTRY"] = "qaxserver.lib";
		if ( project->isActiveConfig( "dll" ) )
		    project->variables()["TQMAKE_LIBS"] += project->variables()["TQMAKE_LIBS_TQT_ENTRY"];
	    }
	    if ( !project->isActiveConfig("dll") && !project->isActiveConfig("plugin") ) {
		project->variables()["TQMAKE_LIBS"] +=project->variables()["TQMAKE_LIBS_TQT_ENTRY"];
	    }
	}
    }
    if ( project->isActiveConfig("opengl") ) {
	project->variables()["TQMAKE_LIBS"] += project->variables()["TQMAKE_LIBS_OPENGL"];
	project->variables()["TQMAKE_LFLAGS"] += project->variables()["TQMAKE_LFLAGS_OPENGL"];
    }
    if ( project->isActiveConfig("dll") ) {
	project->variables()["TQMAKE_CFLAGS_CONSOLE_ANY"] = project->variables()["TQMAKE_CFLAGS_CONSOLE_DLL"];
	project->variables()["TQMAKE_CXXFLAGS_CONSOLE_ANY"] = project->variables()["TQMAKE_CXXFLAGS_CONSOLE_DLL"];
	project->variables()["TQMAKE_LFLAGS_CONSOLE_ANY"] = project->variables()["TQMAKE_LFLAGS_CONSOLE_DLL"];
	project->variables()["TQMAKE_LFLAGS_WINDOWS_ANY"] = project->variables()["TQMAKE_LFLAGS_WINDOWS_DLL"];
	if ( !project->variables()["TQMAKE_LIB_FLAG"].isEmpty()) {
	    project->variables()["TARGET_EXT"].append(
		TQStringList::split('.',project->first("VERSION")).join("") + ".dll");
	} else {
	    project->variables()["TARGET_EXT"].append(".dll");
	}
    } else {
	project->variables()["TQMAKE_CFLAGS_CONSOLE_ANY"] = project->variables()["TQMAKE_CFLAGS_CONSOLE"];
	project->variables()["TQMAKE_CXXFLAGS_CONSOLE_ANY"] = project->variables()["TQMAKE_CXXFLAGS_CONSOLE"];
	project->variables()["TQMAKE_LFLAGS_CONSOLE_ANY"] = project->variables()["TQMAKE_LFLAGS_CONSOLE"];
	project->variables()["TQMAKE_LFLAGS_WINDOWS_ANY"] = project->variables()["TQMAKE_LFLAGS_WINDOWS"];
	if ( !project->variables()["TQMAKE_APP_FLAG"].isEmpty()) {
	    project->variables()["TARGET_EXT"].append(".exe");
	} else {
	    project->variables()["TARGET_EXT"].append(".lib");
	}
    }
    if ( project->isActiveConfig("windows") ) {
	if ( project->isActiveConfig("console") ) {
	    project->variables()["TQMAKE_CFLAGS"] += project->variables()["TQMAKE_CFLAGS_CONSOLE_ANY"];
	    project->variables()["TQMAKE_CXXFLAGS"] += project->variables()["TQMAKE_CXXFLAGS_CONSOLE_ANY"];
	    project->variables()["TQMAKE_LFLAGS"] += project->variables()["TQMAKE_LFLAGS_CONSOLE_ANY"];
	    project->variables()["TQMAKE_LIBS"] += project->variables()["TQMAKE_LIBS_CONSOLE"];
	} else {
	    project->variables()["TQMAKE_LFLAGS"] += project->variables()["TQMAKE_LFLAGS_WINDOWS_ANY"];
	}
	project->variables()["TQMAKE_LIBS"] += project->variables()["TQMAKE_LIBS_WINDOWS"];
    } else {
	project->variables()["TQMAKE_CFLAGS"] += project->variables()["TQMAKE_CFLAGS_CONSOLE_ANY"];
	project->variables()["TQMAKE_CXXFLAGS"] += project->variables()["TQMAKE_CXXFLAGS_CONSOLE_ANY"];
	project->variables()["TQMAKE_LFLAGS"] += project->variables()["TQMAKE_LFLAGS_CONSOLE_ANY"];
	project->variables()["TQMAKE_LIBS"] += project->variables()["TQMAKE_LIBS_CONSOLE"];
    }
    if ( project->isActiveConfig("stl") ) {
	project->variables()["TQMAKE_CFLAGS"] += project->variables()["TQMAKE_CFLAGS_STL_ON"];
	project->variables()["TQMAKE_CXXFLAGS"] += project->variables()["TQMAKE_CXXFLAGS_STL_ON"];
    } else {
	project->variables()["TQMAKE_CFLAGS"] += project->variables()["TQMAKE_CFLAGS_STL_OFF"];
	project->variables()["TQMAKE_CXXFLAGS"] += project->variables()["TQMAKE_CXXFLAGS_STL_OFF"];
    }
    if ( project->isActiveConfig("exceptions") ) {
	project->variables()["TQMAKE_CFLAGS"] += project->variables()["TQMAKE_CFLAGS_EXCEPTIONS_ON"];
	project->variables()["TQMAKE_CXXFLAGS"] += project->variables()["TQMAKE_CXXFLAGS_EXCEPTIONS_ON"];
    } else {
	project->variables()["TQMAKE_CFLAGS"] += project->variables()["TQMAKE_CFLAGS_EXCEPTIONS_OFF"];
	project->variables()["TQMAKE_CXXFLAGS"] += project->variables()["TQMAKE_CXXFLAGS_EXCEPTIONS_OFF"];
    }
    if ( project->isActiveConfig("rtti") ) {
	project->variables()["TQMAKE_CFLAGS"] += project->variables()["TQMAKE_CFLAGS_RTTI_ON"];
	project->variables()["TQMAKE_CXXFLAGS"] += project->variables()["TQMAKE_CXXFLAGS_RTTI_ON"];
    } else {
	project->variables()["TQMAKE_CFLAGS"] += project->variables()["TQMAKE_CFLAGS_RTTI_OFF"];
	project->variables()["TQMAKE_CXXFLAGS"] += project->variables()["TQMAKE_CXXFLAGS_RTTI_OFF"];
    }


    if ( project->isActiveConfig("tqmoc") )
	setMocAware(TRUE);
    project->variables()["TQMAKE_LIBS"] += project->variables()["LIBS"];

    TQStringList &libList = project->variables()["TQMAKE_LIBS"];
    for( TQStringList::Iterator stIt = libList.begin(); stIt != libList.end(); ) {
	TQString s = *stIt;
	if( s.startsWith( "-l" ) ) {
	    stIt = libList.remove( stIt );
	    stIt = libList.insert( stIt, s.mid( 2 ) + ".lib" );
        } else if( s.startsWith( "-L" ) ) {
	    stIt = libList.remove( stIt );
	    project->variables()["TQMAKE_LIBDIR"].append(TQDir::convertSeparators(s.mid( 2 )));
	} else {
	    stIt++;
	}
    }

    project->variables()["TQMAKE_FILETAGS"] += TQStringList::split(' ',
	"HEADERS SOURCES DEF_FILE RC_FILE TARGET TQMAKE_LIBS DESTDIR DLLDESTDIR INCLUDEPATH");
    TQStringList &l = project->variables()["TQMAKE_FILETAGS"];
    TQStringList::Iterator it;
    for(it = l.begin(); it != l.end(); ++it) {
	TQStringList &gdmf = project->variables()[(*it)];
	for(TQStringList::Iterator inner = gdmf.begin(); inner != gdmf.end(); ++inner)
	    (*inner) = Option::fixPathToTargetOS((*inner), FALSE);
    }

    if ( !project->variables()["DEF_FILE"].isEmpty() )
	project->variables()["TQMAKE_LFLAGS"].append(TQString("/DEF:") + project->first("DEF_FILE"));
    if(!project->isActiveConfig("incremental"))
	project->variables()["TQMAKE_LFLAGS"].append(TQString("/incremental:no"));

    if ( !project->variables()["VERSION"].isEmpty() ) {
	TQString version = project->variables()["VERSION"][0];
	int firstDot = version.find( "." );
	TQString major = version.left( firstDot );
	TQString minor = version.right( version.length() - firstDot - 1 );
	minor.replace( ".", "" );
	project->variables()["TQMAKE_LFLAGS"].append( "/VERSION:" + major + "." + minor );
    }
    if ( !project->variables()["RC_FILE"].isEmpty()) {
	if ( !project->variables()["RES_FILE"].isEmpty()) {
	    fprintf(stderr, "Both .rc and .res file specified.\n");
	    fprintf(stderr, "Please specify one of them, not both.");
	    exit(666);
	}
	project->variables()["RES_FILE"] = project->variables()["RC_FILE"];
	project->variables()["RES_FILE"].first().replace(".rc",".res");
	project->variables()["POST_TARGETDEPS"] += project->variables()["RES_FILE"];
	project->variables()["CLEAN_FILES"] += project->variables()["RES_FILE"];
    }
    if ( !project->variables()["RES_FILE"].isEmpty())
	project->variables()["TQMAKE_LIBS"] += project->variables()["RES_FILE"];

    // Base class init!
    MakefileGenerator::init();

    // Setup PCH variables
    precompH = project->first("PRECOMPILED_HEADER");
    usePCH = !precompH.isEmpty() && project->isActiveConfig("precompile_header");
    if (usePCH) {
	// Created files
	precompObj = var("OBJECTS_DIR") + project->first("TARGET") + "_pch" + Option::obj_ext;
	precompPch = var("OBJECTS_DIR") + project->first("TARGET") + "_pch.pch";
	// Add linking of precompObj (required for whole precompiled classes)
	project->variables()["OBJECTS"]		  += precompObj;
	// Add pch file to cleanup
	project->variables()["TQMAKE_CLEAN"]	  += precompPch;
	// Return to variable pool
	project->variables()["PRECOMPILED_OBJECT"] = precompObj;
	project->variables()["PRECOMPILED_PCH"]    = precompPch;
    }

    if ( !project->variables()["VERSION"].isEmpty()) {
	TQStringList l = TQStringList::split('.', project->first("VERSION"));
	project->variables()["VER_MAJ"].append(l[0]);
	project->variables()["VER_MIN"].append(l[1]);
    }

    TQString version = TQStringList::split('.', project->first("VERSION")).join("");
    if(project->isActiveConfig("dll")) {
	project->variables()["TQMAKE_CLEAN"].append(project->first("DESTDIR") + project->first("TARGET") + version + ".exp");
    }
    if(project->isActiveConfig("debug")) {
	project->variables()["TQMAKE_CLEAN"].append(project->first("DESTDIR") + project->first("TARGET") + version + ".pdb");
	project->variables()["TQMAKE_CLEAN"].append(project->first("DESTDIR") + project->first("TARGET") + version + ".ilk");
	project->variables()["TQMAKE_CLEAN"].append("vc*.pdb");
	project->variables()["TQMAKE_CLEAN"].append("vc*.idb");
    }

    TQStringList &quc = project->variables()["TQMAKE_EXTRA_WIN_COMPILERS"];
    for(it = quc.begin(); it != quc.end(); ++it) {
	TQString tmp_out = project->variables()[(*it) + ".output"].first();
	if(tmp_out.isEmpty())
	    continue;
	TQStringList &tmp = project->variables()[(*it) + ".input"];
	for(TQStringList::Iterator it2 = tmp.begin(); it2 != tmp.end(); ++it2) {
	    TQStringList &inputs = project->variables()[(*it2)];
	    for(TQStringList::Iterator input = inputs.begin(); input != inputs.end(); ++input) {
		TQFileInfo fi(Option::fixPathToLocalOS((*input)));
		TQString in = Option::fixPathToTargetOS((*input), FALSE),
		       out = tmp_out;
		out.replace("${TQMAKE_FILE_BASE}", fi.baseName());
		out.replace("${TQMAKE_FILE_NAME}", fi.filePath());
		if(project->variables()[(*it) + ".CONFIG"].findIndex("no_link") == -1)
		    project->variables()["OBJCOMP"] += out;
	    }
	}
    }
}
