Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 1 | // Copyright 2022 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
| 18 | "testing" |
| 19 | |
| 20 | "android/soong/android" |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame^] | 21 | |
| 22 | "github.com/google/blueprint/proptools" |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | func TestR8(t *testing.T) { |
| 26 | result := PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd.RunTestWithBp(t, ` |
| 27 | android_app { |
| 28 | name: "app", |
| 29 | srcs: ["foo.java"], |
| 30 | libs: ["lib"], |
| 31 | static_libs: ["static_lib"], |
| 32 | platform_apis: true, |
| 33 | } |
| 34 | |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 35 | android_app { |
| 36 | name: "stable_app", |
| 37 | srcs: ["foo.java"], |
| 38 | sdk_version: "current", |
| 39 | min_sdk_version: "31", |
| 40 | } |
| 41 | |
| 42 | android_app { |
| 43 | name: "core_platform_app", |
| 44 | srcs: ["foo.java"], |
| 45 | sdk_version: "core_platform", |
| 46 | } |
| 47 | |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 48 | java_library { |
| 49 | name: "lib", |
| 50 | srcs: ["foo.java"], |
| 51 | } |
| 52 | |
| 53 | java_library { |
| 54 | name: "static_lib", |
| 55 | srcs: ["foo.java"], |
| 56 | } |
| 57 | `) |
| 58 | |
| 59 | app := result.ModuleForTests("app", "android_common") |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 60 | stableApp := result.ModuleForTests("stable_app", "android_common") |
| 61 | corePlatformApp := result.ModuleForTests("core_platform_app", "android_common") |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 62 | lib := result.ModuleForTests("lib", "android_common") |
| 63 | staticLib := result.ModuleForTests("static_lib", "android_common") |
| 64 | |
| 65 | appJavac := app.Rule("javac") |
| 66 | appR8 := app.Rule("r8") |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 67 | stableAppR8 := stableApp.Rule("r8") |
| 68 | corePlatformAppR8 := corePlatformApp.Rule("r8") |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 69 | libHeader := lib.Output("turbine-combined/lib.jar").Output |
| 70 | staticLibHeader := staticLib.Output("turbine-combined/static_lib.jar").Output |
| 71 | |
| 72 | android.AssertStringDoesContain(t, "expected lib header jar in app javac classpath", |
| 73 | appJavac.Args["classpath"], libHeader.String()) |
| 74 | android.AssertStringDoesContain(t, "expected static_lib header jar in app javac classpath", |
| 75 | appJavac.Args["classpath"], staticLibHeader.String()) |
| 76 | |
| 77 | android.AssertStringDoesContain(t, "expected lib header jar in app r8 classpath", |
| 78 | appR8.Args["r8Flags"], libHeader.String()) |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame^] | 79 | android.AssertStringDoesNotContain(t, "expected no static_lib header jar in app r8 classpath", |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 80 | appR8.Args["r8Flags"], staticLibHeader.String()) |
Remi NGUYEN VAN | bdad314 | 2022-08-04 13:19:03 +0900 | [diff] [blame] | 81 | android.AssertStringDoesContain(t, "expected -ignorewarnings in app r8 flags", |
| 82 | appR8.Args["r8Flags"], "-ignorewarnings") |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 83 | android.AssertStringDoesContain(t, "expected --android-platform-build in app r8 flags", |
| 84 | appR8.Args["r8Flags"], "--android-platform-build") |
| 85 | android.AssertStringDoesNotContain(t, "expected no --android-platform-build in stable_app r8 flags", |
| 86 | stableAppR8.Args["r8Flags"], "--android-platform-build") |
| 87 | android.AssertStringDoesContain(t, "expected --android-platform-build in core_platform_app r8 flags", |
| 88 | corePlatformAppR8.Args["r8Flags"], "--android-platform-build") |
Remi NGUYEN VAN | bdad314 | 2022-08-04 13:19:03 +0900 | [diff] [blame] | 89 | } |
| 90 | |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame^] | 91 | func TestR8TransitiveDeps(t *testing.T) { |
| 92 | bp := ` |
| 93 | override_android_app { |
| 94 | name: "override_app", |
| 95 | base: "app", |
| 96 | } |
| 97 | |
| 98 | android_app { |
| 99 | name: "app", |
| 100 | srcs: ["foo.java"], |
| 101 | libs: [ |
| 102 | "lib", |
| 103 | "uses_libs_dep_import", |
| 104 | ], |
| 105 | static_libs: [ |
| 106 | "static_lib", |
| 107 | "repeated_dep", |
| 108 | ], |
| 109 | platform_apis: true, |
| 110 | } |
| 111 | |
| 112 | java_library { |
| 113 | name: "static_lib", |
| 114 | srcs: ["foo.java"], |
| 115 | } |
| 116 | |
| 117 | java_library { |
| 118 | name: "lib", |
| 119 | libs: [ |
| 120 | "transitive_lib", |
| 121 | "repeated_dep", |
| 122 | "prebuilt_lib", |
| 123 | ], |
| 124 | static_libs: ["transitive_static_lib"], |
| 125 | srcs: ["foo.java"], |
| 126 | } |
| 127 | |
| 128 | java_library { |
| 129 | name: "repeated_dep", |
| 130 | srcs: ["foo.java"], |
| 131 | } |
| 132 | |
| 133 | java_library { |
| 134 | name: "transitive_static_lib", |
| 135 | srcs: ["foo.java"], |
| 136 | } |
| 137 | |
| 138 | java_library { |
| 139 | name: "transitive_lib", |
| 140 | srcs: ["foo.java"], |
| 141 | libs: ["transitive_lib_2"], |
| 142 | } |
| 143 | |
| 144 | java_library { |
| 145 | name: "transitive_lib_2", |
| 146 | srcs: ["foo.java"], |
| 147 | } |
| 148 | |
| 149 | java_import { |
| 150 | name: "lib", |
| 151 | jars: ["lib.jar"], |
| 152 | } |
| 153 | |
| 154 | java_library { |
| 155 | name: "uses_lib", |
| 156 | srcs: ["foo.java"], |
| 157 | } |
| 158 | |
| 159 | java_library { |
| 160 | name: "optional_uses_lib", |
| 161 | srcs: ["foo.java"], |
| 162 | } |
| 163 | |
| 164 | android_library { |
| 165 | name: "uses_libs_dep", |
| 166 | uses_libs: ["uses_lib"], |
| 167 | optional_uses_libs: ["optional_uses_lib"], |
| 168 | } |
| 169 | |
| 170 | android_library_import { |
| 171 | name: "uses_libs_dep_import", |
| 172 | aars: ["aar.aar"], |
| 173 | static_libs: ["uses_libs_dep"], |
| 174 | } |
| 175 | ` |
| 176 | |
| 177 | testcases := []struct { |
| 178 | name string |
| 179 | unbundled bool |
| 180 | }{ |
| 181 | { |
| 182 | name: "non-unbundled build", |
| 183 | unbundled: false, |
| 184 | }, |
| 185 | { |
| 186 | name: "unbundled build", |
| 187 | unbundled: true, |
| 188 | }, |
| 189 | } |
| 190 | |
| 191 | for _, tc := range testcases { |
| 192 | t.Run(tc.name, func(t *testing.T) { |
| 193 | fixturePreparer := PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd |
| 194 | if tc.unbundled { |
| 195 | fixturePreparer = android.GroupFixturePreparers( |
| 196 | fixturePreparer, |
| 197 | android.FixtureModifyProductVariables( |
| 198 | func(variables android.FixtureProductVariables) { |
| 199 | variables.Unbundled_build = proptools.BoolPtr(true) |
| 200 | }, |
| 201 | ), |
| 202 | ) |
| 203 | } |
| 204 | result := fixturePreparer.RunTestWithBp(t, bp) |
| 205 | |
| 206 | getHeaderJar := func(name string) android.Path { |
| 207 | mod := result.ModuleForTests(name, "android_common") |
| 208 | return mod.Output("turbine-combined/" + name + ".jar").Output |
| 209 | } |
| 210 | |
| 211 | appR8 := result.ModuleForTests("app", "android_common").Rule("r8") |
| 212 | overrideAppR8 := result.ModuleForTests("app", "android_common_override_app").Rule("r8") |
| 213 | appHeader := getHeaderJar("app") |
| 214 | overrideAppHeader := result.ModuleForTests("app", "android_common_override_app").Output("turbine-combined/app.jar").Output |
| 215 | libHeader := getHeaderJar("lib") |
| 216 | transitiveLibHeader := getHeaderJar("transitive_lib") |
| 217 | transitiveLib2Header := getHeaderJar("transitive_lib_2") |
| 218 | staticLibHeader := getHeaderJar("static_lib") |
| 219 | transitiveStaticLibHeader := getHeaderJar("transitive_static_lib") |
| 220 | repeatedDepHeader := getHeaderJar("repeated_dep") |
| 221 | usesLibHeader := getHeaderJar("uses_lib") |
| 222 | optionalUsesLibHeader := getHeaderJar("optional_uses_lib") |
| 223 | prebuiltLibHeader := result.ModuleForTests("prebuilt_lib", "android_common").Output("combined/lib.jar").Output |
| 224 | |
| 225 | for _, rule := range []android.TestingBuildParams{appR8, overrideAppR8} { |
| 226 | android.AssertStringDoesNotContain(t, "expected no app header jar in app r8 classpath", |
| 227 | rule.Args["r8Flags"], appHeader.String()) |
| 228 | android.AssertStringDoesNotContain(t, "expected no override_app header jar in app r8 classpath", |
| 229 | rule.Args["r8Flags"], overrideAppHeader.String()) |
| 230 | android.AssertStringDoesContain(t, "expected transitive lib header jar in app r8 classpath", |
| 231 | rule.Args["r8Flags"], transitiveLibHeader.String()) |
| 232 | android.AssertStringDoesContain(t, "expected transitive lib ^2 header jar in app r8 classpath", |
| 233 | rule.Args["r8Flags"], transitiveLib2Header.String()) |
| 234 | android.AssertStringDoesContain(t, "expected lib header jar in app r8 classpath", |
| 235 | rule.Args["r8Flags"], libHeader.String()) |
| 236 | android.AssertStringDoesContain(t, "expected uses_lib header jar in app r8 classpath", |
| 237 | rule.Args["r8Flags"], usesLibHeader.String()) |
| 238 | android.AssertStringDoesContain(t, "expected optional_uses_lib header jar in app r8 classpath", |
| 239 | rule.Args["r8Flags"], optionalUsesLibHeader.String()) |
| 240 | android.AssertStringDoesNotContain(t, "expected no static_lib header jar in app r8 classpath", |
| 241 | rule.Args["r8Flags"], staticLibHeader.String()) |
| 242 | android.AssertStringDoesNotContain(t, "expected no transitive static_lib header jar in app r8 classpath", |
| 243 | rule.Args["r8Flags"], transitiveStaticLibHeader.String()) |
| 244 | // we shouldn't list this dep because it is already included as static_libs in the app |
| 245 | android.AssertStringDoesNotContain(t, "expected no repeated_dep header jar in app r8 classpath", |
| 246 | rule.Args["r8Flags"], repeatedDepHeader.String()) |
| 247 | // skip a prebuilt transitive dep if the source is also a transitive dep |
| 248 | android.AssertStringDoesNotContain(t, "expected no prebuilt header jar in app r8 classpath", |
| 249 | rule.Args["r8Flags"], prebuiltLibHeader.String()) |
| 250 | android.AssertStringDoesContain(t, "expected -ignorewarnings in app r8 flags", |
| 251 | rule.Args["r8Flags"], "-ignorewarnings") |
| 252 | android.AssertStringDoesContain(t, "expected --android-platform-build in app r8 flags", |
| 253 | rule.Args["r8Flags"], "--android-platform-build") |
| 254 | } |
| 255 | }) |
| 256 | } |
| 257 | } |
| 258 | |
Remi NGUYEN VAN | bdad314 | 2022-08-04 13:19:03 +0900 | [diff] [blame] | 259 | func TestR8Flags(t *testing.T) { |
| 260 | result := PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd.RunTestWithBp(t, ` |
| 261 | android_app { |
| 262 | name: "app", |
| 263 | srcs: ["foo.java"], |
| 264 | platform_apis: true, |
| 265 | optimize: { |
| 266 | shrink: false, |
| 267 | optimize: false, |
| 268 | obfuscate: false, |
| 269 | ignore_warnings: false, |
| 270 | }, |
| 271 | } |
| 272 | `) |
| 273 | |
| 274 | app := result.ModuleForTests("app", "android_common") |
| 275 | appR8 := app.Rule("r8") |
| 276 | android.AssertStringDoesContain(t, "expected -dontshrink in app r8 flags", |
| 277 | appR8.Args["r8Flags"], "-dontshrink") |
| 278 | android.AssertStringDoesContain(t, "expected -dontoptimize in app r8 flags", |
| 279 | appR8.Args["r8Flags"], "-dontoptimize") |
| 280 | android.AssertStringDoesContain(t, "expected -dontobfuscate in app r8 flags", |
| 281 | appR8.Args["r8Flags"], "-dontobfuscate") |
| 282 | android.AssertStringDoesNotContain(t, "expected no -ignorewarnings in app r8 flags", |
| 283 | appR8.Args["r8Flags"], "-ignorewarnings") |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 284 | android.AssertStringDoesContain(t, "expected --android-platform-build in app r8 flags", |
| 285 | appR8.Args["r8Flags"], "--android-platform-build") |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | func TestD8(t *testing.T) { |
| 289 | result := PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd.RunTestWithBp(t, ` |
| 290 | java_library { |
| 291 | name: "foo", |
| 292 | srcs: ["foo.java"], |
| 293 | libs: ["lib"], |
| 294 | static_libs: ["static_lib"], |
| 295 | installable: true, |
| 296 | } |
| 297 | |
| 298 | java_library { |
| 299 | name: "lib", |
| 300 | srcs: ["foo.java"], |
| 301 | } |
| 302 | |
| 303 | java_library { |
| 304 | name: "static_lib", |
| 305 | srcs: ["foo.java"], |
| 306 | } |
| 307 | `) |
| 308 | |
| 309 | foo := result.ModuleForTests("foo", "android_common") |
| 310 | lib := result.ModuleForTests("lib", "android_common") |
| 311 | staticLib := result.ModuleForTests("static_lib", "android_common") |
| 312 | |
| 313 | fooJavac := foo.Rule("javac") |
| 314 | fooD8 := foo.Rule("d8") |
| 315 | libHeader := lib.Output("turbine-combined/lib.jar").Output |
| 316 | staticLibHeader := staticLib.Output("turbine-combined/static_lib.jar").Output |
| 317 | |
| 318 | android.AssertStringDoesContain(t, "expected lib header jar in foo javac classpath", |
| 319 | fooJavac.Args["classpath"], libHeader.String()) |
| 320 | android.AssertStringDoesContain(t, "expected static_lib header jar in foo javac classpath", |
| 321 | fooJavac.Args["classpath"], staticLibHeader.String()) |
| 322 | |
| 323 | android.AssertStringDoesContain(t, "expected lib header jar in foo d8 classpath", |
| 324 | fooD8.Args["d8Flags"], libHeader.String()) |
| 325 | android.AssertStringDoesNotContain(t, "expected no static_lib header jar in foo javac classpath", |
| 326 | fooD8.Args["d8Flags"], staticLibHeader.String()) |
| 327 | } |
Jared Duke | 5979b30 | 2022-12-19 21:08:39 +0000 | [diff] [blame] | 328 | |
| 329 | func TestProguardFlagsInheritance(t *testing.T) { |
| 330 | result := PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd.RunTestWithBp(t, ` |
| 331 | android_app { |
| 332 | name: "app", |
| 333 | static_libs: [ |
| 334 | "primary_android_lib", |
| 335 | "primary_lib", |
| 336 | ], |
| 337 | platform_apis: true, |
| 338 | } |
| 339 | |
| 340 | java_library { |
| 341 | name: "primary_lib", |
| 342 | optimize: { |
| 343 | proguard_flags_files: ["primary.flags"], |
| 344 | }, |
| 345 | } |
| 346 | |
| 347 | android_library { |
| 348 | name: "primary_android_lib", |
| 349 | static_libs: ["secondary_lib"], |
| 350 | optimize: { |
| 351 | proguard_flags_files: ["primary_android.flags"], |
| 352 | }, |
| 353 | } |
| 354 | |
| 355 | java_library { |
| 356 | name: "secondary_lib", |
| 357 | static_libs: ["tertiary_lib"], |
| 358 | optimize: { |
| 359 | proguard_flags_files: ["secondary.flags"], |
| 360 | }, |
| 361 | } |
| 362 | |
| 363 | java_library { |
| 364 | name: "tertiary_lib", |
| 365 | optimize: { |
| 366 | proguard_flags_files: ["tertiary.flags"], |
| 367 | }, |
| 368 | } |
| 369 | `) |
| 370 | |
| 371 | app := result.ModuleForTests("app", "android_common") |
| 372 | appR8 := app.Rule("r8") |
| 373 | android.AssertStringDoesContain(t, "expected primary_lib's proguard flags from direct dep", |
| 374 | appR8.Args["r8Flags"], "primary.flags") |
| 375 | android.AssertStringDoesContain(t, "expected primary_android_lib's proguard flags from direct dep", |
| 376 | appR8.Args["r8Flags"], "primary_android.flags") |
| 377 | android.AssertStringDoesContain(t, "expected secondary_lib's proguard flags from inherited dep", |
| 378 | appR8.Args["r8Flags"], "secondary.flags") |
| 379 | android.AssertStringDoesContain(t, "expected tertiary_lib's proguard flags from inherited dep", |
| 380 | appR8.Args["r8Flags"], "tertiary.flags") |
| 381 | } |