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

Add support for Bzlmod #1006

Merged
merged 27 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 24 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
30 changes: 30 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@ 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"
tasks:
ubuntu1804:
name: "Tests without Bzlmod (Ubuntu 18.04)"
build_flags:
- "--enable_bzlmod=false"
test_flags:
- "--enable_bzlmod=false"
test_targets:
- "//:all_tests"
build_targets:
- "//:rules_kotlin_release"
ubuntu2004_bzlmod:
name: "Tests (Ubuntu 20.04)"
platform: ubuntu2004
test_targets:
- "//:all_tests"
build_targets:
- "//:rules_kotlin_release"
macos:
name: "Tests (Latest Macos)"
test_targets:
- "//:all_tests"
build_targets:
- "//:rules_kotlin_release"
rbe_ubuntu1604:
test_targets:
- "--"
Expand Down Expand Up @@ -68,6 +83,21 @@ tasks:
- //...
test_targets:
- //...
examples-trivial-bzlmod-ubuntu1804:
name: "Example - Trivial Bzlmod (Ubuntu 18.04)"
platform: ubuntu1804
working_directory: examples/trivial
build_flags:
- "--enable_bzlmod=true"
test_flags:
- "--enable_bzlmod=true"
include_json_profile:
- build
- test
build_targets:
- //...
test_targets:
- //...
examples-trivial-windows:
name: "Example - Trivial (Windows)"
platform: windows
Expand Down
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
common --enable_bzlmod=true
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enabling Bzlmod by default for rules_kotlin


build --strategy=KotlinCompile=worker
build --test_output=all
build --verbose_failures
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ To ease development, pains should be taken to keep the packages in the archive t
### Multi-repo runtime

The `rules_kotlin` runtime is comprised of multiple repositories. The end user will interact with a single repository, that repository delegates to
versioned feature sub-repositories. Currently, the delegation is managed by using well known names (e.g. core lives in `@io_bazel_rules_kotlin_configured`),
versioned feature sub-repositories. Currently, the delegation is managed by using well known names (e.g. core lives in `@rules_kotlin_configured`),
a necessity while the initial repository can be named arbitrarily. Future development intends to remove this restriction.

### Versioning
Expand Down
67 changes: 67 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module(
name = "rules_kotlin",
version = "1.8.1",
compatibility_level = 1,
repo_name = "rules_kotlin",
)

bazel_dep(name = "platforms", version = "0.0.6")
bazel_dep(name = "bazel_skylib", version = "1.3.0")
bazel_dep(name = "rules_java", version = "6.4.0")
bazel_dep(name = "rules_python", version = "0.24.0")
bazel_dep(name = "rules_cc", version = "0.0.8")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rules_cc is needed for the remote execution configs.


rules_kotlin_extensions = use_extension("//src/main/starlark/core/repositories:bzlmod_setup.bzl", "rules_kotlin_extensions")
use_repo(
rules_kotlin_extensions,
"build_bazel_rules_android",
"buildkite_config",
"com_github_google_ksp",
"com_github_jetbrains_kotlin",
"com_github_pinterest_ktlint",
"kt_java_stub_template",
)

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

# Development dependencies

bazel_dep(name = "rules_jvm_external", version = "4.4.2")

maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
maven.install(
name = "kotlin_rules_maven",
artifacts = [
"com.google.code.findbugs:jsr305:3.0.2",
"junit:junit:4.13-beta-3",
"com.google.protobuf:protobuf-java:3.6.0",
"com.google.protobuf:protobuf-java-util:3.6.0",
"com.google.guava:guava:27.1-jre",
"com.google.truth:truth:0.45",
"com.google.auto.service:auto-service:1.0.1",
"com.google.auto.service:auto-service-annotations:1.0.1",
"com.google.auto.value:auto-value:1.10.1",
"com.google.auto.value:auto-value-annotations:1.10.1",
"com.google.dagger:dagger:2.43.2",
"com.google.dagger:dagger-compiler:2.43.2",
"com.google.dagger:dagger-producers:2.43.2",
"javax.annotation:javax.annotation-api:1.3.2",
"javax.inject:javax.inject:1",
"org.pantsbuild:jarjar:1.7.2",
"org.jetbrains.kotlinx:atomicfu-js:0.15.2",
"org.jetbrains.kotlinx:kotlinx-serialization-runtime:1.0-M1-1.4.0-rc",
],
fetch_sources = True,
repositories = [
"https://maven-central.storage.googleapis.com/repos/central/data/",
"https://maven.google.com",
"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")

register_toolchains("@rules_kotlin//kotlin/internal:default_toolchain")
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
rules_kotlin_version = "1.8"
rules_kotlin_sha = "01293740a16e474669aba5b5a1fe3d368de5832442f164e4fbfc566815a8bc3a"
http_archive(
name = "io_bazel_rules_kotlin",
name = "rules_kotlin",
urls = ["https://github.com/bazelbuild/rules_kotlin/releases/download/v%s/rules_kotlin_release.tgz" % rules_kotlin_version],
sha256 = rules_kotlin_sha,
)

load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories")
load("@rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories")
kotlin_repositories() # if you want the default. Otherwise see custom kotlinc distribution below

load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_register_toolchains")
load("@rules_kotlin//kotlin:core.bzl", "kt_register_toolchains")
kt_register_toolchains() # to use the default toolchain, otherwise see toolchains below
```

Expand All @@ -76,7 +76,7 @@ kt_register_toolchains() # to use the default toolchain, otherwise see toolchain
In your project's `BUILD` files, load the Kotlin rules and use them like so:

```python
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")

kt_jvm_library(
name = "package_name",
Expand All @@ -93,7 +93,7 @@ To enable a custom toolchain (to configure language level, etc.)
do the following. In a `<workspace>/BUILD.bazel` file define the following:

```python
load("@io_bazel_rules_kotlin//kotlin:core.bzl", "define_kt_toolchain")
load("@rules_kotlin//kotlin:core.bzl", "define_kt_toolchain")

define_kt_toolchain(
name = "kotlin_toolchain",
Expand All @@ -115,7 +115,7 @@ To choose a different `kotlinc` distribution (1.3 and 1.4 variants supported), d
in your `WORKSPACE` file (or import from a `.bzl` file:

```python
load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories", "kotlinc_version")
load("@rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories", "kotlinc_version")

kotlin_repositories(
compiler_release = kotlinc_version(
Expand All @@ -141,19 +141,19 @@ In the project's `WORKSPACE`, change the setup:

# Use local check-out of repo rules (or a commit-archive from github via http_archive or git_repository)
local_repository(
name = "io_bazel_rules_kotlin",
name = "rules_kotlin",
path = "../path/to/rules_kotlin_clone/",
)

load("@io_bazel_rules_kotlin//kotlin:dependencies.bzl", "kt_download_local_dev_dependencies")
load("@rules_kotlin//kotlin:dependencies.bzl", "kt_download_local_dev_dependencies")

kt_download_local_dev_dependencies()

load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories", "versions")
load("@rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories", "versions")

kotlin_repositories()

load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_register_toolchains")
load("@rules_kotlin//kotlin:core.bzl", "kt_register_toolchains")

kt_register_toolchains()
```
Expand All @@ -164,16 +164,16 @@ rules to this:
```python
# Download master or specific revisions
http_archive(
name = "io_bazel_rules_kotlin",
name = "rules_kotlin",
strip_prefix = "rules_kotlin-master",
urls = ["https://github.com/bazelbuild/rules_kotlin/archive/refs/heads/master.zip"],
)

load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories")
load("@rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories")

kotlin_repositories()

load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_register_toolchains")
load("@rules_kotlin//kotlin:core.bzl", "kt_register_toolchains")

kt_register_toolchains()
```
Expand All @@ -185,10 +185,10 @@ To attach debugger and step through native action code when using local checkout
1. Open `rules_kotlin` project in Android Studio, using existing `.bazelproject` file as project view.
2. In terminal, build the kotlin target you want to debug, using the subcommand option, ex: `bazel build //lib/mylib:main_kt -s`. You can also use `bazel aquery` to get this info.
3. Locate the subcommand for the kotlin action you want to debug, let's say `KotlinCompile`. Note: If you don't see it, target rebuild may have been skipped (in this case `touch` one of the source .kt file to trigger rebuild).
4. Export `REPOSITORY_NAME` as specified in action env, ex : `export REPOSITORY_NAME=io_bazel_rules_kotlin`
5. Copy the command line, ex : `bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/external/io_bazel_rules_kotlin/src/main/kotlin/build '--flagfile=bazel-out/darwin_arm64-fastbuild/bin/lib/mylib/main_kt-kt.jar-0.params'`
4. Export `REPOSITORY_NAME` as specified in action env, ex : `export REPOSITORY_NAME=rules_kotlin`
5. Copy the command line, ex : `bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/external/rules_kotlin/src/main/kotlin/build '--flagfile=bazel-out/darwin_arm64-fastbuild/bin/lib/mylib/main_kt-kt.jar-0.params'`
6. Change directory into the [execRoot](https://bazel.build/remote/output-directories#layout-diagram), normally `bazel-MYPROJECT`, available via `bazel info | grep execution_root`.
7. Add `--debug=5005` to command line to make the action wait for a debugger to attach, ex: `bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/external/io_bazel_rules_kotlin/src/main/kotlin/build --debug=5005 '--flagfile=bazel-out/darwin_arm64-fastbuild/bin/lib/mylib/main_kt-kt.jar-0.params'`. Note: if command invokes `java` toolchain directly, use `-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005` instead.
7. Add `--debug=5005` to command line to make the action wait for a debugger to attach, ex: `bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/external/rules_kotlin/src/main/kotlin/build --debug=5005 '--flagfile=bazel-out/darwin_arm64-fastbuild/bin/lib/mylib/main_kt-kt.jar-0.params'`. Note: if command invokes `java` toolchain directly, use `-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005` instead.
8. You should see in output that action is waiting for debugger. Use a default `Remote JVM Debug` configuration in Android Studio, set breakpoint in kotlin action java/kt code, and attach debugger. Breakpoints should be hit.

# Kotlin and Java compiler flags
Expand All @@ -199,7 +199,7 @@ Note: Not all compiler flags are supported in all language versions. When this h

For example you can define global compiler flags by doing:
```python
load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_kotlinc_options", "kt_javac_options", "define_kt_toolchain")
load("@rules_kotlin//kotlin:core.bzl", "kt_kotlinc_options", "kt_javac_options", "define_kt_toolchain")

kt_kotlinc_options(
name = "kt_kotlinc_options",
Expand All @@ -225,8 +225,8 @@ Compiler flags that are passed to the rule definitions will be taken over the to

Example:
```python
load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_kotlinc_options", "kt_javac_options")
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("@rules_kotlin//kotlin:core.bzl", "kt_kotlinc_options", "kt_javac_options")
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")

kt_kotlinc_options(
name = "kt_kotlinc_options_for_package_name",
Expand Down Expand Up @@ -270,8 +270,8 @@ A few things to note:
```

```python
load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_ksp_plugin")
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("@rules_kotlin//kotlin:core.bzl", "kt_ksp_plugin")
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")

kt_ksp_plugin(
name = "moshi-kotlin-codegen",
Expand All @@ -294,7 +294,7 @@ To choose a different `ksp_version` distribution,
do the following in your `WORKSPACE` file (or import from a `.bzl` file):

```python
load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories", "ksp_version")
load("@rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories", "ksp_version")

kotlin_repositories(
ksp_compiler_release = ksp_version(
Expand All @@ -310,8 +310,8 @@ The `kt_compiler_plugin` rule allows running Kotlin compiler plugins, such as no

For example, you can add allopen to your project like this:
```python
load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_compiler_plugin")
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("@rules_kotlin//kotlin:core.bzl", "kt_compiler_plugin")
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")

kt_compiler_plugin(
name = "open_for_testing_plugin",
Expand Down
Empty file added WORKSPACE.bzlmod
Empty file.
4 changes: 2 additions & 2 deletions WORKSPACE.dev.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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.
workspace(name = "io_bazel_rules_kotlin")
workspace(name = "rules_kotlin")

load("//src/main/starlark/core/repositories:download.bzl", "kt_download_local_dev_dependencies")

Expand All @@ -21,4 +21,4 @@ load("//kotlin:repositories.bzl", "kotlin_repositories")

kotlin_repositories()

register_toolchains("@io_bazel_rules_kotlin//kotlin/internal:default_toolchain")
register_toolchains("@rules_kotlin//kotlin/internal:default_toolchain")
2 changes: 1 addition & 1 deletion WORKSPACE.release.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
# 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.
workspace(name = "io_bazel_rules_kotlin")
workspace(name = "rules_kotlin")
5 changes: 3 additions & 2 deletions docs/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ kt_jvm_test(<a href="#kt_jvm_test-name">name</a>, <a href="#kt_jvm_test-associat

**Notes:**
* The kotlin test library is not added implicitly, it is available with the label
`@io_bazel_rules_kotlin//kotlin/compiler:kotlin-test`.
`@rules_kotlin//kotlin/compiler:kotlin-test`.


**ATTRIBUTES**
Expand Down Expand Up @@ -529,7 +529,7 @@ This macro registers the kotlin toolchain.
## kotlin_repositories

<pre>
kotlin_repositories(<a href="#kotlin_repositories-compiler_repository_name">compiler_repository_name</a>, <a href="#kotlin_repositories-ksp_repository_name">ksp_repository_name</a>, <a href="#kotlin_repositories-compiler_release">compiler_release</a>,
kotlin_repositories(<a href="#kotlin_repositories-is_bzlmod">is_bzlmod</a>, <a href="#kotlin_repositories-compiler_repository_name">compiler_repository_name</a>, <a href="#kotlin_repositories-ksp_repository_name">ksp_repository_name</a>, <a href="#kotlin_repositories-compiler_release">compiler_release</a>,
<a href="#kotlin_repositories-ksp_compiler_release">ksp_compiler_release</a>)
</pre>

Expand All @@ -540,6 +540,7 @@ Call this in the WORKSPACE file to setup the Kotlin rules.

| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="kotlin_repositories-is_bzlmod"></a>is_bzlmod | <p align="center"> - </p> | <code>False</code> |
| <a id="kotlin_repositories-compiler_repository_name"></a>compiler_repository_name | for the kotlinc compiler repository. | <code>"com_github_jetbrains_kotlin"</code> |
| <a id="kotlin_repositories-ksp_repository_name"></a>ksp_repository_name | <p align="center"> - </p> | <code>"com_github_google_ksp"</code> |
| <a id="kotlin_repositories-compiler_release"></a>compiler_release | version provider from versions.bzl. | <code>struct()</code> |
Expand Down
4 changes: 2 additions & 2 deletions examples/android/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ local_repository(
load("@release_archive//:repository.bzl", "archive_repository")

archive_repository(
name = "io_bazel_rules_kotlin",
name = "rules_kotlin",
local_path = "../..",
)

load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories", "versions")
load("@rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories", "versions")

kotlin_repositories()

Expand Down
4 changes: 2 additions & 2 deletions examples/android/bzl/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@io_bazel_rules_kotlin//kotlin:core.bzl", "define_kt_toolchain", "kt_kotlinc_options")
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_javac_options")
load("@rules_kotlin//kotlin:core.bzl", "define_kt_toolchain", "kt_kotlinc_options")
load("@rules_kotlin//kotlin:jvm.bzl", "kt_javac_options")

kt_kotlinc_options(
name = "default_kotlinc_options",
Expand Down
2 changes: 1 addition & 1 deletion examples/android/libAndroid/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@build_bazel_rules_android//android:rules.bzl", "android_library")
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")

android_library(
name = "my_android",
Expand Down
6 changes: 3 additions & 3 deletions examples/android/libKtAndroid/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_compiler_plugin", "kt_ksp_plugin")
load("@io_bazel_rules_kotlin//kotlin:android.bzl", "kt_android_library")
load("@rules_kotlin//kotlin:core.bzl", "kt_compiler_plugin", "kt_ksp_plugin")
load("@rules_kotlin//kotlin:android.bzl", "kt_android_library")
load("@rules_java//java:defs.bzl", "java_plugin")

kt_compiler_plugin(
Expand All @@ -8,7 +8,7 @@ kt_compiler_plugin(
id = "org.jetbrains.kotlin.serialization",
stubs_phase = True,
deps = [
"@io_bazel_rules_kotlin//kotlin/compiler:kotlinx-serialization-compiler-plugin",
"@rules_kotlin//kotlin/compiler:kotlinx-serialization-compiler-plugin",
],
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_kotlin//kotlin:android.bzl", "kt_android_local_test")
load("@rules_kotlin//kotlin:android.bzl", "kt_android_local_test")

kt_android_local_test(
name = "SomeTest",
Expand Down
2 changes: 1 addition & 1 deletion examples/anvil/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_kotlin//kotlin:core.bzl", "define_kt_toolchain")
load("@rules_kotlin//kotlin:core.bzl", "define_kt_toolchain")

define_kt_toolchain(
name = "kotlin_toolchain",
Expand Down
Loading