forked from bazelbuild/rules_scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scala_config.bzl
97 lines (83 loc) · 3.73 KB
/
scala_config.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
load("//scala:scala_cross_version.bzl", "extract_major_version", "extract_minor_version", "version_suffix")
def _default_scala_version():
"""return the scala version for use in maven coordinates"""
return "2.12.19"
def _validate_supported_scala_version(scala_major_version, scala_minor_version):
if scala_major_version == "2.11" and int(scala_minor_version) != 12:
fail("Scala version must be 2.11.12 to use compiler dependency tracking with 2.11.")
if scala_major_version == "2.12" and int(scala_minor_version) < 1:
fail("Scala version must be newer or equal to 2.12.1 to use compiler dependency tracking.")
def _config_setting(scala_version):
return """config_setting(
name = "scala_version{version_suffix}",
flag_values = {{":scala_version": "{version}"}},
visibility = ["//visibility:public"],
)
""".format(version_suffix = version_suffix(scala_version), version = scala_version)
def _config_settings(scala_versions):
return "".join([_config_setting(v) for v in scala_versions])
def _store_config(repository_ctx):
# Default version
scala_version = repository_ctx.os.environ.get(
"SCALA_VERSION",
repository_ctx.attr.scala_version,
)
# All versions supported
scala_versions = repository_ctx.attr.scala_versions
if not scala_versions:
scala_versions = [scala_version]
elif scala_version not in scala_versions:
fail("You have to include the default Scala version (%s) in the `scala_versions` list." % scala_version)
enable_compiler_dependency_tracking = repository_ctx.os.environ.get(
"ENABLE_COMPILER_DEPENDENCY_TRACKING",
str(repository_ctx.attr.enable_compiler_dependency_tracking),
)
scala_major_version = extract_major_version(scala_version)
scala_minor_version = extract_minor_version(scala_version)
if enable_compiler_dependency_tracking == "True":
_validate_supported_scala_version(scala_major_version, scala_minor_version)
config_file_content = "\n".join([
"SCALA_VERSION='" + scala_version + "'",
"SCALA_VERSIONS=" + str(scala_versions),
"SCALA_MAJOR_VERSION='" + scala_major_version + "'",
"SCALA_MINOR_VERSION='" + scala_minor_version + "'",
"ENABLE_COMPILER_DEPENDENCY_TRACKING=" + enable_compiler_dependency_tracking,
])
build_file_content = """load("@bazel_skylib//rules:common_settings.bzl", "string_setting")
string_setting(
name = "scala_version",
build_setting_default = "{scala_version}",
values = {scala_versions},
visibility = ["//visibility:public"],
)
""".format(scala_versions = scala_versions, scala_version = scala_version)
build_file_content += _config_settings(scala_versions)
repository_ctx.file("config.bzl", config_file_content)
repository_ctx.file("BUILD", build_file_content)
_config_repository = repository_rule(
implementation = _store_config,
attrs = {
"scala_version": attr.string(
mandatory = True,
doc = "Default Scala version",
),
"scala_versions": attr.string_list(
mandatory = True,
doc = "List of all Scala versions to configure. Must include the default one.",
),
"enable_compiler_dependency_tracking": attr.bool(
mandatory = True,
),
},
environ = ["SCALA_VERSION", "ENABLE_COMPILER_DEPENDENCY_TRACKING"],
)
def scala_config(
scala_version = _default_scala_version(),
scala_versions = [],
enable_compiler_dependency_tracking = False):
_config_repository(
name = "io_bazel_rules_scala_config",
scala_version = scala_version,
scala_versions = scala_versions,
enable_compiler_dependency_tracking = enable_compiler_dependency_tracking,
)