1// Copyright 2021 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	"strings"
19	"testing"
20)
21
22func TestVendorPublicLibraries(t *testing.T) {
23	ctx := testCc(t, `
24	cc_library_headers {
25		name: "libvendorpublic_headers",
26		product_available: true,
27		export_include_dirs: ["my_include"],
28	}
29	cc_library {
30		name: "libvendorpublic",
31		srcs: ["foo.c"],
32		vendor: true,
33		no_libcrt: true,
34		nocrt: true,
35		vendor_public_library: {
36			symbol_file: "libvendorpublic.map.txt",
37			export_public_headers: ["libvendorpublic_headers"],
38		},
39	}
40
41	cc_library {
42		name: "libsystem",
43		shared_libs: ["libvendorpublic"],
44		vendor: false,
45		srcs: ["foo.c"],
46		no_libcrt: true,
47		nocrt: true,
48	}
49	cc_library {
50		name: "libproduct",
51		shared_libs: ["libvendorpublic"],
52		product_specific: true,
53		srcs: ["foo.c"],
54		no_libcrt: true,
55		nocrt: true,
56	}
57	cc_library {
58		name: "libvendor",
59		shared_libs: ["libvendorpublic"],
60		vendor: true,
61		srcs: ["foo.c"],
62		no_libcrt: true,
63		nocrt: true,
64	}
65	`)
66
67	coreVariant := "android_arm64_armv8-a_shared"
68	vendorVariant := "android_vendor.29_arm64_armv8-a_shared"
69	productVariant := "android_product.29_arm64_armv8-a_shared"
70
71	// test if header search paths are correctly added
72	// _static variant is used since _shared reuses *.o from the static variant
73	cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
74	cflags := cc.Args["cFlags"]
75	if !strings.Contains(cflags, "-Imy_include") {
76		t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
77	}
78
79	// test if libsystem is linked to the stub
80	ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
81	libflags := ld.Args["libFlags"]
82	stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic"})
83	if !strings.Contains(libflags, stubPaths[0].String()) {
84		t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
85	}
86
87	// test if libsystem is linked to the stub
88	ld = ctx.ModuleForTests("libproduct", productVariant).Rule("ld")
89	libflags = ld.Args["libFlags"]
90	stubPaths = getOutputPaths(ctx, productVariant, []string{"libvendorpublic"})
91	if !strings.Contains(libflags, stubPaths[0].String()) {
92		t.Errorf("libflags for libproduct must contain %#v, but was %#v", stubPaths[0], libflags)
93	}
94
95	// test if libvendor is linked to the real shared lib
96	ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
97	libflags = ld.Args["libFlags"]
98	stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
99	if !strings.Contains(libflags, stubPaths[0].String()) {
100		t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
101	}
102}
103