diff options
author | Colin Cross <ccross@android.com> | 2017-11-16 14:29:11 -0800 |
---|---|---|
committer | Colin Cross <ccross@android.com> | 2017-11-17 11:22:04 -0800 |
commit | ad59e75a567f0638bae504543664a57582326a58 (patch) | |
tree | 6940234b70aa79ece73f363718b5dc7fcd8df3ed /cc | |
parent | 20823f95e6d90d7961b3c97ec57b4bcb2b8a24ab (diff) | |
download | build_soong-ad59e75a567f0638bae504543664a57582326a58.tar.gz build_soong-ad59e75a567f0638bae504543664a57582326a58.tar.bz2 build_soong-ad59e75a567f0638bae504543664a57582326a58.zip |
Add cc_library tests
Add tests around reusing objects between static and shared
libraries.
Test: library_test.go
Change-Id: I1a1a01c5ea9f9edfbcaa5b29c39c281630e04f70
Diffstat (limited to 'cc')
-rw-r--r-- | cc/cc_test.go | 5 | ||||
-rw-r--r-- | cc/library.go | 2 | ||||
-rw-r--r-- | cc/library_test.go | 185 |
3 files changed, 191 insertions, 1 deletions
diff --git a/cc/cc_test.go b/cc/cc_test.go index 32cffe4e..b451f17c 100644 --- a/cc/cc_test.go +++ b/cc/cc_test.go @@ -108,12 +108,17 @@ func testCc(t *testing.T, bp string) *android.TestContext { name: "crtend_so", } + cc_library { + name: "libprotobuf-cpp-lite", + } + ` ctx.MockFileSystem(map[string][]byte{ "Android.bp": []byte(bp), "foo.c": nil, "bar.c": nil, + "a.proto": nil, }) _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) diff --git a/cc/library.go b/cc/library.go index d4ed7c63..23e4f44c 100644 --- a/cc/library.go +++ b/cc/library.go @@ -651,7 +651,7 @@ func vndkVsNdk(ctx ModuleContext) bool { func (library *libraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path { - objs = objs.Append(deps.Objs) + objs = deps.Objs.Copy().Append(objs) var out android.Path if library.static() || library.header() { out = library.linkStatic(ctx, flags, deps, objs) diff --git a/cc/library_test.go b/cc/library_test.go new file mode 100644 index 00000000..859b05af --- /dev/null +++ b/cc/library_test.go @@ -0,0 +1,185 @@ +// Copyright 2017 Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cc + +import ( + "reflect" + "testing" +) + +func TestLibraryReuse(t *testing.T) { + t.Run("simple", func(t *testing.T) { + ctx := testCc(t, ` + cc_library { + name: "libfoo", + srcs: ["foo.c"], + }`) + + libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld") + libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a") + + if len(libfooShared.Inputs) != 1 { + t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings()) + } + + if len(libfooStatic.Inputs) != 1 { + t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings()) + } + + if libfooShared.Inputs[0] != libfooStatic.Inputs[0] { + t.Errorf("static object not reused for shared library") + } + }) + + t.Run("extra static source", func(t *testing.T) { + ctx := testCc(t, ` + cc_library { + name: "libfoo", + srcs: ["foo.c"], + static: { + srcs: ["bar.c"] + }, + }`) + + libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld") + libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a") + + if len(libfooShared.Inputs) != 1 { + t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings()) + } + + if len(libfooStatic.Inputs) != 2 { + t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings()) + } + + if libfooShared.Inputs[0] != libfooStatic.Inputs[0] { + t.Errorf("static object not reused for shared library") + } + }) + + t.Run("extra shared source", func(t *testing.T) { + ctx := testCc(t, ` + cc_library { + name: "libfoo", + srcs: ["foo.c"], + shared: { + srcs: ["bar.c"] + }, + }`) + + libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld") + libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a") + + if len(libfooShared.Inputs) != 2 { + t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings()) + } + + if len(libfooStatic.Inputs) != 1 { + t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings()) + } + + if libfooShared.Inputs[0] != libfooStatic.Inputs[0] { + t.Errorf("static object not reused for shared library") + } + }) + + t.Run("extra static cflags", func(t *testing.T) { + ctx := testCc(t, ` + cc_library { + name: "libfoo", + srcs: ["foo.c"], + static: { + cflags: ["-DFOO"], + }, + }`) + + libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld") + libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a") + + if len(libfooShared.Inputs) != 1 { + t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings()) + } + + if len(libfooStatic.Inputs) != 1 { + t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings()) + } + + if libfooShared.Inputs[0] == libfooStatic.Inputs[0] { + t.Errorf("static object reused for shared library when it shouldn't be") + } + }) + + t.Run("extra shared cflags", func(t *testing.T) { + ctx := testCc(t, ` + cc_library { + name: "libfoo", + srcs: ["foo.c"], + shared: { + cflags: ["-DFOO"], + }, + }`) + + libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld") + libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a") + + if len(libfooShared.Inputs) != 1 { + t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings()) + } + + if len(libfooStatic.Inputs) != 1 { + t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings()) + } + + if libfooShared.Inputs[0] == libfooStatic.Inputs[0] { + t.Errorf("static object reused for shared library when it shouldn't be") + } + }) + + t.Run("global cflags for reused generated sources", func(t *testing.T) { + ctx := testCc(t, ` + cc_library { + name: "libfoo", + srcs: [ + "foo.c", + "a.proto", + ], + shared: { + srcs: [ + "bar.c", + ], + }, + }`) + + libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld") + libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a") + + if len(libfooShared.Inputs) != 3 { + t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings()) + } + + if len(libfooStatic.Inputs) != 2 { + t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings()) + } + + if !reflect.DeepEqual(libfooShared.Inputs[0:2].Strings(), libfooStatic.Inputs.Strings()) { + t.Errorf("static objects not reused for shared library") + } + + libfoo := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Module().(*Module) + if !inList("-DGOOGLE_PROTOBUF_NO_RTTI", libfoo.flags.CFlags) { + t.Errorf("missing protobuf cflags") + } + }) +} |