Skip to content

Commit

Permalink
Java 11 compilation works in rules_android
Browse files Browse the repository at this point in the history
Should fix bazelbuild/bazel#17000

PiperOrigin-RevId: 693168450
Change-Id: Ib6b8cc922c793d5e0a31f138692e6f4d138731dd
  • Loading branch information
ted-xie authored and copybara-github committed Nov 5, 2024
1 parent 3f66a2d commit 20c8e5b
Show file tree
Hide file tree
Showing 16 changed files with 647 additions and 7 deletions.
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
use_repo(
go_deps,
"org_bitbucket_creachadair_stringset",
"com_github_golang_glog",
"com_github_google_go_cmp",
"org_golang_google_protobuf",
Expand Down
1 change: 1 addition & 0 deletions examples/basicapp/java/com/basicapp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ android_binary(
android_library(
name = "basic_lib",
srcs = ["BasicActivity.java"],
javacopts = ["-source 11 -target 11"],
manifest = "AndroidManifest.xml",
resource_files = glob(["res/**"]),
)
2 changes: 1 addition & 1 deletion examples/basicapp/java/com/basicapp/BasicActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.button_id_fizz), findViewById(R.id.button_id_buzz),
};

for (Button b : buttons) {
for (var b : buttons) {
b.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/bazelbuild/rules_android
go 1.21.1

require (
bitbucket.org/creachadair/stringset v0.0.14
github.com/bazelbuild/rules_go v0.50.1
github.com/golang/glog v1.1.2
github.com/google/go-cmp v0.5.9
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/bazelbuild/rules_go v0.48.0 h1:fZgo6mCUKeL/+GQiMWy5/QU1FjNXGPnTd5bAeao1pbg=
github.com/bazelbuild/rules_go v0.48.0/go.mod h1:Dhcz716Kqg1RHNWos+N6MlXNkjNP2EwZQ0LukRKJfMs=
bitbucket.org/creachadair/stringset v0.0.14 h1:t1ejQyf8utS4GZV/4fM+1gvYucggZkfhb+tMobDxYOE=
bitbucket.org/creachadair/stringset v0.0.14/go.mod h1:Ej8fsr6rQvmeMDf6CCWMWGb14H9mz8kmDgPPTdiVT0w=
github.com/bazelbuild/rules_go v0.50.1 h1:/BUvuaB8MEiUA2oLPPCGtuw5V+doAYyiGTFyoSWlkrw=
github.com/bazelbuild/rules_go v0.50.1/go.mod h1:Dhcz716Kqg1RHNWos+N6MlXNkjNP2EwZQ0LukRKJfMs=
github.com/golang/glog v1.1.2 h1:DVjP2PbBOzHyzA+dn3WhHIq4NdVu3Q+pvivFICf/7fo=
github.com/golang/glog v1.1.2/go.mod h1:zR+okUeTbrL6EL3xHUDxZuEtGv04p5shwip1+mL/rLQ=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
Expand Down
7 changes: 6 additions & 1 deletion rules/android_sdk_repository/helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def create_android_sdk_rules(
] + [
"platforms/android-%d/%s" % (api_level, filename)
for api_level in api_levels
for filename in ["android.jar", "framework.aidl"]
for filename in ["android.jar", "core-for-system-modules.jar", "framework.aidl"]
] + select({
":windows": windows_only_files,
"//conditions:default": linux_only_files,
Expand Down Expand Up @@ -272,6 +272,11 @@ def create_android_sdk_rules(
actual = ":sdk-%d-toolchain" % default_api_level,
)

java_import(
name = "core-for-system-modules-jar",
jars = ["platforms/android-%d/core-for-system-modules.jar" % default_api_level],
)

java_binary(
name = "apksigner",
main_class = "com.android.apksigner.ApkSignerTool",
Expand Down
16 changes: 16 additions & 0 deletions src/tools/jar_to_module_info/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_binary(
name = "jar_to_module_info",
embed = [":jar_to_module_info_lib"],
visibility = ["//visibility:public"],
)

go_library(
name = "jar_to_module_info_lib",
importpath = "src/tools/jar_to_module_info/jar_to_module_info_lib",

srcs = ["jar_to_module_info.go"],
visibility = ["//visibility:private"],
deps = ["@org_bitbucket_creachadair_stringset//:stringset"],
)
74 changes: 74 additions & 0 deletions src/tools/jar_to_module_info/jar_to_module_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2024 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// 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.

// Creates a module-info.java that exports all packages in the provided .jar file; see README.md
package main

import (
"archive/zip"
"bytes"
"context"
"fmt"
"strings"

"bitbucket.org/creachadair/stringset"
"flag"
"log"
"os"
)

var (
inputPath = flag.String("input", "", "input jar path")
outputPath = flag.String("output", "", "output modile-info path")
)

func writeFilePortable(ctx context.Context, filename string, data []byte) error {
// A portable shim around the google-internal WriteFile() and the more commonly-used public version.
// The Google-internal WriteFile() takes an additional Context object.
return os.WriteFile(filename, data, 0o400)
}

func portableInit() {
flag.Parse()
}

func main() {
portableInit()
reader, err := zip.OpenReader(*inputPath)
if err != nil {
log.Fatal(err)
}
defer reader.Close()
packages := stringset.New()
for _, f := range reader.File {
if !strings.HasSuffix(f.Name, ".class") {
continue
}
idx := strings.LastIndex(f.Name, "/")
if idx == -1 {
continue
}
packages.Add(f.Name[:idx])
}
var output bytes.Buffer
fmt.Fprintln(&output, "module java.base {")
for _, p := range packages.Elements() {
fmt.Fprintf(&output, " exports %s;\n", strings.Replace(p, "/", ".", -1))
}
fmt.Fprintln(&output, "}")
err = writeFilePortable(context.Background(), *outputPath, output.Bytes())
if err != nil {
log.Fatal(err)
}
}
16 changes: 16 additions & 0 deletions src/tools/split_core_jar/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_binary(
name = "split_core_jar",
embed = [":split_core_jar_lib"],
visibility = ["//visibility:public"],
)

go_library(
name = "split_core_jar_lib",
importpath = "src/tools/split_core_jar/split_core_jar_lib",

srcs = ["split_core_jar.go"],
visibility = ["//visibility:private"],
deps = ["@org_bitbucket_creachadair_stringset//:stringset"],
)
168 changes: 168 additions & 0 deletions src/tools/split_core_jar/split_core_jar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Copyright 2024 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// 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.

// split_core_jar splits the given jar into a 'core' jar containing packages found in --core_jars,
// and an 'auxiliary' jar containing the remaining packages
package main

import (
"archive/zip"
"io"
"os"
"strings"
"time"

"flag"
"log"
"bitbucket.org/creachadair/stringset"
)

var (
inputPath = flag.String("input", "", "input jar path")
coreJarPathsStr = flag.String("core_jars", "", "input core jar paths, to filter against")
outputCoreJarPath = flag.String("output_core_jar", "", "output core jar path")
outputAuxiliaryJarPath = flag.String("output_auxiliary_jar", "", "output auxiliary jar path")
exclusionsStr = flag.String("exclusions", "", "packages to skip in core jar")

martinEpoch = parseTimeOrDie(time.RFC3339, "2010-01-01T00:00:00Z")
)

func parseTimeOrDie(layout, value string) time.Time {
t, err := time.Parse(layout, value)
if err != nil {
panic(err)
}
return t
}

type outputZip struct {
f *os.File
w *zip.Writer
}

func createOutputZip(p string) (*outputZip, error) {
z := &outputZip{}
var err error
z.f, err = os.Create(p)
if err != nil {
return nil, err
}
z.w = zip.NewWriter(z.f)
return z, nil
}

func (z *outputZip) Close() error {
err := z.w.Close()
if err != nil {
return err
}
err = z.f.Close()
if err != nil {
return err
}
return nil
}

func (z *outputZip) Copy(f *zip.File) error {
w, err := z.w.CreateHeader(&zip.FileHeader{Name: f.Name, Modified: martinEpoch})
if err != nil {
return err
}
r, err := f.Open()
if err != nil {
return err
}
defer r.Close()
_, err = io.Copy(w, r)
if err != nil {
return err
}
return nil
}

func portableInit() {
flag.Parse()
}

func main() {
portableInit()

packages := stringset.New()
coreJarPathsArr := strings.Split(*coreJarPathsStr, ",")
coreJarPaths := &coreJarPathsArr
exclusionsArr := strings.Split(*exclusionsStr, ",")
exclusions := &exclusionsArr

for _, coreJar := range *coreJarPaths {
r, err := zip.OpenReader(coreJar)
if err != nil {
log.Fatal(err)
}
defer r.Close()
for _, f := range r.File {
if !strings.HasSuffix(f.Name, ".class") {
continue
}
idx := strings.LastIndex(f.Name, "/")
if idx == -1 {
continue
}
packages.Add(f.Name[:idx])
}
}

packages.Discard(*exclusions...)

reader, err := zip.OpenReader(*inputPath)
if err != nil {
log.Fatal(err)
}
coreJar, err := createOutputZip(*outputCoreJarPath)
if err != nil {
log.Fatal(err)
}
outputAuxiliaryJarPath, err := createOutputZip(*outputAuxiliaryJarPath)
if err != nil {
log.Fatal(err)
}
for _, f := range reader.File {
if !strings.HasSuffix(f.Name, ".class") {
continue
}
idx := strings.LastIndex(f.Name, "/")
if idx == -1 {
continue
}
if packages.Contains(f.Name[:idx]) {
err := coreJar.Copy(f)
if err != nil {
log.Fatal(err)
}
} else {
err := outputAuxiliaryJarPath.Copy(f)
if err != nil {
log.Fatal(err)
}
}
}
if err = coreJar.Close(); err != nil {
log.Fatal(err)
}
if err = outputAuxiliaryJarPath.Close(); err != nil {
log.Fatal(err)
}
if err = reader.Close(); err != nil {
log.Fatal(err)
}
}
6 changes: 6 additions & 0 deletions tools/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ alias(
visibility = ["//visibility:public"],
)

alias(
name = "unzip",
actual = "//toolchains/android:unzip",
visibility = ["//visibility:public"],
)

genrule(
name = "full_desugar_jdk_libs_config_json",
srcs = [
Expand Down
Loading

0 comments on commit 20c8e5b

Please sign in to comment.