sdk_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // Copyright 2019 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package java
  15. import (
  16. "path/filepath"
  17. "strings"
  18. "testing"
  19. "github.com/google/blueprint/proptools"
  20. "android/soong/android"
  21. "android/soong/java/config"
  22. )
  23. func TestClasspath(t *testing.T) {
  24. const frameworkAidl = "-I" + defaultJavaDir + "/framework/aidl"
  25. var classpathTestcases = []struct {
  26. name string
  27. unbundled bool
  28. moduleType string
  29. host android.OsClass
  30. properties string
  31. // for java 8
  32. bootclasspath []string
  33. java8classpath []string
  34. // for java 9
  35. system string
  36. java9classpath []string
  37. forces8 bool // if set, javac will always be called with java 8 arguments
  38. aidl string
  39. }{
  40. {
  41. name: "default",
  42. bootclasspath: config.StableCorePlatformBootclasspathLibraries,
  43. system: config.StableCorePlatformSystemModules,
  44. java8classpath: config.FrameworkLibraries,
  45. java9classpath: config.FrameworkLibraries,
  46. aidl: frameworkAidl,
  47. },
  48. {
  49. name: `sdk_version:"core_platform"`,
  50. properties: `sdk_version:"core_platform"`,
  51. bootclasspath: config.StableCorePlatformBootclasspathLibraries,
  52. system: config.StableCorePlatformSystemModules,
  53. java8classpath: []string{},
  54. aidl: "",
  55. },
  56. {
  57. name: "blank sdk version",
  58. properties: `sdk_version: "",`,
  59. bootclasspath: config.StableCorePlatformBootclasspathLibraries,
  60. system: config.StableCorePlatformSystemModules,
  61. java8classpath: config.FrameworkLibraries,
  62. java9classpath: config.FrameworkLibraries,
  63. aidl: frameworkAidl,
  64. },
  65. {
  66. name: "sdk v29",
  67. properties: `sdk_version: "29",`,
  68. bootclasspath: []string{`""`},
  69. forces8: true,
  70. java8classpath: []string{"prebuilts/sdk/29/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
  71. aidl: "-pprebuilts/sdk/29/public/framework.aidl",
  72. },
  73. {
  74. name: "sdk v30",
  75. properties: `sdk_version: "30",`,
  76. bootclasspath: []string{`""`},
  77. system: "sdk_public_30_system_modules",
  78. java8classpath: []string{"prebuilts/sdk/30/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
  79. java9classpath: []string{"prebuilts/sdk/30/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
  80. aidl: "-pprebuilts/sdk/30/public/framework.aidl",
  81. },
  82. {
  83. name: "current",
  84. properties: `sdk_version: "current",`,
  85. bootclasspath: []string{"android_stubs_current", "core-lambda-stubs"},
  86. system: "core-current-stubs-system-modules",
  87. java9classpath: []string{"android_stubs_current"},
  88. aidl: "-pout/soong/framework.aidl",
  89. },
  90. {
  91. name: "system_current",
  92. properties: `sdk_version: "system_current",`,
  93. bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"},
  94. system: "core-current-stubs-system-modules",
  95. java9classpath: []string{"android_system_stubs_current"},
  96. aidl: "-pout/soong/framework.aidl",
  97. },
  98. {
  99. name: "system_29",
  100. properties: `sdk_version: "system_29",`,
  101. bootclasspath: []string{`""`},
  102. forces8: true,
  103. java8classpath: []string{"prebuilts/sdk/29/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
  104. aidl: "-pprebuilts/sdk/29/public/framework.aidl",
  105. },
  106. {
  107. name: "system_30",
  108. properties: `sdk_version: "system_30",`,
  109. bootclasspath: []string{`""`},
  110. system: "sdk_public_30_system_modules",
  111. java8classpath: []string{"prebuilts/sdk/30/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
  112. java9classpath: []string{"prebuilts/sdk/30/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
  113. aidl: "-pprebuilts/sdk/30/public/framework.aidl",
  114. },
  115. {
  116. name: "test_current",
  117. properties: `sdk_version: "test_current",`,
  118. bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"},
  119. system: "core-current-stubs-system-modules",
  120. java9classpath: []string{"android_test_stubs_current"},
  121. aidl: "-pout/soong/framework.aidl",
  122. },
  123. {
  124. name: "core_current",
  125. properties: `sdk_version: "core_current",`,
  126. bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"},
  127. system: "core-current-stubs-system-modules",
  128. },
  129. {
  130. name: "nostdlib",
  131. properties: `sdk_version: "none", system_modules: "none"`,
  132. system: "none",
  133. bootclasspath: []string{`""`},
  134. java8classpath: []string{},
  135. },
  136. {
  137. name: "nostdlib system_modules",
  138. properties: `sdk_version: "none", system_modules: "stable-core-platform-api-stubs-system-modules"`,
  139. system: "stable-core-platform-api-stubs-system-modules",
  140. bootclasspath: []string{"stable-core-platform-api-stubs-system-modules-lib"},
  141. java8classpath: []string{},
  142. },
  143. {
  144. name: "host default",
  145. moduleType: "java_library_host",
  146. properties: ``,
  147. host: android.Host,
  148. bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
  149. java8classpath: []string{},
  150. },
  151. {
  152. name: "host supported default",
  153. host: android.Host,
  154. properties: `host_supported: true,`,
  155. java8classpath: []string{},
  156. bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
  157. },
  158. {
  159. name: "host supported nostdlib",
  160. host: android.Host,
  161. properties: `host_supported: true, sdk_version: "none", system_modules: "none"`,
  162. java8classpath: []string{},
  163. },
  164. {
  165. name: "unbundled sdk v29",
  166. unbundled: true,
  167. properties: `sdk_version: "29",`,
  168. bootclasspath: []string{`""`},
  169. forces8: true,
  170. java8classpath: []string{"prebuilts/sdk/29/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
  171. aidl: "-pprebuilts/sdk/29/public/framework.aidl",
  172. },
  173. {
  174. name: "unbundled sdk v30",
  175. unbundled: true,
  176. properties: `sdk_version: "30",`,
  177. bootclasspath: []string{`""`},
  178. system: "sdk_public_30_system_modules",
  179. java8classpath: []string{"prebuilts/sdk/30/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
  180. java9classpath: []string{"prebuilts/sdk/30/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
  181. aidl: "-pprebuilts/sdk/30/public/framework.aidl",
  182. },
  183. {
  184. name: "unbundled current",
  185. unbundled: true,
  186. properties: `sdk_version: "current",`,
  187. bootclasspath: []string{`""`},
  188. system: "sdk_public_current_system_modules",
  189. java8classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
  190. java9classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
  191. aidl: "-pprebuilts/sdk/current/public/framework.aidl",
  192. },
  193. {
  194. name: "module_current",
  195. properties: `sdk_version: "module_current",`,
  196. bootclasspath: []string{"android_module_lib_stubs_current", "core-lambda-stubs"},
  197. system: "core-current-stubs-system-modules",
  198. java9classpath: []string{"android_module_lib_stubs_current"},
  199. aidl: "-pout/soong/framework_non_updatable.aidl",
  200. },
  201. {
  202. name: "system_server_current",
  203. properties: `sdk_version: "system_server_current",`,
  204. bootclasspath: []string{"android_system_server_stubs_current", "core-lambda-stubs"},
  205. system: "core-current-stubs-system-modules",
  206. java9classpath: []string{"android_system_server_stubs_current"},
  207. aidl: "-pout/soong/framework.aidl",
  208. },
  209. }
  210. for _, testcase := range classpathTestcases {
  211. t.Run(testcase.name, func(t *testing.T) {
  212. moduleType := "java_library"
  213. if testcase.moduleType != "" {
  214. moduleType = testcase.moduleType
  215. }
  216. props := `
  217. name: "foo",
  218. srcs: ["a.java"],
  219. target: {
  220. android: {
  221. srcs: ["bar-doc/IFoo.aidl"],
  222. },
  223. },
  224. `
  225. bp := moduleType + " {" + props + testcase.properties + `
  226. }`
  227. bpJava8 := moduleType + " {" + props + `java_version: "1.8",
  228. ` + testcase.properties + `
  229. }`
  230. variant := "android_common"
  231. if testcase.host == android.Host {
  232. variant = android.BuildOs.String() + "_common"
  233. }
  234. convertModulesToPaths := func(cp []string) []string {
  235. ret := make([]string, len(cp))
  236. for i, e := range cp {
  237. ret[i] = defaultModuleToPath(e)
  238. }
  239. return ret
  240. }
  241. bootclasspath := convertModulesToPaths(testcase.bootclasspath)
  242. java8classpath := convertModulesToPaths(testcase.java8classpath)
  243. java9classpath := convertModulesToPaths(testcase.java9classpath)
  244. bc := ""
  245. var bcDeps []string
  246. if len(bootclasspath) > 0 {
  247. bc = "-bootclasspath " + strings.Join(bootclasspath, ":")
  248. if bootclasspath[0] != `""` {
  249. bcDeps = bootclasspath
  250. }
  251. }
  252. j8c := ""
  253. if len(java8classpath) > 0 {
  254. j8c = "-classpath " + strings.Join(java8classpath, ":")
  255. }
  256. j9c := ""
  257. if len(java9classpath) > 0 {
  258. j9c = "-classpath " + strings.Join(java9classpath, ":")
  259. }
  260. system := ""
  261. var systemDeps []string
  262. if testcase.system == "none" {
  263. system = "--system=none"
  264. } else if testcase.system != "" {
  265. dir := ""
  266. if strings.HasPrefix(testcase.system, "sdk_public_") {
  267. dir = "prebuilts/sdk"
  268. } else {
  269. dir = defaultJavaDir
  270. }
  271. system = "--system=" + filepath.Join("out", "soong", ".intermediates", dir, testcase.system, "android_common", "system")
  272. // The module-relative parts of these paths are hardcoded in system_modules.go:
  273. systemDeps = []string{
  274. filepath.Join("out", "soong", ".intermediates", dir, testcase.system, "android_common", "system", "lib", "modules"),
  275. filepath.Join("out", "soong", ".intermediates", dir, testcase.system, "android_common", "system", "lib", "jrt-fs.jar"),
  276. filepath.Join("out", "soong", ".intermediates", dir, testcase.system, "android_common", "system", "release"),
  277. }
  278. }
  279. checkClasspath := func(t *testing.T, result *android.TestResult, isJava8 bool) {
  280. foo := result.ModuleForTests("foo", variant)
  281. javac := foo.Rule("javac")
  282. var deps []string
  283. aidl := foo.MaybeRule("aidl")
  284. if aidl.Rule != nil {
  285. deps = append(deps, android.PathRelativeToTop(aidl.Output))
  286. }
  287. got := javac.Args["bootClasspath"]
  288. expected := ""
  289. if isJava8 || testcase.forces8 {
  290. expected = bc
  291. deps = append(deps, bcDeps...)
  292. } else {
  293. expected = system
  294. deps = append(deps, systemDeps...)
  295. }
  296. if got != expected {
  297. t.Errorf("bootclasspath expected %q != got %q", expected, got)
  298. }
  299. if isJava8 || testcase.forces8 {
  300. expected = j8c
  301. deps = append(deps, java8classpath...)
  302. } else {
  303. expected = j9c
  304. deps = append(deps, java9classpath...)
  305. }
  306. got = javac.Args["classpath"]
  307. if got != expected {
  308. t.Errorf("classpath expected %q != got %q", expected, got)
  309. }
  310. android.AssertPathsRelativeToTopEquals(t, "implicits", deps, javac.Implicits)
  311. }
  312. fixtureFactory := android.GroupFixturePreparers(
  313. prepareForJavaTest,
  314. FixtureWithPrebuiltApis(map[string][]string{
  315. "29": {},
  316. "30": {},
  317. "current": {},
  318. }),
  319. android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
  320. if testcase.unbundled {
  321. variables.Unbundled_build = proptools.BoolPtr(true)
  322. variables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
  323. }
  324. }),
  325. android.FixtureModifyEnv(func(env map[string]string) {
  326. if env["ANDROID_JAVA8_HOME"] == "" {
  327. env["ANDROID_JAVA8_HOME"] = "jdk8"
  328. }
  329. }),
  330. )
  331. // Test with legacy javac -source 1.8 -target 1.8
  332. t.Run("Java language level 8", func(t *testing.T) {
  333. result := fixtureFactory.RunTestWithBp(t, bpJava8)
  334. checkClasspath(t, result, true /* isJava8 */)
  335. if testcase.host != android.Host {
  336. aidl := result.ModuleForTests("foo", variant).Rule("aidl")
  337. android.AssertStringDoesContain(t, "aidl command", aidl.RuleParams.Command, testcase.aidl+" -I.")
  338. }
  339. })
  340. // Test with default javac -source 9 -target 9
  341. t.Run("Java language level 9", func(t *testing.T) {
  342. result := fixtureFactory.RunTestWithBp(t, bp)
  343. checkClasspath(t, result, false /* isJava8 */)
  344. if testcase.host != android.Host {
  345. aidl := result.ModuleForTests("foo", variant).Rule("aidl")
  346. android.AssertStringDoesContain(t, "aidl command", aidl.RuleParams.Command, testcase.aidl+" -I.")
  347. }
  348. })
  349. prepareWithPlatformVersionRel := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
  350. variables.Platform_sdk_codename = proptools.StringPtr("REL")
  351. variables.Platform_sdk_final = proptools.BoolPtr(true)
  352. })
  353. // Test again with PLATFORM_VERSION_CODENAME=REL, javac -source 8 -target 8
  354. t.Run("REL + Java language level 8", func(t *testing.T) {
  355. result := fixtureFactory.Extend(prepareWithPlatformVersionRel).RunTestWithBp(t, bpJava8)
  356. checkClasspath(t, result, true /* isJava8 */)
  357. })
  358. // Test again with PLATFORM_VERSION_CODENAME=REL, javac -source 9 -target 9
  359. t.Run("REL + Java language level 9", func(t *testing.T) {
  360. result := fixtureFactory.Extend(prepareWithPlatformVersionRel).RunTestWithBp(t, bp)
  361. checkClasspath(t, result, false /* isJava8 */)
  362. })
  363. })
  364. }
  365. }