Last active
September 25, 2020 05:23
-
-
Save XVilka/97d35f18ef8b829e0c351cbb0b0e2035 to your computer and use it in GitHub Desktop.
Simple Python script to search the files by pattern in the active GCC include paths.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# pylint: disable=missing-docstring | |
# pylint: disable=invalid-name | |
"""GCC Includes Search Script | |
This script allows to search the files among all paths that GCC currently set in the includes. | |
It is helpful for debugging problems with the old files and finding conflicts between multiple headers | |
of different versions of the same library. | |
""" | |
import os | |
import os.path | |
import argparse | |
import subprocess | |
import locale | |
import pathlib | |
# Force the UTF-8 locale here | |
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') | |
# For now works only for GCC | |
def get_gcc_include_paths(): | |
startline = "#include <...> search starts here:" | |
cmdline = ["cpp", "-v", "/dev/null", "-o", "/dev/null"] | |
p = subprocess.Popen(cmdline, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
_, err = p.communicate() | |
# Interesting output is in stderr: | |
lines = err.decode('utf-8').split('\n') | |
includes = [] | |
if startline in lines: | |
start = lines.index(startline) | |
end = len(lines) - start | |
for i in range(1, end): | |
line = lines[start + i].lstrip() | |
if os.path.exists(os.path.dirname(line)): | |
includes.append(pathlib.Path(line)) | |
return includes | |
def include_search(directories, names): | |
matches = [] | |
for includedir in directories: | |
for name in names: | |
for path in includedir.rglob(name + '*.h'): | |
matches.append(path) | |
return matches | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Search among compiler include paths") | |
parser.add_argument("-n", "--names", nargs="+", help="Names to search") | |
args = parser.parse_args() | |
if args.names is not None: | |
includedirs = get_gcc_include_paths() | |
print(includedirs) | |
matches = include_search(includedirs, args.names) | |
if matches: | |
print("Found {0:d} matches".format(len(matches))) | |
for m in matches: | |
print(m) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment