Kotlin: add aliases for default versions

This commit is contained in:
Paolo Tranquilli
2024-04-04 15:34:48 +02:00
parent 55ff7109fa
commit 5d6baea174
5 changed files with 80 additions and 2 deletions

View File

@@ -61,6 +61,7 @@ kotlin_extractor_deps = use_extension("//java/kotlin-extractor:deps.bzl", "kotli
# following list can be kept in sync by running `bazel mod tidy` in `codeql`
use_repo(
kotlin_extractor_deps,
"codeql_kotlin_defaults",
"codeql_kotlin_embeddable",
"kotlin-compiler-1.4.32",
"kotlin-compiler-1.5.0",

View File

@@ -8,6 +8,7 @@ load(
)
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("@rules_kotlin//kotlin:core.bzl", "kt_kotlinc_options")
load("@codeql_kotlin_defaults//:defaults.bzl", "kotlin_extractor_defaults")
_for_embeddable = repo_name().endswith("codeql_kotlin_embeddable")
@@ -114,3 +115,41 @@ _resources = [
)
for v in VERSIONS
]
# default aliases, based on the kotlinc version installed on the host
# * default version can be overridden with env variableCODEQL_KOTLIN_SINGLE_VERSION
# * setting CODEQL_KOTLIN_SINGLE_VERSION_EMBEDDABLE=true overrides the default variant
# * when a new kotlinc version is installed, you'll need to either `bazel clean` or
# `bazel fetch --force @codeql_kotlin_defaults//:all` to refresh the default
(
alias(
name = "%s-standalone" % _common_extractor_name_prefix,
actual = "%s-standalone-%s" % (
_common_extractor_name_prefix,
kotlin_extractor_defaults.version,
),
visibility = ["//visibility:public"],
),
alias(
name = "%s-embeddable" % _common_extractor_name_prefix,
actual = "%s-embeddable-%s" % (
_common_extractor_name_prefix,
kotlin_extractor_defaults.version,
),
visibility = ["//visibility:public"],
),
alias(
name = _common_extractor_name_prefix,
actual = "%s-%s-%s" % (
_common_extractor_name_prefix,
kotlin_extractor_defaults.variant,
kotlin_extractor_defaults.version,
),
visibility = ["//visibility:public"],
),
alias(
name = "kotlin-extractor",
actual = _common_extractor_name_prefix,
visibility = ["//visibility:public"],
),
) if not _for_embeddable else None

View File

@@ -1,4 +1,4 @@
load("//java/kotlin-extractor:versions.bzl", "VERSIONS")
load("//java/kotlin-extractor:versions.bzl", "DEFAULT_FALLBACK_VERSION", "VERSIONS", "version_less")
load("//misc/bazel:lfs.bzl", "lfs_smudge")
_kotlin_dep_build = """
@@ -70,6 +70,41 @@ def _embeddable_source_impl(repository_ctx):
_embeddable_source = repository_rule(implementation = _embeddable_source_impl)
def _get_default_version(repository_ctx):
default_version = repository_ctx.getenv("CODEQL_KOTLIN_SINGLE_VERSION")
if default_version:
if default_version not in VERSIONS:
fail("overriding CODEQL_KOTLIN_SINGLE_VERSION=%s not known, must be one of:\n %s" %
(default_version, " ".join(VERSIONS)))
return default_version
kotlinc = repository_ctx.which("kotlinc")
if not kotlinc:
return DEFAULT_FALLBACK_VERSION
res = repository_ctx.execute([kotlinc, "-version"])
if not res:
fail("kotlinc -version failed: %s" % res.stderr)
out = res.stderr.split(" ")
if len(out) < 3:
fail("malformed kotlinc -version output: %s" % res.stdout)
host_version = out[2]
for version in reversed(VERSIONS):
if version_less(version, host_version) or version == host_version:
return version
fail("no relevant version found for host version %s among:\n %s" % (host_version, " ".join(VERSIONS)))
def _defaults_impl(repository_ctx):
default_version = _get_default_version(repository_ctx)
default_variant = "standalone"
if repository_ctx.getenv("CODEQL_KOTLIN_SINGLE_VERSION_EMBEDDABLE") in ("true", "1"):
default_variant = "embeddable"
repository_ctx.file(
"defaults.bzl",
"kotlin_extractor_defaults = struct(version = '%s', variant = '%s')\n" % (default_version, default_variant),
)
repository_ctx.file("BUILD.bazel")
_defaults = repository_rule(implementation = _defaults_impl)
def _add_rule(rules, rule, *, name, **kwargs):
rule(name = name, **kwargs)
rules.append(name)
@@ -80,6 +115,7 @@ def _kotlin_deps_impl(module_ctx):
for lib in ("compiler", "compiler-embeddable", "stdlib"):
_add_rule(deps, _kotlin_dep, name = "kotlin-%s-%s" % (lib, v))
_add_rule(deps, _embeddable_source, name = "codeql_kotlin_embeddable")
_add_rule(deps, _defaults, name = "codeql_kotlin_defaults")
return module_ctx.extension_metadata(
root_module_direct_deps = deps,
root_module_direct_dev_deps = [],

View File

@@ -62,6 +62,7 @@ def get_single_version(fakeVersionOutput = None):
raise KotlincNotFoundException()
versionOutput = subprocess.run([kotlinc, '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).stderr if fakeVersionOutput is None else fakeVersionOutput
m = re.match(r'.* kotlinc-jvm ([0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z][a-zA-Z0-9]*)?) .*', versionOutput)
if m is None:
if m is None:
raise Exception('Cannot detect version of kotlinc (got ' + str(versionOutput) + ')')
current_version = version_string_to_version(m.group(1))
@@ -87,4 +88,3 @@ if __name__ == "__main__":
print(get_single_version(*args[2:]))
else:
raise Exception("Unknown command: " + command)

View File

@@ -16,6 +16,8 @@ VERSIONS = [
"2.0.255-SNAPSHOT",
]
DEFAULT_FALLBACK_VERSION = "1.9.0-Beta"
def _version_to_tuple(v):
v, _, tail = v.partition("-")
v = tuple([int(x) for x in v.split(".")])