2017-11-17 06:29:11 +08:00
|
|
|
// 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"
|
2020-04-02 00:41:41 +08:00
|
|
|
|
|
|
|
"android/soong/android"
|
2017-11-17 06:29:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLibraryReuse(t *testing.T) {
|
|
|
|
t.Run("simple", func(t *testing.T) {
|
|
|
|
ctx := testCc(t, `
|
|
|
|
cc_library {
|
|
|
|
name: "libfoo",
|
2019-08-17 03:14:32 +08:00
|
|
|
srcs: ["foo.c", "baz.o"],
|
2017-11-17 06:29:11 +08:00
|
|
|
}`)
|
|
|
|
|
2019-11-21 08:39:12 +08:00
|
|
|
libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
|
|
|
|
libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_static").Output("libfoo.a")
|
2017-11-17 06:29:11 +08:00
|
|
|
|
2019-08-17 03:14:32 +08:00
|
|
|
if len(libfooShared.Inputs) != 2 {
|
2017-11-17 06:29:11 +08:00
|
|
|
t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
|
|
|
|
}
|
|
|
|
|
2019-08-17 03:14:32 +08:00
|
|
|
if len(libfooStatic.Inputs) != 2 {
|
2017-11-17 06:29:11 +08:00
|
|
|
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")
|
|
|
|
}
|
2019-08-17 03:14:32 +08:00
|
|
|
if libfooShared.Inputs[1] != libfooStatic.Inputs[1] {
|
|
|
|
t.Errorf("static object not reused for shared library")
|
|
|
|
}
|
2017-11-17 06:29:11 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("extra static source", func(t *testing.T) {
|
|
|
|
ctx := testCc(t, `
|
|
|
|
cc_library {
|
|
|
|
name: "libfoo",
|
|
|
|
srcs: ["foo.c"],
|
|
|
|
static: {
|
|
|
|
srcs: ["bar.c"]
|
|
|
|
},
|
|
|
|
}`)
|
|
|
|
|
2019-11-21 08:39:12 +08:00
|
|
|
libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
|
|
|
|
libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_static").Output("libfoo.a")
|
2017-11-17 06:29:11 +08:00
|
|
|
|
|
|
|
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"]
|
|
|
|
},
|
|
|
|
}`)
|
|
|
|
|
2019-11-21 08:39:12 +08:00
|
|
|
libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
|
|
|
|
libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_static").Output("libfoo.a")
|
2017-11-17 06:29:11 +08:00
|
|
|
|
|
|
|
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"],
|
|
|
|
},
|
|
|
|
}`)
|
|
|
|
|
2019-11-21 08:39:12 +08:00
|
|
|
libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
|
|
|
|
libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_static").Output("libfoo.a")
|
2017-11-17 06:29:11 +08:00
|
|
|
|
|
|
|
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"],
|
|
|
|
},
|
|
|
|
}`)
|
|
|
|
|
2019-11-21 08:39:12 +08:00
|
|
|
libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
|
|
|
|
libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_static").Output("libfoo.a")
|
2017-11-17 06:29:11 +08:00
|
|
|
|
|
|
|
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",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}`)
|
|
|
|
|
2019-11-21 08:39:12 +08:00
|
|
|
libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
|
|
|
|
libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_static").Output("libfoo.a")
|
2017-11-17 06:29:11 +08:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-11-21 08:39:12 +08:00
|
|
|
libfoo := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Module().(*Module)
|
2019-11-05 01:37:55 +08:00
|
|
|
if !inList("-DGOOGLE_PROTOBUF_NO_RTTI", libfoo.flags.Local.CFlags) {
|
2017-11-17 06:29:11 +08:00
|
|
|
t.Errorf("missing protobuf cflags")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-04-02 00:41:41 +08:00
|
|
|
|
|
|
|
func TestStubsVersions(t *testing.T) {
|
|
|
|
bp := `
|
|
|
|
cc_library {
|
|
|
|
name: "libfoo",
|
|
|
|
srcs: ["foo.c"],
|
|
|
|
stubs: {
|
2020-07-23 13:32:17 +08:00
|
|
|
versions: ["29", "R", "current"],
|
2020-04-02 00:41:41 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
`
|
|
|
|
config := TestConfig(buildDir, android.Android, nil, bp, nil)
|
|
|
|
config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
|
|
|
|
ctx := testCcWithConfig(t, config)
|
|
|
|
|
|
|
|
variants := ctx.ModuleVariantsForTests("libfoo")
|
2020-07-23 13:32:17 +08:00
|
|
|
for _, expectedVer := range []string{"29", "R", "current"} {
|
2020-04-02 00:41:41 +08:00
|
|
|
expectedVariant := "android_arm_armv7-a-neon_shared_" + expectedVer
|
|
|
|
if !inList(expectedVariant, variants) {
|
|
|
|
t.Errorf("missing expected variant: %q", expectedVariant)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStubsVersions_NotSorted(t *testing.T) {
|
|
|
|
bp := `
|
|
|
|
cc_library {
|
|
|
|
name: "libfoo",
|
|
|
|
srcs: ["foo.c"],
|
|
|
|
stubs: {
|
2020-07-23 13:32:17 +08:00
|
|
|
versions: ["29", "current", "R"],
|
2020-04-02 00:41:41 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
`
|
|
|
|
config := TestConfig(buildDir, android.Android, nil, bp, nil)
|
|
|
|
config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
|
|
|
|
testCcErrorWithConfig(t, `"libfoo" .*: versions: not sorted`, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStubsVersions_ParseError(t *testing.T) {
|
|
|
|
bp := `
|
|
|
|
cc_library {
|
|
|
|
name: "libfoo",
|
|
|
|
srcs: ["foo.c"],
|
|
|
|
stubs: {
|
2020-07-23 13:32:17 +08:00
|
|
|
versions: ["29", "current", "X"],
|
2020-04-02 00:41:41 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2020-07-23 13:32:17 +08:00
|
|
|
testCcError(t, `"libfoo" .*: versions: "X" could not be parsed as an integer and is not a recognized codename`, bp)
|
2020-04-02 00:41:41 +08:00
|
|
|
}
|