blob: 859b05aff2da5b0ccf4ca8575eab7342dcabdd3c [file] [log] [blame]
Colin Crossad59e752017-11-16 14:29:11 -08001// Copyright 2017 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 "reflect"
19 "testing"
20)
21
22func TestLibraryReuse(t *testing.T) {
23 t.Run("simple", func(t *testing.T) {
24 ctx := testCc(t, `
25 cc_library {
26 name: "libfoo",
27 srcs: ["foo.c"],
28 }`)
29
30 libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld")
31 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a")
32
33 if len(libfooShared.Inputs) != 1 {
34 t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
35 }
36
37 if len(libfooStatic.Inputs) != 1 {
38 t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
39 }
40
41 if libfooShared.Inputs[0] != libfooStatic.Inputs[0] {
42 t.Errorf("static object not reused for shared library")
43 }
44 })
45
46 t.Run("extra static source", func(t *testing.T) {
47 ctx := testCc(t, `
48 cc_library {
49 name: "libfoo",
50 srcs: ["foo.c"],
51 static: {
52 srcs: ["bar.c"]
53 },
54 }`)
55
56 libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld")
57 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a")
58
59 if len(libfooShared.Inputs) != 1 {
60 t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
61 }
62
63 if len(libfooStatic.Inputs) != 2 {
64 t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
65 }
66
67 if libfooShared.Inputs[0] != libfooStatic.Inputs[0] {
68 t.Errorf("static object not reused for shared library")
69 }
70 })
71
72 t.Run("extra shared source", func(t *testing.T) {
73 ctx := testCc(t, `
74 cc_library {
75 name: "libfoo",
76 srcs: ["foo.c"],
77 shared: {
78 srcs: ["bar.c"]
79 },
80 }`)
81
82 libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld")
83 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a")
84
85 if len(libfooShared.Inputs) != 2 {
86 t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
87 }
88
89 if len(libfooStatic.Inputs) != 1 {
90 t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
91 }
92
93 if libfooShared.Inputs[0] != libfooStatic.Inputs[0] {
94 t.Errorf("static object not reused for shared library")
95 }
96 })
97
98 t.Run("extra static cflags", func(t *testing.T) {
99 ctx := testCc(t, `
100 cc_library {
101 name: "libfoo",
102 srcs: ["foo.c"],
103 static: {
104 cflags: ["-DFOO"],
105 },
106 }`)
107
108 libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld")
109 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a")
110
111 if len(libfooShared.Inputs) != 1 {
112 t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
113 }
114
115 if len(libfooStatic.Inputs) != 1 {
116 t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
117 }
118
119 if libfooShared.Inputs[0] == libfooStatic.Inputs[0] {
120 t.Errorf("static object reused for shared library when it shouldn't be")
121 }
122 })
123
124 t.Run("extra shared cflags", func(t *testing.T) {
125 ctx := testCc(t, `
126 cc_library {
127 name: "libfoo",
128 srcs: ["foo.c"],
129 shared: {
130 cflags: ["-DFOO"],
131 },
132 }`)
133
134 libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld")
135 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a")
136
137 if len(libfooShared.Inputs) != 1 {
138 t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
139 }
140
141 if len(libfooStatic.Inputs) != 1 {
142 t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
143 }
144
145 if libfooShared.Inputs[0] == libfooStatic.Inputs[0] {
146 t.Errorf("static object reused for shared library when it shouldn't be")
147 }
148 })
149
150 t.Run("global cflags for reused generated sources", func(t *testing.T) {
151 ctx := testCc(t, `
152 cc_library {
153 name: "libfoo",
154 srcs: [
155 "foo.c",
156 "a.proto",
157 ],
158 shared: {
159 srcs: [
160 "bar.c",
161 ],
162 },
163 }`)
164
165 libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld")
166 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a")
167
168 if len(libfooShared.Inputs) != 3 {
169 t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
170 }
171
172 if len(libfooStatic.Inputs) != 2 {
173 t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
174 }
175
176 if !reflect.DeepEqual(libfooShared.Inputs[0:2].Strings(), libfooStatic.Inputs.Strings()) {
177 t.Errorf("static objects not reused for shared library")
178 }
179
180 libfoo := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Module().(*Module)
181 if !inList("-DGOOGLE_PROTOBUF_NO_RTTI", libfoo.flags.CFlags) {
182 t.Errorf("missing protobuf cflags")
183 }
184 })
185}