Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 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 | |
| 15 | package sdk |
| 16 | |
| 17 | import ( |
Martin Stjernholm | 3ff2e66 | 2020-07-15 14:38:15 +0100 | [diff] [blame] | 18 | "log" |
| 19 | "os" |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 20 | "runtime" |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 21 | "testing" |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 22 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 23 | "android/soong/android" |
Paul Duffin | 39abf8f | 2021-09-24 14:58:27 +0100 | [diff] [blame] | 24 | "android/soong/java" |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 25 | |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 26 | "github.com/google/blueprint/proptools" |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 27 | ) |
| 28 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 29 | // Needed in an _test.go file in this package to ensure tests run correctly, particularly in IDE. |
| 30 | func TestMain(m *testing.M) { |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 31 | if runtime.GOOS != "linux" { |
Martin Stjernholm | 3ff2e66 | 2020-07-15 14:38:15 +0100 | [diff] [blame] | 32 | // b/145598135 - Generating host snapshots for anything other than linux is not supported. |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 33 | log.Printf("Skipping as sdk snapshot generation is only supported on linux not %s", runtime.GOOS) |
Martin Stjernholm | 3ff2e66 | 2020-07-15 14:38:15 +0100 | [diff] [blame] | 34 | os.Exit(0) |
| 35 | } |
| 36 | |
Paul Duffin | abbf63d | 2021-03-18 01:47:31 +0000 | [diff] [blame] | 37 | os.Exit(m.Run()) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 38 | } |
| 39 | |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 40 | // Ensure that prebuilt modules have the same effective visibility as the source |
| 41 | // modules. |
| 42 | func TestSnapshotVisibility(t *testing.T) { |
| 43 | packageBp := ` |
| 44 | package { |
| 45 | default_visibility: ["//other/foo"], |
| 46 | } |
| 47 | |
| 48 | sdk { |
| 49 | name: "mysdk", |
| 50 | visibility: [ |
| 51 | "//other/foo", |
| 52 | // This short form will be replaced with //package:__subpackages__ in the |
| 53 | // generated sdk_snapshot. |
| 54 | ":__subpackages__", |
| 55 | ], |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 56 | prebuilt_visibility: [ |
| 57 | "//prebuilts/mysdk", |
| 58 | ], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 59 | java_header_libs: [ |
| 60 | "myjavalib", |
| 61 | "mypublicjavalib", |
| 62 | "mydefaultedjavalib", |
Martin Stjernholm | 64aeaad | 2020-05-13 22:11:40 +0100 | [diff] [blame] | 63 | "myprivatejavalib", |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 64 | ], |
| 65 | } |
| 66 | |
| 67 | java_library { |
| 68 | name: "myjavalib", |
| 69 | // Uses package default visibility |
| 70 | srcs: ["Test.java"], |
| 71 | system_modules: "none", |
| 72 | sdk_version: "none", |
| 73 | } |
| 74 | |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 75 | java_defaults { |
| 76 | name: "java-defaults", |
Jooyung Han | 5e9013b | 2020-03-10 06:23:13 +0900 | [diff] [blame] | 77 | visibility: ["//other/bar"], |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 80 | java_library { |
| 81 | name: "mypublicjavalib", |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 82 | defaults: ["java-defaults"], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 83 | visibility: ["//visibility:public"], |
| 84 | srcs: ["Test.java"], |
| 85 | system_modules: "none", |
| 86 | sdk_version: "none", |
| 87 | } |
| 88 | |
| 89 | java_defaults { |
| 90 | name: "myjavadefaults", |
| 91 | visibility: ["//other/bar"], |
| 92 | } |
| 93 | |
| 94 | java_library { |
| 95 | name: "mydefaultedjavalib", |
| 96 | defaults: ["myjavadefaults"], |
| 97 | srcs: ["Test.java"], |
| 98 | system_modules: "none", |
| 99 | sdk_version: "none", |
| 100 | } |
Martin Stjernholm | 64aeaad | 2020-05-13 22:11:40 +0100 | [diff] [blame] | 101 | |
| 102 | java_library { |
| 103 | name: "myprivatejavalib", |
| 104 | srcs: ["Test.java"], |
| 105 | visibility: ["//visibility:private"], |
| 106 | system_modules: "none", |
| 107 | sdk_version: "none", |
| 108 | } |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 109 | ` |
| 110 | |
| 111 | result := testSdkWithFs(t, ``, |
| 112 | map[string][]byte{ |
| 113 | "package/Test.java": nil, |
| 114 | "package/Android.bp": []byte(packageBp), |
| 115 | }) |
| 116 | |
Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 117 | CheckSnapshot(t, result, "mysdk", "package", |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 118 | checkAndroidBpContents(` |
| 119 | // This is auto-generated. DO NOT EDIT. |
| 120 | |
| 121 | java_import { |
| 122 | name: "mysdk_myjavalib@current", |
| 123 | sdk_member_name: "myjavalib", |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame] | 124 | visibility: [ |
| 125 | "//other/foo", |
| 126 | "//package", |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 127 | "//prebuilts/mysdk", |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame] | 128 | ], |
Martin Stjernholm | 1e04109 | 2020-11-03 00:11:09 +0000 | [diff] [blame] | 129 | apex_available: ["//apex_available:platform"], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 130 | jars: ["java/myjavalib.jar"], |
| 131 | } |
| 132 | |
| 133 | java_import { |
| 134 | name: "myjavalib", |
| 135 | prefer: false, |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame] | 136 | visibility: [ |
| 137 | "//other/foo", |
| 138 | "//package", |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 139 | "//prebuilts/mysdk", |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame] | 140 | ], |
Martin Stjernholm | 1e04109 | 2020-11-03 00:11:09 +0000 | [diff] [blame] | 141 | apex_available: ["//apex_available:platform"], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 142 | jars: ["java/myjavalib.jar"], |
| 143 | } |
| 144 | |
| 145 | java_import { |
| 146 | name: "mysdk_mypublicjavalib@current", |
| 147 | sdk_member_name: "mypublicjavalib", |
| 148 | visibility: ["//visibility:public"], |
Martin Stjernholm | 1e04109 | 2020-11-03 00:11:09 +0000 | [diff] [blame] | 149 | apex_available: ["//apex_available:platform"], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 150 | jars: ["java/mypublicjavalib.jar"], |
| 151 | } |
| 152 | |
| 153 | java_import { |
| 154 | name: "mypublicjavalib", |
| 155 | prefer: false, |
| 156 | visibility: ["//visibility:public"], |
Martin Stjernholm | 1e04109 | 2020-11-03 00:11:09 +0000 | [diff] [blame] | 157 | apex_available: ["//apex_available:platform"], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 158 | jars: ["java/mypublicjavalib.jar"], |
| 159 | } |
| 160 | |
| 161 | java_import { |
| 162 | name: "mysdk_mydefaultedjavalib@current", |
| 163 | sdk_member_name: "mydefaultedjavalib", |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame] | 164 | visibility: [ |
| 165 | "//other/bar", |
| 166 | "//package", |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 167 | "//prebuilts/mysdk", |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame] | 168 | ], |
Martin Stjernholm | 1e04109 | 2020-11-03 00:11:09 +0000 | [diff] [blame] | 169 | apex_available: ["//apex_available:platform"], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 170 | jars: ["java/mydefaultedjavalib.jar"], |
| 171 | } |
| 172 | |
| 173 | java_import { |
| 174 | name: "mydefaultedjavalib", |
| 175 | prefer: false, |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame] | 176 | visibility: [ |
| 177 | "//other/bar", |
| 178 | "//package", |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 179 | "//prebuilts/mysdk", |
Martin Stjernholm | 0641d18 | 2020-05-13 02:20:06 +0100 | [diff] [blame] | 180 | ], |
Martin Stjernholm | 1e04109 | 2020-11-03 00:11:09 +0000 | [diff] [blame] | 181 | apex_available: ["//apex_available:platform"], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 182 | jars: ["java/mydefaultedjavalib.jar"], |
| 183 | } |
| 184 | |
Martin Stjernholm | 64aeaad | 2020-05-13 22:11:40 +0100 | [diff] [blame] | 185 | java_import { |
| 186 | name: "mysdk_myprivatejavalib@current", |
| 187 | sdk_member_name: "myprivatejavalib", |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 188 | visibility: [ |
| 189 | "//package", |
| 190 | "//prebuilts/mysdk", |
| 191 | ], |
Martin Stjernholm | 1e04109 | 2020-11-03 00:11:09 +0000 | [diff] [blame] | 192 | apex_available: ["//apex_available:platform"], |
Martin Stjernholm | 64aeaad | 2020-05-13 22:11:40 +0100 | [diff] [blame] | 193 | jars: ["java/myprivatejavalib.jar"], |
| 194 | } |
| 195 | |
| 196 | java_import { |
| 197 | name: "myprivatejavalib", |
| 198 | prefer: false, |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 199 | visibility: [ |
| 200 | "//package", |
| 201 | "//prebuilts/mysdk", |
| 202 | ], |
Martin Stjernholm | 1e04109 | 2020-11-03 00:11:09 +0000 | [diff] [blame] | 203 | apex_available: ["//apex_available:platform"], |
Martin Stjernholm | 64aeaad | 2020-05-13 22:11:40 +0100 | [diff] [blame] | 204 | jars: ["java/myprivatejavalib.jar"], |
| 205 | } |
| 206 | |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 207 | sdk_snapshot { |
| 208 | name: "mysdk@current", |
| 209 | visibility: [ |
Martin Stjernholm | 01407c5 | 2020-05-13 01:54:21 +0100 | [diff] [blame] | 210 | "//other/foo", |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 211 | "//package:__subpackages__", |
| 212 | ], |
| 213 | java_header_libs: [ |
| 214 | "mysdk_myjavalib@current", |
| 215 | "mysdk_mypublicjavalib@current", |
| 216 | "mysdk_mydefaultedjavalib@current", |
Martin Stjernholm | 64aeaad | 2020-05-13 22:11:40 +0100 | [diff] [blame] | 217 | "mysdk_myprivatejavalib@current", |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 218 | ], |
| 219 | } |
| 220 | `)) |
| 221 | } |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 222 | |
Paul Duffin | 157f40f | 2020-09-29 16:01:08 +0100 | [diff] [blame] | 223 | func TestPrebuiltVisibilityProperty_IsValidated(t *testing.T) { |
| 224 | testSdkError(t, `prebuilt_visibility: cannot mix "//visibility:private" with any other visibility rules`, ` |
| 225 | sdk { |
| 226 | name: "mysdk", |
| 227 | prebuilt_visibility: [ |
| 228 | "//foo", |
| 229 | "//visibility:private", |
| 230 | ], |
| 231 | } |
| 232 | `) |
| 233 | } |
| 234 | |
| 235 | func TestPrebuiltVisibilityProperty_AddPrivate(t *testing.T) { |
| 236 | testSdkError(t, `prebuilt_visibility: "//visibility:private" does not widen the visibility`, ` |
| 237 | sdk { |
| 238 | name: "mysdk", |
| 239 | prebuilt_visibility: [ |
| 240 | "//visibility:private", |
| 241 | ], |
| 242 | java_header_libs: [ |
| 243 | "myjavalib", |
| 244 | ], |
| 245 | } |
| 246 | |
| 247 | java_library { |
| 248 | name: "myjavalib", |
| 249 | // Uses package default visibility |
| 250 | srcs: ["Test.java"], |
| 251 | system_modules: "none", |
| 252 | sdk_version: "none", |
| 253 | } |
| 254 | `) |
| 255 | } |
| 256 | |
Paul Duffin | 8edc99c | 2021-03-09 23:02:20 +0000 | [diff] [blame] | 257 | func TestSdkInstall(t *testing.T) { |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 258 | sdk := ` |
| 259 | sdk { |
| 260 | name: "mysdk", |
| 261 | } |
| 262 | ` |
Paul Duffin | 8edc99c | 2021-03-09 23:02:20 +0000 | [diff] [blame] | 263 | result := testSdkWithFs(t, sdk, nil) |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 264 | |
Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 265 | CheckSnapshot(t, result, "mysdk", "", |
| 266 | checkAllOtherCopyRules(`.intermediates/mysdk/common_os/mysdk-current.zip -> mysdk-current.zip`)) |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 267 | } |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 268 | |
| 269 | type EmbeddedPropertiesStruct struct { |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 270 | S_Embedded_Common string `android:"arch_variant"` |
| 271 | S_Embedded_Different string `android:"arch_variant"` |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | type testPropertiesStruct struct { |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 275 | name string |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 276 | private string |
| 277 | Public_Kept string `sdk:"keep"` |
| 278 | S_Common string |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 279 | S_Different string `android:"arch_variant"` |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 280 | A_Common []string |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 281 | A_Different []string `android:"arch_variant"` |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 282 | F_Common *bool |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 283 | F_Different *bool `android:"arch_variant"` |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 284 | EmbeddedPropertiesStruct |
| 285 | } |
| 286 | |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 287 | func (p *testPropertiesStruct) optimizableProperties() interface{} { |
| 288 | return p |
| 289 | } |
| 290 | |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 291 | func (p *testPropertiesStruct) String() string { |
| 292 | return p.name |
| 293 | } |
| 294 | |
| 295 | var _ propertiesContainer = (*testPropertiesStruct)(nil) |
| 296 | |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 297 | func TestCommonValueOptimization(t *testing.T) { |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 298 | common := &testPropertiesStruct{name: "common"} |
Paul Duffin | f34f6d8 | 2020-04-30 15:48:31 +0100 | [diff] [blame] | 299 | structs := []propertiesContainer{ |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 300 | &testPropertiesStruct{ |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 301 | name: "struct-0", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 302 | private: "common", |
| 303 | Public_Kept: "common", |
| 304 | S_Common: "common", |
| 305 | S_Different: "upper", |
| 306 | A_Common: []string{"first", "second"}, |
| 307 | A_Different: []string{"alpha", "beta"}, |
| 308 | F_Common: proptools.BoolPtr(false), |
| 309 | F_Different: proptools.BoolPtr(false), |
| 310 | EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{ |
| 311 | S_Embedded_Common: "embedded_common", |
| 312 | S_Embedded_Different: "embedded_upper", |
| 313 | }, |
| 314 | }, |
| 315 | &testPropertiesStruct{ |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 316 | name: "struct-1", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 317 | private: "common", |
| 318 | Public_Kept: "common", |
| 319 | S_Common: "common", |
| 320 | S_Different: "lower", |
| 321 | A_Common: []string{"first", "second"}, |
| 322 | A_Different: []string{"alpha", "delta"}, |
| 323 | F_Common: proptools.BoolPtr(false), |
| 324 | F_Different: proptools.BoolPtr(true), |
| 325 | EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{ |
| 326 | S_Embedded_Common: "embedded_common", |
| 327 | S_Embedded_Different: "embedded_lower", |
| 328 | }, |
| 329 | }, |
| 330 | } |
| 331 | |
| 332 | extractor := newCommonValueExtractor(common) |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 333 | |
Paul Duffin | c459f89 | 2020-04-30 18:08:29 +0100 | [diff] [blame] | 334 | err := extractor.extractCommonProperties(common, structs) |
Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 335 | android.AssertDeepEquals(t, "unexpected error", nil, err) |
Paul Duffin | c459f89 | 2020-04-30 18:08:29 +0100 | [diff] [blame] | 336 | |
Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 337 | android.AssertDeepEquals(t, "common properties not correct", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 338 | &testPropertiesStruct{ |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 339 | name: "common", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 340 | private: "", |
| 341 | Public_Kept: "", |
| 342 | S_Common: "common", |
| 343 | S_Different: "", |
| 344 | A_Common: []string{"first", "second"}, |
| 345 | A_Different: []string(nil), |
| 346 | F_Common: proptools.BoolPtr(false), |
| 347 | F_Different: nil, |
| 348 | EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{ |
| 349 | S_Embedded_Common: "embedded_common", |
| 350 | S_Embedded_Different: "", |
| 351 | }, |
Paul Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 352 | }, |
| 353 | common) |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 354 | |
Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 355 | android.AssertDeepEquals(t, "updated properties[0] not correct", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 356 | &testPropertiesStruct{ |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 357 | name: "struct-0", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 358 | private: "common", |
| 359 | Public_Kept: "common", |
| 360 | S_Common: "", |
| 361 | S_Different: "upper", |
| 362 | A_Common: nil, |
| 363 | A_Different: []string{"alpha", "beta"}, |
| 364 | F_Common: nil, |
| 365 | F_Different: proptools.BoolPtr(false), |
| 366 | EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{ |
| 367 | S_Embedded_Common: "", |
| 368 | S_Embedded_Different: "embedded_upper", |
| 369 | }, |
Paul Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 370 | }, |
| 371 | structs[0]) |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 372 | |
Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 373 | android.AssertDeepEquals(t, "updated properties[1] not correct", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 374 | &testPropertiesStruct{ |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 375 | name: "struct-1", |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 376 | private: "common", |
| 377 | Public_Kept: "common", |
| 378 | S_Common: "", |
| 379 | S_Different: "lower", |
| 380 | A_Common: nil, |
| 381 | A_Different: []string{"alpha", "delta"}, |
| 382 | F_Common: nil, |
| 383 | F_Different: proptools.BoolPtr(true), |
| 384 | EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{ |
| 385 | S_Embedded_Common: "", |
| 386 | S_Embedded_Different: "embedded_lower", |
| 387 | }, |
Paul Duffin | 1d6c0df | 2020-05-06 12:50:19 +0100 | [diff] [blame] | 388 | }, |
| 389 | structs[1]) |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 390 | } |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 391 | |
| 392 | func TestCommonValueOptimization_InvalidArchSpecificVariants(t *testing.T) { |
| 393 | common := &testPropertiesStruct{name: "common"} |
| 394 | structs := []propertiesContainer{ |
| 395 | &testPropertiesStruct{ |
| 396 | name: "struct-0", |
| 397 | S_Common: "should-be-but-is-not-common0", |
| 398 | }, |
| 399 | &testPropertiesStruct{ |
| 400 | name: "struct-1", |
| 401 | S_Common: "should-be-but-is-not-common1", |
| 402 | }, |
| 403 | } |
| 404 | |
| 405 | extractor := newCommonValueExtractor(common) |
| 406 | |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 407 | err := extractor.extractCommonProperties(common, structs) |
Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 408 | android.AssertErrorMessageEquals(t, "unexpected error", `field "S_Common" is not tagged as "arch_variant" but has arch specific properties: |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 409 | "struct-0" has value "should-be-but-is-not-common0" |
| 410 | "struct-1" has value "should-be-but-is-not-common1"`, err) |
| 411 | } |
Paul Duffin | 62035b5 | 2021-05-05 21:35:49 +0100 | [diff] [blame] | 412 | |
| 413 | // Ensure that sdk snapshot related environment variables work correctly. |
| 414 | func TestSnapshot_EnvConfiguration(t *testing.T) { |
| 415 | bp := ` |
| 416 | sdk { |
| 417 | name: "mysdk", |
| 418 | java_header_libs: ["myjavalib"], |
| 419 | } |
| 420 | |
| 421 | java_library { |
| 422 | name: "myjavalib", |
| 423 | srcs: ["Test.java"], |
| 424 | system_modules: "none", |
| 425 | sdk_version: "none", |
| 426 | compile_dex: true, |
| 427 | host_supported: true, |
| 428 | } |
| 429 | ` |
| 430 | preparer := android.GroupFixturePreparers( |
| 431 | prepareForSdkTestWithJava, |
| 432 | android.FixtureWithRootAndroidBp(bp), |
| 433 | ) |
| 434 | |
| 435 | checkZipFile := func(t *testing.T, result *android.TestResult, expected string) { |
| 436 | zipRule := result.ModuleForTests("mysdk", "common_os").Rule("SnapshotZipFiles") |
| 437 | android.AssertStringEquals(t, "snapshot zip file", expected, zipRule.Output.String()) |
| 438 | } |
| 439 | |
| 440 | t.Run("no env variables", func(t *testing.T) { |
| 441 | result := preparer.RunTest(t) |
| 442 | |
| 443 | checkZipFile(t, result, "out/soong/.intermediates/mysdk/common_os/mysdk-current.zip") |
| 444 | |
| 445 | CheckSnapshot(t, result, "mysdk", "", |
| 446 | checkAndroidBpContents(` |
| 447 | // This is auto-generated. DO NOT EDIT. |
| 448 | |
| 449 | java_import { |
| 450 | name: "mysdk_myjavalib@current", |
| 451 | sdk_member_name: "myjavalib", |
| 452 | visibility: ["//visibility:public"], |
| 453 | apex_available: ["//apex_available:platform"], |
| 454 | jars: ["java/myjavalib.jar"], |
| 455 | } |
| 456 | |
| 457 | java_import { |
| 458 | name: "myjavalib", |
| 459 | prefer: false, |
| 460 | visibility: ["//visibility:public"], |
| 461 | apex_available: ["//apex_available:platform"], |
| 462 | jars: ["java/myjavalib.jar"], |
| 463 | } |
| 464 | |
| 465 | sdk_snapshot { |
| 466 | name: "mysdk@current", |
| 467 | visibility: ["//visibility:public"], |
| 468 | java_header_libs: ["mysdk_myjavalib@current"], |
| 469 | } |
| 470 | `), |
| 471 | ) |
| 472 | }) |
Paul Duffin | 64fb526 | 2021-05-05 21:36:04 +0100 | [diff] [blame] | 473 | |
| 474 | t.Run("SOONG_SDK_SNAPSHOT_PREFER=true", func(t *testing.T) { |
| 475 | result := android.GroupFixturePreparers( |
| 476 | preparer, |
| 477 | android.FixtureMergeEnv(map[string]string{ |
| 478 | "SOONG_SDK_SNAPSHOT_PREFER": "true", |
| 479 | }), |
| 480 | ).RunTest(t) |
| 481 | |
| 482 | checkZipFile(t, result, "out/soong/.intermediates/mysdk/common_os/mysdk-current.zip") |
| 483 | |
| 484 | CheckSnapshot(t, result, "mysdk", "", |
| 485 | checkAndroidBpContents(` |
| 486 | // This is auto-generated. DO NOT EDIT. |
| 487 | |
| 488 | java_import { |
| 489 | name: "mysdk_myjavalib@current", |
| 490 | sdk_member_name: "myjavalib", |
| 491 | visibility: ["//visibility:public"], |
| 492 | apex_available: ["//apex_available:platform"], |
| 493 | jars: ["java/myjavalib.jar"], |
| 494 | } |
| 495 | |
| 496 | java_import { |
| 497 | name: "myjavalib", |
| 498 | prefer: true, |
| 499 | visibility: ["//visibility:public"], |
| 500 | apex_available: ["//apex_available:platform"], |
| 501 | jars: ["java/myjavalib.jar"], |
| 502 | } |
| 503 | |
| 504 | sdk_snapshot { |
| 505 | name: "mysdk@current", |
| 506 | visibility: ["//visibility:public"], |
| 507 | java_header_libs: ["mysdk_myjavalib@current"], |
| 508 | } |
| 509 | `), |
| 510 | ) |
| 511 | }) |
Paul Duffin | 43f7bf0 | 2021-05-05 22:00:51 +0100 | [diff] [blame] | 512 | |
Paul Duffin | fb9a7f9 | 2021-07-06 17:18:42 +0100 | [diff] [blame] | 513 | t.Run("SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR=module:build_from_source", func(t *testing.T) { |
| 514 | result := android.GroupFixturePreparers( |
| 515 | preparer, |
| 516 | android.FixtureMergeEnv(map[string]string{ |
| 517 | "SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR": "module:build_from_source", |
| 518 | }), |
| 519 | ).RunTest(t) |
| 520 | |
| 521 | checkZipFile(t, result, "out/soong/.intermediates/mysdk/common_os/mysdk-current.zip") |
| 522 | |
| 523 | CheckSnapshot(t, result, "mysdk", "", |
| 524 | checkAndroidBpContents(` |
| 525 | // This is auto-generated. DO NOT EDIT. |
| 526 | |
| 527 | java_import { |
| 528 | name: "mysdk_myjavalib@current", |
| 529 | sdk_member_name: "myjavalib", |
| 530 | visibility: ["//visibility:public"], |
| 531 | apex_available: ["//apex_available:platform"], |
| 532 | jars: ["java/myjavalib.jar"], |
| 533 | } |
| 534 | |
| 535 | java_import { |
| 536 | name: "myjavalib", |
| 537 | prefer: false, |
| 538 | use_source_config_var: { |
| 539 | config_namespace: "module", |
| 540 | var_name: "build_from_source", |
| 541 | }, |
| 542 | visibility: ["//visibility:public"], |
| 543 | apex_available: ["//apex_available:platform"], |
| 544 | jars: ["java/myjavalib.jar"], |
| 545 | } |
| 546 | |
| 547 | sdk_snapshot { |
| 548 | name: "mysdk@current", |
| 549 | visibility: ["//visibility:public"], |
| 550 | java_header_libs: ["mysdk_myjavalib@current"], |
| 551 | } |
| 552 | `), |
| 553 | ) |
| 554 | }) |
| 555 | |
Paul Duffin | 43f7bf0 | 2021-05-05 22:00:51 +0100 | [diff] [blame] | 556 | t.Run("SOONG_SDK_SNAPSHOT_VERSION=unversioned", func(t *testing.T) { |
| 557 | result := android.GroupFixturePreparers( |
| 558 | preparer, |
| 559 | android.FixtureMergeEnv(map[string]string{ |
| 560 | "SOONG_SDK_SNAPSHOT_VERSION": "unversioned", |
| 561 | }), |
| 562 | ).RunTest(t) |
| 563 | |
| 564 | checkZipFile(t, result, "out/soong/.intermediates/mysdk/common_os/mysdk.zip") |
| 565 | |
| 566 | CheckSnapshot(t, result, "mysdk", "", |
| 567 | checkAndroidBpContents(` |
| 568 | // This is auto-generated. DO NOT EDIT. |
| 569 | |
| 570 | java_import { |
| 571 | name: "myjavalib", |
| 572 | prefer: false, |
| 573 | visibility: ["//visibility:public"], |
| 574 | apex_available: ["//apex_available:platform"], |
| 575 | jars: ["java/myjavalib.jar"], |
| 576 | } |
| 577 | `), |
| 578 | ) |
| 579 | }) |
| 580 | |
| 581 | t.Run("SOONG_SDK_SNAPSHOT_VERSION=current", func(t *testing.T) { |
| 582 | result := android.GroupFixturePreparers( |
| 583 | preparer, |
| 584 | android.FixtureMergeEnv(map[string]string{ |
| 585 | "SOONG_SDK_SNAPSHOT_VERSION": "current", |
| 586 | }), |
| 587 | ).RunTest(t) |
| 588 | |
| 589 | checkZipFile(t, result, "out/soong/.intermediates/mysdk/common_os/mysdk-current.zip") |
| 590 | |
| 591 | CheckSnapshot(t, result, "mysdk", "", |
| 592 | checkAndroidBpContents(` |
| 593 | // This is auto-generated. DO NOT EDIT. |
| 594 | |
| 595 | java_import { |
| 596 | name: "mysdk_myjavalib@current", |
| 597 | sdk_member_name: "myjavalib", |
| 598 | visibility: ["//visibility:public"], |
| 599 | apex_available: ["//apex_available:platform"], |
| 600 | jars: ["java/myjavalib.jar"], |
| 601 | } |
| 602 | |
| 603 | java_import { |
| 604 | name: "myjavalib", |
| 605 | prefer: false, |
| 606 | visibility: ["//visibility:public"], |
| 607 | apex_available: ["//apex_available:platform"], |
| 608 | jars: ["java/myjavalib.jar"], |
| 609 | } |
| 610 | |
| 611 | sdk_snapshot { |
| 612 | name: "mysdk@current", |
| 613 | visibility: ["//visibility:public"], |
| 614 | java_header_libs: ["mysdk_myjavalib@current"], |
| 615 | } |
| 616 | `), |
| 617 | ) |
| 618 | }) |
| 619 | |
| 620 | t.Run("SOONG_SDK_SNAPSHOT_VERSION=2", func(t *testing.T) { |
| 621 | result := android.GroupFixturePreparers( |
| 622 | preparer, |
| 623 | android.FixtureMergeEnv(map[string]string{ |
| 624 | "SOONG_SDK_SNAPSHOT_VERSION": "2", |
| 625 | }), |
| 626 | ).RunTest(t) |
| 627 | |
| 628 | checkZipFile(t, result, "out/soong/.intermediates/mysdk/common_os/mysdk-2.zip") |
| 629 | |
| 630 | CheckSnapshot(t, result, "mysdk", "", |
| 631 | checkAndroidBpContents(` |
| 632 | // This is auto-generated. DO NOT EDIT. |
| 633 | |
| 634 | java_import { |
| 635 | name: "mysdk_myjavalib@2", |
| 636 | sdk_member_name: "myjavalib", |
| 637 | visibility: ["//visibility:public"], |
| 638 | apex_available: ["//apex_available:platform"], |
| 639 | jars: ["java/myjavalib.jar"], |
| 640 | } |
| 641 | |
| 642 | sdk_snapshot { |
| 643 | name: "mysdk@2", |
| 644 | visibility: ["//visibility:public"], |
| 645 | java_header_libs: ["mysdk_myjavalib@2"], |
| 646 | } |
| 647 | `), |
| 648 | // A versioned snapshot cannot be used on its own so add the source back in. |
| 649 | snapshotTestPreparer(checkSnapshotWithoutSource, android.FixtureWithRootAndroidBp(bp)), |
| 650 | ) |
| 651 | }) |
Paul Duffin | 39abf8f | 2021-09-24 14:58:27 +0100 | [diff] [blame] | 652 | |
| 653 | t.Run("SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE=S", func(t *testing.T) { |
| 654 | result := android.GroupFixturePreparers( |
| 655 | prepareForSdkTestWithJava, |
| 656 | java.PrepareForTestWithJavaDefaultModules, |
| 657 | java.PrepareForTestWithJavaSdkLibraryFiles, |
| 658 | java.FixtureWithLastReleaseApis("mysdklibrary"), |
| 659 | android.FixtureWithRootAndroidBp(` |
| 660 | sdk { |
| 661 | name: "mysdk", |
| 662 | bootclasspath_fragments: ["mybootclasspathfragment"], |
| 663 | } |
| 664 | |
| 665 | bootclasspath_fragment { |
| 666 | name: "mybootclasspathfragment", |
| 667 | apex_available: ["myapex"], |
| 668 | contents: ["mysdklibrary"], |
| 669 | } |
| 670 | |
| 671 | java_sdk_library { |
| 672 | name: "mysdklibrary", |
| 673 | srcs: ["Test.java"], |
| 674 | compile_dex: true, |
| 675 | public: {enabled: true}, |
| 676 | permitted_packages: ["mysdklibrary"], |
| 677 | } |
| 678 | `), |
| 679 | android.FixtureMergeEnv(map[string]string{ |
| 680 | "SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE": "S", |
| 681 | }), |
| 682 | ).RunTest(t) |
| 683 | |
| 684 | CheckSnapshot(t, result, "mysdk", "", |
| 685 | checkUnversionedAndroidBpContents(` |
| 686 | // This is auto-generated. DO NOT EDIT. |
| 687 | |
| 688 | prebuilt_bootclasspath_fragment { |
| 689 | name: "mybootclasspathfragment", |
| 690 | prefer: false, |
| 691 | visibility: ["//visibility:public"], |
| 692 | apex_available: ["myapex"], |
| 693 | contents: ["mysdklibrary"], |
| 694 | hidden_api: { |
| 695 | annotation_flags: "hiddenapi/annotation-flags.csv", |
| 696 | metadata: "hiddenapi/metadata.csv", |
| 697 | index: "hiddenapi/index.csv", |
Paul Duffin | 191be3a | 2021-08-10 16:14:16 +0100 | [diff] [blame] | 698 | stub_flags: "hiddenapi/stub-flags.csv", |
| 699 | all_flags: "hiddenapi/all-flags.csv", |
Paul Duffin | 39abf8f | 2021-09-24 14:58:27 +0100 | [diff] [blame] | 700 | }, |
| 701 | } |
| 702 | |
| 703 | java_sdk_library_import { |
| 704 | name: "mysdklibrary", |
| 705 | prefer: false, |
| 706 | visibility: ["//visibility:public"], |
| 707 | apex_available: ["//apex_available:platform"], |
| 708 | shared_library: true, |
| 709 | compile_dex: true, |
| 710 | permitted_packages: ["mysdklibrary"], |
| 711 | public: { |
| 712 | jars: ["sdk_library/public/mysdklibrary-stubs.jar"], |
| 713 | stub_srcs: ["sdk_library/public/mysdklibrary_stub_sources"], |
| 714 | current_api: "sdk_library/public/mysdklibrary.txt", |
| 715 | removed_api: "sdk_library/public/mysdklibrary-removed.txt", |
| 716 | sdk_version: "current", |
| 717 | }, |
| 718 | } |
| 719 | `), |
| 720 | |
| 721 | checkAllCopyRules(` |
| 722 | .intermediates/mybootclasspathfragment/android_common/modular-hiddenapi/annotation-flags.csv -> hiddenapi/annotation-flags.csv |
| 723 | .intermediates/mybootclasspathfragment/android_common/modular-hiddenapi/metadata.csv -> hiddenapi/metadata.csv |
| 724 | .intermediates/mybootclasspathfragment/android_common/modular-hiddenapi/index.csv -> hiddenapi/index.csv |
Paul Duffin | 191be3a | 2021-08-10 16:14:16 +0100 | [diff] [blame] | 725 | .intermediates/mybootclasspathfragment/android_common/modular-hiddenapi/stub-flags.csv -> hiddenapi/stub-flags.csv |
| 726 | .intermediates/mybootclasspathfragment/android_common/modular-hiddenapi/all-flags.csv -> hiddenapi/all-flags.csv |
Paul Duffin | 39abf8f | 2021-09-24 14:58:27 +0100 | [diff] [blame] | 727 | .intermediates/mysdklibrary.stubs/android_common/javac/mysdklibrary.stubs.jar -> sdk_library/public/mysdklibrary-stubs.jar |
| 728 | .intermediates/mysdklibrary.stubs.source/android_common/metalava/mysdklibrary.stubs.source_api.txt -> sdk_library/public/mysdklibrary.txt |
| 729 | .intermediates/mysdklibrary.stubs.source/android_common/metalava/mysdklibrary.stubs.source_removed.txt -> sdk_library/public/mysdklibrary-removed.txt |
| 730 | `), |
| 731 | ) |
| 732 | }) |
| 733 | |
Paul Duffin | 62035b5 | 2021-05-05 21:35:49 +0100 | [diff] [blame] | 734 | } |