-
Notifications
You must be signed in to change notification settings - Fork 3
Add rules_python toolchain to the rule #174
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
Changes from all commits
4ba7fc4
a5b167d
f45d8d6
000b554
ae8da57
960746c
cdd9086
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ load( | |
| ) | ||
| load( | ||
| "common.bzl", | ||
| "python_interpreter_tool", | ||
| "python_path", | ||
| "python_toolchain_type", | ||
| "version_specific_attributes", | ||
|
|
@@ -97,14 +98,17 @@ def _codechecker_impl(ctx): | |
| config_file, codechecker_env = get_config_file(ctx) | ||
|
|
||
| codechecker_files = ctx.actions.declare_directory(ctx.label.name + "/codechecker-files") | ||
| # For hermeticism the executable should be a python interpreter | ||
| # provided by Bazel. This interpreter cannot always be added to the script | ||
| # as a shebang. Because of this this script is explicitly marked as | ||
| # non executable. | ||
| ctx.actions.expand_template( | ||
| template = ctx.file._codechecker_script_template, | ||
| output = ctx.outputs.codechecker_script, | ||
| is_executable = True, | ||
| is_executable = False, | ||
Szelethus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| substitutions = { | ||
| "{Mode}": "Run", | ||
| "{Verbosity}": "DEBUG", | ||
| "{PythonPath}": python_path(ctx), # "/usr/bin/env python3", | ||
| "{codechecker_bin}": CODECHECKER_BIN_PATH, | ||
| "{compile_commands}": ctx.outputs.codechecker_commands.path, | ||
| "{codechecker_skipfile}": ctx.outputs.codechecker_skipfile.path, | ||
|
|
@@ -129,13 +133,12 @@ def _codechecker_impl(ctx): | |
| codechecker_files, | ||
| ctx.outputs.codechecker_log, | ||
| ], | ||
| executable = ctx.outputs.codechecker_script, | ||
| arguments = [], | ||
| # executable = python_path(ctx), | ||
| # arguments = [ctx.outputs.codechecker_script.path], | ||
| executable = python_path(ctx), | ||
| tools = python_interpreter_tool(ctx), | ||
| arguments = [ctx.outputs.codechecker_script.path], | ||
| mnemonic = "CodeChecker", | ||
| progress_message = "CodeChecker %s" % str(ctx.label), | ||
| # use_default_shell_env = True, | ||
| use_default_shell_env = True, | ||
| ) | ||
|
|
||
| # List all files required at build and run (test) time | ||
|
|
@@ -226,27 +229,57 @@ def _codechecker_test_impl(ctx): | |
| fail("Execution results required for codechecker test are not available") | ||
|
|
||
| # Create test script from template | ||
| # For hermeticism the executable should be a python interpreter | ||
| # provided by Bazel. This interpreter cannot always be added to the script | ||
| # as a shebang. Because of this this script is explicitly marked as | ||
| # non executable. | ||
| ctx.actions.expand_template( | ||
| template = ctx.file._codechecker_script_template, | ||
| output = ctx.outputs.codechecker_test_script, | ||
| is_executable = True, | ||
| is_executable = False, | ||
| substitutions = { | ||
| "{Mode}": "Test", | ||
| "{Verbosity}": "INFO", | ||
| "{PythonPath}": python_path(ctx), # "/usr/bin/env python3", | ||
| "{codechecker_bin}": CODECHECKER_BIN_PATH, | ||
| "{codechecker_files}": codechecker_files.short_path, | ||
| "{Severities}": " ".join(ctx.attr.severities), | ||
| }, | ||
| ) | ||
|
|
||
| # If we use our custom toolchain, the path will be an absolute path, | ||
| # executable by bazel | ||
| python_interpreter_path = python_path(ctx) | ||
|
|
||
| # If we can find an interpreter tool provided by bazel, | ||
| # use that as an executable | ||
| if python_interpreter_tool(ctx) != []: | ||
| python_interpreter_path = python_interpreter_tool(ctx)[0].short_path | ||
|
|
||
| # Since we cannot give parameters to the runfiles | ||
| # we wrap the executable (a python binary) in a script. | ||
| # In the script we give the CodeChecker script as an argument. | ||
| ctx.actions.write( | ||
| output = ctx.outputs.test_script_wrapper, | ||
| is_executable = True, | ||
| content = """ | ||
| {python_bin} {test_script} | ||
| """.format( | ||
| python_bin = python_interpreter_path, | ||
| test_script = ctx.outputs.codechecker_test_script.short_path, | ||
| ), | ||
| ) | ||
|
|
||
|
Comment on lines
+249
to
+271
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part is exactly why I dont like this change. |
||
| # Return test script and all required files | ||
| run_files = default_runfiles + [ctx.outputs.codechecker_test_script] | ||
| run_files = default_runfiles + [ | ||
| ctx.outputs.codechecker_test_script, | ||
| ctx.outputs.test_script_wrapper, | ||
| ] + python_interpreter_tool(ctx) | ||
|
Comment on lines
+261
to
+276
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nettle I understand you find this solution inelegant, and I won't contest it. However, would it be agreeable for you if we pushed this PR as-is, and found a nicer solution after the fact? This code is not the architectural backbone of the patch, so I think it is fine to improve it later.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the problem with current implementation?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We cannot use the Python binary provided by rules_python with the old implementation (shebangs).
I guess a nicer solution would be if the script were a py_binary target in the BUILD file.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I have tried out the py_binary solution in #199. |
||
|
|
||
| return [ | ||
| DefaultInfo( | ||
| files = depset(all_files), | ||
| runfiles = ctx.runfiles(files = run_files), | ||
| executable = ctx.outputs.codechecker_test_script, | ||
| executable = ctx.outputs.test_script_wrapper, | ||
| ), | ||
| ] | ||
|
|
||
|
|
@@ -300,6 +333,7 @@ _codechecker_test = rule( | |
| "codechecker_script": "%{name}/codechecker_script.py", | ||
| "codechecker_log": "%{name}/codechecker.log", | ||
| "codechecker_test_script": "%{name}/codechecker_test_script.py", | ||
| "test_script_wrapper": "%{name}/test_script_wrapper.sh", | ||
| }, | ||
| toolchains = [python_toolchain_type()], | ||
| test = True, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| #!{PythonPath} | ||
|
|
||
| # Copyright 2023 Ericsson AB | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| #!{PythonPath} | ||
|
|
||
| # Copyright 2023 Ericsson AB | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the story with these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried everything to leave the toolchain registration for the old toolchain in the MODULE file, and have rules_python have a higher priority, but I failed.
These options I have come across during that. (I left them in thinking they might be interesting)