0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisnerimport itertools
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisnerimport os
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisnerimport subprocess
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisnerdef GetFlagsFromMakefile(varname):
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner return subprocess.check_output([
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner "make", "-s", "print-%s" % varname]).decode().split()
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisnerdef Flatten(lists):
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner return list(itertools.chain.from_iterable(lists))
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisnerdef DirectoryOfThisScript():
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner return os.path.dirname(os.path.abspath(__file__))
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisnerdef MakeRelativePathsInFlagsAbsolute(flags, working_directory):
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner if not working_directory:
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner return flags
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner new_flags = []
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner make_next_absolute = False
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner for flag in flags:
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner new_flag = flag
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner if make_next_absolute:
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner make_next_absolute = False
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner if not flag.startswith('/'):
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner new_flag = os.path.join(working_directory, flag)
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner for path_flag in path_flags:
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner if flag == path_flag:
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner make_next_absolute = True
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner break
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner if flag.startswith(path_flag):
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner path = flag[ len(path_flag): ]
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner new_flag = path_flag + os.path.join(working_directory, path)
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner break
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner if new_flag:
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner new_flags.append(new_flag)
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner return new_flags
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisnerdef FlagsForFile(filename):
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner relative_to = DirectoryOfThisScript()
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner return {
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner 'flags': MakeRelativePathsInFlagsAbsolute(flags, relative_to),
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner 'do_cache': True
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner }
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisnerflags = Flatten(map(GetFlagsFromMakefile, [
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner 'AM_CPPFLAGS',
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner 'CPPFLAGS',
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner 'AM_CFLAGS',
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner 'CFLAGS',
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner]))
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner# these flags cause crashes in libclang, so remove them
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisnerflags.remove('-Wlogical-op')
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisnerflags.remove('-Wsuggest-attribute=noreturn')
d0069261a71665a965b26d208dee098ea629cbbeDave Reisnerflags.remove('-Wdate-time')
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner# vim: set et ts=2 sw=2: