Skip to content

Instantly share code, notes, and snippets.

@marler8997
Created February 16, 2024 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marler8997/7195db030d44df9d1888a2b45b49e605 to your computer and use it in GitHub Desktop.
Save marler8997/7195db030d44df9d1888a2b45b49e605 to your computer and use it in GitHub Desktop.
C Get Defines
#!/usr/bin/env python3
import sys
import os
import re
import argparse
import subprocess
def scan_path(defines, path):
for entry_base in os.listdir(path):
entry = os.path.join(path, entry_base)
if os.path.isfile(entry):
if entry.endswith(".ninja"):
with open(entry, "r") as f:
content = f.read()
matches = re.findall(r"-D([^ \n]+)", content)
for match in matches:
#print(f"File '{entry}': {match}")
defines.add(match)
elif os.path.isdir(entry):
scan_path(defines, entry)
else:
sys.exit(f"todo: handle non-file/directory '{entry}'")
def find(path, needle):
for entry_base in os.listdir(path):
entry = os.path.join(path, entry_base)
if os.path.isfile(entry):
if entry.endswith(".h") or entry.endswith(".hpp"):
with open(entry, "rb") as f:
content = f.read()
if needle in content:
return True
elif os.path.isdir(entry):
if find(entry, needle):
return True
else:
sys.exit(f"todo: handle non-file/directory '{entry}'")
return False
def is_in_headers(define, src_path):
args = ["rg", "--max-count", "1", "--count", "-g", "*.h*", define, src_path]
print(subprocess.list2cmdline(args))
result = subprocess.run(args)
return result.returncode == 0
#return find(src_path, define)
def get_name(define):
parts = re.match(r"([^=]*)=", define)
if parts:
return parts[1]
return define
def main():
parser = argparse.ArgumentParser()
parser.add_argument("build_path", action="store")
parser.add_argument("src_path", action="store")
cmdline_args = parser.parse_args()
build_path = cmdline_args.build_path
src_path = cmdline_args.src_path
defines = set()
scan_path(defines, build_path)
filtered = set()
for define in defines:
name = get_name(define)
# print(f"Checking {name}")
sys.stdout.flush()
if is_in_headers(name, src_path):
filtered.add(name)
print("--------------------------------------------------------------------------------")
for name in filtered:
print(name)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment