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

[tools] Add compilation database generator for Bazel #3226

Merged
merged 1 commit into from
Apr 6, 2021

Conversation

nettle
Copy link
Contributor

@nettle nettle commented Mar 12, 2021

The tool takes bazel build command as an input parameter
and executes Bazel Action Query (bazel aquery)
to extract all compile commands then saves them
to compile_commands.json file.

@nettle nettle requested a review from csordasmarton as a code owner March 12, 2021 13:43
@@ -0,0 +1,255 @@
#!/usr/bin/env python3
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better to rename this module and the upper directory to bazel_to_compile_commands. It is more expressive than just compile_commands.

Also it would be good to put this script into the bin folder during the package phase similar to this:

codechecker/Makefile

Lines 49 to 57 in e02baa7

build_tu_collector:
$(MAKE) -C $(ROOT)/tools/tu_collector build
package_tu_collector: build_tu_collector package_dir_structure
# Copy tu_collector files.
cp -rp $(CC_TOOLS)/tu_collector/build/tu_collector/tu_collector $(CC_BUILD_LIB_DIR) && \
chmod u+x $(CC_BUILD_LIB_DIR)/tu_collector/tu_collector.py && \
cd $(CC_BUILD_DIR) && \
ln -sf ../lib/python3/tu_collector/tu_collector.py bin/tu_collector

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for reviewing @csordasmarton!
I'll check and update the patch as soon as I have time.

Meanwhile I have a few questions:

  1. Regarding the module name I think bazel_to_compile_commands has kind of wrong meaning, it is not a converter.
    Probably bazel_compile_commands would be better. Agree?
  2. I put this module into bazel folder, is it OK?
  3. How can find information about verification infrastructure you use?
    Are there any guidelines? Check list?

Copy link
Contributor

Choose a reason for hiding this comment

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

@nettle

  1. Yes, you are right, bazel_compile_commands will be good.
  2. You can put this into the bazel folder (tools/bazel/bazel_compile_commands/bazel_compile_commands.py)
  3. No, there isn't any guidline for this. This is how we built our others tools (https://github.com/Ericsson/codechecker/tree/master/tools, https://github.com/Ericsson/codechecker/tree/master/analyzer/tools). And it is good to be consistent in the whole repository.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done:

  • Renamed to bazel_compile_commands
  • Using bazel folder
  • Added copying to bin folder

tools/bazel/tests/Makefile Show resolved Hide resolved
long_description = fh.read()

