Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [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 cc |
| 16 | |
| 17 | import ( |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame^] | 18 | "runtime" |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 19 | "testing" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | |
| 23 | "github.com/google/blueprint" |
| 24 | ) |
| 25 | |
Paul Duffin | 8567f22 | 2021-03-23 00:02:06 +0000 | [diff] [blame] | 26 | var prepareForPrebuiltTest = android.GroupFixturePreparers( |
| 27 | prepareForCcTest, |
Paul Duffin | 6a1160e | 2021-03-07 15:47:42 +0000 | [diff] [blame] | 28 | android.PrepareForTestWithAndroidMk, |
| 29 | ) |
Martin Stjernholm | adeb088 | 2020-04-01 23:02:57 +0100 | [diff] [blame] | 30 | |
Paul Duffin | 6a1160e | 2021-03-07 15:47:42 +0000 | [diff] [blame] | 31 | func testPrebuilt(t *testing.T, bp string, fs android.MockFS, handlers ...android.FixturePreparer) *android.TestContext { |
Paul Duffin | 8567f22 | 2021-03-23 00:02:06 +0000 | [diff] [blame] | 32 | result := android.GroupFixturePreparers( |
| 33 | prepareForPrebuiltTest, |
Paul Duffin | 6a1160e | 2021-03-07 15:47:42 +0000 | [diff] [blame] | 34 | fs.AddToFixture(), |
Paul Duffin | 8567f22 | 2021-03-23 00:02:06 +0000 | [diff] [blame] | 35 | android.GroupFixturePreparers(handlers...), |
| 36 | ).RunTestWithBp(t, bp) |
Martin Stjernholm | adeb088 | 2020-04-01 23:02:57 +0100 | [diff] [blame] | 37 | |
Paul Duffin | 6a1160e | 2021-03-07 15:47:42 +0000 | [diff] [blame] | 38 | return result.TestContext |
Martin Stjernholm | adeb088 | 2020-04-01 23:02:57 +0100 | [diff] [blame] | 39 | } |
| 40 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 41 | type configCustomizer func(config android.Config) |
| 42 | |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 43 | func TestPrebuilt(t *testing.T) { |
| 44 | bp := ` |
| 45 | cc_library { |
| 46 | name: "liba", |
| 47 | } |
| 48 | |
| 49 | cc_prebuilt_library_shared { |
| 50 | name: "liba", |
| 51 | srcs: ["liba.so"], |
| 52 | } |
| 53 | |
| 54 | cc_library { |
| 55 | name: "libb", |
| 56 | } |
| 57 | |
| 58 | cc_prebuilt_library_static { |
| 59 | name: "libb", |
| 60 | srcs: ["libb.a"], |
| 61 | } |
| 62 | |
| 63 | cc_library_shared { |
| 64 | name: "libd", |
| 65 | } |
| 66 | |
| 67 | cc_prebuilt_library_shared { |
| 68 | name: "libd", |
| 69 | srcs: ["libd.so"], |
| 70 | } |
| 71 | |
| 72 | cc_library_static { |
| 73 | name: "libe", |
| 74 | } |
| 75 | |
| 76 | cc_prebuilt_library_static { |
| 77 | name: "libe", |
| 78 | srcs: ["libe.a"], |
| 79 | } |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 80 | |
| 81 | cc_library { |
| 82 | name: "libf", |
| 83 | } |
| 84 | |
| 85 | cc_prebuilt_library { |
| 86 | name: "libf", |
| 87 | static: { |
| 88 | srcs: ["libf.a"], |
| 89 | }, |
| 90 | shared: { |
| 91 | srcs: ["libf.so"], |
| 92 | }, |
| 93 | } |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 94 | |
| 95 | cc_object { |
| 96 | name: "crtx", |
| 97 | } |
| 98 | |
| 99 | cc_prebuilt_object { |
| 100 | name: "crtx", |
| 101 | srcs: ["crtx.o"], |
| 102 | } |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 103 | ` |
| 104 | |
Martin Stjernholm | adeb088 | 2020-04-01 23:02:57 +0100 | [diff] [blame] | 105 | ctx := testPrebuilt(t, bp, map[string][]byte{ |
| 106 | "liba.so": nil, |
| 107 | "libb.a": nil, |
| 108 | "libd.so": nil, |
| 109 | "libe.a": nil, |
| 110 | "libf.a": nil, |
| 111 | "libf.so": nil, |
| 112 | "crtx.o": nil, |
| 113 | }) |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 114 | |
| 115 | // Verify that all the modules exist and that their dependencies were connected correctly |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 116 | liba := ctx.ModuleForTests("liba", "android_arm64_armv8-a_shared").Module() |
| 117 | libb := ctx.ModuleForTests("libb", "android_arm64_armv8-a_static").Module() |
| 118 | libd := ctx.ModuleForTests("libd", "android_arm64_armv8-a_shared").Module() |
| 119 | libe := ctx.ModuleForTests("libe", "android_arm64_armv8-a_static").Module() |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 120 | libfStatic := ctx.ModuleForTests("libf", "android_arm64_armv8-a_static").Module() |
| 121 | libfShared := ctx.ModuleForTests("libf", "android_arm64_armv8-a_shared").Module() |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 122 | crtx := ctx.ModuleForTests("crtx", "android_arm64_armv8-a").Module() |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 123 | |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 124 | prebuiltLiba := ctx.ModuleForTests("prebuilt_liba", "android_arm64_armv8-a_shared").Module() |
| 125 | prebuiltLibb := ctx.ModuleForTests("prebuilt_libb", "android_arm64_armv8-a_static").Module() |
| 126 | prebuiltLibd := ctx.ModuleForTests("prebuilt_libd", "android_arm64_armv8-a_shared").Module() |
| 127 | prebuiltLibe := ctx.ModuleForTests("prebuilt_libe", "android_arm64_armv8-a_static").Module() |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 128 | prebuiltLibfStatic := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_static").Module() |
| 129 | prebuiltLibfShared := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_shared").Module() |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 130 | prebuiltCrtx := ctx.ModuleForTests("prebuilt_crtx", "android_arm64_armv8-a").Module() |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 131 | |
| 132 | hasDep := func(m android.Module, wantDep android.Module) bool { |
| 133 | t.Helper() |
| 134 | var found bool |
| 135 | ctx.VisitDirectDeps(m, func(dep blueprint.Module) { |
| 136 | if dep == wantDep { |
| 137 | found = true |
| 138 | } |
| 139 | }) |
| 140 | return found |
| 141 | } |
| 142 | |
| 143 | if !hasDep(liba, prebuiltLiba) { |
| 144 | t.Errorf("liba missing dependency on prebuilt_liba") |
| 145 | } |
| 146 | |
| 147 | if !hasDep(libb, prebuiltLibb) { |
| 148 | t.Errorf("libb missing dependency on prebuilt_libb") |
| 149 | } |
| 150 | |
| 151 | if !hasDep(libd, prebuiltLibd) { |
| 152 | t.Errorf("libd missing dependency on prebuilt_libd") |
| 153 | } |
| 154 | |
| 155 | if !hasDep(libe, prebuiltLibe) { |
| 156 | t.Errorf("libe missing dependency on prebuilt_libe") |
| 157 | } |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 158 | |
| 159 | if !hasDep(libfStatic, prebuiltLibfStatic) { |
| 160 | t.Errorf("libf static missing dependency on prebuilt_libf") |
| 161 | } |
| 162 | |
| 163 | if !hasDep(libfShared, prebuiltLibfShared) { |
| 164 | t.Errorf("libf shared missing dependency on prebuilt_libf") |
| 165 | } |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 166 | |
| 167 | if !hasDep(crtx, prebuiltCrtx) { |
| 168 | t.Errorf("crtx missing dependency on prebuilt_crtx") |
| 169 | } |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 172 | func TestPrebuiltLibraryShared(t *testing.T) { |
| 173 | ctx := testPrebuilt(t, ` |
| 174 | cc_prebuilt_library_shared { |
| 175 | name: "libtest", |
| 176 | srcs: ["libf.so"], |
| 177 | strip: { |
| 178 | none: true, |
| 179 | }, |
| 180 | } |
Martin Stjernholm | adeb088 | 2020-04-01 23:02:57 +0100 | [diff] [blame] | 181 | `, map[string][]byte{ |
| 182 | "libf.so": nil, |
| 183 | }) |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 184 | |
| 185 | shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module) |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 186 | assertString(t, shared.OutputFile().Path().Base(), "libtest.so") |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | func TestPrebuiltLibraryStatic(t *testing.T) { |
| 190 | ctx := testPrebuilt(t, ` |
| 191 | cc_prebuilt_library_static { |
| 192 | name: "libtest", |
| 193 | srcs: ["libf.a"], |
| 194 | } |
Martin Stjernholm | adeb088 | 2020-04-01 23:02:57 +0100 | [diff] [blame] | 195 | `, map[string][]byte{ |
| 196 | "libf.a": nil, |
| 197 | }) |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 198 | |
| 199 | static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module) |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 200 | assertString(t, static.OutputFile().Path().Base(), "libf.a") |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | func TestPrebuiltLibrary(t *testing.T) { |
| 204 | ctx := testPrebuilt(t, ` |
| 205 | cc_prebuilt_library { |
| 206 | name: "libtest", |
| 207 | static: { |
| 208 | srcs: ["libf.a"], |
| 209 | }, |
| 210 | shared: { |
| 211 | srcs: ["libf.so"], |
| 212 | }, |
| 213 | strip: { |
| 214 | none: true, |
| 215 | }, |
| 216 | } |
Martin Stjernholm | adeb088 | 2020-04-01 23:02:57 +0100 | [diff] [blame] | 217 | `, map[string][]byte{ |
| 218 | "libf.a": nil, |
| 219 | "libf.so": nil, |
| 220 | }) |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 221 | |
| 222 | shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module) |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 223 | assertString(t, shared.OutputFile().Path().Base(), "libtest.so") |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 224 | |
| 225 | static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module) |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 226 | assertString(t, static.OutputFile().Path().Base(), "libf.a") |
| 227 | } |
| 228 | |
| 229 | func TestPrebuiltLibraryStem(t *testing.T) { |
| 230 | ctx := testPrebuilt(t, ` |
| 231 | cc_prebuilt_library { |
| 232 | name: "libfoo", |
| 233 | stem: "libbar", |
| 234 | static: { |
| 235 | srcs: ["libfoo.a"], |
| 236 | }, |
| 237 | shared: { |
| 238 | srcs: ["libfoo.so"], |
| 239 | }, |
| 240 | strip: { |
| 241 | none: true, |
| 242 | }, |
| 243 | } |
| 244 | `, map[string][]byte{ |
| 245 | "libfoo.a": nil, |
| 246 | "libfoo.so": nil, |
| 247 | }) |
| 248 | |
| 249 | static := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module) |
| 250 | assertString(t, static.OutputFile().Path().Base(), "libfoo.a") |
| 251 | |
| 252 | shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module) |
| 253 | assertString(t, shared.OutputFile().Path().Base(), "libbar.so") |
| 254 | } |
| 255 | |
| 256 | func TestPrebuiltLibrarySharedStem(t *testing.T) { |
| 257 | ctx := testPrebuilt(t, ` |
| 258 | cc_prebuilt_library_shared { |
| 259 | name: "libfoo", |
| 260 | stem: "libbar", |
| 261 | srcs: ["libfoo.so"], |
| 262 | strip: { |
| 263 | none: true, |
| 264 | }, |
| 265 | } |
| 266 | `, map[string][]byte{ |
| 267 | "libfoo.so": nil, |
| 268 | }) |
| 269 | |
| 270 | shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module) |
| 271 | assertString(t, shared.OutputFile().Path().Base(), "libbar.so") |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 272 | } |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 273 | |
| 274 | func TestPrebuiltSymlinkedHostBinary(t *testing.T) { |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame^] | 275 | if runtime.GOOS != "linux" { |
| 276 | t.Skipf("Skipping host prebuilt testing that is only supported on linux not %s", runtime.GOOS) |
Martin Stjernholm | 6a9a146 | 2020-09-15 02:56:19 +0100 | [diff] [blame] | 277 | } |
| 278 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 279 | ctx := testPrebuilt(t, ` |
| 280 | cc_prebuilt_library_shared { |
| 281 | name: "libfoo", |
| 282 | device_supported: false, |
| 283 | host_supported: true, |
| 284 | target: { |
| 285 | linux_glibc_x86_64: { |
| 286 | srcs: ["linux_glibc_x86_64/lib64/libfoo.so"], |
| 287 | }, |
| 288 | }, |
| 289 | } |
| 290 | |
| 291 | cc_prebuilt_binary { |
| 292 | name: "foo", |
| 293 | device_supported: false, |
| 294 | host_supported: true, |
| 295 | shared_libs: ["libfoo"], |
| 296 | target: { |
| 297 | linux_glibc_x86_64: { |
| 298 | srcs: ["linux_glibc_x86_64/bin/foo"], |
| 299 | }, |
| 300 | }, |
| 301 | } |
| 302 | `, map[string][]byte{ |
| 303 | "libfoo.so": nil, |
| 304 | "foo": nil, |
| 305 | }) |
| 306 | |
| 307 | fooRule := ctx.ModuleForTests("foo", "linux_glibc_x86_64").Rule("Symlink") |
Paul Duffin | e8366da | 2021-03-24 10:40:38 +0000 | [diff] [blame] | 308 | assertString(t, fooRule.Output.String(), "out/soong/.intermediates/foo/linux_glibc_x86_64/foo") |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 309 | assertString(t, fooRule.Args["fromPath"], "$$PWD/linux_glibc_x86_64/bin/foo") |
| 310 | |
| 311 | var libfooDep android.Path |
| 312 | for _, dep := range fooRule.Implicits { |
| 313 | if dep.Base() == "libfoo.so" { |
| 314 | libfooDep = dep |
| 315 | break |
| 316 | } |
| 317 | } |
Paul Duffin | e8366da | 2021-03-24 10:40:38 +0000 | [diff] [blame] | 318 | assertString(t, libfooDep.String(), "out/soong/.intermediates/libfoo/linux_glibc_x86_64_shared/libfoo.so") |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 319 | } |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 320 | |
| 321 | func TestPrebuiltLibrarySanitized(t *testing.T) { |
| 322 | bp := `cc_prebuilt_library { |
| 323 | name: "libtest", |
| 324 | static: { |
| 325 | sanitized: { none: { srcs: ["libf.a"], }, hwaddress: { srcs: ["libf.hwasan.a"], }, }, |
| 326 | }, |
| 327 | shared: { |
| 328 | sanitized: { none: { srcs: ["libf.so"], }, hwaddress: { srcs: ["hwasan/libf.so"], }, }, |
| 329 | }, |
| 330 | } |
| 331 | cc_prebuilt_library_static { |
| 332 | name: "libtest_static", |
| 333 | sanitized: { none: { srcs: ["libf.a"], }, hwaddress: { srcs: ["libf.hwasan.a"], }, }, |
| 334 | } |
| 335 | cc_prebuilt_library_shared { |
| 336 | name: "libtest_shared", |
| 337 | sanitized: { none: { srcs: ["libf.so"], }, hwaddress: { srcs: ["hwasan/libf.so"], }, }, |
| 338 | }` |
| 339 | |
| 340 | fs := map[string][]byte{ |
| 341 | "libf.a": nil, |
| 342 | "libf.hwasan.a": nil, |
| 343 | "libf.so": nil, |
| 344 | "hwasan/libf.so": nil, |
| 345 | } |
| 346 | |
| 347 | // Without SANITIZE_TARGET. |
| 348 | ctx := testPrebuilt(t, bp, fs) |
| 349 | |
| 350 | shared_rule := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Rule("android/soong/cc.strip") |
| 351 | assertString(t, shared_rule.Input.String(), "libf.so") |
| 352 | |
| 353 | static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module) |
| 354 | assertString(t, static.OutputFile().Path().Base(), "libf.a") |
| 355 | |
| 356 | shared_rule2 := ctx.ModuleForTests("libtest_shared", "android_arm64_armv8-a_shared").Rule("android/soong/cc.strip") |
| 357 | assertString(t, shared_rule2.Input.String(), "libf.so") |
| 358 | |
| 359 | static2 := ctx.ModuleForTests("libtest_static", "android_arm64_armv8-a_static").Module().(*Module) |
| 360 | assertString(t, static2.OutputFile().Path().Base(), "libf.a") |
| 361 | |
| 362 | // With SANITIZE_TARGET=hwaddress |
Paul Duffin | 6a1160e | 2021-03-07 15:47:42 +0000 | [diff] [blame] | 363 | ctx = testPrebuilt(t, bp, fs, |
| 364 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 365 | variables.SanitizeDevice = []string{"hwaddress"} |
| 366 | }), |
| 367 | ) |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 368 | |
| 369 | shared_rule = ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared_hwasan").Rule("android/soong/cc.strip") |
| 370 | assertString(t, shared_rule.Input.String(), "hwasan/libf.so") |
| 371 | |
| 372 | static = ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static_hwasan").Module().(*Module) |
| 373 | assertString(t, static.OutputFile().Path().Base(), "libf.hwasan.a") |
| 374 | |
| 375 | shared_rule2 = ctx.ModuleForTests("libtest_shared", "android_arm64_armv8-a_shared_hwasan").Rule("android/soong/cc.strip") |
| 376 | assertString(t, shared_rule2.Input.String(), "hwasan/libf.so") |
| 377 | |
| 378 | static2 = ctx.ModuleForTests("libtest_static", "android_arm64_armv8-a_static_hwasan").Module().(*Module) |
| 379 | assertString(t, static2.OutputFile().Path().Base(), "libf.hwasan.a") |
| 380 | } |