summaryrefslogtreecommitdiffstats
path: root/buildtools/autotools/makefilehandler.cpp
blob: ef2354f1d203227f9acbcdaff3371e9e9a29863c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
  KDevelop Autotools Support
  Copyright (c) 2005 by Matt Rogers <[email protected]>

***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************
*/

#include <tqdir.h>
#include <tqglobal.h>
#include <tqmap.h>
#include <tqregexp.h>
#include <tqstring.h>
#include <tqvaluelist.h>

#include <kdebug.h>

#include <autotoolsast.h>
#include <autotoolsdriver.h>

#include "makefilehandler.h"

typedef TQValueList<AutoTools::AST*> ASTList;

class MakefileHandler::Private
{
public:
    TQMap<TQString, AutoTools::ProjectAST*> projects;
    TQMap<TQString, TQString> folderToFileMap;
};

MakefileHandler::MakefileHandler()
{
    d = new MakefileHandler::Private;
}

MakefileHandler::~MakefileHandler()
{
    delete d;
}

void MakefileHandler::parse( const TQString& folder, bool recursive )
{
    //look for either Makefile.am.in, Makefile.am, or Makefile.in, in that order
    AutoTools::ProjectAST* ast;
    int ret = -1;
    TQString filePath = folder + "/Makefile.am.in";
    if ( TQFile::exists( filePath ) )
        ret = AutoTools::Driver::parseFile( filePath, &ast );
    else
    {
        filePath = folder + "/Makefile.am";
        if ( TQFile::exists( filePath ) )
            ret = AutoTools::Driver::parseFile( filePath, &ast );
        else
        {
            filePath = folder + "/Makefile.in";
            if ( TQFile::exists( filePath ) )
                ret = AutoTools::Driver::parseFile( filePath, &ast );
            else
                kdDebug(9020) << k_funcinfo << "no appropriate file to parse in "
                              << folder << endl;
        }
    }

    if ( ret != 0 )
    {
        return;
    }

    kdDebug(9020) << k_funcinfo << filePath << " was parsed correctly. Adding information" << endl;
    Q_ASSERT( ast != 0 );
    d->projects[filePath] = ast;
    d->folderToFileMap[folder] = filePath;

    if ( recursive && ast && ast->hasChildren() )
    {
        TQValueList<AutoTools::AST*> astChildList = ast->tqchildren();
        TQValueList<AutoTools::AST*>::iterator it(astChildList.begin()), clEnd(astChildList.end());
        for ( ; it != clEnd; ++it )
        {
            if ( (*it)->nodeType() == AutoTools::AST::AssignmentAST )
            {
                AutoTools::AssignmentAST* assignment = static_cast<AutoTools::AssignmentAST*>( (*it) );
                if ( assignment->scopedID == "SUBDIRS"  )
                {
                    TQString list = assignment->values.join( TQString::null );
                    list.simplifyWhiteSpace();
                    kdDebug(9020) << k_funcinfo << "subdirs is " << list << endl;
                    TQStringList subdirList = TQStringList::split( " ",  list );
                    TQStringList::iterator vit = subdirList.begin();
                    for ( ; vit != subdirList.end(); ++vit )
                    {
                        TQString realDir = ( *vit );
                        if ( realDir.startsWith( "\\" ) )
                            realDir.remove( 0, 1 );

                        realDir = realDir.stripWhiteSpace();
                        if ( realDir != "." && realDir != ".." && !realDir.isEmpty() )
                        {
                            if ( isVariable( realDir ) )
                            {
                                kdDebug(9020) << k_funcinfo << "'" << realDir << "' is a variable" << endl;
                                realDir = resolveVariable( realDir, ast );
                            }

                            kdDebug(9020) << k_funcinfo << "Beginning parsing of '" << realDir << "'" << endl;
                            parse( folder + '/' + realDir, recursive );
                        }
                    }
                }
            }
        }
    }
}

AutoTools::ProjectAST* MakefileHandler::astForFolder( const TQString& folderPath )
{
    if ( d->folderToFileMap.tqcontains( folderPath ) )
    {
        TQString filePath = d->folderToFileMap[folderPath];
        return d->projects[filePath];
    }
    else
        return 0;
}

bool MakefileHandler::isVariable( const TQString& item ) const
{
    if ( item.contains( TQRegExp( "(\\$\\([a-zA-Z0-9_-]*\\)|@[a-zA-Z0-9_-]*@)" ) ) )
        return true;
    else
        return false;
}

TQString MakefileHandler::resolveVariable( const TQString& variable, AutoTools::ProjectAST* ast )
{
    if ( !ast )
        return variable;

    kdDebug(9020) << k_funcinfo << "attempting to resolve '" << variable << "'"<< endl;
    ASTList childList = ast->tqchildren();
    ASTList::iterator it( childList.begin() ), clEnd( childList.end() );
    for ( ; it != clEnd; ++it )
    {
        if ( ( *it )->nodeType() == AutoTools::AST::AssignmentAST )
        {
            AutoTools::AssignmentAST* assignment = static_cast<AutoTools::AssignmentAST*>( ( *it ) );
            if ( variable.tqfind( assignment->scopedID ) != -1 )
            {
                kdDebug(9020) << k_funcinfo << "Resolving variable '" << variable << "' to '"
                              << assignment->values.join( TQString::null ).stripWhiteSpace() << "'" << endl;
                return assignment->values.join( TQString::null ).stripWhiteSpace();
            }
        }
    }

    return variable;
}
//kate: space-indent on; indent-width 4;