Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added comments for better understanding #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions generate_compdb.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3

# Import necessary libraries and modules
from __future__ import print_function, division

import argparse
import fnmatch
import functools
Expand All @@ -12,31 +12,31 @@
import re
import sys


# Regular expressions to extract command and source variables
CMD_VAR_RE = re.compile(r'^\s*(?:saved)?cmd_(\S+)\s*:=\s*(.+)\s*$', re.MULTILINE)
SOURCE_VAR_RE = re.compile(r'^\s*source_(\S+)\s*:=\s*(.+)\s*$', re.MULTILINE)


# Function to print a progress bar
def print_progress_bar(progress):
progress_bar = '[' + '|' * int(50 * progress) + '-' * int(50 * (1.0 - progress)) + ']'
print('\r', progress_bar, "{0:.1%}".format(progress), end='\r', file=sys.stderr)


# Function to parse a command file and extract compile commands
def parse_cmd_file(out_dir, cmdfile_path):
with open(cmdfile_path, 'r') as cmdfile:
cmdfile_content = cmdfile.read()

commands = { match.group(1): match.group(2) for match in CMD_VAR_RE.finditer(cmdfile_content) }
sources = { match.group(1): match.group(2) for match in SOURCE_VAR_RE.finditer(cmdfile_content) }
commands = {match.group(1): match.group(2) for match in CMD_VAR_RE.finditer(cmdfile_content)}
sources = {match.group(1): match.group(2) for match in SOURCE_VAR_RE.finditer(cmdfile_content)}

return [{
'directory': out_dir,
'command': commands[o_file_name],
'file': source,
'output': o_file_name
} for o_file_name, source in sources.items()]

'directory': out_dir,
'command': commands[o_file_name],
'file': source,
'output': o_file_name
} for o_file_name, source in sources.items()]

# Function to generate compile_commands.json from *.o.cmd files
def gen_compile_commands(cmd_file_search_path, out_dir):
print("Building *.o.cmd file list...", file=sys.stderr)

Expand All @@ -47,7 +47,7 @@ def gen_compile_commands(cmd_file_search_path, out_dir):

cmd_files = []
for search_path in cmd_file_search_path:
if (os.path.isdir(search_path)):
if os.path.isdir(search_path):
for cur_dir, subdir, files in os.walk(search_path):
cmd_files.extend(os.path.join(cur_dir, cmdfile_name) for cmdfile_name in fnmatch.filter(files, '*.o.cmd'))
else:
Expand Down Expand Up @@ -80,13 +80,12 @@ def gen_compile_commands(cmd_file_search_path, out_dir):
with open('compile_commands.json', 'w') as compdb_file:
json.dump(compdb, compdb_file, indent=1)


# Main function to parse command line arguments and generate compile_commands.json
def main():
cmd_parser = argparse.ArgumentParser()
cmd_parser.add_argument('-O', '--out-dir', type=str, default=os.getcwd(), help="Build output directory")
cmd_parser.add_argument('cmd_file_search_path', nargs='*', help="*.cmd file search path")
gen_compile_commands(**vars(cmd_parser.parse_args()))


if __name__ == '__main__':
main()