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

Update java templates in rbeconfigsgen.go #999

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
60 changes: 47 additions & 13 deletions pkg/rbeconfigsgen/rbeconfigsgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,33 @@ java_runtime(
srcs = [],
java_home = "{{ .JavaHome }}",
)
`))

// javaBuildTemplateLt7 is the Java toolchain config BUILD file template for Bazel versions
// >=5.0.0 (tentative?) and < 7.0.0.
javaBuildTemplateLt7 = template.Must(template.New("javaBuild").Parse(buildHeader + `
load("@bazel_tools//tools/jdk:local_java_repository.bzl", "local_java_runtime")

package(default_visibility = ["//visibility:public"])

alias(
name = "jdk",
actual = "rbe_jdk",
)

local_java_runtime(
name = "rbe_jdk",
java_home = "{{ .JavaHome }}",
version = "{{ .JavaVersion }}",
)
`))

// javaBuildTemplate is the Java toolchain config BUILD file template for Bazel versions
// >=5.0.0 (tentative?).
// >=7.0.0 (including pre-releases).
// The difference between the older template is directly referencing to @rules_java
// instead of the indirection via @bazel_tools
javaBuildTemplate = template.Must(template.New("javaBuild").Parse(buildHeader + `
load("@bazel_tools//tools/jdk:local_java_repository.bzl", "local_java_runtime")
load("@rules_java//toolchains:local_java_repository.bzl", "local_java_runtime")

package(default_visibility = ["//visibility:public"])

Expand Down Expand Up @@ -562,6 +583,25 @@ func UsesLocalJavaRuntime(bazelVersion string) (bool, error) {
return !bv.LessThan(*semver.New("5.0.0")), nil
}

func getJavaTemplate(o *Options) (*template.Template, error) {
usesNewJavaRule := o.JavaUseLocalRuntime
if !usesNewJavaRule {
var err error
usesNewJavaRule, err = UsesLocalJavaRuntime(o.BazelVersion)
if (err != nil) {
return nil, fmt.Errorf("unable to determine what Java toolchain rule to use for Bazel %q: %w", o.BazelVersion, err)
}
}
if !usesNewJavaRule {
return legacyJavaBuildTemplate, nil
}
// use latest template if BazelVersion is unspecified
if o.BazelVersion != "" && o.BazelVersion < "7" {
return javaBuildTemplateLt7, nil
}
return javaBuildTemplate, nil
}

// genJavaConfigs returns a BUILD file containing a Java toolchain rule definition that contains
// the following attributes determined by probing details about the JDK version installed in the
// running toolchain container.
Expand Down Expand Up @@ -613,17 +653,11 @@ func genJavaConfigs(d *dockerRunner, o *Options) (generatedFile, error) {
}
log.Printf("Java version: '%s'.", javaVersion)

usesNewJavaRule := o.JavaUseLocalRuntime
if !usesNewJavaRule {
usesNewJavaRule, err = UsesLocalJavaRuntime(o.BazelVersion)
if err != nil {
return generatedFile{}, fmt.Errorf("unable to determine what Java toolchain rule to use for Bazel %q: %w", o.BazelVersion, err)
}
}
t := legacyJavaBuildTemplate
if usesNewJavaRule {
t = javaBuildTemplate
}
t, err := getJavaTemplate(o)
if err != nil {
return generatedFile{}, err
}

buf := bytes.NewBuffer(nil)
if err := t.Execute(buf, &javaBuildTemplateParams{
JavaHome: javaHome,
Expand Down
100 changes: 100 additions & 0 deletions pkg/rbeconfigsgen/rbeconfigsgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package rbeconfigsgen

import (
"testing"
"text/template"
)

func TestGenCppToolchainTarget(t *testing.T) {
Expand Down Expand Up @@ -84,3 +85,102 @@ func TestGenCppToolchainTarget(t *testing.T) {
})
}
}

func TestGetJavaTemplate(t *testing.T) {
tests := []struct {
name string
want *template.Template
opt *Options
}{
{
name: "bazel 4, choose legacy",
want: legacyJavaBuildTemplate,
opt: &Options{
BazelVersion: "4.0.0",
},
},
{
name: "bazel 5, choose BazelLt7",
want: javaBuildTemplateLt7,
opt: &Options{
BazelVersion: "5.0.0",
},
},
{
name: "bazel 7, choose latest",
want: javaBuildTemplate,
opt: &Options{
BazelVersion: "7.0.0",
},
},
{
name: "bazel 7-pre, choose latest",
want: javaBuildTemplate,
opt: &Options{
BazelVersion: "7.0.0-pre.20230724.1",
},
},
{
name: "useLocalRuntime forced, choose latest",
want: javaBuildTemplate,
opt: &Options{
JavaUseLocalRuntime: true,
},
},
{
name: "useLocalRuntime forced, bazel 4, choose BazelLt7",
want: javaBuildTemplateLt7,
opt: &Options{
BazelVersion: "4.0.0",
JavaUseLocalRuntime: true,
},
},
{
name: "useLocalRuntime forced, bazel 5, choose BazelLt7",
want: javaBuildTemplateLt7,
opt: &Options{
BazelVersion: "5.0.0",
JavaUseLocalRuntime: true,
},
},
{
name: "useLocalRuntime forced, bazel 6, choose BazelLt7",
want: javaBuildTemplateLt7,
opt: &Options{
BazelVersion: "6.0.0",
JavaUseLocalRuntime: true,
},
},
{
name: "useLocalRuntime forced, bazel 7, choose latest",
want: javaBuildTemplate,
opt: &Options{
BazelVersion: "7.0.0",
JavaUseLocalRuntime: true,
},
},
{
name: "useLocalRuntime forced, bazel 7-pre, choose latest",
want: javaBuildTemplate,
opt: &Options{
BazelVersion: "7.0.0-pre.20200202",
JavaUseLocalRuntime: true,
},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
// We skip validation since we don't set all options required for
// regular execution.
got, err := getJavaTemplate(tc.opt);
if err != nil {
t.Fatalf("getJavaTemplate failed: %v, wanted: %v", err, tc.want)
} else if got != tc.want {
t.Fatalf("getJavaTemplate: %v, wanted %v", got, tc.want)
}
})
}
}
4 changes: 2 additions & 2 deletions rules/exec_properties/exec_properties.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ PARAMS = {
),
}

def create_exec_properties_dict(**kwargs):
def create_exec_properties_dict(**_kwargs):
fail("create_exec_properties_dict is deprecated. Please use create_rbe_exec_properties_dict instead.")

def create_rbe_exec_properties_dict(**kwargs):
Expand Down Expand Up @@ -242,7 +242,7 @@ def create_rbe_exec_properties_dict(**kwargs):

return dict

def merge_dicts(*dict_args):
def merge_dicts(*_dict_args):
fail("merge_dicts is deprecated. Please use dicts.add() instead. See https://github.com/bazelbuild/bazel-skylib/blob/master/docs/dicts_doc.md")

def _exec_property_sets_repository_impl(repository_ctx):
Expand Down
Loading