Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 1 | // Copyright 2018 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 ( |
Martin Stjernholm | 0e4cceb | 2021-05-13 02:38:35 +0100 | [diff] [blame] | 18 | "fmt" |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 19 | "runtime" |
Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 20 | "testing" |
Martin Stjernholm | 0e4cceb | 2021-05-13 02:38:35 +0100 | [diff] [blame] | 21 | |
| 22 | "android/soong/android" |
| 23 | "android/soong/cc" |
| 24 | "android/soong/dexpreopt" |
Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | func TestDexpreoptEnabled(t *testing.T) { |
| 28 | tests := []struct { |
Adrian Roos | 92346c4 | 2021-09-15 14:11:07 +0000 | [diff] [blame^] | 29 | name string |
| 30 | bp string |
| 31 | enabled bool |
Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 32 | }{ |
| 33 | { |
| 34 | name: "app", |
| 35 | bp: ` |
| 36 | android_app { |
| 37 | name: "foo", |
| 38 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 39 | sdk_version: "current", |
Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 40 | }`, |
| 41 | enabled: true, |
| 42 | }, |
| 43 | { |
| 44 | name: "installable java library", |
| 45 | bp: ` |
| 46 | java_library { |
| 47 | name: "foo", |
| 48 | installable: true, |
| 49 | srcs: ["a.java"], |
| 50 | }`, |
| 51 | enabled: true, |
| 52 | }, |
| 53 | { |
| 54 | name: "java binary", |
| 55 | bp: ` |
| 56 | java_binary { |
| 57 | name: "foo", |
| 58 | srcs: ["a.java"], |
| 59 | }`, |
| 60 | enabled: true, |
| 61 | }, |
Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 62 | { |
| 63 | name: "app without sources", |
| 64 | bp: ` |
| 65 | android_app { |
| 66 | name: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 67 | sdk_version: "current", |
Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 68 | }`, |
Jaewoong Jung | a24af3b | 2019-05-13 09:23:20 -0700 | [diff] [blame] | 69 | enabled: false, |
| 70 | }, |
| 71 | { |
| 72 | name: "app with libraries", |
| 73 | bp: ` |
| 74 | android_app { |
| 75 | name: "foo", |
| 76 | static_libs: ["lib"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 77 | sdk_version: "current", |
Jaewoong Jung | a24af3b | 2019-05-13 09:23:20 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | java_library { |
| 81 | name: "lib", |
| 82 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 83 | sdk_version: "current", |
Jaewoong Jung | a24af3b | 2019-05-13 09:23:20 -0700 | [diff] [blame] | 84 | }`, |
Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 85 | enabled: true, |
| 86 | }, |
| 87 | { |
| 88 | name: "installable java library without sources", |
| 89 | bp: ` |
| 90 | java_library { |
| 91 | name: "foo", |
| 92 | installable: true, |
| 93 | }`, |
Jaewoong Jung | a24af3b | 2019-05-13 09:23:20 -0700 | [diff] [blame] | 94 | enabled: false, |
Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 95 | }, |
Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 96 | { |
| 97 | name: "static java library", |
| 98 | bp: ` |
| 99 | java_library { |
| 100 | name: "foo", |
| 101 | srcs: ["a.java"], |
| 102 | }`, |
| 103 | enabled: false, |
| 104 | }, |
| 105 | { |
Colin Cross | e302687 | 2019-01-05 22:30:13 -0800 | [diff] [blame] | 106 | name: "java test", |
| 107 | bp: ` |
| 108 | java_test { |
| 109 | name: "foo", |
| 110 | srcs: ["a.java"], |
| 111 | }`, |
| 112 | enabled: false, |
| 113 | }, |
| 114 | { |
Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 115 | name: "android test", |
| 116 | bp: ` |
| 117 | android_test { |
| 118 | name: "foo", |
| 119 | srcs: ["a.java"], |
| 120 | }`, |
| 121 | enabled: false, |
| 122 | }, |
| 123 | { |
| 124 | name: "android test helper app", |
| 125 | bp: ` |
| 126 | android_test_helper_app { |
| 127 | name: "foo", |
| 128 | srcs: ["a.java"], |
| 129 | }`, |
| 130 | enabled: false, |
| 131 | }, |
Colin Cross | dc2da91 | 2019-01-05 22:13:05 -0800 | [diff] [blame] | 132 | { |
| 133 | name: "compile_dex", |
| 134 | bp: ` |
| 135 | java_library { |
| 136 | name: "foo", |
| 137 | srcs: ["a.java"], |
| 138 | compile_dex: true, |
| 139 | }`, |
| 140 | enabled: false, |
| 141 | }, |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 142 | { |
| 143 | name: "dex_import", |
| 144 | bp: ` |
| 145 | dex_import { |
| 146 | name: "foo", |
| 147 | jars: ["a.jar"], |
| 148 | }`, |
| 149 | enabled: true, |
| 150 | }, |
Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | for _, test := range tests { |
| 154 | t.Run(test.name, func(t *testing.T) { |
Adrian Roos | 92346c4 | 2021-09-15 14:11:07 +0000 | [diff] [blame^] | 155 | ctx, _ := testJava(t, test.bp) |
Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 156 | |
Adrian Roos | 92346c4 | 2021-09-15 14:11:07 +0000 | [diff] [blame^] | 157 | dexpreopt := ctx.ModuleForTests("foo", "android_common").MaybeRule("dexpreopt") |
Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 158 | enabled := dexpreopt.Rule != nil |
| 159 | |
| 160 | if enabled != test.enabled { |
| 161 | t.Fatalf("want dexpreopt %s, got %s", enabledString(test.enabled), enabledString(enabled)) |
| 162 | } |
| 163 | }) |
| 164 | |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | func enabledString(enabled bool) string { |
| 169 | if enabled { |
| 170 | return "enabled" |
| 171 | } else { |
| 172 | return "disabled" |
| 173 | } |
| 174 | } |
Martin Stjernholm | 0e4cceb | 2021-05-13 02:38:35 +0100 | [diff] [blame] | 175 | |
| 176 | func TestDex2oatToolDeps(t *testing.T) { |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 177 | if runtime.GOOS != "linux" { |
Martin Stjernholm | 1b784a7 | 2021-05-20 15:24:34 +0100 | [diff] [blame] | 178 | // The host binary paths checked below are build OS dependent. |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 179 | t.Skipf("Unsupported build OS %s", runtime.GOOS) |
Martin Stjernholm | 1b784a7 | 2021-05-20 15:24:34 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Martin Stjernholm | 0e4cceb | 2021-05-13 02:38:35 +0100 | [diff] [blame] | 182 | preparers := android.GroupFixturePreparers( |
| 183 | cc.PrepareForTestWithCcDefaultModules, |
| 184 | PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd, |
| 185 | dexpreopt.PrepareForTestByEnablingDexpreopt) |
| 186 | |
| 187 | testDex2oatToolDep := func(sourceEnabled, prebuiltEnabled, prebuiltPreferred bool, |
| 188 | expectedDex2oatPath string) { |
| 189 | name := fmt.Sprintf("sourceEnabled:%t,prebuiltEnabled:%t,prebuiltPreferred:%t", |
| 190 | sourceEnabled, prebuiltEnabled, prebuiltPreferred) |
| 191 | t.Run(name, func(t *testing.T) { |
| 192 | result := preparers.RunTestWithBp(t, fmt.Sprintf(` |
| 193 | cc_binary { |
| 194 | name: "dex2oatd", |
| 195 | enabled: %t, |
| 196 | host_supported: true, |
| 197 | } |
| 198 | cc_prebuilt_binary { |
| 199 | name: "dex2oatd", |
| 200 | enabled: %t, |
| 201 | prefer: %t, |
| 202 | host_supported: true, |
| 203 | srcs: ["x86_64/bin/dex2oatd"], |
| 204 | } |
| 205 | java_library { |
| 206 | name: "myjavalib", |
| 207 | } |
| 208 | `, sourceEnabled, prebuiltEnabled, prebuiltPreferred)) |
| 209 | pathContext := android.PathContextForTesting(result.Config) |
| 210 | dex2oatPath := dexpreopt.GetCachedGlobalSoongConfig(pathContext).Dex2oat |
| 211 | android.AssertStringEquals(t, "Testing "+name, expectedDex2oatPath, android.NormalizePathForTesting(dex2oatPath)) |
| 212 | }) |
| 213 | } |
| 214 | |
| 215 | sourceDex2oatPath := "host/linux-x86/bin/dex2oatd" |
| 216 | prebuiltDex2oatPath := ".intermediates/prebuilt_dex2oatd/linux_glibc_x86_64/dex2oatd" |
| 217 | |
| 218 | testDex2oatToolDep(true, false, false, sourceDex2oatPath) |
| 219 | testDex2oatToolDep(true, true, false, sourceDex2oatPath) |
| 220 | testDex2oatToolDep(true, true, true, prebuiltDex2oatPath) |
| 221 | testDex2oatToolDep(false, true, false, prebuiltDex2oatPath) |
| 222 | } |