xref: /aoo42x/main/solenv/bin/buildalyzer (revision 5b501c92)
1cdf0e10cSrcweir#!/usr/bin/env python
2*5b501c92SAndrew Rist# *************************************************************
3*5b501c92SAndrew Rist#
4*5b501c92SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
5*5b501c92SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
6*5b501c92SAndrew Rist#  distributed with this work for additional information
7*5b501c92SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
8*5b501c92SAndrew Rist#  to you under the Apache License, Version 2.0 (the
9*5b501c92SAndrew Rist#  "License"); you may not use this file except in compliance
10*5b501c92SAndrew Rist#  with the License.  You may obtain a copy of the License at
11*5b501c92SAndrew Rist#
12*5b501c92SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
13*5b501c92SAndrew Rist#
14*5b501c92SAndrew Rist#  Unless required by applicable law or agreed to in writing,
15*5b501c92SAndrew Rist#  software distributed under the License is distributed on an
16*5b501c92SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17*5b501c92SAndrew Rist#  KIND, either express or implied.  See the License for the
18*5b501c92SAndrew Rist#  specific language governing permissions and limitations
19*5b501c92SAndrew Rist#  under the License.
20*5b501c92SAndrew Rist#
21*5b501c92SAndrew Rist# *************************************************************
22cdf0e10cSrcweirimport sys
23cdf0e10cSrcweirimport os
24cdf0e10cSrcweir
25cdf0e10cSrcweirclass CxxTarget:
26cdf0e10cSrcweir    def __init__(self, line):
27cdf0e10cSrcweir        self.directory = ''
28cdf0e10cSrcweir        self.outputfile = ''
29cdf0e10cSrcweir        self.includeflags = []
30cdf0e10cSrcweir        self.cxxflags = []
31cdf0e10cSrcweir        self.inputfiles = []
32cdf0e10cSrcweir        self.nolink = False
33cdf0e10cSrcweir        options = line[:-1].split(' ')
34cdf0e10cSrcweir        self.directory = options.pop(0)
35cdf0e10cSrcweir        parsing_outputfile = False
36cdf0e10cSrcweir        for option in options:
37cdf0e10cSrcweir            if parsing_outputfile:
38cdf0e10cSrcweir                self.outputfile = option
39cdf0e10cSrcweir                parsing_outputfile = False
40cdf0e10cSrcweir            elif option == '-o':
41cdf0e10cSrcweir                parsing_outputfile = True
42cdf0e10cSrcweir            elif option == '-c':
43cdf0e10cSrcweir                self.nolink = True
44cdf0e10cSrcweir            elif option.startswith('-I'):
45cdf0e10cSrcweir                self.includeflags.append(CxxFlag(option))
46cdf0e10cSrcweir            elif option.startswith('-'):
47cdf0e10cSrcweir                self.cxxflags.append(CxxFlag(option))
48cdf0e10cSrcweir            else:
49cdf0e10cSrcweir                self.inputfiles.append(option)
50cdf0e10cSrcweir        self.cxxflags.sort()
51cdf0e10cSrcweir        self.includeflags.sort()
52cdf0e10cSrcweir        cxxflags_tmp = dict()
53cdf0e10cSrcweir        for flag in self.cxxflags:
54cdf0e10cSrcweir            cxxflags_tmp[flag.name] = flag
55cdf0e10cSrcweir        self.cxxflags = cxxflags_tmp.values()
56cdf0e10cSrcweir        includeflags_tmp = dict()
57cdf0e10cSrcweir        for flag in self.includeflags:
58cdf0e10cSrcweir            includeflags_tmp[flag.name] = flag
59cdf0e10cSrcweir        self.includeflags = includeflags_tmp.values()
60cdf0e10cSrcweir        CxxTargets.by_name[self.getFullOutputname()] = self
61cdf0e10cSrcweir    def __str__(self):
62cdf0e10cSrcweir        return '%s' % (self.getFullOutputname())
63cdf0e10cSrcweir    def getFullOutputname(self):
64cdf0e10cSrcweir        return self.directory + '/' + self.outputfile
65cdf0e10cSrcweir    def __cmp__(self, other):
66cdf0e10cSrcweir        return cmp(self.getFullOutputname(), other.getFullOutputname())
67cdf0e10cSrcweir
68cdf0e10cSrcweirclass CxxFlag:
69cdf0e10cSrcweir    def __init__(self, name):
70cdf0e10cSrcweir        self.name = name
71cdf0e10cSrcweir        CxxFlags.by_name[self.name] = self
72cdf0e10cSrcweir    def __str__(self):
73cdf0e10cSrcweir        return 'Flag %s' % (self.name)
74cdf0e10cSrcweir    def __cmp__(self, other):
75cdf0e10cSrcweir        return cmp(self.name, other.name)
76cdf0e10cSrcweir
77cdf0e10cSrcweirclass CxxFlags:
78cdf0e10cSrcweir    by_name = dict()
79cdf0e10cSrcweir
80cdf0e10cSrcweirclass CxxTargets:
81cdf0e10cSrcweir    by_name = dict()
82cdf0e10cSrcweir
83cdf0e10cSrcweirif __name__ == '__main__':
84cdf0e10cSrcweir    [CxxTarget(line) for line in sys.stdin.readlines()]
85cdf0e10cSrcweir    compile_targets = [target for target in CxxTargets.by_name.values() if target.nolink]
86cdf0e10cSrcweir    link_targets = [target for target in CxxTargets.by_name.values() if not target.nolink]
87cdf0e10cSrcweir    common_compile_flags = []
88cdf0e10cSrcweir    for flag in CxxFlags.by_name.values():
89cdf0e10cSrcweir        if sum((flag in target.cxxflags for target in compile_targets)) == len(compile_targets):
90cdf0e10cSrcweir            common_compile_flags.append(flag)
91cdf0e10cSrcweir    common_link_flags = []
92cdf0e10cSrcweir    for flag in CxxFlags.by_name.values():
93cdf0e10cSrcweir        if sum((flag in target.cxxflags for target in compile_targets)) == len(compile_targets):
94cdf0e10cSrcweir            common_link_flags.append(flag)
95cdf0e10cSrcweir
96cdf0e10cSrcweir    for target in compile_targets:
97cdf0e10cSrcweir        target.cxxflags = [flag for flag in target.cxxflags if flag not in common_compile_flags]
98cdf0e10cSrcweir        target.cxxflags.sort()
99cdf0e10cSrcweir    for target in link_targets:
100cdf0e10cSrcweir        target.cxxflags = [flag for flag in target.cxxflags if flag not in common_link_flags]
101cdf0e10cSrcweir        target.cxxflags.sort()
102cdf0e10cSrcweir
103cdf0e10cSrcweir    common_compile_flags.sort()
104cdf0e10cSrcweir    common_link_flags.sort()
105cdf0e10cSrcweir    print 'common compile flags: %s' % (' '.join(flag.name for flag in common_compile_flags))
106cdf0e10cSrcweir    print 'common link flags: %s' % (' '.join(flag.name for flag in common_link_flags))
107cdf0e10cSrcweir
108cdf0e10cSrcweir    by_flagset = dict()
109cdf0e10cSrcweir    for target in CxxTargets.by_name.values():
110cdf0e10cSrcweir        flagset = ' '.join((flag.name for flag in target.cxxflags))
111cdf0e10cSrcweir        if flagset not in by_flagset:
112cdf0e10cSrcweir            by_flagset[flagset] = list()
113cdf0e10cSrcweir        by_flagset[flagset].append(target)
114cdf0e10cSrcweir    for targetlist in by_flagset.values():
115cdf0e10cSrcweir        targetlist.sort()
116cdf0e10cSrcweir    flagsets = by_flagset.keys()
117cdf0e10cSrcweir    flagsets.sort()
118cdf0e10cSrcweir    print '%d compilerflag groups:' % (len(flagsets))
119cdf0e10cSrcweir    for flagset in flagsets:
120cdf0e10cSrcweir        print flagset
121cdf0e10cSrcweir        for target in by_flagset[flagset]:
122cdf0e10cSrcweir            print '%s' % (target)
123cdf0e10cSrcweir        print
124cdf0e10cSrcweir
125cdf0e10cSrcweir    by_flagset = dict()
126cdf0e10cSrcweir    for target in CxxTargets.by_name.values():
127cdf0e10cSrcweir        flagset = ' '.join((flag.name for flag in target.includeflags))
128cdf0e10cSrcweir        if flagset not in by_flagset:
129cdf0e10cSrcweir            by_flagset[flagset] = list()
130cdf0e10cSrcweir        by_flagset[flagset].append(target)
131cdf0e10cSrcweir    for targetlist in by_flagset.values():
132cdf0e10cSrcweir        targetlist.sort()
133cdf0e10cSrcweir    flagsets = by_flagset.keys()
134cdf0e10cSrcweir    flagsets.sort()
135cdf0e10cSrcweir    print '%d include flag groups:' % (len(flagsets))
136cdf0e10cSrcweir    for flagset in flagsets:
137cdf0e10cSrcweir        print flagset
138cdf0e10cSrcweir        for target in by_flagset[flagset]:
139cdf0e10cSrcweir            print '%s' % (target)
140cdf0e10cSrcweir        print
141cdf0e10cSrcweir
142cdf0e10cSrcweir    print 'sources:'
143cdf0e10cSrcweir    by_name = dict()
144cdf0e10cSrcweir    for target in CxxTargets.by_name.values():
145cdf0e10cSrcweir        by_name[os.path.basename(target.outputfile)] = target
146cdf0e10cSrcweir    names = by_name.keys()
147cdf0e10cSrcweir    names.sort()
148cdf0e10cSrcweir    for target in CxxTargets.by_name.values():
149cdf0e10cSrcweir        if len(target.inputfiles) > 1:
150cdf0e10cSrcweir            objects = [os.path.basename(object) for object in target.inputfiles]
151cdf0e10cSrcweir            sources = list()
152cdf0e10cSrcweir            for object in objects:
153cdf0e10cSrcweir                if object in by_name:
154cdf0e10cSrcweir                    sources.append(by_name[object].inputfiles[0])
155cdf0e10cSrcweir                else:
156cdf0e10cSrcweir                    sources.append('!!!!' + object)
157cdf0e10cSrcweir            sources.sort()
158cdf0e10cSrcweir            print '%s %s' % (target.getFullOutputname(), ' '.join(sources))
159