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		toFile = toFile.RelativeToTop()
70
71		rule := from.Description("link")
72		for _, dep := range rule.Implicits {
73			if dep.String() == toFile.String() {
74				found = true
75			}
76		}
77		if !found {
78			t.Errorf("expected %q in %q", toFile.String(), rule.Implicits.Strings())
79		}
80	}
81
82	ctx := testCc(t, bp)
83
84	libsdkNDK := ctx.ModuleForTests("libsdk", "android_arm64_armv8-a_sdk_shared")
85	libsdkPlatform := ctx.ModuleForTests("libsdk", "android_arm64_armv8-a_shared")
86	libsdkdepNDK := ctx.ModuleForTests("libsdkdep", "android_arm64_armv8-a_sdk_shared")
87	libsdkdepPlatform := ctx.ModuleForTests("libsdkdep", "android_arm64_armv8-a_shared")
88	libplatform := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared")
89	platformbinary := ctx.ModuleForTests("platformbinary", "android_arm64_armv8-a")
90	sdkbinary := ctx.ModuleForTests("sdkbinary", "android_arm64_armv8-a_sdk")
91
92	libcxxNDK := ctx.ModuleForTests("ndk_libc++_shared", "android_arm64_armv8-a_sdk_shared")
93	libcxxPlatform := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared")
94
95	assertDep(t, libsdkNDK, libsdkdepNDK)
96	assertDep(t, libsdkPlatform, libsdkdepPlatform)
97	assertDep(t, libplatform, libsdkPlatform)
98	assertDep(t, platformbinary, libplatform)
99	assertDep(t, sdkbinary, libsdkNDK)
100
101	assertDep(t, libsdkNDK, libcxxNDK)
102	assertDep(t, libsdkPlatform, libcxxPlatform)
103}
104