qmake/main.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 "project.h"
       
    43 #include "property.h"
       
    44 #include "option.h"
       
    45 #include "cachekeys.h"
       
    46 #include "metamakefile.h"
       
    47 #include <qnamespace.h>
       
    48 #include <qdebug.h>
       
    49 #include <qregexp.h>
       
    50 #include <qdir.h>
       
    51 #include <stdio.h>
       
    52 #include <stdlib.h>
       
    53 #include <ctype.h>
       
    54 #include <fcntl.h>
       
    55 #include <sys/types.h>
       
    56 #include <sys/stat.h>
       
    57 
       
    58 QT_BEGIN_NAMESPACE
       
    59 
       
    60 // for Borland, main is defined to qMain which breaks qmake
       
    61 #undef main
       
    62 #ifdef Q_OS_MAC
       
    63 #endif
       
    64 
       
    65 /* This is to work around lame implementation on Darwin. It has been noted that the getpwd(3) function
       
    66    is much too slow, and called much too often inside of Qt (every fileFixify). With this we use a locally
       
    67    cached copy because I can control all the times it is set (because Qt never sets the pwd under me).
       
    68 */
       
    69 static QString pwd;
       
    70 QString qmake_getpwd()
       
    71 {
       
    72     if(pwd.isNull())
       
    73         pwd = QDir::currentPath();
       
    74     return pwd;
       
    75 }
       
    76 bool qmake_setpwd(const QString &p)
       
    77 {
       
    78     if(QDir::setCurrent(p)) {
       
    79         pwd = QDir::currentPath();
       
    80         return true;
       
    81     }
       
    82     return false;
       
    83 }
       
    84 
       
    85 int runQMake(int argc, char **argv)
       
    86 {
       
    87     // stderr is unbuffered by default, but stdout buffering depends on whether
       
    88     // there is a terminal attached. Buffering can make output from stderr and stdout
       
    89     // appear out of sync, so force stdout to be unbuffered as well.
       
    90     // This is particularly important for things like QtCreator and scripted builds.
       
    91     setvbuf(stdout, (char *)NULL, _IONBF, 0);
       
    92 
       
    93     // parse command line
       
    94     int ret = Option::init(argc, argv);
       
    95     if(ret != Option::QMAKE_CMDLINE_SUCCESS) {
       
    96         if ((ret & Option::QMAKE_CMDLINE_ERROR) != 0)
       
    97             return 1;
       
    98         return 0;
       
    99     }
       
   100 
       
   101     QString oldpwd = qmake_getpwd();
       
   102 #ifdef Q_WS_WIN
       
   103     if(!(oldpwd.length() == 3 && oldpwd[0].isLetter() && oldpwd.endsWith(":/")))
       
   104 #endif
       
   105     {
       
   106         if(oldpwd.right(1) != QString(QChar(QDir::separator())))
       
   107             oldpwd += QDir::separator();
       
   108     }
       
   109     Option::output_dir = oldpwd; //for now this is the output dir
       
   110 
       
   111     if(Option::output.fileName() != "-") {
       
   112         QFileInfo fi(Option::output);
       
   113         QString dir;
       
   114         if(fi.isDir()) {
       
   115             dir = fi.filePath();
       
   116         } else {
       
   117             QString tmp_dir = fi.path();
       
   118             if(!tmp_dir.isEmpty() && QFile::exists(tmp_dir))
       
   119                 dir = tmp_dir;
       
   120         }
       
   121         if(!dir.isNull() && dir != ".")
       
   122             Option::output_dir = dir;
       
   123         if(QDir::isRelativePath(Option::output_dir))
       
   124             Option::output_dir.prepend(oldpwd);
       
   125         Option::output_dir = QDir::cleanPath(Option::output_dir);
       
   126     }
       
   127 
       
   128     QMakeProperty prop;
       
   129     if(Option::qmake_mode == Option::QMAKE_QUERY_PROPERTY || Option::qmake_mode == Option::QMAKE_SET_PROPERTY)
       
   130         return prop.exec() ? 0 : 101;
       
   131 
       
   132     QMakeProject project(&prop);
       
   133     int exit_val = 0;
       
   134     QStringList files;
       
   135     if(Option::qmake_mode == Option::QMAKE_GENERATE_PROJECT)
       
   136         files << "(*hack*)"; //we don't even use files, but we do the for() body once
       
   137     else
       
   138         files = Option::mkfile::project_files;
       
   139     for(QStringList::Iterator pfile = files.begin(); pfile != files.end(); pfile++) {
       
   140         if(Option::qmake_mode == Option::QMAKE_GENERATE_MAKEFILE ||
       
   141            Option::qmake_mode == Option::QMAKE_GENERATE_PRL) {
       
   142             QString fn = Option::fixPathToLocalOS((*pfile));
       
   143             if(!QFile::exists(fn)) {
       
   144                 fprintf(stderr, "Cannot find file: %s.\n", fn.toLatin1().constData());
       
   145                 exit_val = 2;
       
   146                 continue;
       
   147             }
       
   148 
       
   149             //setup pwd properly
       
   150             debug_msg(1, "Resetting dir to: %s", oldpwd.toLatin1().constData());
       
   151             qmake_setpwd(oldpwd); //reset the old pwd
       
   152             int di = fn.lastIndexOf(Option::dir_sep);
       
   153             if(di != -1) {
       
   154                 debug_msg(1, "Changing dir to: %s", fn.left(di).toLatin1().constData());
       
   155                 if(!qmake_setpwd(fn.left(di)))
       
   156                     fprintf(stderr, "Cannot find directory: %s\n", fn.left(di).toLatin1().constData());
       
   157                 fn = fn.right(fn.length() - di - 1);
       
   158             }
       
   159 
       
   160             // read project..
       
   161             if(!project.read(fn)) {
       
   162                 fprintf(stderr, "Error processing project file: %s\n",
       
   163                         fn == "-" ? "(stdin)" : (*pfile).toLatin1().constData());
       
   164                 exit_val = 3;
       
   165                 continue;
       
   166             }
       
   167             if(Option::mkfile::do_preprocess) //no need to create makefile
       
   168                 continue;
       
   169         }
       
   170 
       
   171         bool success = true;
       
   172         MetaMakefileGenerator *mkfile = MetaMakefileGenerator::createMetaGenerator(&project, QString(), false, &success);
       
   173         if (!success)
       
   174             exit_val = 3;
       
   175 
       
   176         if(mkfile && !mkfile->write(oldpwd)) {
       
   177             if(Option::qmake_mode == Option::QMAKE_GENERATE_PROJECT)
       
   178                 fprintf(stderr, "Unable to generate project file.\n");
       
   179             else
       
   180                 fprintf(stderr, "Unable to generate makefile for: %s\n", (*pfile).toLatin1().constData());
       
   181             exit_val = 5;
       
   182         }
       
   183         delete mkfile;
       
   184         mkfile = NULL;
       
   185     }
       
   186     qmakeClearCaches();
       
   187     return exit_val;
       
   188 }
       
   189 
       
   190 QT_END_NAMESPACE
       
   191 
       
   192 int main(int argc, char **argv)
       
   193 {
       
   194     return QT_PREPEND_NAMESPACE(runQMake)(argc, argv);
       
   195 }