sdk_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. "reflect"
  18. "strings"
  19. "testing"
  20. "github.com/google/blueprint/proptools"
  21. "android/soong/android"
  22. "android/soong/java/config"
  23. )
  24. func TestClasspath(t *testing.T) {
  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: "-Iframework/aidl",
  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: "-Iframework/aidl",
  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: "-p" + buildDir + "/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: "-p" + buildDir + "/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: "-p" + buildDir + "/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: "-p" + buildDir + "/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: "-p" + buildDir + "/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] = moduleToPath(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. }
  269. system = "--system=" + filepath.Join(buildDir, ".intermediates", dir, testcase.system, "android_common", "system")
  270. // The module-relative parts of these paths are hardcoded in system_modules.go:
  271. systemDeps = []string{
  272. filepath.Join(buildDir, ".intermediates", dir, testcase.system, "android_common", "system", "lib", "modules"),
  273. filepath.Join(buildDir, ".intermediates", dir, testcase.system, "android_common", "system", "lib", "jrt-fs.jar"),
  274. filepath.Join(buildDir, ".intermediates", dir, testcase.system, "android_common", "system", "release"),
  275. }
  276. }
  277. checkClasspath := func(t *testing.T, ctx *android.TestContext, isJava8 bool) {
  278. foo := ctx.ModuleForTests("foo", variant)
  279. javac := foo.Rule("javac")
  280. var deps []string
  281. aidl := foo.MaybeRule("aidl")
  282. if aidl.Rule != nil {
  283. deps = append(deps, aidl.Output.String())
  284. }
  285. got := javac.Args["bootClasspath"]
  286. expected := ""
  287. if isJava8 || testcase.forces8 {
  288. expected = bc
  289. deps = append(deps, bcDeps...)
  290. } else {
  291. expected = system
  292. deps = append(deps, systemDeps...)
  293. }
  294. if got != expected {
  295. t.Errorf("bootclasspath expected %q != got %q", expected, got)
  296. }
  297. if isJava8 || testcase.forces8 {
  298. expected = j8c
  299. deps = append(deps, java8classpath...)
  300. } else {
  301. expected = j9c
  302. deps = append(deps, java9classpath...)
  303. }
  304. got = javac.Args["classpath"]
  305. if got != expected {
  306. t.Errorf("classpath expected %q != got %q", expected, got)
  307. }
  308. if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
  309. t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
  310. }
  311. }
  312. // Test with legacy javac -source 1.8 -target 1.8
  313. t.Run("Java language level 8", func(t *testing.T) {
  314. config := testConfig(nil, bpJava8, nil)
  315. if testcase.unbundled {
  316. config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
  317. config.TestProductVariables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
  318. }
  319. ctx := testContext()
  320. run(t, ctx, config)
  321. checkClasspath(t, ctx, true /* isJava8 */)
  322. if testcase.host != android.Host {
  323. aidl := ctx.ModuleForTests("foo", variant).Rule("aidl")
  324. if g, w := aidl.RuleParams.Command, testcase.aidl+" -I."; !strings.Contains(g, w) {
  325. t.Errorf("want aidl command to contain %q, got %q", w, g)
  326. }
  327. }
  328. })
  329. // Test with default javac -source 9 -target 9
  330. t.Run("Java language level 9", func(t *testing.T) {
  331. config := testConfig(nil, bp, nil)
  332. if testcase.unbundled {
  333. config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
  334. config.TestProductVariables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
  335. }
  336. ctx := testContext()
  337. run(t, ctx, config)
  338. checkClasspath(t, ctx, false /* isJava8 */)
  339. if testcase.host != android.Host {
  340. aidl := ctx.ModuleForTests("foo", variant).Rule("aidl")
  341. if g, w := aidl.RuleParams.Command, testcase.aidl+" -I."; !strings.Contains(g, w) {
  342. t.Errorf("want aidl command to contain %q, got %q", w, g)
  343. }
  344. }
  345. })
  346. // Test again with PLATFORM_VERSION_CODENAME=REL, javac -source 8 -target 8
  347. t.Run("REL + Java language level 8", func(t *testing.T) {
  348. config := testConfig(nil, bpJava8, nil)
  349. config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("REL")
  350. config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(true)
  351. if testcase.unbundled {
  352. config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
  353. config.TestProductVariables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
  354. }
  355. ctx := testContext()
  356. run(t, ctx, config)
  357. checkClasspath(t, ctx, true /* isJava8 */)
  358. })
  359. // Test again with PLATFORM_VERSION_CODENAME=REL, javac -source 9 -target 9
  360. t.Run("REL + Java language level 9", func(t *testing.T) {
  361. config := testConfig(nil, bp, nil)
  362. config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("REL")
  363. config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(true)
  364. if testcase.unbundled {
  365. config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
  366. config.TestProductVariables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
  367. }
  368. ctx := testContext()
  369. run(t, ctx, config)
  370. checkClasspath(t, ctx, false /* isJava8 */)
  371. })
  372. })
  373. }
  374. }