-
Notifications
You must be signed in to change notification settings - Fork 237
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 Kotlin JARs with inlined functions, and an Android Kotlin example app #69
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8097ac7
Initial commit of an Android Kotlin example app
jin a09d051
Add support for generating Kotlin jar import targets with no_ijar_jav…
jin 762ecc7
Add the test.
jin c15d40b
Move InlineFunctionTest.kt to rules_jvm_external's own tests
jin 66c3715
Try to execute jar in all manners possible
jin 2005c2b
Disable Kotlin tests and examples on Windows
jin 90800f8
Always use the no-ijar java_import
jin 460fa0b
Fix formatting
jin c95b974
fix indent
jin dc69c2e
Fix call to repository_ctx.template
jin 2d53cee
Exclude macOS test on Bazel CI
jin 499f125
Flatten all the unit test targets into @regression_testing
jin 3cfe381
Revert "Exclude macOS test on Bazel CI"
jin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ load("//:specs.bzl", "parse", "utils") | |
|
||
_BUILD = """ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
load("@{repository_name}//:jvm_import.bzl", "jvm_import") | ||
|
||
{imports} | ||
""" | ||
|
||
|
@@ -73,16 +76,17 @@ def _relativize_and_symlink_file(repository_ctx, absolute_path): | |
# Get the reverse dependencies of an artifact from the Coursier parsed | ||
# dependency tree. | ||
def _get_reverse_deps(coord, dep_tree): | ||
reverse_deps = [] | ||
# For all potential reverse dep artifacts, | ||
for maybe_rdep in dep_tree["dependencies"]: | ||
# For all dependencies of this artifact, | ||
for maybe_rdep_coord in maybe_rdep["dependencies"]: | ||
# If this artifact depends on the missing artifact, | ||
if maybe_rdep_coord == coord: | ||
# Then this artifact is an rdep :-) | ||
reverse_deps.append(maybe_rdep) | ||
return reverse_deps | ||
reverse_deps = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. drive by formatting fix. |
||
|
||
# For all potential reverse dep artifacts, | ||
for maybe_rdep in dep_tree["dependencies"]: | ||
# For all dependencies of this artifact, | ||
for maybe_rdep_coord in maybe_rdep["dependencies"]: | ||
# If this artifact depends on the missing artifact, | ||
if maybe_rdep_coord == coord: | ||
# Then this artifact is an rdep :-) | ||
reverse_deps.append(maybe_rdep) | ||
return reverse_deps | ||
|
||
# Generate BUILD file with java_import and aar_import for each artifact in | ||
# the transitive closure, with their respective deps mapped to the resolved | ||
|
@@ -154,7 +158,10 @@ def generate_imports(repository_ctx, dep_tree, srcs_dep_tree = None): | |
# | ||
packaging = artifact_relative_path.split(".").pop() | ||
if packaging == "jar": | ||
target_import_string = ["java_import("] | ||
# Regular `java_import` invokes ijar on all JARs, causing some Scala and | ||
# Kotlin compile interface JARs to be incorrect. We replace java_import | ||
# with a simple jvm_import Starlark rule that skips ijar. | ||
target_import_string = ["jvm_import("] | ||
elif packaging == "aar": | ||
target_import_string = ["aar_import("] | ||
else: | ||
|
@@ -382,7 +389,7 @@ def _coursier_fetch_impl(repository_ctx): | |
cmd = _generate_coursier_command(repository_ctx) | ||
cmd.extend(["fetch"]) | ||
cmd.extend(artifact_coordinates) | ||
cmd.extend(["--artifact-type", ",".join(_COURSIER_PACKAGING_TYPES)]) | ||
cmd.extend(["--artifact-type", ",".join(_COURSIER_PACKAGING_TYPES + ["src"])]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. drive-by fix to add |
||
cmd.append("--quiet") | ||
cmd.append("--no-default") | ||
cmd.extend(["--json-output-file", "dep-tree.json"]) | ||
|
@@ -439,9 +446,19 @@ def _coursier_fetch_impl(repository_ctx): | |
srcs_dep_tree = srcs_dep_tree, | ||
) | ||
|
||
repository_ctx.template( | ||
"jvm_import.bzl", | ||
repository_ctx.attr._jvm_import, | ||
substitutions = {}, | ||
executable = False, # not executable | ||
) | ||
|
||
repository_ctx.file( | ||
"BUILD", | ||
_BUILD.format(imports = generated_imports), | ||
_BUILD.format( | ||
repository_name = repository_ctx.name, | ||
imports = generated_imports, | ||
), | ||
False, # not executable | ||
) | ||
|
||
|
@@ -457,7 +474,10 @@ def _coursier_fetch_impl(repository_ctx): | |
|
||
coursier_fetch = repository_rule( | ||
attrs = { | ||
"_coursier": attr.label(default = "//:third_party/coursier/coursier"), # vendor coursier, it's just a jar | ||
"_coursier": attr.label( | ||
default = "//:third_party/coursier/coursier", | ||
), # vendor coursier, it's just a jar | ||
"_jvm_import": attr.label(default = "//:private/jvm_import.bzl"), | ||
"repositories": attr.string_list(), # list of repository objects, each as json | ||
"artifacts": attr.string_list(), # list of artifact objects, each as json | ||
"fetch_sources": attr.bool(default = False), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.example.bazel" | ||
android:versionCode="1" | ||
android:versionName="1.0"> | ||
|
||
<uses-sdk | ||
android:minSdkVersion="21" | ||
android:targetSdkVersion="26" /> | ||
|
||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
android_binary( | ||
name = "app", | ||
manifest = "AndroidManifest.xml", | ||
custom_package = "com.example.bazel", | ||
deps = [ | ||
"//src:lib", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
android_sdk_repository(name = "androidsdk") | ||
|
||
# BEGIN io_bazel_rules_kotlin | ||
|
||
RULES_KOTLIN_VERSION = "da1232eda2ef90d4375e2d1677b32c7ddf09e8a1" | ||
http_archive( | ||
name = "io_bazel_rules_kotlin", | ||
strip_prefix = "rules_kotlin-%s" % RULES_KOTLIN_VERSION, | ||
url = "https://github.com/bazelbuild/rules_kotlin/archive/%s.tar.gz" % RULES_KOTLIN_VERSION, | ||
sha256 = "0bbb0e5e536f0c775f37bded59d4f8cfb8556e6c3d926fcc0f58bf3489bff470", | ||
) | ||
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains") | ||
|
||
kotlin_repositories() | ||
kt_register_toolchains() | ||
|
||
# END io_bazel_rules_kotlin | ||
|
||
local_repository( | ||
name = "rules_jvm_external", | ||
path = "../../", | ||
) | ||
|
||
load("@rules_jvm_external//:defs.bzl", "maven_install") | ||
|
||
maven_install( | ||
artifacts = [ | ||
"androidx.appcompat:appcompat:1.0.2", | ||
"androidx.core:core-ktx:1.0.1", | ||
], | ||
repositories = [ | ||
"https://maven.google.com", | ||
"https://repo1.maven.org/maven2", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.example.bazel" | ||
android:versionCode="1" | ||
android:versionName="1.0" > | ||
|
||
<uses-sdk | ||
android:minSdkVersion="21" | ||
android:targetSdkVersion="26" /> | ||
|
||
<application | ||
android:label="rules_jvm_external Kotlin App"> | ||
<activity | ||
android:name=".MainActivity" | ||
android:theme="@style/AppTheme" | ||
android:label="rules_jvm_external Kotlin App"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_android_library") | ||
load("@rules_jvm_external//:defs.bzl", "artifact") | ||
|
||
kt_android_library( | ||
name = "lib", | ||
srcs = glob(["java/com/example/bazel/*.kt"]), | ||
manifest = "AndroidManifest.xml", | ||
custom_package = "com.example.bazel", | ||
resource_files = glob(["res/**"]), | ||
deps = [ | ||
"@maven//:androidx_appcompat_appcompat", | ||
"@maven//:androidx_core_core", | ||
"@maven//:androidx_core_core_ktx", | ||
"@maven//:androidx_drawerlayout_drawerlayout", | ||
"@maven//:androidx_fragment_fragment", | ||
"@maven//:androidx_lifecycle_lifecycle_common", | ||
"@maven//:androidx_lifecycle_lifecycle_viewmodel", | ||
], | ||
visibility = ["//:__pkg__"] | ||
) |
13 changes: 13 additions & 0 deletions
13
examples/android_kotlin_app/src/java/com/example/bazel/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.bazel | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
this.setContentView(R.layout.activity_main) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/container" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".MainActivity" | ||
tools:ignore="MergeRootFrame,UnusedIds" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
|
||
<!-- Base application theme. --> | ||
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> | ||
<!--<!– Customize your theme here. –>--> | ||
</style> | ||
|
||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Stripped down version of a java_import Starlark rule, without invoking ijar | ||
# to create interface jars. | ||
|
||
# Inspired by Square's implementation of `raw_jvm_import` [0] and discussions | ||
# on the GitHub thread [1] about ijar's interaction with Kotlin JARs. | ||
# | ||
# [0]: https://github.com/square/bazel_maven_repository/pull/48 | ||
# [1]: https://github.com/bazelbuild/bazel/issues/4549 | ||
|
||
|
||
def _jvm_import_impl(ctx): | ||
if len(ctx.files.jars) != 1: | ||
fail("Please only specify one jar to import in the jars attribute.") | ||
|
||
return [ | ||
DefaultInfo( | ||
files = depset(ctx.files.jars) | ||
), | ||
JavaInfo( | ||
compile_jar = ctx.files.jars[0], | ||
output_jar = ctx.files.jars[0], | ||
source_jar = ctx.file.srcjar, | ||
deps = [ | ||
dep[JavaInfo] | ||
for dep | ||
in ctx.attr.deps | ||
if JavaInfo in dep | ||
], | ||
) | ||
] | ||
|
||
|
||
jvm_import = rule( | ||
attrs={ | ||
"jars": | ||
attr.label_list( | ||
allow_files=True, | ||
mandatory=True, | ||
cfg="target", | ||
), | ||
"srcjar": | ||
attr.label( | ||
allow_single_file=True, | ||
mandatory=False, | ||
cfg="target", | ||
), | ||
"deps": | ||
attr.label_list( | ||
default=[], | ||
providers=[JavaInfo], | ||
), | ||
}, | ||
implementation=_jvm_import_impl, | ||
provides=[JavaInfo], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_test") | ||
|
||
kt_jvm_test( | ||
name = "inline_function_test", | ||
srcs = ["com/example/bazel/InlineFunctionTest.kt"], | ||
test_class = "com.example.bazel.InlineFunctionTest", | ||
deps = [ | ||
"@kotlin_tests//:junit_junit", | ||
"@kotlin_tests//:org_jetbrains_kotlin_kotlin_test", | ||
] | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does the "--" do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
allows using the target subtraction syntax
-//:target