Skip to content

Commit

Permalink
Build rules_kotlin using rules_kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
restingbull committed Dec 1, 2023
1 parent 66c9b48 commit 357051c
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 124 deletions.
76 changes: 63 additions & 13 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
module(
name = "rules_kotlin",
version = "1.8.1",
compatibility_level = 1,
repo_name = "rules_kotlin",
version = "1.9.0",
)

bazel_dep(name = "platforms", version = "0.0.6")
bazel_dep(name = "bazel_skylib", version = "1.4.2")
bazel_dep(name = "rules_java", version = "6.4.0")
bazel_dep(name = "rules_python", version = "0.23.1")
bazel_dep(name = "rules_cc", version = "0.0.8")
bazel_dep(
name = "platforms",
version = "0.0.6",
)

bazel_dep(
name = "bazel_skylib",
version = "1.4.2",
)

bazel_dep(
name = "rules_java",
version = "6.4.0",
)

bazel_dep(
name = "rules_python",
version = "0.23.1",
)

bazel_dep(
name = "rules_cc",
version = "0.0.8",
)

rules_kotlin_extensions = use_extension(
"//src/main/starlark/core/repositories:bzlmod_setup.bzl",
"rules_kotlin_extensions",
dev_dependency = True,
)

rules_kotlin_extensions = use_extension("//src/main/starlark/core/repositories:bzlmod_setup.bzl", "rules_kotlin_extensions")
use_repo(
rules_kotlin_extensions,
"buildkite_config",
Expand All @@ -20,21 +43,35 @@ use_repo(
"com_github_pinterest_ktlint",
"kt_java_stub_template",
"rules_android",
"released_rules_kotlin",
)

# Register toolchain for bootstrapping the current rules.
register_toolchains("@released_rules_kotlin//kotlin/internal:default_toolchain")

register_toolchains("//kotlin/internal:default_toolchain")

# TODO(bencodes) We should be able to remove this once rules_android has rolled out official Bzlmod support
remote_android_extensions = use_extension("@bazel_tools//tools/android:android_extensions.bzl", "remote_android_tools_extensions")

use_repo(remote_android_extensions, "android_gmaven_r8", "android_tools")

# Development dependencies
# TODO(bencodes) A bunch of these dependencies need to be marked as dev_dependencies but before we can do that
# we need to sort out a few cases around how these rules are consumed in various ways.

bazel_dep(name = "rules_jvm_external", version = "5.3")
bazel_dep(
name = "rules_jvm_external",
dev_dependency = True,
version = "5.3",
)

maven = use_extension(
"@rules_jvm_external//:extensions.bzl",
"maven",
dev_dependency = True,
)

maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
maven.install(
name = "kotlin_rules_maven",
artifacts = [
Expand Down Expand Up @@ -64,8 +101,21 @@ maven.install(
"https://repo1.maven.org/maven2",
],
)

use_repo(maven, "kotlin_rules_maven")

bazel_dep(name = "rules_pkg", version = "0.7.0")
bazel_dep(name = "stardoc", version = "0.5.6", repo_name = "io_bazel_stardoc")
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")
bazel_dep(
name = "rules_pkg",
version = "0.7.0",
)

bazel_dep(
name = "stardoc",
repo_name = "io_bazel_stardoc",
version = "0.5.6",
)

bazel_dep(
name = "rules_proto",
version = "5.3.0-21.7",
)
116 changes: 9 additions & 107 deletions src/main/kotlin/bootstrap.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
load("@released_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("@rules_java//java:defs.bzl", "java_binary", "java_import")
load("//kotlin:jvm.bzl", _for_ide = "kt_jvm_library")
load("//kotlin:lint.bzl", _ktlint_fix = "ktlint_fix", _ktlint_test = "ktlint_test")
load("//third_party:jarjar.bzl", "jar_jar")

_BOOTSTRAP_LIB_ARGS = ["-jvm-target", "1.8"]

def _resolve_dep_label(d):
if d.startswith("///src/main/kotlin/io/bazel/kotlin") and not d.endswith("_for_ide"):
_, _, target = d.rpartition(":")
if target == None:
# untested
return d + "_for_ide"
else:
_, _, target = d.rpartition("/")
return d + ":" + target + "_for_ide"
else:
return d

def kt_bootstrap_library(name, srcs, visibility = [], deps = [], neverlink_deps = [], runtime_deps = []):
def kt_bootstrap_library(name, deps = [], neverlink_deps = [], **kwargs):
"""
Simple compilation of a kotlin library using a non-persistent worker. The target is a JavaInfo provider.
Expand All @@ -41,101 +28,16 @@ def kt_bootstrap_library(name, srcs, visibility = [], deps = [], neverlink_deps
deps: the dependenices, the are setup as runtime_deps of the library.
neverlink_deps: deps that won't be linked. These deps are added to the `"for_ide"` target.
"""
jar_label = name + "_jar"
dep_label = name + "_deps"
native.filegroup(
name = dep_label,
srcs = deps + neverlink_deps,
tags = ["no-ide"],
visibility = ["//visibility:private"],
kt_jvm_library(
name = "%s_neverlink" % name,
exports = neverlink_deps,
neverlink = True,
)
command = """
function join_by { local IFS="$$1"; shift; echo "$$*"; }
case "$$(uname -s)" in
CYGWIN*|MINGW32*|MSYS*)
SEP=";"
;;
*)
SEP=":"
;;
esac
NAME=%s
CP="%s"
ARGS="%s"
CMD="$(JAVA) -Xmx256M -Xms32M -noverify \
-cp $(location //kotlin/compiler:kotlin-preloader) org.jetbrains.kotlin.preloading.Preloader \
-cp $(location //kotlin/compiler:kotlin-compiler) org.jetbrains.kotlin.cli.jvm.K2JVMCompiler \
$$CP -d $(@D)/$${NAME}_temp.jar $${ARGS} $(SRCS)"
$$CMD
case "$(location @bazel_tools//tools/jdk:singlejar)" in
*.jar)
SJ="$(JAVA) -jar $(location @bazel_tools//tools/jdk:singlejar)"
;;
*)
SJ="$(location @bazel_tools//tools/jdk:singlejar)"
;;
esac

$$SJ \
--normalize \
--compression \
--sources $(@D)/$${NAME}_temp.jar \
--output $(OUTS)
rm $(@D)/$${NAME}_temp.jar
""" % (name, "-cp $$(join_by $$SEP $(locations :%s)) " % dep_label if deps + neverlink_deps else "", " ".join(_BOOTSTRAP_LIB_ARGS))
native.genrule(
name = jar_label,
tools = [
"@com_github_jetbrains_kotlin//:home",
"//kotlin/compiler:kotlin-preloader",
"//kotlin/compiler:kotlin-compiler",
"@bazel_tools//tools/jdk:singlejar",
dep_label,
],
srcs = srcs,
outs = [name + ".jar"],
tags = ["no-ide"],
visibility = ["//visibility:private"],
toolchains = [
"@bazel_tools//tools/jdk:current_host_java_runtime",
],
cmd = command,
)
java_import(
kt_jvm_library(
name = name,
jars = [jar_label],
tags = ["no-ide"],
runtime_deps = deps + runtime_deps,
visibility = visibility,
)

# hsyed todo this part of the graph should not be wired up outside of development.
_for_ide(
name = name + "_for_ide",
srcs = srcs,
neverlink = 1,
deps = [_resolve_dep_label(d) for d in deps] + neverlink_deps,
visibility = ["//visibility:private"],
)

_ktlint_test(
name = "%s_ktlint_test" % name,
srcs = srcs,
visibility = ["//visibility:private"],
config = "//:ktlint_editorconfig",
tags = ["no-ide", "ktlint"],
)

_ktlint_fix(
name = "%s_ktlint_fix" % name,
srcs = srcs,
visibility = ["//visibility:private"],
config = "//:ktlint_editorconfig",
tags = ["no-ide", "ktlint"],
deps = deps + ["%s_neverlink" % name],
**kwargs
)

def kt_bootstrap_binary(
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/io/bazel/kotlin/builder/cmd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ kt_bootstrap_binary(
"//kotlin/compiler:symbol-processing-cmdline",
"//src/main/kotlin:jdeps-gen",
"//src/main/kotlin:skip-code-gen",
"//src/main/kotlin/io/bazel/kotlin/compiler",
"//src/main/kotlin/io/bazel/kotlin/compiler:compiler.jar",
"@com_github_jetbrains_kotlin//:home",
],
jvm_flags = [
Expand All @@ -31,7 +31,7 @@ kt_bootstrap_binary(
"-D@com_github_jetbrains_kotlin...kapt=$(rlocationpath //kotlin/compiler:kotlin-annotation-processing)",
"-D@rules_kotlin...jdeps-gen=$(rlocationpath //src/main/kotlin:jdeps-gen)",
"-D@rules_kotlin...skip-code-gen=$(rlocationpath //src/main/kotlin:skip-code-gen)",
"-D@rules_kotlin...compiler=$(rlocationpath //src/main/kotlin/io/bazel/kotlin/compiler)",
"-D@rules_kotlin...compiler=$(rlocationpath //src/main/kotlin/io/bazel/kotlin/compiler:compiler.jar)",
"-D@com_github_google_ksp...symbol-processing-api=$(rlocationpath //kotlin/compiler:symbol-processing-api)",
"-D@com_github_google_ksp...symbol-processing-cmdline=$(rlocationpath //kotlin/compiler:symbol-processing-cmdline)",
"-XX:-MaxFDLimit",
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/io/bazel/kotlin/compiler/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ release_archive(
name = "pkg",
package_dir = "io/bazel/kotlin/compiler",
src_map = {
":compiler": "compiler.jar",
":compiler.jar": "compiler.jar",
"BUILD.release.bazel": "BUILD.bazel",
},
)
10 changes: 10 additions & 0 deletions src/main/starlark/core/repositories/initialize.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
"""This file contains the Kotlin compiler repository definitions. It should not be loaded directly by client workspaces.
"""

load(
"@bazel_tools//tools/build_defs/repo:http.bzl",
"http_archive",
)
load(
":initialize.release.bzl",
_kotlinc_version = "kotlinc_version",
Expand All @@ -40,6 +44,12 @@ def kotlin_repositories(
"""
_release_kotlin_repositories(is_bzlmod = is_bzlmod, compiler_release = compiler_release, ksp_compiler_release = ksp_compiler_release)

versions.use_repository(
name = "released_rules_kotlin",
rule = http_archive,
version = versions.RULES_KOTLIN,
)

# When Bzlmod is enabled skip over the toolchain setup entirely
if is_bzlmod:
return
Expand Down
8 changes: 8 additions & 0 deletions src/main/starlark/core/repositories/versions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ versions = struct(
sha256 = "8a298e832762eda1830597d64fe7db58178aa84cd5926d76d5b744d6558941c2",
),
# needed for rules_pkg and java
RULES_KOTLIN = version(
version = "1.9.0",
url_templates = [
"https://github.com/bazelbuild/rules_kotlin/releases/download/v{version}/rules_kotlin-v{version}.tar.gz",
],
sha256 = "5766f1e599acf551aa56f49dab9ab9108269b03c557496c54acaf41f98e2b8d6",
),
# needed for rules_pkg and java
RULES_PYTHON = version(
version = "0.23.1",
strip_prefix_template = "rules_python-{version}",
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/io/bazel/kotlin/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def kt_rules_test(name, **kwargs):

args["deps"] = args.setdefault("deps", []) + ["//src/test/kotlin/io/bazel/kotlin/builder:test_lib"]
for dep in [
"//src/main/kotlin/io/bazel/kotlin/compiler",
"//src/main/kotlin/io/bazel/kotlin/compiler:compiler.jar",
"//src/main/kotlin:skip-code-gen",
"//src/main/kotlin:jdeps-gen",
"//kotlin/compiler:symbol-processing-api",
Expand Down

0 comments on commit 357051c

Please sign in to comment.