gen_compile_command: Add support for separate KBUILD_OUTPUT directory
PATCH v2 1/1 gen_compile_command: Add support for separate KBUILD_OUTPUT directory
gen_compile_command.py currently assumes that the .cmd files and the source code live in the same directory, which is not the case when a separate KBUILD_OUTPUT directory is used.
Add a new option to specify this the kbuild output directory. If the option is not set the kernel source directory is used.
---
Changes in v2:
- added trailing space to help strings
- added tags from Tom and Nick
--- scripts/gen_compile_commands.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)
scripts/gen_compile_commands.py +19 -9
@@ -31,15 +31,21 @@ def parse_arguments(): Returns: log_level: A logging level to filter log output. - directory: The directory to search for .cmd files. + source_directory: The kernel source directory. + kbuild_output_directory: The directory to search for .cmd files. output: Where to write the compile-commands JSON file. """ usage = 'Creates a compile_commands.json database from kernel .cmd files' parser = argparse.ArgumentParser(description=usage) - directory_help = ('Path to the kernel source directory to search ' + directory_help = ('Path to the kernel source directory ' '(defaults to the working directory)') parser.add_argument('-d', '--directory', type=str, help=directory_help) + kbuild_output_directory_help = ('Path to the directory to search for ' + '.cmd files ' + '(defaults to the kernel source directory)') + parser.add_argument('-k', '--kbuild-output-directory', type=str, + help=kbuild_output_directory_help) output_help = ('The location to write compile_commands.json (defaults to ' 'compile_commands.json in the search directory)')
@@ -58,11 +64,14 @@ def parse_arguments(): if log_level not in _VALID_LOG_LEVELS: raise ValueError('%s is not a valid log level' % log_level) - directory = args.directory or os.getcwd() - output = args.output or os.path.join(directory, _DEFAULT_OUTPUT) - directory = os.path.abspath(directory) + source_directory = args.directory or os.getcwd() + output = args.output or os.path.join(source_directory, _DEFAULT_OUTPUT) + source_directory = os.path.abspath(source_directory) - return log_level, directory, output + kbuild_output_directory = args.kbuild_output_directory or source_directory + kbuild_output_directory = os.path.abspath(kbuild_output_directory) + + return log_level, source_directory, kbuild_output_directory, output def process_line(root_directory, file_directory, command_prefix, relative_path):
@@ -109,7 +118,8 @@ def process_line(root_directory, file_directory, command_prefix, relative_path): def main(): """Walks through the directory and finds and parses .cmd files.""" - log_level, directory, output = parse_arguments() + log_level, source_directory, kbuild_output_directory, output = \ + parse_arguments() level = getattr(logging, log_level) logging.basicConfig(format='%(levelname)s: %(message)s', level=level)
@@ -118,7 +128,7 @@ def main(): line_matcher = re.compile(_LINE_PATTERN) compile_commands = [] - for dirpath, _, filenames in os.walk(directory): + for dirpath, _, filenames in os.walk(kbuild_output_directory): for filename in filenames: if not filename_matcher.match(filename): continue
@@ -131,7 +141,7 @@ def main(): continue try: - entry = process_line(directory, dirpath, + entry = process_line(source_directory, dirpath, result.group(1), result.group(2)) compile_commands.append(entry) except ValueError as err:
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
Quoted 7 lines · Matthias Kaehlcke
gen_compile_command.pycurrently assumes that the .cmd files and the source code live in the same directory, which is not the case when a separateKBUILD_OUTPUTdirectory is used.Add a new option to specify this the kbuild output directory. If the option is not set the kernel source directory is used.
I do not understand this patch.
In my understanding, this tool already provides -d, --directory option, which is supposed to point to the kbuild output directory, not a source directory.
This works except under tools/.
scripts/gen_compile_commands.py def main(): · line 145
Quoted 3 lines
scripts/gen_compile_commands.py +1
-1- entry = process_line(directory, dirpath, + entry = process_line(source_directory, dirpath, result.group(1), result.group(2))
This is wrong.
result.group(2) returns the relative path to the output tree instead of the source tree.
(or absolute path).
[Example of breakage]
$ make -j8 O=foo defconfig all $ scripts/gen_compile_commands.py -d . -k foo WARNING: Found 0 entries. Have you compiled the kernel?
Signature · 2 lines
Best Regards Masahiro Yamada
Quoted 6 lines · Masahiro Yamada
Quoted 5 lines · Matthias Kaehlcke
gen_compile_command.pycurrently assumes that the .cmd files and the source code live in the same directory, which is not the case when a separateKBUILD_OUTPUTdirectory is used.
Quoted 8 lines
Quoted 2 lines
Add a new option to specify this the kbuild output directory. If the option is not set the kernel source directory is used.
I do not understand this patch.
In my understanding, this tool already provides -d, --directory option, which is supposed to point to the kbuild output directory, not a source directory.
You are right specifying the output path with -d works.
The help string claims the directory passed to -d is the kernel *source* directory though:
-d DIRECTORY, --directory DIRECTORY
Path to the kernel source directory to search (defaults to the working directory)
I recall getting plenty of errors of files not being found (possibly after adding debug logs). Chrome OS builds the kernel inside a chroot, I guess I ran the script from outside the chroot and got the errors because the (chroot) file paths in the .cmd files don't exist outside the chroot, but drew wrong conclusions.
Please disregard this patch.
Matthias