depend_filter.py 1.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#!/usr/bin/env python
# -*- encoding: utf8 -*-

# ----------------------------------------------------------------------------
# Copyright (C) 2012-2018 Jolla Ltd.
# Contact: Simo Piiroinen <simo.piiroinen@jollamobile.com>
# License: GPLv2
# ----------------------------------------------------------------------------

import sys,os

def is_local(path):
    return not path.startswith("/")

def print_rule(dest, srce):
    print "%s\\\n" % "\\\n\t".join(["%s:" % dest] + srce)

def set_extension(path, ext):
    return os.path.splitext(path)[0] + ext

def normalize_path(srce):
    return os.path.normpath(srce)

def fix_directory(dest, srce):
    srce = os.path.dirname(srce)
    dest = os.path.basename(dest)
    return os.path.join(srce, dest)

if __name__ == "__main__":

    data = sys.stdin.readlines()
    data = map(lambda x:x.rstrip(), data)
    data.reverse()

    deps = []

    # Note: dependencies are sorted only to keep
    #       future diffs cleaner

    while data:
        line = data.pop()
        while line.endswith('\\'):
            line = line[:-1] + data.pop()
        if not ':' in line:
            continue

        dest,srce = line.split(":",1)
        srce = srce.split()
        srce,temp = srce[0],srce[1:]

        # take dest path primary srce
        dest = fix_directory(dest, srce)

        # remove secondary deps with absolute path
        temp = filter(is_local, temp)

        # sort secondary sources
        temp.sort()

        srce = [srce] + temp
        srce = map(normalize_path, srce)

        deps.append((dest,srce))

    for dest,srce in sorted(deps):
        # emit normal compilation dependencies
        print_rule(dest, srce)
        # and for -fPIC in case it is needed
        dest = set_extension(dest, ".pic.o")
        print_rule(dest, srce)