#!/usr/bin/env python
# *************************************************************
#  
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#  
#    http://www.apache.org/licenses/LICENSE-2.0
#  
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.
#  
# *************************************************************
import sys
import os

class CxxTarget:
    def __init__(self, line):
        self.directory = ''
        self.outputfile = ''
        self.includeflags = []
        self.cxxflags = []
        self.inputfiles = []
        self.nolink = False
        options = line[:-1].split(' ')
        self.directory = options.pop(0)
        parsing_outputfile = False
        for option in options:
            if parsing_outputfile:
                self.outputfile = option
                parsing_outputfile = False
            elif option == '-o':
                parsing_outputfile = True
            elif option == '-c':
                self.nolink = True
            elif option.startswith('-I'):
                self.includeflags.append(CxxFlag(option))
            elif option.startswith('-'):
                self.cxxflags.append(CxxFlag(option))
            else:
                self.inputfiles.append(option)
        self.cxxflags.sort()
        self.includeflags.sort()
        cxxflags_tmp = dict()
        for flag in self.cxxflags:
            cxxflags_tmp[flag.name] = flag
        self.cxxflags = cxxflags_tmp.values()
        includeflags_tmp = dict()
        for flag in self.includeflags:
            includeflags_tmp[flag.name] = flag
        self.includeflags = includeflags_tmp.values()
        CxxTargets.by_name[self.getFullOutputname()] = self
    def __str__(self):
        return '%s' % (self.getFullOutputname())
    def getFullOutputname(self):
        return self.directory + '/' + self.outputfile
    def __cmp__(self, other):
        return cmp(self.getFullOutputname(), other.getFullOutputname())

class CxxFlag:
    def __init__(self, name):
        self.name = name
        CxxFlags.by_name[self.name] = self
    def __str__(self):
        return 'Flag %s' % (self.name)
    def __cmp__(self, other):
        return cmp(self.name, other.name)

class CxxFlags:
    by_name = dict()

class CxxTargets:
    by_name = dict()

if __name__ == '__main__':
    [CxxTarget(line) for line in sys.stdin.readlines()]
    compile_targets = [target for target in CxxTargets.by_name.values() if target.nolink]
    link_targets = [target for target in CxxTargets.by_name.values() if not target.nolink]
    common_compile_flags = []
    for flag in CxxFlags.by_name.values():
        if sum((flag in target.cxxflags for target in compile_targets)) == len(compile_targets):
            common_compile_flags.append(flag)
    common_link_flags = []
    for flag in CxxFlags.by_name.values():
        if sum((flag in target.cxxflags for target in compile_targets)) == len(compile_targets):
            common_link_flags.append(flag)

    for target in compile_targets:
        target.cxxflags = [flag for flag in target.cxxflags if flag not in common_compile_flags]
        target.cxxflags.sort()
    for target in link_targets:
        target.cxxflags = [flag for flag in target.cxxflags if flag not in common_link_flags]
        target.cxxflags.sort()

    common_compile_flags.sort()
    common_link_flags.sort()
    print 'common compile flags: %s' % (' '.join(flag.name for flag in common_compile_flags))
    print 'common link flags: %s' % (' '.join(flag.name for flag in common_link_flags))

    by_flagset = dict()
    for target in CxxTargets.by_name.values():
        flagset = ' '.join((flag.name for flag in target.cxxflags))
        if flagset not in by_flagset:
            by_flagset[flagset] = list()
        by_flagset[flagset].append(target)
    for targetlist in by_flagset.values():
        targetlist.sort()
    flagsets = by_flagset.keys()
    flagsets.sort()
    print '%d compilerflag groups:' % (len(flagsets))
    for flagset in flagsets:
        print flagset
        for target in by_flagset[flagset]:
            print '%s' % (target)
        print

    by_flagset = dict()
    for target in CxxTargets.by_name.values():
        flagset = ' '.join((flag.name for flag in target.includeflags))
        if flagset not in by_flagset:
            by_flagset[flagset] = list()
        by_flagset[flagset].append(target)
    for targetlist in by_flagset.values():
        targetlist.sort()
    flagsets = by_flagset.keys()
    flagsets.sort()
    print '%d include flag groups:' % (len(flagsets))
    for flagset in flagsets:
        print flagset
        for target in by_flagset[flagset]:
            print '%s' % (target)
        print

    print 'sources:'
    by_name = dict()
    for target in CxxTargets.by_name.values():
        by_name[os.path.basename(target.outputfile)] = target
    names = by_name.keys()
    names.sort()
    for target in CxxTargets.by_name.values():
        if len(target.inputfiles) > 1:
            objects = [os.path.basename(object) for object in target.inputfiles]
            sources = list()
            for object in objects:
                if object in by_name:
                    sources.append(by_name[object].inputfiles[0])
                else:
                    sources.append('!!!!' + object)
            sources.sort()
            print '%s %s' % (target.getFullOutputname(), ' '.join(sources))