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