Skip to content
Open
Show file tree
Hide file tree
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
13 changes: 11 additions & 2 deletions src/clang.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,14 @@ def _run_analyzer(

def check_valid_file_type(src):
"""
Checks if the file is a cpp related file.

Returns True if the file type matches one of the permitted
srcs file types for C and C++ header/source files.
Args:
src: Path of a single source file.
Returns:
Boolean value.
"""
permitted_file_types = [
".c",
Expand Down Expand Up @@ -326,6 +332,8 @@ CompileInfo = provider(
},
)

# buildifier: disable=unused-variable
# The headers variable is used: headers += dep[CompileInfo].headers.to_list()
def _process_all_deps(ctx, arguments, headers):
for attr in SOURCE_ATTR:
if hasattr(ctx.rule.attr, attr):
Expand Down Expand Up @@ -391,7 +399,7 @@ compile_info_aspect = aspect(

def _clang_test(ctx, tool):
all_files = []
all_headers = depset()
all_headers_list = []
for target in ctx.attr.targets:
if CompileInfo in target:
if hasattr(target[CompileInfo], "arguments"):
Expand All @@ -411,8 +419,9 @@ def _clang_test(ctx, tool):
ctx.attr.name,
)
all_files.append(report)
all_headers = depset(transitive = [all_headers, headers])
all_headers_list.extend(headers.to_list())

all_headers = depset(all_headers_list)
ctx.actions.write(
output = ctx.outputs.test_script,
is_executable = True,
Expand Down
20 changes: 12 additions & 8 deletions src/clang_ctu.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Ruleset for running the clang static analyzer with CTU analysis.

load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load("common.bzl", "SOURCE_ATTR", "version_specific_attributes")
load("common.bzl", "SOURCE_ATTR")

CLANG_CTU_WRAPPER_SCRIPT = """#!/usr/bin/env bash
#set -x
Expand Down Expand Up @@ -132,8 +132,14 @@ def _run_clang_ctu(

def check_valid_file_type(src):
"""
Checks if the file is a cpp related file.

Returns True if the file type matches one of the permitted
srcs file types for C and C++ header/source files.
Args:
src: Path of a single source file.
Returns:
Boolean value.
"""
permitted_file_types = [
".c",
Expand All @@ -158,8 +164,8 @@ def check_valid_file_type(src):
def _rule_sources(ctx):
srcs = []
if hasattr(ctx.rule.attr, "srcs"):
for src in ctx.rule.attr.srcs:
srcs += [src for src in src.files.to_list() if src.is_source and check_valid_file_type(src)]
for src_outer in ctx.rule.attr.srcs:
srcs += [src for src in src_outer.files.to_list() if src.is_source and check_valid_file_type(src)]
return srcs

def _toolchain_flags(ctx, action_name, with_compiler = False):
Expand Down Expand Up @@ -324,7 +330,7 @@ def _generate_ast_and_def_files(ctx, target, all_sources):

def _collect_all_sources_and_headers(ctx):
all_files = []
headers = depset()
headers_list = []
for target in ctx.attr.targets:
if not CcInfo in target:
continue
Expand All @@ -333,10 +339,8 @@ def _collect_all_sources_and_headers(ctx):
srcs = target[CompileInfo].arguments.keys()
all_files += srcs
compilation_context = target[CcInfo].compilation_context
headers = depset(
transitive = [headers, compilation_context.headers],
)
sources_and_headers = all_files + headers.to_list()
headers_list.extend(compilation_context.headers.to_list())
sources_and_headers = all_files + headers_list
return sources_and_headers

def _clang_ctu_impl(ctx):
Expand Down
42 changes: 38 additions & 4 deletions src/codechecker.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def get_platform_alias(platform):
"""
Get platform alias for full platform names being used

Args:
platform: A string containing the platform
Returns:
string: If the full platform name is consistent with
valid syntax, returns the short alias to represent it.
Expand Down Expand Up @@ -190,7 +192,7 @@ codechecker = rule(
"_compile_commands_filter": attr.label(
allow_files = True,
executable = True,
cfg = "host",
cfg = "exec",
default = ":compile_commands_filter",
),
"_codechecker_script_template": attr.label(
Expand Down Expand Up @@ -267,7 +269,7 @@ _codechecker_test = rule(
"_compile_commands_filter": attr.label(
allow_files = True,
executable = True,
cfg = "host",
cfg = "exec",
default = ":compile_commands_filter",
),
"_codechecker_script_template": attr.label(
Expand Down Expand Up @@ -316,7 +318,24 @@ def codechecker_test(
tags = [],
per_file = False,
**kwargs):
""" Bazel test to run CodeChecker """
"""
Macro to choose the appropriate codechecker rule

Args:
name: Name of the target invoking CodeChecker.
targets: List of targets to be analyzed by CodeChecker.
platform: Platform to be analyzed on.
severities: List of warning levels that should be considered failing.
skip: Skip patterns for CodeChecker.
config: Config target for CodeChecker.
analyze: List of analyze command arguments.
tags: Bazel tags
per_file: Boolean value, toggles wether to run the analysis
with the experimental per_file rule.
**kwargs: Other miscellaneous arguments.
Returns:
none
"""
codechecker_tags = [] + tags
if "codechecker" not in tags:
codechecker_tags.append("codechecker")
Expand Down Expand Up @@ -352,7 +371,22 @@ def codechecker_suite(
analyze = [],
tags = [],
**kwargs):
""" Bazel test suite to run CodeChecker for different platforms """
"""
Bazel test suite to run CodeChecker for different platforms

Args:
name: Name of the target invoking CodeChecker.
targets: List of targets to be analyzed by CodeChecker.
platforms: List of platform to be analyzed on.
severities: List of warning levels that should be considered failing.
skip: Skip patterns for CodeChecker.
config: Config target for CodeChecker.
analyze: List of analyze command arguments.
tags: Bazel tags
**kwargs: Other miscellaneous arguments.
Returns:
none
"""
tests = []
for platform in platforms:
shortname = get_platform_alias(platform)
Expand Down
15 changes: 13 additions & 2 deletions src/codechecker_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ Ruleset for configuring codechecker.

def _get_config_file_name(ctx):
"""
Get the name of the config file.

Returns the name of the config file to be used, with correct extension
If no config file is given, we write the configuration options into a
config.json file.
Args:
ctx: The context variable
Returns:
Path of the config file
"""
if ctx.attr.config:
if type(ctx.attr.config) == "list":
Expand All @@ -37,8 +43,13 @@ def _get_config_file_name(ctx):

def get_config_file(ctx):
"""
Returns (config_file, environment_variables)
config_file is a file object that is readable during Codechecker execution
Create config file for analysis

Args:
ctx: The context variable
Returns:
A tuple: (config_file, environment_variables)
config_file is the file object to be read by Codechecker
"""

# Declare config file to use during analysis
Expand Down
8 changes: 7 additions & 1 deletion src/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ def python_toolchain_type():

def python_path(ctx):
"""
Returns version specific Python path
Function to obtain a python executable path

Args:
ctx: The context variable
Returns:
version specific Python path
"""
py_toolchain = ctx.toolchains[python_toolchain_type()]
if hasattr(py_toolchain, "py3_runtime_info"):
Expand All @@ -74,4 +79,5 @@ def warning(ctx, msg):
NOTE: "debug" in tags works only for rules, not aspects
"""
if hasattr(ctx.attr, "tags") and "debug" in ctx.attr.tags:
# buildifier: disable=print
print(msg)
27 changes: 26 additions & 1 deletion src/compile_commands.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ QUOTE_INCLUDE = "-iquote "
def get_compile_flags(ctx, dep):
""" Return a list of compile options

Args:
ctx: The context variable.
dep: A target with CcInfo.
Returns:
List of compile options.
"""
Expand Down Expand Up @@ -137,6 +140,8 @@ def get_compile_flags(ctx, dep):
def get_sources(ctx):
""" Return a list of source files

Args:
ctx: The context variable.
Returns:
List of source files.
"""
Expand Down Expand Up @@ -211,6 +216,9 @@ def _cc_compiler_info(ctx, target, src, feature_configuration, cc_toolchain):
def get_compilation_database(target, ctx):
""" Return a "compilation database" or "compile commands" ready to create a JSON file

Args:
ctx: The context variable.
target: Target to create the compilation database for.
Returns:
List of struct(file, command, directory).
"""
Expand Down Expand Up @@ -262,6 +270,9 @@ def get_compilation_database(target, ctx):
def collect_headers(target, ctx):
""" Return list of required header files

Args:
ctx: The context variable.
target: Target which headers should be collected
Returns:
depset of header files
"""
Expand Down Expand Up @@ -375,6 +386,8 @@ def _compile_commands_json(compilation_db):
def compile_commands_impl(ctx):
""" Creates compile_commands.json file for given targets and platform

Args:
ctx: The context variable.
Returns:
DefaultInfo(
files, # as compile_commands.json
Expand Down Expand Up @@ -448,7 +461,18 @@ def compile_commands(
platform = "", #"@platforms//os:linux",
tags = [],
**kwargs):
""" Bazel rule to generate compile_commands.json file """
"""
Bazel rule to generate compile_commands.json file

Args:
name: Name of the target.
targets: List of targets to generate compile_commands.json for.
platform: Platform to consider during compile database creation.
tags: Bazel tags
**kwargs: Other miscellaneous arguments.
Returns:
None
"""
compile_commands_tags = [] + tags
if "compile_commands" not in tags:
compile_commands_tags.append("compile_commands")
Expand All @@ -457,4 +481,5 @@ def compile_commands(
platform = platform,
targets = targets,
tags = compile_commands_tags,
**kwargs
)
10 changes: 6 additions & 4 deletions src/per_file.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ Rulesets for running codechecker in a different Bazel action
for each translation unit.
"""

load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load("codechecker_config.bzl", "get_config_file")
load("common.bzl", "SOURCE_ATTR")
load(
"compile_commands.bzl",
"SourceFilesInfo",
"compile_commands_aspect",
"compile_commands_impl",
"platforms_transition",
)

def _run_code_checker(
Expand Down Expand Up @@ -84,8 +80,14 @@ def _run_code_checker(

def check_valid_file_type(src):
"""
Checks if the file is a cpp related file.

Returns True if the file type matches one of the permitted
srcs file types for C and C++ source files.
Args:
src: Path of a single source file.
Returns:
Boolean value.
"""
permitted_file_types = [
".c",
Expand Down
4 changes: 4 additions & 0 deletions src/tools.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ default_python_tools = repository_rule(
implementation = _python_local_repository_impl,
)

# buildifier: disable=unused-variable
# This parameter is provided, regardless if we use it or not
def register_default_python_toolchain(ctx = None):
default_python_tools(name = "default_python_tools")

Expand Down Expand Up @@ -120,6 +122,8 @@ default_codechecker_tools = repository_rule(
implementation = _codechecker_local_repository_impl,
)

# buildifier: disable=unused-variable
# This parameter is provided, regardless if we use it or not
def register_default_codechecker(ctx = None):
default_codechecker_tools(name = "default_codechecker_tools")

Expand Down