setuptools.setup(
name="bazel-compile-commands",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
name="bazel-compile-commands",
name="bazel-to-compile-commands",

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 36 to 39
parser.add_argument("-v", "--verbosity",
default=0,
action="count",
help="increase output verbosity (e.g., -v or -vv)")
Copy link
Contributor

Choose a reason for hiding this comment

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

In other commands this option has no value, it will enabled the debug logging. Maybe we can use the same logic here too:

parser.add_argument('-v', '--verbose',
action='store_true',
dest='verbose',
help="Enable debug level logging.")
args = parser.parse_args()
# --- Checking the existence of input files. --- #
if 'verbose' in args and args.verbose:
LOG.setLevel(logging.DEBUG)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done:

  • Verbosity option (-v or --verbose) works the same way as in tu_collector

make venv
source $PWD/venv/bin/activate

# Build and install plist-to-html package.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# Build and install plist-to-html package.
# Build and install bazel-to-compile-commands package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 211 to 212

bazel_info = split_to_list(build_command)[0] + " info "
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
bazel_info = split_to_list(build_command)[0] + " info "
cmd = shlex.split(build_command)
bazel_info = cmd[0] + " info "

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I agree that we can use shlex.split however split_to_list is more convenient and safer.
I would keep it if you dont mind.

logging.info("Bazel work dir: %s", bazel_work_dir)

logging.info("Bazel build options: %s", build_command)
data = bazel_aquery(build_command)
Copy link
Contributor

Choose a reason for hiding this comment

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

Use the cmd here, so this function will expect a list instead of a string. And there will be no need to call the split function on the argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I agree that we can use shlex.split however split_to_list is more convenient and safer.
I would keep it if you dont mind.

"""
Run shell command
"""
cmd = split_to_list(command)
Copy link
Contributor

Choose a reason for hiding this comment

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

On the call side pass the command as a list of string and there will be no need to split the argument here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I agree that we can use shlex.split however split_to_list is more convenient and safer.
I would keep it if you dont mind.

Comment on lines 115 to 119
if out:
data = json.loads(out)
return data
else:
return False
Copy link
Contributor

Choose a reason for hiding this comment

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

Now the function may return with None (see the code above L101), a dictionary or a Boolean (False) value. Couldn't we return here with None if the output is empty?

Suggested change
if out:
data = json.loads(out)
return data
else:
return False
if out:
return json.loads(out)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


def get_full_path(file, directories):
"""
Return full path for a file and try to prepend directories
Copy link
Contributor

Choose a reason for hiding this comment

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

This function will return not only with the full path but the parent diectory too. It would be better to return only with the full path or None and use the os.path.dirname to get the parent directory for the file if its not None on the call side.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done:

  • Changed the name and docstring of the function since get_full_path is meisleading

@csordasmarton csordasmarton added this to the release 6.16.0 milestone Mar 16, 2021
@csordasmarton csordasmarton added the tools 🛠️ Meta-tag for all the additional tools supplied with CodeChecker: plist2html, tu_collector, etc. label Mar 16, 2021
@csordasmarton
Copy link
Contributor

Please also fix the commit message: [tols] -> [tools]

@nettle nettle force-pushed the bazel-compile-commands branch 2 times, most recently from 6223890 to 22a168b Compare March 25, 2021 09:03
@nettle nettle changed the title [tols] Add compilation database generator for Bazel [tools] Add compilation database generator for Bazel Mar 25, 2021
@nettle nettle force-pushed the bazel-compile-commands branch from 22a168b to 54ecd04 Compare March 25, 2021 11:43
@nettle nettle requested a review from bruntib as a code owner March 25, 2021 11:43
@nettle nettle force-pushed the bazel-compile-commands branch 2 times, most recently from d91df39 to 84e4174 Compare March 25, 2021 18:14
@nettle
Copy link
Contributor Author

nettle commented Mar 26, 2021

Hi @csordasmarton,

I've updated the patch upon comments.
Hopefully now it has all required infrastructural parts :)
Please note that a few test cases I've added are just a placeholder and more tests will be added later.
The patch is also missing some functional improvements which I plan to add later.
I guess it is important to move consecutively, so I consider this patch as earlier version of compile commands generator for Bazel, it will be evolving.

Copy link
Contributor

@csordasmarton csordasmarton left a comment

Choose a reason for hiding this comment

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

I have some small comments otherwise its LGTM!

stop=1

# do not capture stdout
nocapture=1
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
nocapture=1
#nocapture=1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

parser.add_argument("-b", "--build",
help="bazel build command arguments")
parser.add_argument("-v", "--verbosity",
default=0,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the default log level should be 1 because if I run this command there will be no output at all and it can confuse the users: did this command run successfully or not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK
Done

@nettle nettle force-pushed the bazel-compile-commands branch from 84e4174 to e981664 Compare April 6, 2021 09:48
The tool takes bazel build command as an input parameter
and executes Bazel Action Query (bazel aquery)
to extract all compile commands then saves them
to compile_commands.json file.
@nettle nettle force-pushed the bazel-compile-commands branch from e981664 to 3a3a928 Compare April 6, 2021 10:10
@nettle
Copy link
Contributor Author

nettle commented Apr 6, 2021

Hi @csordasmarton,
The patch is updated, please take a look!

Copy link
Contributor

@csordasmarton csordasmarton left a comment

Choose a reason for hiding this comment

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

It's LGTM! Thank you for your time that you implemented it 😊

Do you plan to add anything to this PR or can we merge it?

@nettle
Copy link
Contributor Author

nettle commented Apr 6, 2021

Thanks for review @csordasmarton!
I think this patch is good to go now.
I will prepare some changes and submit them later as a new PR, if you dont mind.

@csordasmarton csordasmarton added documentation 📖 Changes to documentation. test ☑️ Adding or refactoring tests labels Apr 6, 2021
@csordasmarton csordasmarton merged commit a628f5e into Ericsson:master Apr 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation 📖 Changes to documentation. test ☑️ Adding or refactoring tests tools 🛠️ Meta-tag for all the additional tools supplied with CodeChecker: plist2html, tu_collector, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants