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