Parse compile flags.

def parse_flags(*flags, **kwargs):
    """Parse compile flags."""

    parsed = {
        'ASFLAGS':       OrderedSet(),
        'CFLAGS':        OrderedSet(),
        'DEFINES':       OrderedSet(),
        'INCLUDES':      OrderedSet(),
        'FRAMEWORKS':    OrderedSet(),
        'FRAMEWORKPATH': OrderedSet(),
        'LDFLAGS':       OrderedSet(),
        'LIBS':          OrderedSet(),
        'LIBPATH':       OrderedSet(),
    }

    def _parse(arg):
        if not arg:
            return

        if not isinstance(arg, STR_TYPES):
            for item in arg:
                _parse(arg)
            return

        def add_define(name):
            part = name.split('=')
            if len(part) == 1:
                parsed['DEFINES'].add(name)
            else:
                parsed['DEFINES'].add([part[0], '='.join(part[1:])])

        part = shlex.split(arg)
        curr = None   # For multiword arguments
        for item in part:
            if curr is not None:
                if curr == 'DEFINES':
                    add_define(item)
                elif curr == '-include':
                    parsed['CFLAGS'].add(('-include', item))
                elif curr == '-isysroot':
                    parsed['CFLAGS'].add(('-isysroot', item))
                    parsed['LDFLAGS'].add(('-isysroot', item))
                elif curr == '-arch':
                    parsed['CFLAGS'].add(('-arch', item))
                    parsed['LDFLAGS'].add(('-arch', item))
                else:
                    parsed[curr].add(item)
                curr = None

            elif not item[0] in '-+':
                parsed['LIBS'].add(item)

            elif item == '-dylib_file':
                parsed['LDFLAGS'].add(item)
                curr = 'LDFLAGS'

            elif item[:2] == '-L':
                if item[2:]:
                    parsed['LIBPATH'].add(item[2:])
                else:
                    curr = 'LIBPATH'

            elif item[:2] == '-l':
                if item[2:]:
                    parsed['LIBS'].add(item[2:])
                else:
                    curr = 'LIBS'

            elif item[:2] == '-I':
                if item[2:]:
                    parsed['INCLUDES'].add(item[2:])
                else:
                    curr = 'INCLUDES'

            elif item[:4] == '-Wa,':
                parsed['ASFLAGS'].add(arg[4:])
                parsed['CFLAGS'].add(item)

            elif item[:4] == '-Wp,':
                parsed['CFLAGS'].add(item)

            elif item[:2] == '-D':
                if item[2:]:
                    parsed['DEFINES'].add(item[2:])
                else:
                    curr = 'DEFINES'

            elif item == '-framework':
                curr = 'FRAMEWORKS'

            elif item[:14] == '-frameworkdir=':
                parsed['FRAMEWORKPATH'].add(item[14:])

            elif item[:2] == '-F':
                if item[2:]:
                    parsed['FRAMEWORKPATH'].add(item[2:])
                else:
                    curr = 'FRAMEWORKPATH'

            elif item in ('-mno-cygwin', '-pthread', '-openmp', '-fopenmp'):
                parsed['CFLAGS'].add(item)
                parsed['LDFLAGS'].add(item)

            elif item == '-mwindows':
                parsed['LDFLAGS'].add(item)

            elif item[:5] == '-std=':
                parsed['CFLAGS'].add(item)

            elif item[0] == '+':
                parsed['CFLAGS'].add(item)
                parsed['LDFLAGS'].add(item)

            elif item in ('-include', '-isysroot', '-arch'):
                curr = item

            else:
                parsed[kwargs.get('origin', 'CFLAGS')].add(item)

    for arg in flags:
        _parse(arg)

    return parsed