Skip to content

Commit 9c180ac

Browse files
restingbulloliviernotteghem
authored andcommitted
Build rules_kotlin using rules_kotlin (bazelbuild#1081)
* Build rules_kotlin using rules_kotlin * Fix non-bzlmod, format * Add reflect for js. * Remove unused variable * remove dev dependency arguments, clean cache * Restore lint, remove hacks * Fix module formatting * Fix ktlint * Compose classpath issue -- is it the preloader? * toolchain with reflect needed? * Ensure the bootstrap repos are configured * isolated bootstrap * Remove rogue file * Apply formatting * Add release MODULE, clean up unused imports * Fix lexical order * Learning portable sed * The journy of sed continues * variations. * pipes are not my friend * pipes are not my friend * to perl! * Adjust rbe java settings * Fix format * Disable bazel 6 android, as the rules_java are not compatible * downgrade rules java * Set java toolchains * fix lint error * buildifier, again * [ci] Attempt to prebuild and override bazel 6 example * Remove unused in .bazelrc * clean up presubmit * Fix module formatting * Remove clean from CI * Fix module formatting * Remove lock * remove rules_cc and buildkite * Restore buildkite * Restore rules_cc * Use bzlmod with ubuntu * Revert "Use bzlmod with ubuntu" This reverts commit 339a9c0.
1 parent 4dea295 commit 9c180ac

File tree

24 files changed

+236
-153
lines changed

24 files changed

+236
-153
lines changed

.bazelci/presubmit.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ tasks:
3131
build_targets:
3232
- "//:rules_kotlin_release"
3333
rbe_ubuntu1604:
34+
build_flags:
35+
- "--enable_bzlmod=false"
36+
- "--config=rbe"
37+
- "--host_java_toolchain=@bazel_tools//tools/jdk:toolchain_java11"
38+
- "--java_toolchain=@bazel_tools//tools/jdk:toolchain_java11"
3439
test_targets:
3540
- "--"
3641
- "//src/test/kotlin/io/bazel/kotlin/builder:builder_tests"
@@ -42,6 +47,7 @@ tasks:
4247
# Override the default worker strategy for remote builds (worker strategy
4348
# cannot be used with remote builds)
4449
- "--strategy=KotlinCompile=remote"
50+
- "--config=rbe"
4551
# TODO: Enable these tests once this example is building against the release rules_kotlin binary
4652
# example-android-5.x:
4753
# name: "Example - Android using Bazel 5.x"

.bazelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
common --enable_bzlmod=true
22

3+
common:rbe --java_runtime_version=11
4+
common:rbe --tool_java_runtime_version=11
5+
36
build --strategy=KotlinCompile=worker
47
build --test_output=all
58
build --verbose_failures

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
**/.DS_Store
77
trace.profile.gz
88
.idea
9+
MODULE.bazel.lock

BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ test_suite(
6161
release_archive(
6262
name = "rules_kotlin_release",
6363
srcs = [
64-
"MODULE.bazel",
6564
"WORKSPACE.bzlmod",
6665
],
6766
src_map = {
6867
"BUILD.release.bazel": "BUILD.bazel",
6968
"WORKSPACE.release.bazel": "WORKSPACE",
69+
"MODULE.release.bazel": "MODULE.bazel",
7070
},
7171
deps = [
7272
"//kotlin:pkg",

MODULE.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,25 @@ use_repo(
1818
"com_github_google_ksp",
1919
"com_github_jetbrains_kotlin",
2020
"com_github_pinterest_ktlint",
21+
"released_rules_kotlin",
2122
"rules_android",
2223
)
2324

25+
# Once the released rules_koltin is defined, configure it.
26+
rules_kotlin_bootstrap_extensions = use_extension(
27+
"//src/main/starlark/core/repositories:bzlmod_bootstrap.bzl",
28+
"rules_kotlin_bootstrap_extensions",
29+
)
30+
use_repo(
31+
rules_kotlin_bootstrap_extensions,
32+
"kt_java_stub_template",
33+
"released_com_github_google_ksp",
34+
"released_com_github_jetbrains_kotlin",
35+
)
36+
37+
register_toolchains("@released_rules_kotlin//kotlin/internal:default_toolchain")
38+
39+
# Back to the regularly scheduled configuration.
2440
register_toolchains("//kotlin/internal:default_toolchain")
2541

2642
# TODO(bencodes) We should be able to remove this once rules_android has rolled out official Bzlmod support

MODULE.release.bazel

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module(
2+
name = "rules_kotlin",
3+
version = "1.9.0",
4+
repo_name = "rules_kotlin",
5+
)
6+
7+
bazel_dep(name = "platforms", version = "0.0.6")
8+
bazel_dep(name = "bazel_skylib", version = "1.4.2")
9+
bazel_dep(name = "rules_java", version = "7.2.0")
10+
bazel_dep(name = "rules_python", version = "0.23.1")
11+
bazel_dep(name = "rules_cc", version = "0.0.8")
12+
13+
rules_kotlin_extensions = use_extension(
14+
"//src/main/starlark/core/repositories:bzlmod_setup.bzl",
15+
"rules_kotlin_extensions",
16+
)
17+
use_repo(
18+
rules_kotlin_extensions,
19+
"buildkite_config",
20+
"com_github_google_ksp",
21+
"com_github_jetbrains_kotlin",
22+
"com_github_pinterest_ktlint",
23+
"rules_android",
24+
)
25+
26+
register_toolchains("//kotlin/internal:default_toolchain")
27+
28+
# TODO(bencodes) We should be able to remove this once rules_android has rolled out official Bzlmod support
29+
remote_android_extensions = use_extension("@bazel_tools//tools/android:android_extensions.bzl", "remote_android_tools_extensions")
30+
use_repo(remote_android_extensions, "android_gmaven_r8", "android_tools")
31+
32+
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")

scripts/reflow_skylark

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818

1919
buildifier --warnings=-confusing-name,-constant-glob,-duplicated-name,-function-docstring,-function-docstring-args,-function-docstring-header,-module-docstring,-name-conventions,-no-effect,-constant-glob,-provider-params,-print,-rule-impl-return,-bzl-visibility,-unnamed-macro,-uninitialized,-unreachable -lint warn $(find . -type f \
2020
-iname "*.bzl" -or \
21-
-name "*.bazel" -or \
21+
-name "*.bazel" -and -not -name "MODULE.*" -or \
2222
-name "WORKSPACE"
2323
)

src/main/kotlin/BUILD.release.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ java_binary(
4242
"//kotlin/compiler:jvm-abi-gen",
4343
"//kotlin/compiler:kotlin-annotation-processing",
4444
"//kotlin/compiler:kotlin-compiler",
45+
"//kotlin/compiler:kotlin-reflect",
4546
"//kotlin/compiler:symbol-processing-api",
4647
"//kotlin/compiler:symbol-processing-cmdline",
4748
"//src/main/kotlin/io/bazel/kotlin/compiler",
@@ -56,6 +57,7 @@ java_binary(
5657
"-D@rules_kotlin...compiler=$(rlocationpath //src/main/kotlin/io/bazel/kotlin/compiler)",
5758
"-D@com_github_google_ksp...symbol-processing-api=$(rlocationpath //kotlin/compiler:symbol-processing-api)",
5859
"-D@com_github_google_ksp...symbol-processing-cmdline=$(rlocationpath //kotlin/compiler:symbol-processing-cmdline)",
60+
"-D@rules_kotlin..kotlin.compiler.kotlin-reflect=$(rlocationpath //kotlin/compiler:kotlin-reflect)",
5961
"-XX:-MaxFDLimit",
6062
],
6163
main_class = "io.bazel.kotlin.builder.cmd.Build",

src/main/kotlin/bootstrap.bzl

Lines changed: 13 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -11,115 +11,30 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
load("@rules_java//java:defs.bzl", "java_binary", "java_import")
15-
load("//kotlin:jvm.bzl", _for_ide = "kt_jvm_library")
14+
load("@released_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
15+
load("@rules_java//java:defs.bzl", "java_binary")
1616
load("//kotlin:lint.bzl", _ktlint_fix = "ktlint_fix", _ktlint_test = "ktlint_test")
1717
load("//third_party:jarjar.bzl", "jar_jar")
1818

19-
_BOOTSTRAP_LIB_ARGS = ["-jvm-target", "1.8"]
20-
21-
def _resolve_dep_label(d):
22-
if d.startswith("///src/main/kotlin/io/bazel/kotlin") and not d.endswith("_for_ide"):
23-
_, _, target = d.rpartition(":")
24-
if target == None:
25-
# untested
26-
return d + "_for_ide"
27-
else:
28-
_, _, target = d.rpartition("/")
29-
return d + ":" + target + "_for_ide"
30-
else:
31-
return d
32-
33-
def kt_bootstrap_library(name, srcs, visibility = [], deps = [], neverlink_deps = [], runtime_deps = []):
19+
def kt_bootstrap_library(name, deps = [], neverlink_deps = [], srcs = [], visibility = [], **kwargs):
3420
"""
3521
Simple compilation of a kotlin library using a non-persistent worker. The target is a JavaInfo provider.
3622
37-
The target is tagged `"no-ide"` as intellij can't compile it. A seperate private target is created which is suffixed
38-
with `_for_ide`. If the dep is under the package `//src/main/kotlin/io/bazel/kotlin/builder/...` then it will be
39-
added to the `_for_ide` target by adding a `_for_ide` prefix.
40-
4123
deps: the dependenices, the are setup as runtime_deps of the library.
42-
neverlink_deps: deps that won't be linked. These deps are added to the `"for_ide"` target.
24+
neverlink_deps: deps that won't be linked.
4325
"""
44-
jar_label = name + "_jar"
45-
dep_label = name + "_deps"
46-
native.filegroup(
47-
name = dep_label,
48-
srcs = deps + neverlink_deps,
49-
tags = ["no-ide"],
50-
visibility = ["//visibility:private"],
26+
kt_jvm_library(
27+
name = "%s_neverlink" % name,
28+
exports = neverlink_deps,
29+
neverlink = True,
5130
)
52-
command = """
53-
function join_by { local IFS="$$1"; shift; echo "$$*"; }
54-
case "$$(uname -s)" in
55-
CYGWIN*|MINGW32*|MSYS*)
56-
SEP=";"
57-
;;
58-
*)
59-
SEP=":"
60-
;;
61-
esac
62-
NAME=%s
63-
CP="%s"
64-
ARGS="%s"
65-
66-
CMD="$(JAVA) -Xmx256M -Xms32M -noverify \
67-
-cp $(location //kotlin/compiler:kotlin-preloader) org.jetbrains.kotlin.preloading.Preloader \
68-
-cp $(location //kotlin/compiler:kotlin-compiler) org.jetbrains.kotlin.cli.jvm.K2JVMCompiler \
69-
$$CP -d $(@D)/$${NAME}_temp.jar $${ARGS} $(SRCS)"
7031

71-
$$CMD
72-
73-
case "$(location @bazel_tools//tools/jdk:singlejar)" in
74-
*.jar)
75-
SJ="$(JAVA) -jar $(location @bazel_tools//tools/jdk:singlejar)"
76-
;;
77-
*)
78-
SJ="$(location @bazel_tools//tools/jdk:singlejar)"
79-
;;
80-
esac
81-
82-
$$SJ \
83-
--normalize \
84-
--compression \
85-
--sources $(@D)/$${NAME}_temp.jar \
86-
--output $(OUTS)
87-
88-
rm $(@D)/$${NAME}_temp.jar
89-
""" % (name, "-cp $$(join_by $$SEP $(locations :%s)) " % dep_label if deps + neverlink_deps else "", " ".join(_BOOTSTRAP_LIB_ARGS))
90-
native.genrule(
91-
name = jar_label,
92-
tools = [
93-
"@com_github_jetbrains_kotlin//:home",
94-
"//kotlin/compiler:kotlin-preloader",
95-
"//kotlin/compiler:kotlin-compiler",
96-
"@bazel_tools//tools/jdk:singlejar",
97-
dep_label,
98-
],
99-
srcs = srcs,
100-
outs = [name + ".jar"],
101-
tags = ["no-ide"],
102-
visibility = ["//visibility:private"],
103-
toolchains = [
104-
"@bazel_tools//tools/jdk:current_host_java_runtime",
105-
],
106-
cmd = command,
107-
)
108-
java_import(
32+
kt_jvm_library(
10933
name = name,
110-
jars = [jar_label],
111-
tags = ["no-ide"],
112-
runtime_deps = deps + runtime_deps,
113-
visibility = visibility,
114-
)
115-
116-
# hsyed todo this part of the graph should not be wired up outside of development.
117-
_for_ide(
118-
name = name + "_for_ide",
11934
srcs = srcs,
120-
neverlink = 1,
121-
deps = [_resolve_dep_label(d) for d in deps] + neverlink_deps,
122-
visibility = ["//visibility:private"],
35+
visibility = visibility,
36+
deps = deps + ["%s_neverlink" % name],
37+
**kwargs
12338
)
12439

12540
_ktlint_test(
@@ -136,6 +51,7 @@ rm $(@D)/$${NAME}_temp.jar
13651
visibility = ["//visibility:private"],
13752
config = "//:ktlint_editorconfig",
13853
tags = ["no-ide", "ktlint"],
54+
**kwargs
13955
)
14056

14157
def kt_bootstrap_binary(

src/main/kotlin/io/bazel/kotlin/builder/cmd/BUILD.bazel

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ kt_bootstrap_binary(
1818
"//kotlin/compiler:jvm-abi-gen",
1919
"//kotlin/compiler:kotlin-annotation-processing",
2020
"//kotlin/compiler:kotlin-compiler",
21+
"//kotlin/compiler:kotlin-reflect",
2122
"//kotlin/compiler:symbol-processing-api",
2223
"//kotlin/compiler:symbol-processing-cmdline",
2324
"//src/main/kotlin:jdeps-gen",
2425
"//src/main/kotlin:skip-code-gen",
25-
"//src/main/kotlin/io/bazel/kotlin/compiler",
26+
"//src/main/kotlin/io/bazel/kotlin/compiler:compiler.jar",
2627
"@com_github_jetbrains_kotlin//:home",
2728
],
2829
jvm_flags = [
@@ -31,9 +32,10 @@ kt_bootstrap_binary(
3132
"-D@com_github_jetbrains_kotlin...kapt=$(rlocationpath //kotlin/compiler:kotlin-annotation-processing)",
3233
"-D@rules_kotlin...jdeps-gen=$(rlocationpath //src/main/kotlin:jdeps-gen)",
3334
"-D@rules_kotlin...skip-code-gen=$(rlocationpath //src/main/kotlin:skip-code-gen)",
34-
"-D@rules_kotlin...compiler=$(rlocationpath //src/main/kotlin/io/bazel/kotlin/compiler)",
35+
"-D@rules_kotlin...compiler=$(rlocationpath //src/main/kotlin/io/bazel/kotlin/compiler:compiler.jar)",
3536
"-D@com_github_google_ksp...symbol-processing-api=$(rlocationpath //kotlin/compiler:symbol-processing-api)",
3637
"-D@com_github_google_ksp...symbol-processing-cmdline=$(rlocationpath //kotlin/compiler:symbol-processing-cmdline)",
38+
"-D@rules_kotlin..kotlin.compiler.kotlin-reflect=$(rlocationpath //kotlin/compiler:kotlin-reflect)",
3739
"-XX:-MaxFDLimit",
3840
],
3941
main_class = "io.bazel.kotlin.builder.cmd.Build",

0 commit comments

Comments
 (0)