blob: 0eca97f5728d6ac3d9fa7b913aeeec358fd8210a [file] [log] [blame]
Colin Cross33b2fb72019-05-14 14:07:01 -07001// 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
15package cc
16
17import (
18 "testing"
19
20 "android/soong/android"
21
22 "github.com/google/blueprint"
23)
24
25func TestPrebuilt(t *testing.T) {
26 bp := `
27 cc_library {
28 name: "liba",
29 }
30
31 cc_prebuilt_library_shared {
32 name: "liba",
33 srcs: ["liba.so"],
34 }
35
36 cc_library {
37 name: "libb",
38 }
39
40 cc_prebuilt_library_static {
41 name: "libb",
42 srcs: ["libb.a"],
43 }
44
45 cc_library_shared {
46 name: "libd",
47 }
48
49 cc_prebuilt_library_shared {
50 name: "libd",
51 srcs: ["libd.so"],
52 }
53
54 cc_library_static {
55 name: "libe",
56 }
57
58 cc_prebuilt_library_static {
59 name: "libe",
60 srcs: ["libe.a"],
61 }
Paul Duffinbce90da2020-03-12 20:17:14 +000062
63 cc_library {
64 name: "libf",
65 }
66
67 cc_prebuilt_library {
68 name: "libf",
69 static: {
70 srcs: ["libf.a"],
71 },
72 shared: {
73 srcs: ["libf.so"],
74 },
75 }
Colin Cross33b2fb72019-05-14 14:07:01 -070076 `
77
Paul Duffinbce90da2020-03-12 20:17:14 +000078 ctx := testPrebuilt(t, bp)
Colin Cross33b2fb72019-05-14 14:07:01 -070079
80 // Verify that all the modules exist and that their dependencies were connected correctly
Colin Cross7113d202019-11-20 16:39:12 -080081 liba := ctx.ModuleForTests("liba", "android_arm64_armv8-a_shared").Module()
82 libb := ctx.ModuleForTests("libb", "android_arm64_armv8-a_static").Module()
83 libd := ctx.ModuleForTests("libd", "android_arm64_armv8-a_shared").Module()
84 libe := ctx.ModuleForTests("libe", "android_arm64_armv8-a_static").Module()
Paul Duffinbce90da2020-03-12 20:17:14 +000085 libfStatic := ctx.ModuleForTests("libf", "android_arm64_armv8-a_static").Module()
86 libfShared := ctx.ModuleForTests("libf", "android_arm64_armv8-a_shared").Module()
Colin Cross33b2fb72019-05-14 14:07:01 -070087
Colin Cross7113d202019-11-20 16:39:12 -080088 prebuiltLiba := ctx.ModuleForTests("prebuilt_liba", "android_arm64_armv8-a_shared").Module()
89 prebuiltLibb := ctx.ModuleForTests("prebuilt_libb", "android_arm64_armv8-a_static").Module()
90 prebuiltLibd := ctx.ModuleForTests("prebuilt_libd", "android_arm64_armv8-a_shared").Module()
91 prebuiltLibe := ctx.ModuleForTests("prebuilt_libe", "android_arm64_armv8-a_static").Module()
Paul Duffinbce90da2020-03-12 20:17:14 +000092 prebuiltLibfStatic := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_static").Module()
93 prebuiltLibfShared := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_shared").Module()
Colin Cross33b2fb72019-05-14 14:07:01 -070094
95 hasDep := func(m android.Module, wantDep android.Module) bool {
96 t.Helper()
97 var found bool
98 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
99 if dep == wantDep {
100 found = true
101 }
102 })
103 return found
104 }
105
106 if !hasDep(liba, prebuiltLiba) {
107 t.Errorf("liba missing dependency on prebuilt_liba")
108 }
109
110 if !hasDep(libb, prebuiltLibb) {
111 t.Errorf("libb missing dependency on prebuilt_libb")
112 }
113
114 if !hasDep(libd, prebuiltLibd) {
115 t.Errorf("libd missing dependency on prebuilt_libd")
116 }
117
118 if !hasDep(libe, prebuiltLibe) {
119 t.Errorf("libe missing dependency on prebuilt_libe")
120 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000121
122 if !hasDep(libfStatic, prebuiltLibfStatic) {
123 t.Errorf("libf static missing dependency on prebuilt_libf")
124 }
125
126 if !hasDep(libfShared, prebuiltLibfShared) {
127 t.Errorf("libf shared missing dependency on prebuilt_libf")
128 }
129}
130
131func testPrebuilt(t *testing.T, bp string) *android.TestContext {
132 fs := map[string][]byte{
133 "liba.so": nil,
134 "libb.a": nil,
135 "libd.so": nil,
136 "libe.a": nil,
137 "libf.a": nil,
138 "libf.so": nil,
139 }
140 config := TestConfig(buildDir, android.Android, nil, bp, fs)
141 ctx := CreateTestContext()
142
143 // Enable androidmk support.
144 // * Register the singleton
145 // * Configure that we are inside make
146 // * Add CommonOS to ensure that androidmk processing works.
147 android.RegisterAndroidMkBuildComponents(ctx)
148 android.SetInMakeForTests(config)
149
150 ctx.Register(config)
151 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
152 android.FailIfErrored(t, errs)
153 _, errs = ctx.PrepareBuildActions(config)
154 android.FailIfErrored(t, errs)
155 return ctx
156}
157
158func TestPrebuiltLibraryShared(t *testing.T) {
159 ctx := testPrebuilt(t, `
160 cc_prebuilt_library_shared {
161 name: "libtest",
162 srcs: ["libf.so"],
163 strip: {
164 none: true,
165 },
166 }
167 `)
168
169 shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
170 assertString(t, shared.OutputFile().String(), "libf.so")
171}
172
173func TestPrebuiltLibraryStatic(t *testing.T) {
174 ctx := testPrebuilt(t, `
175 cc_prebuilt_library_static {
176 name: "libtest",
177 srcs: ["libf.a"],
178 }
179 `)
180
181 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
182 assertString(t, static.OutputFile().String(), "libf.a")
183}
184
185func TestPrebuiltLibrary(t *testing.T) {
186 ctx := testPrebuilt(t, `
187 cc_prebuilt_library {
188 name: "libtest",
189 static: {
190 srcs: ["libf.a"],
191 },
192 shared: {
193 srcs: ["libf.so"],
194 },
195 strip: {
196 none: true,
197 },
198 }
199 `)
200
201 shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
202 assertString(t, shared.OutputFile().String(), "libf.so")
203
204 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
205 assertString(t, static.OutputFile().String(), "libf.a")
Colin Cross33b2fb72019-05-14 14:07:01 -0700206}