Initial support for converting jars to java9 system modules

Adds a java_system_modules module type that (when
EXPERIMENTAL_USE_OPENJDK9 is set to true) converts a list of
java library modules and prebuilt jars into system modules,
and plumbs the system modules through to the javac command
line.

Also exports the location of the system modules to make
variables, as well as the name of the default system module.

Test: TestClasspath in java_test.go, runs automatically as part of the build
Bug: 63986449
Change-Id: I27bd5d2010092422a27b69c91568e49010e02f40
diff --git a/java/java_test.go b/java/java_test.go
index a86973d..4729313 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -50,9 +50,12 @@
 
 	os.Exit(run())
 }
-
 func testJava(t *testing.T, bp string) *android.TestContext {
-	config := android.TestArchConfig(buildDir, nil)
+	return testJavaWithEnv(t, bp, nil)
+}
+
+func testJavaWithEnv(t *testing.T, bp string, env map[string]string) *android.TestContext {
+	config := android.TestArchConfig(buildDir, env)
 
 	ctx := android.NewTestArchContext()
 	ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
@@ -60,6 +63,7 @@
 	ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
 	ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
 	ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
+	ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
 	ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
 	ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
 	ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
@@ -84,10 +88,28 @@
 				name: "%s",
 				srcs: ["a.java"],
 				no_standard_libs: true,
+				system_modules: "core-system-modules",
 			}
 		`, extra)
 	}
 
+	if config.TargetOpenJDK9() {
+		systemModules := []string{
+			"core-system-modules",
+			"android_stubs_current_system_modules",
+			"android_system_stubs_current_system_modules",
+			"android_test_stubs_current_system_modules",
+		}
+
+		for _, extra := range systemModules {
+			bp += fmt.Sprintf(`
+			java_system_modules {
+				name: "%s",
+			}
+		`, extra)
+		}
+	}
+
 	ctx.MockFileSystem(map[string][]byte{
 		"Android.bp": []byte(bp),
 		"a.java":     nil,
@@ -190,17 +212,20 @@
 	host          android.OsClass
 	properties    string
 	bootclasspath []string
+	system        string
 	classpath     []string
 }{
 	{
 		name:          "default",
 		bootclasspath: []string{"core-oj", "core-libart"},
+		system:        "core-system-modules",
 		classpath:     []string{"ext", "framework", "okhttp"},
 	},
 	{
 		name:          "blank sdk version",
 		properties:    `sdk_version: "",`,
 		bootclasspath: []string{"core-oj", "core-libart"},
+		system:        "core-system-modules",
 		classpath:     []string{"ext", "framework", "okhttp"},
 	},
 	{
@@ -208,6 +233,7 @@
 		name:          "sdk v14",
 		properties:    `sdk_version: "14",`,
 		bootclasspath: []string{`""`},
+		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
 		classpath:     []string{"prebuilts/sdk/14/android.jar"},
 	},
 	{
@@ -215,6 +241,7 @@
 		name:          "current",
 		properties:    `sdk_version: "current",`,
 		bootclasspath: []string{"android_stubs_current"},
+		system:        "android_stubs_current_system_modules",
 		classpath:     []string{},
 	},
 	{
@@ -222,6 +249,7 @@
 		name:          "system_current",
 		properties:    `sdk_version: "system_current",`,
 		bootclasspath: []string{"android_system_stubs_current"},
+		system:        "android_system_stubs_current_system_modules",
 		classpath:     []string{},
 	},
 	{
@@ -229,12 +257,22 @@
 		name:          "test_current",
 		properties:    `sdk_version: "test_current",`,
 		bootclasspath: []string{"android_test_stubs_current"},
+		system:        "android_test_stubs_current_system_modules",
 		classpath:     []string{},
 	},
 	{
 
 		name:          "nostdlib",
-		properties:    `no_standard_libs: true`,
+		properties:    `no_standard_libs: true, system_modules: "none"`,
+		system:        "none",
+		bootclasspath: []string{`""`},
+		classpath:     []string{},
+	},
+	{
+
+		name:          "nostdlib system_modules",
+		properties:    `no_standard_libs: true, system_modules: "core-system-modules"`,
+		system:        "core-system-modules",
 		bootclasspath: []string{`""`},
 		classpath:     []string{},
 	},
@@ -263,7 +301,7 @@
 	{
 		name:       "host supported nostdlib",
 		host:       android.Host,
-		properties: `host_supported: true, no_standard_libs: true`,
+		properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
 		classpath:  []string{},
 	},
 }
@@ -275,12 +313,17 @@
 			if testcase.moduleType != "" {
 				moduleType = testcase.moduleType
 			}
-			ctx := testJava(t, moduleType+` {
+
+			bp := moduleType + ` {
 				name: "foo",
 				srcs: ["a.java"],
-				`+testcase.properties+`
+				` + testcase.properties + `
+			}`
+
+			variant := "android_common"
+			if testcase.host == android.Host {
+				variant = android.BuildOs.String() + "_common"
 			}
-			`)
 
 			convertModulesToPaths := func(cp []string) []string {
 				ret := make([]string, len(cp))
@@ -293,33 +336,63 @@
 			bootclasspath := convertModulesToPaths(testcase.bootclasspath)
 			classpath := convertModulesToPaths(testcase.classpath)
 
-			variant := "android_common"
-			if testcase.host == android.Host {
-				variant = android.BuildOs.String() + "_common"
-			}
-			javac := ctx.ModuleForTests("foo", variant).Rule("javac")
-
-			got := strings.TrimPrefix(javac.Args["bootClasspath"], "-bootclasspath ")
 			bc := strings.Join(bootclasspath, ":")
-			if got != bc {
-				t.Errorf("bootclasspath expected %q != got %q", bc, got)
+			if bc != "" {
+				bc = "-bootclasspath " + bc
 			}
 
-			got = strings.TrimPrefix(javac.Args["classpath"], "-classpath ")
 			c := strings.Join(classpath, ":")
-			if got != c {
-				t.Errorf("classpath expected %q != got %q", c, got)
+			if c != "" {
+				c = "-classpath " + c
+			}
+			system := ""
+			if testcase.system == "none" {
+				system = "--system=none"
+			} else if testcase.system != "" {
+				system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
 			}
 
-			var deps []string
-			if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
-				deps = append(deps, bootclasspath...)
-			}
-			deps = append(deps, classpath...)
+			t.Run("1.8", func(t *testing.T) {
+				// Test default javac 1.8
+				ctx := testJava(t, bp)
 
-			if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
-				t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
-			}
+				javac := ctx.ModuleForTests("foo", variant).Rule("javac")
+
+				got := javac.Args["bootClasspath"]
+				if got != bc {
+					t.Errorf("bootclasspath expected %q != got %q", bc, got)
+				}
+
+				got = javac.Args["classpath"]
+				if got != c {
+					t.Errorf("classpath expected %q != got %q", c, got)
+				}
+
+				var deps []string
+				if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
+					deps = append(deps, bootclasspath...)
+				}
+				deps = append(deps, classpath...)
+
+				if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
+					t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
+				}
+			})
+
+			// Test again with javac 1.9
+			t.Run("1.9", func(t *testing.T) {
+				ctx := testJavaWithEnv(t, bp, map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
+
+				javac := ctx.ModuleForTests("foo", variant).Rule("javac")
+				got := javac.Args["bootClasspath"]
+				expected := system
+				if testcase.system == "bootclasspath" {
+					expected = bc
+				}
+				if got != expected {
+					t.Errorf("bootclasspath expected %q != got %q", expected, got)
+				}
+			})
 		})
 	}