Skip to content

Conversation

@furtib
Copy link
Contributor

@furtib furtib commented Oct 21, 2025

Why:
Currently, we use a different, inferior implementation for generating compile_commands.json in the pre_file rule.
We should use the same as in the monolithic rule. This would prevent us from having to maintain both rules simultaneously.

What:
Replaces the compile_commands.json generating implementation in the pre_file rule.
Fixes the unittest for compile_commands, the per_file compile_commands.json location changed

Addresses:
Fixes: #120
Fixes: #128

@furtib furtib requested review from Szelethus and nettle October 21, 2025 11:41
@furtib furtib self-assigned this Oct 21, 2025
@furtib furtib added enhancement New feature or request non-functional change ☮️ The patch doesn't change any functionality, e.g. refactoring, documentation, test-only. labels Oct 21, 2025
@furtib furtib force-pushed the unify-compile_commands branch from ff788bf to c1b559c Compare November 24, 2025 12:34
@furtib
Copy link
Contributor Author

furtib commented Nov 24, 2025

Fails because #99 does not affect the "monolithic" compile command generator implementation. should rebase after merging #100

@furtib furtib force-pushed the unify-compile_commands branch from c1b559c to fea15a0 Compare November 24, 2025 13:56
Copy link
Contributor

@Szelethus Szelethus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you delete all unnecessary functions, just to see how much simpler this solution would be?

@furtib furtib force-pushed the unify-compile_commands branch from fea15a0 to 65034d3 Compare January 10, 2026 22:12
@furtib
Copy link
Contributor Author

furtib commented Jan 10, 2026

This patch might deserve a bit more love.
The file collection still happens through the aspect written inside the per file rule.
We might want to use the solution from compile_commands.bzl there too, the fix I have implemented this has been removed (we concentrated on the monolithic rule for the time being, the patch was: #127), but I think that could have placed here.

@furtib
Copy link
Contributor Author

furtib commented Jan 12, 2026

Using the compile_commands.bzl aspect for file collection, there are some things to keep in mind. Mainly, despite the name transitive_source_files suggests only having source files, it actually can contain header files (this might be because it collects the files under the srcs=[] part of the cc_lib/bin rules)

@furtib furtib force-pushed the unify-compile_commands branch from 7827b46 to cba2a86 Compare January 12, 2026 07:32
Copy link
Contributor

@Szelethus Szelethus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just like that, and everything runs? Damn.

Comment on lines +148 to +163
if not check_valid_file_type(src):
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When does this happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly, despite the name transitive_source_files suggests only having source files, it actually can contain header files (this might be because it collects the files under the srcs=[] part of the cc_lib/bin rules)

@furtib furtib force-pushed the unify-compile_commands branch from 3fb354f to 936ed44 Compare January 12, 2026 11:39
@furtib furtib force-pushed the unify-compile_commands branch from 936ed44 to 4ce7518 Compare January 12, 2026 11:46
@furtib furtib force-pushed the unify-compile_commands branch from 4ce7518 to e4b2503 Compare January 12, 2026 12:09
Copy link
Contributor

@Szelethus Szelethus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to understand this a bit better before merging...

Comment on lines +119 to +123
for elem in headers_mid:
if type(elem) == "depset":
headers.extend(elem.to_list())
else:
headers.append(elem)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to do this for the monolithic version? If not, why not?

Comment on lines +60 to +64
trans_depsets = [compilation_context.headers]
for dset in target[SourceFilesInfo].headers.to_list():
trans_depsets.append(dset)
headers = depset(direct = [src],
transitive = trans_depsets)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets turn this into a function.

Comment on lines -61 to -73
# Get compile_commands.json file and source files
compile_commands = None
source_files = None
for output in compile_commands_impl(ctx):
if type(output) == "DefaultInfo":
compile_commands = output.files.to_list()[0]
source_files = output.default_runfiles.files.to_list()
if not compile_commands:
fail("Failed to generate compile_commands.json file!")
if not source_files:
fail("Failed to collect source files!")
if compile_commands != ctx.outputs.compile_commands:
fail("Seems compile_commands.json file is incorrect!")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer not touching this for compatibility sake

trans_depsets = [compilation_context.headers]
for dset in target[SourceFilesInfo].headers.to_list():
trans_depsets.append(dset)
headers = depset(direct = [src],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand this line...
So, this is not just headers but src as well?

# NOTE: we collect only headers, so CTU may not work!
headers = depset([src], transitive = [compilation_context.headers])
trans_depsets = [compilation_context.headers]
for dset in target[SourceFilesInfo].headers.to_list():
Copy link
Collaborator

@nettle nettle Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess dest is actually a header :)

),
]

def get_compile_commands_json_and_srcs(ctx):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm mistaken, at least I do not see that yet, but I guess adding this function does not give us much benefits:
As I mentioned I would prefer original implementation in src/codechecker.bzl.
And in src/per_file.bzl we even do not need this function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is literally copy-pasted from codechecker.bzl, so this is above and beyond NFC. Since the output of compile_commands_impl needs the same handling both in the per-file and monolithic implementations, I think this is very sensible refactoring step.

In terms of merging order, sure, we can land the creating of this convenience function before or after replacing the compile commands generation parts.

Copy link
Collaborator

@nettle nettle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I suggest changing only src/per_file.bzl implementation first, which seems reasonable to me and not touching src/codechecker.bzl and src/compile_commands.bzl at least yet.

@Szelethus
Copy link
Contributor

May I suggest changing only src/per_file.bzl implementation first, which seems reasonable to me and not touching src/codechecker.bzl and src/compile_commands.bzl at least yet.

It is a more than reasonable ask to land parts of this patch independently, but I'm strongly against not touching those two files to keep them stable, especially with NFC patches.

Copy link
Contributor

@Szelethus Szelethus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meant to send this inline comment yesterday.

),
]

def get_compile_commands_json_and_srcs(ctx):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is literally copy-pasted from codechecker.bzl, so this is above and beyond NFC. Since the output of compile_commands_impl needs the same handling both in the per-file and monolithic implementations, I think this is very sensible refactoring step.

In terms of merging order, sure, we can land the creating of this convenience function before or after replacing the compile commands generation parts.

Comment on lines +145 to +146
compile_commands_json, source_files = \
get_compile_commands_json_and_srcs(ctx)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following can be used to get compile_commands_json:

for output in compile_commands_impl(ctx):
    if type(output) == "DefaultInfo":
        compile_commands_json = output.files.to_list()[0]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request non-functional change ☮️ The patch doesn't change any functionality, e.g. refactoring, documentation, test-only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing header files from external repositories during analysis Missing header files during analysis

3 participants