~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to buildtools/autotools/makefilehandler.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  KDevelop Autotools Support
 
3
  Copyright (c) 2005 by Matt Rogers <mattr@kde.org>
 
4
 
 
5
***************************************************************************
 
6
*                                                                         *
 
7
*   This program is free software; you can redistribute it and/or modify  *
 
8
*   it under the terms of the GNU General Public License as published by  *
 
9
*   the Free Software Foundation; either version 2 of the License, or     *
 
10
*   (at your option) any later version.                                   *
 
11
*                                                                         *
 
12
***************************************************************************
 
13
*/
 
14
 
 
15
#include <qdir.h>
 
16
#include <qglobal.h>
 
17
#include <qmap.h>
 
18
#include <qregexp.h>
 
19
#include <qstring.h>
 
20
#include <qvaluelist.h>
 
21
 
 
22
#include <kdebug.h>
 
23
 
 
24
#include <autotoolsast.h>
 
25
#include <autotoolsdriver.h>
 
26
 
 
27
#include "makefilehandler.h"
 
28
 
 
29
typedef QValueList<AutoTools::AST*> ASTList;
 
30
 
 
31
class MakefileHandler::Private
 
32
{
 
33
public:
 
34
    QMap<QString, AutoTools::ProjectAST*> projects;
 
35
    QMap<QString, QString> folderToFileMap;
 
36
};
 
37
 
 
38
MakefileHandler::MakefileHandler()
 
39
{
 
40
    d = new MakefileHandler::Private;
 
41
}
 
42
 
 
43
MakefileHandler::~MakefileHandler()
 
44
{
 
45
    delete d;
 
46
}
 
47
 
 
48
void MakefileHandler::parse( const QString& folder, bool recursive )
 
49
{
 
50
    //look for either Makefile.am.in, Makefile.am, or Makefile.in, in that order
 
51
    AutoTools::ProjectAST* ast;
 
52
    int ret = -1;
 
53
    QString filePath = folder + "/Makefile.am.in";
 
54
    if ( QFile::exists( filePath ) )
 
55
        ret = AutoTools::Driver::parseFile( filePath, &ast );
 
56
    else
 
57
    {
 
58
        filePath = folder + "/Makefile.am";
 
59
        if ( QFile::exists( filePath ) )
 
60
            ret = AutoTools::Driver::parseFile( filePath, &ast );
 
61
        else
 
62
        {
 
63
            filePath = folder + "/Makefile.in";
 
64
            if ( QFile::exists( filePath ) )
 
65
                ret = AutoTools::Driver::parseFile( filePath, &ast );
 
66
            else
 
67
                kdDebug(9020) << k_funcinfo << "no appropriate file to parse in "
 
68
                              << folder << endl;
 
69
        }
 
70
    }
 
71
 
 
72
    if ( ret != 0 )
 
73
    {
 
74
        return;
 
75
    }
 
76
 
 
77
    kdDebug(9020) << k_funcinfo << filePath << " was parsed correctly. Adding information" << endl;
 
78
    Q_ASSERT( ast != 0 );
 
79
    d->projects[filePath] = ast;
 
80
    d->folderToFileMap[folder] = filePath;
 
81
 
 
82
    if ( recursive && ast && ast->hasChildren() )
 
83
    {
 
84
        QValueList<AutoTools::AST*> astChildList = ast->children();
 
85
        QValueList<AutoTools::AST*>::iterator it(astChildList.begin()), clEnd(astChildList.end());
 
86
        for ( ; it != clEnd; ++it )
 
87
        {
 
88
            if ( (*it)->nodeType() == AutoTools::AST::AssignmentAST )
 
89
            {
 
90
                AutoTools::AssignmentAST* assignment = static_cast<AutoTools::AssignmentAST*>( (*it) );
 
91
                if ( assignment->scopedID == "SUBDIRS"  )
 
92
                {
 
93
                    QString list = assignment->values.join( QString::null );
 
94
                    list.simplifyWhiteSpace();
 
95
                    kdDebug(9020) << k_funcinfo << "subdirs is " << list << endl;
 
96
                    QStringList subdirList = QStringList::split( " ",  list );
 
97
                    QStringList::iterator vit = subdirList.begin();
 
98
                    for ( ; vit != subdirList.end(); ++vit )
 
99
                    {
 
100
                        QString realDir = ( *vit );
 
101
                        if ( realDir.startsWith( "\\" ) )
 
102
                            realDir.remove( 0, 1 );
 
103
 
 
104
                        realDir = realDir.stripWhiteSpace();
 
105
                        if ( realDir != "." && realDir != ".." && !realDir.isEmpty() )
 
106
                        {
 
107
                            if ( isVariable( realDir ) )
 
108
                            {
 
109
                                kdDebug(9020) << k_funcinfo << "'" << realDir << "' is a variable" << endl;
 
110
                                realDir = resolveVariable( realDir, ast );
 
111
                            }
 
112
 
 
113
                            kdDebug(9020) << k_funcinfo << "Beginning parsing of '" << realDir << "'" << endl;
 
114
                            parse( folder + '/' + realDir, recursive );
 
115
                        }
 
116
                    }
 
117
                }
 
118
            }
 
119
        }
 
120
    }
 
121
}
 
122
 
 
123
AutoTools::ProjectAST* MakefileHandler::astForFolder( const QString& folderPath )
 
124
{
 
125
    if ( d->folderToFileMap.contains( folderPath ) )
 
126
    {
 
127
        QString filePath = d->folderToFileMap[folderPath];
 
128
        return d->projects[filePath];
 
129
    }
 
130
    else
 
131
        return 0;
 
132
}
 
133
 
 
134
bool MakefileHandler::isVariable( const QString& item ) const
 
135
{
 
136
    if ( item.contains( QRegExp( "(\\$\\([a-zA-Z0-9_-]*\\)|@[a-zA-Z0-9_-]*@)" ) ) )
 
137
        return true;
 
138
    else
 
139
        return false;
 
140
}
 
141
 
 
142
QString MakefileHandler::resolveVariable( const QString& variable, AutoTools::ProjectAST* ast )
 
143
{
 
144
    if ( !ast )
 
145
        return variable;
 
146
 
 
147
    kdDebug(9020) << k_funcinfo << "attempting to resolve '" << variable << "'"<< endl;
 
148
    ASTList childList = ast->children();
 
149
    ASTList::iterator it( childList.begin() ), clEnd( childList.end() );
 
150
    for ( ; it != clEnd; ++it )
 
151
    {
 
152
        if ( ( *it )->nodeType() == AutoTools::AST::AssignmentAST )
 
153
        {
 
154
            AutoTools::AssignmentAST* assignment = static_cast<AutoTools::AssignmentAST*>( ( *it ) );
 
155
            if ( variable.find( assignment->scopedID ) != -1 )
 
156
            {
 
157
                kdDebug(9020) << k_funcinfo << "Resolving variable '" << variable << "' to '"
 
158
                              << assignment->values.join( QString::null ).stripWhiteSpace() << "'" << endl;
 
159
                return assignment->values.join( QString::null ).stripWhiteSpace();
 
160
            }
 
161
        }
 
162
    }
 
163
 
 
164
    return variable;
 
165
}
 
166
//kate: space-indent on; indent-width 4;