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