Skip to content

Commit

Permalink
Add per-jdk version configuration support
Browse files Browse the repository at this point in the history
  • Loading branch information
wendigo authored and martint committed Nov 26, 2024
1 parent 65e8dc2 commit 4d47183
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/main/go/args/java_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
)
Expand All @@ -14,6 +15,30 @@ func getArchSpecificDirectory() string {
return fmt.Sprintf("%s-%s", runtime.GOOS, runtime.GOARCH)
}

func DetectJvmVersion(javaBin string) (string, error) {
command := exec.Command(javaBin, "-version")
output, err := command.CombinedOutput()
if err != nil {
return "", fmt.Errorf("could not exec java to determine jvm version: %w", err)
}

return ExtractJvmVersion(string(output))
}

func ExtractJvmVersion(output string) (string, error) {
regex := regexp.MustCompile("version \"(.*)\"")
matches := regex.FindStringSubmatch(output)
if len(matches) != 2 {
return "", fmt.Errorf("unrecognized java version output: %s", output)
}
return matches[1], nil
}

func GetMajorJavaVersion(javaVersion string) string {
components := strings.Split(javaVersion, ".")
return components[0]
}

func (options *Options) JavaExecution(daemonize bool) ([]string, []string, error) {
var javaBin = "java"

Expand All @@ -27,6 +52,11 @@ func (options *Options) JavaExecution(daemonize bool) ([]string, []string, error
javaBin = filepath.Join(options.JvmDir, "bin/java")
}

jdkVersion, err := DetectJvmVersion(javaBin)
if err != nil {
return nil, nil, err
}

properties := make(map[string]string)
for k, v := range options.SystemProperties {
properties[k] = v
Expand Down Expand Up @@ -61,6 +91,11 @@ func (options *Options) JavaExecution(daemonize bool) ([]string, []string, error

command := []string{javaBin, "-cp", classpath}
command = append(command, options.JvmConfig...)

if perJdkConfig, exists := jvmSpecificConfig[GetMajorJavaVersion(jdkVersion)]; exists {
command = append(command, perJdkConfig...)
}

if len(options.JvmOptions) != 0 {
command = append(command, strings.Join(options.JvmOptions, " "))
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/go/args/java_execution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package args

import "testing"

func TestParsingJvmVersion(t *testing.T) {
testCases := []struct {
output string
expectedVersion string
}{
{
output: "openjdk version \"24-ea\" 2025-03-18\nOpenJDK Runtime Environment (build 24-ea+24-2960)\nOpenJDK 64-Bit Server VM (build 24-ea+24-2960, mixed mode, sharing)",
expectedVersion: "24-ea",
},
{
output: "openjdk version \"23\" 2024-09-17\nOpenJDK Runtime Environment Temurin-23+37 (build 23+37)\nOpenJDK 64-Bit Server VM Temurin-23+37 (build 23+37, mixed mode, sharing)",
expectedVersion: "23",
},
}

for _, testCase := range testCases {
actualVersion, err := ExtractJvmVersion(testCase.output)
if err != nil {
t.Fatalf("Unexpected error while extracting JVM version: %v", err)
}
if actualVersion != testCase.expectedVersion {
t.Fatalf("Expected Java version %s for output %s", testCase.expectedVersion, testCase.output)
}
}
}
10 changes: 10 additions & 0 deletions src/main/go/args/java_static_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package args

const vectorApiIncubating = "--add-modules=jdk.incubator.vector"

var jvmSpecificConfig = map[string][]string{
"22": {vectorApiIncubating},
"23": {vectorApiIncubating},
// 24 is not released yet, but vector API will be still incubating
"24": {vectorApiIncubating},
}

0 comments on commit 4d47183

Please sign in to comment.