1*cdf0e10cSrcweir#!/usr/bin/env python 2*cdf0e10cSrcweirimport sys 3*cdf0e10cSrcweirimport os 4*cdf0e10cSrcweir 5*cdf0e10cSrcweirclass CxxTarget: 6*cdf0e10cSrcweir def __init__(self, line): 7*cdf0e10cSrcweir self.directory = '' 8*cdf0e10cSrcweir self.outputfile = '' 9*cdf0e10cSrcweir self.includeflags = [] 10*cdf0e10cSrcweir self.cxxflags = [] 11*cdf0e10cSrcweir self.inputfiles = [] 12*cdf0e10cSrcweir self.nolink = False 13*cdf0e10cSrcweir options = line[:-1].split(' ') 14*cdf0e10cSrcweir self.directory = options.pop(0) 15*cdf0e10cSrcweir parsing_outputfile = False 16*cdf0e10cSrcweir for option in options: 17*cdf0e10cSrcweir if parsing_outputfile: 18*cdf0e10cSrcweir self.outputfile = option 19*cdf0e10cSrcweir parsing_outputfile = False 20*cdf0e10cSrcweir elif option == '-o': 21*cdf0e10cSrcweir parsing_outputfile = True 22*cdf0e10cSrcweir elif option == '-c': 23*cdf0e10cSrcweir self.nolink = True 24*cdf0e10cSrcweir elif option.startswith('-I'): 25*cdf0e10cSrcweir self.includeflags.append(CxxFlag(option)) 26*cdf0e10cSrcweir elif option.startswith('-'): 27*cdf0e10cSrcweir self.cxxflags.append(CxxFlag(option)) 28*cdf0e10cSrcweir else: 29*cdf0e10cSrcweir self.inputfiles.append(option) 30*cdf0e10cSrcweir self.cxxflags.sort() 31*cdf0e10cSrcweir self.includeflags.sort() 32*cdf0e10cSrcweir cxxflags_tmp = dict() 33*cdf0e10cSrcweir for flag in self.cxxflags: 34*cdf0e10cSrcweir cxxflags_tmp[flag.name] = flag 35*cdf0e10cSrcweir self.cxxflags = cxxflags_tmp.values() 36*cdf0e10cSrcweir includeflags_tmp = dict() 37*cdf0e10cSrcweir for flag in self.includeflags: 38*cdf0e10cSrcweir includeflags_tmp[flag.name] = flag 39*cdf0e10cSrcweir self.includeflags = includeflags_tmp.values() 40*cdf0e10cSrcweir CxxTargets.by_name[self.getFullOutputname()] = self 41*cdf0e10cSrcweir def __str__(self): 42*cdf0e10cSrcweir return '%s' % (self.getFullOutputname()) 43*cdf0e10cSrcweir def getFullOutputname(self): 44*cdf0e10cSrcweir return self.directory + '/' + self.outputfile 45*cdf0e10cSrcweir def __cmp__(self, other): 46*cdf0e10cSrcweir return cmp(self.getFullOutputname(), other.getFullOutputname()) 47*cdf0e10cSrcweir 48*cdf0e10cSrcweirclass CxxFlag: 49*cdf0e10cSrcweir def __init__(self, name): 50*cdf0e10cSrcweir self.name = name 51*cdf0e10cSrcweir CxxFlags.by_name[self.name] = self 52*cdf0e10cSrcweir def __str__(self): 53*cdf0e10cSrcweir return 'Flag %s' % (self.name) 54*cdf0e10cSrcweir def __cmp__(self, other): 55*cdf0e10cSrcweir return cmp(self.name, other.name) 56*cdf0e10cSrcweir 57*cdf0e10cSrcweirclass CxxFlags: 58*cdf0e10cSrcweir by_name = dict() 59*cdf0e10cSrcweir 60*cdf0e10cSrcweirclass CxxTargets: 61*cdf0e10cSrcweir by_name = dict() 62*cdf0e10cSrcweir 63*cdf0e10cSrcweirif __name__ == '__main__': 64*cdf0e10cSrcweir [CxxTarget(line) for line in sys.stdin.readlines()] 65*cdf0e10cSrcweir compile_targets = [target for target in CxxTargets.by_name.values() if target.nolink] 66*cdf0e10cSrcweir link_targets = [target for target in CxxTargets.by_name.values() if not target.nolink] 67*cdf0e10cSrcweir common_compile_flags = [] 68*cdf0e10cSrcweir for flag in CxxFlags.by_name.values(): 69*cdf0e10cSrcweir if sum((flag in target.cxxflags for target in compile_targets)) == len(compile_targets): 70*cdf0e10cSrcweir common_compile_flags.append(flag) 71*cdf0e10cSrcweir common_link_flags = [] 72*cdf0e10cSrcweir for flag in CxxFlags.by_name.values(): 73*cdf0e10cSrcweir if sum((flag in target.cxxflags for target in compile_targets)) == len(compile_targets): 74*cdf0e10cSrcweir common_link_flags.append(flag) 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir for target in compile_targets: 77*cdf0e10cSrcweir target.cxxflags = [flag for flag in target.cxxflags if flag not in common_compile_flags] 78*cdf0e10cSrcweir target.cxxflags.sort() 79*cdf0e10cSrcweir for target in link_targets: 80*cdf0e10cSrcweir target.cxxflags = [flag for flag in target.cxxflags if flag not in common_link_flags] 81*cdf0e10cSrcweir target.cxxflags.sort() 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir common_compile_flags.sort() 84*cdf0e10cSrcweir common_link_flags.sort() 85*cdf0e10cSrcweir print 'common compile flags: %s' % (' '.join(flag.name for flag in common_compile_flags)) 86*cdf0e10cSrcweir print 'common link flags: %s' % (' '.join(flag.name for flag in common_link_flags)) 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir by_flagset = dict() 89*cdf0e10cSrcweir for target in CxxTargets.by_name.values(): 90*cdf0e10cSrcweir flagset = ' '.join((flag.name for flag in target.cxxflags)) 91*cdf0e10cSrcweir if flagset not in by_flagset: 92*cdf0e10cSrcweir by_flagset[flagset] = list() 93*cdf0e10cSrcweir by_flagset[flagset].append(target) 94*cdf0e10cSrcweir for targetlist in by_flagset.values(): 95*cdf0e10cSrcweir targetlist.sort() 96*cdf0e10cSrcweir flagsets = by_flagset.keys() 97*cdf0e10cSrcweir flagsets.sort() 98*cdf0e10cSrcweir print '%d compilerflag groups:' % (len(flagsets)) 99*cdf0e10cSrcweir for flagset in flagsets: 100*cdf0e10cSrcweir print flagset 101*cdf0e10cSrcweir for target in by_flagset[flagset]: 102*cdf0e10cSrcweir print '%s' % (target) 103*cdf0e10cSrcweir print 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir by_flagset = dict() 106*cdf0e10cSrcweir for target in CxxTargets.by_name.values(): 107*cdf0e10cSrcweir flagset = ' '.join((flag.name for flag in target.includeflags)) 108*cdf0e10cSrcweir if flagset not in by_flagset: 109*cdf0e10cSrcweir by_flagset[flagset] = list() 110*cdf0e10cSrcweir by_flagset[flagset].append(target) 111*cdf0e10cSrcweir for targetlist in by_flagset.values(): 112*cdf0e10cSrcweir targetlist.sort() 113*cdf0e10cSrcweir flagsets = by_flagset.keys() 114*cdf0e10cSrcweir flagsets.sort() 115*cdf0e10cSrcweir print '%d include flag groups:' % (len(flagsets)) 116*cdf0e10cSrcweir for flagset in flagsets: 117*cdf0e10cSrcweir print flagset 118*cdf0e10cSrcweir for target in by_flagset[flagset]: 119*cdf0e10cSrcweir print '%s' % (target) 120*cdf0e10cSrcweir print 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir print 'sources:' 123*cdf0e10cSrcweir by_name = dict() 124*cdf0e10cSrcweir for target in CxxTargets.by_name.values(): 125*cdf0e10cSrcweir by_name[os.path.basename(target.outputfile)] = target 126*cdf0e10cSrcweir names = by_name.keys() 127*cdf0e10cSrcweir names.sort() 128*cdf0e10cSrcweir for target in CxxTargets.by_name.values(): 129*cdf0e10cSrcweir if len(target.inputfiles) > 1: 130*cdf0e10cSrcweir objects = [os.path.basename(object) for object in target.inputfiles] 131*cdf0e10cSrcweir sources = list() 132*cdf0e10cSrcweir for object in objects: 133*cdf0e10cSrcweir if object in by_name: 134*cdf0e10cSrcweir sources.append(by_name[object].inputfiles[0]) 135*cdf0e10cSrcweir else: 136*cdf0e10cSrcweir sources.append('!!!!' + object) 137*cdf0e10cSrcweir sources.sort() 138*cdf0e10cSrcweir print '%s %s' % (target.getFullOutputname(), ' '.join(sources)) 139