1// Copyright 2020 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	"testing"
19
20	"android/soong/android"
21)
22
23func TestSdkMutator(t *testing.T) {
24	bp := `
25		cc_library {
26			name: "libsdk",
27			shared_libs: ["libsdkdep"],
28			sdk_version: "current",
29			stl: "c++_shared",
30		}
31
32		cc_library {
33			name: "libsdkdep",
34			sdk_version: "current",
35			stl: "c++_shared",
36		}
37
38		cc_library {
39			name: "libplatform",
40			shared_libs: ["libsdk"],
41			stl: "libc++",
42		}
43
44		cc_binary {
45			name: "platformbinary",
46			shared_libs: ["libplatform"],
47			stl: "libc++",
48		}
49
50		cc_binary {
51			name: "sdkbinary",
52			shared_libs: ["libsdk"],
53			sdk_version: "current",
54			stl: "libc++",
55		}
56	`
57
58	assertDep := func(t *testing.T, from, to android.TestingModule) {
59		t.Helper()
60		found := false
61
62		var toFile android.Path
63		m := to.Module().(*Module)
64		if toc := m.Toc(); toc.Valid() {
65			toFile = toc.Path()
66		} else {
67			toFile = m.outputFile.Path()
68		}
69
70		rule := from.Description("link")
71		for _, dep := range rule.Implicits {
72			if dep.String() == toFile.String() {
73				found = true
74			}
75		}
76		if !found {
77			t.Errorf("expected %q in %q", toFile.String(), rule.Implicits.Strings())
78		}
79	}
80
81	ctx := testCc(t, bp)
82
83	libsdkNDK := ctx.ModuleForTests("libsdk", "android_arm64_armv8-a_sdk_shared")
84	libsdkPlatform := ctx.ModuleForTests("libsdk", "android_arm64_armv8-a_shared")
85	libsdkdepNDK := ctx.ModuleForTests("libsdkdep", "android_arm64_armv8-a_sdk_shared")
86	libsdkdepPlatform := ctx.ModuleForTests("libsdkdep", "android_arm64_armv8-a_shared")
87	libplatform := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared")
88	platformbinary := ctx.ModuleForTests("platformbinary", "android_arm64_armv8-a")
89	sdkbinary := ctx.ModuleForTests("sdkbinary", "android_arm64_armv8-a_sdk")
90
91	libcxxNDK := ctx.ModuleForTests("ndk_libc++_shared", "android_arm64_armv8-a_sdk_shared")
92	libcxxPlatform := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared")
93
94	assertDep(t, libsdkNDK, libsdkdepNDK)
95	assertDep(t, libsdkPlatform, libsdkdepPlatform)
96	assertDep(t, libplatform, libsdkPlatform)
97	assertDep(t, platformbinary, libplatform)
98	assertDep(t, sdkbinary, libsdkNDK)
99
100	assertDep(t, libsdkNDK, libcxxNDK)
101	assertDep(t, libsdkPlatform, libcxxPlatform)
102}
103