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 ( |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 18 | "path/filepath" |
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 | |
Martin Stjernholm | adeb088 | 2020-04-01 23:02:57 +0100 | [diff] [blame] | 26 | func testPrebuilt(t *testing.T, bp string, fs map[string][]byte) *android.TestContext { |
| 27 | config := TestConfig(buildDir, android.Android, nil, bp, fs) |
| 28 | ctx := CreateTestContext() |
| 29 | |
| 30 | // Enable androidmk support. |
| 31 | // * Register the singleton |
| 32 | // * Configure that we are inside make |
| 33 | // * Add CommonOS to ensure that androidmk processing works. |
| 34 | android.RegisterAndroidMkBuildComponents(ctx) |
| 35 | android.SetInMakeForTests(config) |
| 36 | |
| 37 | ctx.Register(config) |
| 38 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 39 | android.FailIfErrored(t, errs) |
| 40 | _, errs = ctx.PrepareBuildActions(config) |
| 41 | android.FailIfErrored(t, errs) |
| 42 | return ctx |
| 43 | } |
| 44 | |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 45 | func TestPrebuilt(t *testing.T) { |
| 46 | bp := ` |
| 47 | cc_library { |
| 48 | name: "liba", |
| 49 | } |
| 50 | |
| 51 | cc_prebuilt_library_shared { |
| 52 | name: "liba", |
| 53 | srcs: ["liba.so"], |
| 54 | } |
| 55 | |
| 56 | cc_library { |
| 57 | name: "libb", |
| 58 | } |
| 59 | |
| 60 | cc_prebuilt_library_static { |
| 61 | name: "libb", |
| 62 | srcs: ["libb.a"], |
| 63 | } |
| 64 | |
| 65 | cc_library_shared { |
| 66 | name: "libd", |
| 67 | } |
| 68 | |
| 69 | cc_prebuilt_library_shared { |
| 70 | name: "libd", |
| 71 | srcs: ["libd.so"], |
| 72 | } |
| 73 | |
| 74 | cc_library_static { |
| 75 | name: "libe", |
| 76 | } |
| 77 | |
| 78 | cc_prebuilt_library_static { |
| 79 | name: "libe", |
| 80 | srcs: ["libe.a"], |
| 81 | } |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 82 | |
| 83 | cc_library { |
| 84 | name: "libf", |
| 85 | } |
| 86 | |
| 87 | cc_prebuilt_library { |
| 88 | name: "libf", |
| 89 | static: { |
| 90 | srcs: ["libf.a"], |
| 91 | }, |
| 92 | shared: { |
| 93 | srcs: ["libf.so"], |
| 94 | }, |
| 95 | } |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 96 | |
| 97 | cc_object { |
| 98 | name: "crtx", |
| 99 | } |
| 100 | |
| 101 | cc_prebuilt_object { |
| 102 | name: "crtx", |
| 103 | srcs: ["crtx.o"], |
| 104 | } |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 105 | ` |
| 106 | |
Martin Stjernholm | adeb088 | 2020-04-01 23:02:57 +0100 | [diff] [blame] | 107 | ctx := testPrebuilt(t, bp, map[string][]byte{ |
| 108 | "liba.so": nil, |
| 109 | "libb.a": nil, |
| 110 | "libd.so": nil, |
| 111 | "libe.a": nil, |
| 112 | "libf.a": nil, |
| 113 | "libf.so": nil, |
| 114 | "crtx.o": nil, |
| 115 | }) |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 116 | |
| 117 | // 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] | 118 | liba := ctx.ModuleForTests("liba", "android_arm64_armv8-a_shared").Module() |
| 119 | libb := ctx.ModuleForTests("libb", "android_arm64_armv8-a_static").Module() |
| 120 | libd := ctx.ModuleForTests("libd", "android_arm64_armv8-a_shared").Module() |
| 121 | libe := ctx.ModuleForTests("libe", "android_arm64_armv8-a_static").Module() |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 122 | libfStatic := ctx.ModuleForTests("libf", "android_arm64_armv8-a_static").Module() |
| 123 | libfShared := ctx.ModuleForTests("libf", "android_arm64_armv8-a_shared").Module() |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 124 | crtx := ctx.ModuleForTests("crtx", "android_arm64_armv8-a").Module() |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 125 | |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 126 | prebuiltLiba := ctx.ModuleForTests("prebuilt_liba", "android_arm64_armv8-a_shared").Module() |
| 127 | prebuiltLibb := ctx.ModuleForTests("prebuilt_libb", "android_arm64_armv8-a_static").Module() |
| 128 | prebuiltLibd := ctx.ModuleForTests("prebuilt_libd", "android_arm64_armv8-a_shared").Module() |
| 129 | prebuiltLibe := ctx.ModuleForTests("prebuilt_libe", "android_arm64_armv8-a_static").Module() |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 130 | prebuiltLibfStatic := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_static").Module() |
| 131 | prebuiltLibfShared := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_shared").Module() |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 132 | prebuiltCrtx := ctx.ModuleForTests("prebuilt_crtx", "android_arm64_armv8-a").Module() |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 133 | |
| 134 | hasDep := func(m android.Module, wantDep android.Module) bool { |
| 135 | t.Helper() |
| 136 | var found bool |
| 137 | ctx.VisitDirectDeps(m, func(dep blueprint.Module) { |
| 138 | if dep == wantDep { |
| 139 | found = true |
| 140 | } |
| 141 | }) |
| 142 | return found |
| 143 | } |
| 144 | |
| 145 | if !hasDep(liba, prebuiltLiba) { |
| 146 | t.Errorf("liba missing dependency on prebuilt_liba") |
| 147 | } |
| 148 | |
| 149 | if !hasDep(libb, prebuiltLibb) { |
| 150 | t.Errorf("libb missing dependency on prebuilt_libb") |
| 151 | } |
| 152 | |
| 153 | if !hasDep(libd, prebuiltLibd) { |
| 154 | t.Errorf("libd missing dependency on prebuilt_libd") |
| 155 | } |
| 156 | |
| 157 | if !hasDep(libe, prebuiltLibe) { |
| 158 | t.Errorf("libe missing dependency on prebuilt_libe") |
| 159 | } |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 160 | |
| 161 | if !hasDep(libfStatic, prebuiltLibfStatic) { |
| 162 | t.Errorf("libf static missing dependency on prebuilt_libf") |
| 163 | } |
| 164 | |
| 165 | if !hasDep(libfShared, prebuiltLibfShared) { |
| 166 | t.Errorf("libf shared missing dependency on prebuilt_libf") |
| 167 | } |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 168 | |
| 169 | if !hasDep(crtx, prebuiltCrtx) { |
| 170 | t.Errorf("crtx missing dependency on prebuilt_crtx") |
| 171 | } |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 174 | func TestPrebuiltLibraryShared(t *testing.T) { |
| 175 | ctx := testPrebuilt(t, ` |
| 176 | cc_prebuilt_library_shared { |
| 177 | name: "libtest", |
| 178 | srcs: ["libf.so"], |
| 179 | strip: { |
| 180 | none: true, |
| 181 | }, |
| 182 | } |
Martin Stjernholm | adeb088 | 2020-04-01 23:02:57 +0100 | [diff] [blame] | 183 | `, map[string][]byte{ |
| 184 | "libf.so": nil, |
| 185 | }) |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 186 | |
| 187 | shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module) |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 188 | assertString(t, shared.OutputFile().Path().Base(), "libtest.so") |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | func TestPrebuiltLibraryStatic(t *testing.T) { |
| 192 | ctx := testPrebuilt(t, ` |
| 193 | cc_prebuilt_library_static { |
| 194 | name: "libtest", |
| 195 | srcs: ["libf.a"], |
| 196 | } |
Martin Stjernholm | adeb088 | 2020-04-01 23:02:57 +0100 | [diff] [blame] | 197 | `, map[string][]byte{ |
| 198 | "libf.a": nil, |
| 199 | }) |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 200 | |
| 201 | static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module) |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 202 | assertString(t, static.OutputFile().Path().Base(), "libf.a") |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | func TestPrebuiltLibrary(t *testing.T) { |
| 206 | ctx := testPrebuilt(t, ` |
| 207 | cc_prebuilt_library { |
| 208 | name: "libtest", |
| 209 | static: { |
| 210 | srcs: ["libf.a"], |
| 211 | }, |
| 212 | shared: { |
| 213 | srcs: ["libf.so"], |
| 214 | }, |
| 215 | strip: { |
| 216 | none: true, |
| 217 | }, |
| 218 | } |
Martin Stjernholm | adeb088 | 2020-04-01 23:02:57 +0100 | [diff] [blame] | 219 | `, map[string][]byte{ |
| 220 | "libf.a": nil, |
| 221 | "libf.so": nil, |
| 222 | }) |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 223 | |
| 224 | shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module) |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 225 | assertString(t, shared.OutputFile().Path().Base(), "libtest.so") |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 226 | |
| 227 | static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module) |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 228 | assertString(t, static.OutputFile().Path().Base(), "libf.a") |
| 229 | } |
| 230 | |
| 231 | func TestPrebuiltLibraryStem(t *testing.T) { |
| 232 | ctx := testPrebuilt(t, ` |
| 233 | cc_prebuilt_library { |
| 234 | name: "libfoo", |
| 235 | stem: "libbar", |
| 236 | static: { |
| 237 | srcs: ["libfoo.a"], |
| 238 | }, |
| 239 | shared: { |
| 240 | srcs: ["libfoo.so"], |
| 241 | }, |
| 242 | strip: { |
| 243 | none: true, |
| 244 | }, |
| 245 | } |
| 246 | `, map[string][]byte{ |
| 247 | "libfoo.a": nil, |
| 248 | "libfoo.so": nil, |
| 249 | }) |
| 250 | |
| 251 | static := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module) |
| 252 | assertString(t, static.OutputFile().Path().Base(), "libfoo.a") |
| 253 | |
| 254 | shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module) |
| 255 | assertString(t, shared.OutputFile().Path().Base(), "libbar.so") |
| 256 | } |
| 257 | |
| 258 | func TestPrebuiltLibrarySharedStem(t *testing.T) { |
| 259 | ctx := testPrebuilt(t, ` |
| 260 | cc_prebuilt_library_shared { |
| 261 | name: "libfoo", |
| 262 | stem: "libbar", |
| 263 | srcs: ["libfoo.so"], |
| 264 | strip: { |
| 265 | none: true, |
| 266 | }, |
| 267 | } |
| 268 | `, map[string][]byte{ |
| 269 | "libfoo.so": nil, |
| 270 | }) |
| 271 | |
| 272 | shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module) |
| 273 | assertString(t, shared.OutputFile().Path().Base(), "libbar.so") |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 274 | } |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 275 | |
| 276 | func TestPrebuiltSymlinkedHostBinary(t *testing.T) { |
Martin Stjernholm | 6a9a146 | 2020-09-15 02:56:19 +0100 | [diff] [blame^] | 277 | if android.BuildOs != android.Linux { |
| 278 | t.Skipf("Skipping host prebuilt testing that is only supported on %s not %s", android.Linux, android.BuildOs) |
| 279 | } |
| 280 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 281 | ctx := testPrebuilt(t, ` |
| 282 | cc_prebuilt_library_shared { |
| 283 | name: "libfoo", |
| 284 | device_supported: false, |
| 285 | host_supported: true, |
| 286 | target: { |
| 287 | linux_glibc_x86_64: { |
| 288 | srcs: ["linux_glibc_x86_64/lib64/libfoo.so"], |
| 289 | }, |
| 290 | }, |
| 291 | } |
| 292 | |
| 293 | cc_prebuilt_binary { |
| 294 | name: "foo", |
| 295 | device_supported: false, |
| 296 | host_supported: true, |
| 297 | shared_libs: ["libfoo"], |
| 298 | target: { |
| 299 | linux_glibc_x86_64: { |
| 300 | srcs: ["linux_glibc_x86_64/bin/foo"], |
| 301 | }, |
| 302 | }, |
| 303 | } |
| 304 | `, map[string][]byte{ |
| 305 | "libfoo.so": nil, |
| 306 | "foo": nil, |
| 307 | }) |
| 308 | |
| 309 | fooRule := ctx.ModuleForTests("foo", "linux_glibc_x86_64").Rule("Symlink") |
| 310 | assertString(t, fooRule.Output.String(), |
| 311 | filepath.Join(buildDir, ".intermediates/foo/linux_glibc_x86_64/foo")) |
| 312 | assertString(t, fooRule.Args["fromPath"], "$$PWD/linux_glibc_x86_64/bin/foo") |
| 313 | |
| 314 | var libfooDep android.Path |
| 315 | for _, dep := range fooRule.Implicits { |
| 316 | if dep.Base() == "libfoo.so" { |
| 317 | libfooDep = dep |
| 318 | break |
| 319 | } |
| 320 | } |
| 321 | assertString(t, libfooDep.String(), |
| 322 | filepath.Join(buildDir, ".intermediates/libfoo/linux_glibc_x86_64_shared/libfoo.so")) |
| 323 | } |