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

Configure rules behaviour/toolchain attributes via build settings #1570

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
162 changes: 90 additions & 72 deletions scala/scala_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ load(
"SCALA_MAJOR_VERSION",
)
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@bazel_skylib//lib:dicts.bzl", _dicts = "dicts")

def _compute_strict_deps_mode(input_strict_deps_mode, dependency_mode):
if dependency_mode == "direct":
Expand Down Expand Up @@ -42,19 +43,82 @@ def _partition_patterns(patterns):
]
return includes, excludes

_config_attrs = {
"dependency_mode": attr.string(
default = "direct",
values = ["direct", "plus-one", "transitive"],
),
"strict_deps_mode": attr.string(
default = "default",
values = ["off", "warn", "error", "default"],
),
"unused_dependency_checker_mode": attr.string(
default = "off",
values = ["off", "warn", "error"],
),
"compiler_deps_mode": attr.string(
default = "off",
values = ["off", "warn", "error"],
),
"dependency_tracking_method": attr.string(
default = "default",
values = ["ast-plus", "ast", "high-level", "default"],
),
"dependency_tracking_strict_deps_patterns": attr.string_list(
doc = "List of target prefixes included for strict deps analysis. Exclude patterns with '-'",
default = [""],
),
"dependency_tracking_unused_deps_patterns": attr.string_list(
doc = "List of target prefixes included for unused deps analysis. Exclude patterns with '-'",
default = [""],
),
"enable_diagnostics_report": attr.bool(
doc = "Enable the output of structured diagnostics through the BEP",
),
"enable_stats_file": attr.bool(
default = True,
doc = "Enable writing of statsfile",
),
"enable_semanticdb": attr.bool(
default = False,
doc = "Enable SemanticDb",
),
"semanticdb_bundle_in_jar": attr.bool(default = False, doc = "Option to bundle the semanticdb files inside the output jar file"),
"use_argument_file_in_runner": attr.bool(
default = False,
doc = "Changes java binaries scripts (including tests) to use argument files and not classpath jars to improve performance, requires java > 8",
),
"scalac_jvm_flags": attr.string_list(),
"scala_test_jvm_flags": attr.string_list(),
}

_config_flags = {
"_" + k: attr.label(default = "//scala/settings:%s" % k)
for k in _config_attrs.keys()
}

def _config(ctx):
if ctx.attr._scala_toolchain_flags[BuildSettingInfo].value:
return struct(**{k: getattr(ctx.attr, "_" + k)[BuildSettingInfo].value for k in _config_attrs.keys()})
else:
return struct(**{k: getattr(ctx.attr, k) for k in _config_attrs.keys()})

def _scala_toolchain_impl(ctx):
dependency_mode = ctx.attr.dependency_mode
config = _config(ctx)

dependency_mode = config.dependency_mode

strict_deps_mode = _compute_strict_deps_mode(
ctx.attr.strict_deps_mode,
config.strict_deps_mode,
dependency_mode,
)

compiler_deps_mode = ctx.attr.compiler_deps_mode
compiler_deps_mode = config.compiler_deps_mode

unused_dependency_checker_mode = ctx.attr.unused_dependency_checker_mode
unused_dependency_checker_mode = config.unused_dependency_checker_mode
dependency_tracking_method = _compute_dependency_tracking_method(
dependency_mode,
ctx.attr.dependency_tracking_method,
config.dependency_tracking_method,
)

# Final quality checks to possibly detect buggy code above
Expand All @@ -73,13 +137,10 @@ def _scala_toolchain_impl(ctx):
if "ast-plus" == dependency_tracking_method and not ENABLE_COMPILER_DEPENDENCY_TRACKING:
fail("To use 'ast-plus' dependency tracking, you must set 'enable_compiler_dependency_tracking' to True in scala_config")

enable_stats_file = ctx.attr.enable_stats_file
enable_diagnostics_report = ctx.attr.enable_diagnostics_report

all_strict_deps_patterns = ctx.attr.dependency_tracking_strict_deps_patterns
all_strict_deps_patterns = config.dependency_tracking_strict_deps_patterns
strict_deps_includes, strict_deps_excludes = _partition_patterns(all_strict_deps_patterns)

all_unused_deps_patterns = ctx.attr.dependency_tracking_unused_deps_patterns
all_unused_deps_patterns = config.dependency_tracking_unused_deps_patterns
unused_deps_includes, unused_deps_excludes = _partition_patterns(all_unused_deps_patterns)

toolchain = platform_common.ToolchainInfo(
Expand All @@ -96,12 +157,12 @@ def _scala_toolchain_impl(ctx):
unused_deps_exclude_patterns = unused_deps_excludes,
scalac_jvm_flags = ctx.attr.scalac_jvm_flags,
scala_test_jvm_flags = ctx.attr.scala_test_jvm_flags,
enable_diagnostics_report = enable_diagnostics_report,
enable_diagnostics_report = config.enable_diagnostics_report,
jacocorunner = ctx.attr.jacocorunner,
enable_stats_file = enable_stats_file,
enable_semanticdb = ctx.attr.enable_semanticdb,
semanticdb_bundle_in_jar = ctx.attr.semanticdb_bundle_in_jar,
use_argument_file_in_runner = ctx.attr.use_argument_file_in_runner,
enable_stats_file = config.enable_stats_file,
enable_semanticdb = config.enable_semanticdb,
semanticdb_bundle_in_jar = config.semanticdb_bundle_in_jar,
use_argument_file_in_runner = config.use_argument_file_in_runner,
scala_version = ctx.attr._scala_version[BuildSettingInfo].value,
)
return [toolchain]
Expand All @@ -120,62 +181,19 @@ def _default_dep_providers():

scala_toolchain = rule(
_scala_toolchain_impl,
attrs = {
"scalacopts": attr.string_list(),
"dep_providers": attr.label_list(
default = _default_dep_providers(),
providers = [_DepsInfo],
),
"dependency_mode": attr.string(
default = "direct",
values = ["direct", "plus-one", "transitive"],
),
"strict_deps_mode": attr.string(
default = "default",
values = ["off", "warn", "error", "default"],
),
"unused_dependency_checker_mode": attr.string(
default = "off",
values = ["off", "warn", "error"],
),
"compiler_deps_mode": attr.string(
default = "off",
values = ["off", "warn", "error"],
),
"dependency_tracking_method": attr.string(
default = "default",
values = ["ast-plus", "ast", "high-level", "default"],
),
"dependency_tracking_strict_deps_patterns": attr.string_list(
doc = "List of target prefixes included for strict deps analysis. Exclude patterns with '-'",
default = [""],
),
"dependency_tracking_unused_deps_patterns": attr.string_list(
doc = "List of target prefixes included for unused deps analysis. Exclude patterns with '-'",
default = [""],
),
"scalac_jvm_flags": attr.string_list(),
"scala_test_jvm_flags": attr.string_list(),
"enable_diagnostics_report": attr.bool(
doc = "Enable the output of structured diagnostics through the BEP",
),
"jacocorunner": attr.label(
default = Label("@bazel_tools//tools/jdk:JacocoCoverage"),
),
"enable_stats_file": attr.bool(
default = True,
doc = "Enable writing of statsfile",
),
"enable_semanticdb": attr.bool(
default = False,
doc = "Enable SemanticDb",
),
"semanticdb_bundle_in_jar": attr.bool(default = False, doc = "Option to bundle the semanticdb files inside the output jar file"),
"use_argument_file_in_runner": attr.bool(
default = False,
doc = "Changes java binaries scripts (including tests) to use argument files and not classpath jars to improve performance, requires java > 8",
),
"_scala_version": attr.label(default = "@io_bazel_rules_scala_config//:scala_version"),
},
attrs = _dicts.add(
_config_attrs,
_config_flags,
{
"dep_providers": attr.label_list(
default = _default_dep_providers(),
providers = [_DepsInfo],
),
"jacocorunner": attr.label(default = Label("@bazel_tools//tools/jdk:JacocoCoverage")),
"scalacopts": attr.string_list(),
"_scala_version": attr.label(default = "@io_bazel_rules_scala_config//:scala_version"),
"_scala_toolchain_flags": attr.label(default = "//scala/settings:scala_toolchain_flags"),
},
),
fragments = ["java"],
)
106 changes: 105 additions & 1 deletion scala/settings/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,111 @@
load("//scala/settings:stamp_settings.bzl", "stamp_scala_import")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag", "string_list_flag")

package(default_visibility = ["//visibility:public"])

stamp_scala_import(
name = "stamp_scala_import",
build_setting_default = True,
visibility = ["//visibility:public"],
)

bool_flag(
name = "scala_toolchain_flags",
build_setting_default = False,
)

string_flag(
name = "dependency_mode",
build_setting_default = "direct",
values = [
"direct",
"plus-one",
"transitive",
],
)

string_flag(
name = "strict_deps_mode",
build_setting_default = "default",
values = [
"off",
"warn",
"error",
"default",
],
)

string_flag(
name = "unused_dependency_checker_mode",
build_setting_default = "off",
values = [
"off",
"warn",
"error",
],
)

string_flag(
name = "compiler_deps_mode",
build_setting_default = "off",
values = [
"off",
"warn",
"error",
],
)

string_flag(
name = "dependency_tracking_method",
build_setting_default = "default",
values = [
"ast-plus",
"ast",
"high-level",
"default",
],
)

string_list_flag(
name = "dependency_tracking_strict_deps_patterns",
build_setting_default = [],
)

string_list_flag(
name = "dependency_tracking_unused_deps_patterns",
build_setting_default = [],
)

bool_flag(
name = "enable_diagnostics_report",
build_setting_default = False,
)

bool_flag(
name = "enable_stats_file",
build_setting_default = True,
)

bool_flag(
name = "enable_semanticdb",
build_setting_default = False,
)

bool_flag(
name = "semanticdb_bundle_in_jar",
build_setting_default = False,
)

bool_flag(
name = "use_argument_file_in_runner",
build_setting_default = False,
)

string_list_flag(
name = "scalac_jvm_flags",
build_setting_default = [],
)

string_list_flag(
name = "scala_test_jvm_flags",
build_setting_default = [],
)