blob: ef92cc87178aaaa8f97b824d0a672b18a2e76237 [file] [log] [blame]
Dennis Shenc5e39f52023-09-14 18:52:49 +00001// Copyright 2023 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
Yu Liueae7b362023-11-16 17:05:47 -080015package codegen
Dennis Shenc5e39f52023-09-14 18:52:49 +000016
17import (
18 "fmt"
19 "testing"
20
21 "android/soong/android"
22 "android/soong/cc"
Dennis Shenc6dc5512024-01-10 14:07:35 +000023
24 "github.com/google/blueprint"
Dennis Shenc5e39f52023-09-14 18:52:49 +000025)
26
Zi Wang275f6542023-11-09 14:59:31 -080027var ccCodegenModeTestData = []struct {
Dennis Shenc5e39f52023-09-14 18:52:49 +000028 setting, expected string
29}{
30 {"", "production"},
Zi Wang275f6542023-11-09 14:59:31 -080031 {"mode: `production`,", "production"},
32 {"mode: `test`,", "test"},
33 {"mode: `exported`,", "exported"},
Zhi Dou70e21242023-12-20 23:14:34 +000034 {"mode: `force-read-only`,", "force-read-only"},
Dennis Shenc5e39f52023-09-14 18:52:49 +000035}
36
37func TestCCCodegenMode(t *testing.T) {
Zi Wang275f6542023-11-09 14:59:31 -080038 for _, testData := range ccCodegenModeTestData {
Dennis Shenc5e39f52023-09-14 18:52:49 +000039 testCCCodegenModeHelper(t, testData.setting, testData.expected)
40 }
41}
42
43func testCCCodegenModeHelper(t *testing.T, bpMode string, ruleMode string) {
44 t.Helper()
45 result := android.GroupFixturePreparers(
46 PrepareForTestWithAconfigBuildComponents,
47 cc.PrepareForTestWithCcDefaultModules).
48 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
49 RunTestWithBp(t, fmt.Sprintf(`
50 aconfig_declarations {
51 name: "my_aconfig_declarations",
52 package: "com.example.package",
53 srcs: ["foo.aconfig"],
54 }
55
56 cc_library {
57 name: "server_configurable_flags",
58 srcs: ["server_configurable_flags.cc"],
59 }
60
61 cc_aconfig_library {
62 name: "my_cc_aconfig_library",
63 aconfig_declarations: "my_aconfig_declarations",
64 %s
65 }
66 `, bpMode))
67
68 module := result.ModuleForTests("my_cc_aconfig_library", "android_arm64_armv8-a_shared")
69 rule := module.Rule("cc_aconfig_library")
70 android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode)
71}
Zi Wang275f6542023-11-09 14:59:31 -080072
73var incorrectCCCodegenModeTestData = []struct {
74 setting, expectedErr string
75}{
76 {"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"},
Zi Wang275f6542023-11-09 14:59:31 -080077}
78
79func TestIncorrectCCCodegenMode(t *testing.T) {
80 for _, testData := range incorrectCCCodegenModeTestData {
81 testIncorrectCCCodegenModeHelper(t, testData.setting, testData.expectedErr)
82 }
83}
84
85func testIncorrectCCCodegenModeHelper(t *testing.T, bpMode string, err string) {
86 t.Helper()
87 android.GroupFixturePreparers(
88 PrepareForTestWithAconfigBuildComponents,
89 cc.PrepareForTestWithCcDefaultModules).
90 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)).
91 RunTestWithBp(t, fmt.Sprintf(`
92 aconfig_declarations {
93 name: "my_aconfig_declarations",
94 package: "com.example.package",
95 srcs: ["foo.aconfig"],
96 }
97
98 cc_library {
99 name: "server_configurable_flags",
100 srcs: ["server_configurable_flags.cc"],
101 }
102
103 cc_aconfig_library {
104 name: "my_cc_aconfig_library",
105 aconfig_declarations: "my_aconfig_declarations",
106 %s
107 }
108 `, bpMode))
109}
Yu Liu6dc93f92023-12-14 01:19:35 +0000110
111func TestAndroidMkCcLibrary(t *testing.T) {
112 bp := `
113 aconfig_declarations {
114 name: "my_aconfig_declarations_foo",
115 package: "com.example.package",
116 srcs: ["foo.aconfig"],
117 container: "vendor",
118 }
119
120 cc_aconfig_library {
121 name: "my_cc_aconfig_library_foo",
122 aconfig_declarations: "my_aconfig_declarations_foo",
123 vendor_available: true,
124 }
125
126 aconfig_declarations {
127 name: "my_aconfig_declarations_bar",
128 package: "com.example.package",
129 srcs: ["bar.aconfig"],
130 }
131
132 cc_aconfig_library {
133 name: "my_cc_aconfig_library_bar",
134 aconfig_declarations: "my_aconfig_declarations_bar",
135 vendor_available: true,
136 }
137
138 cc_library {
139 name: "my_cc_library",
140 srcs: [
141 "src/foo.cc",
142 ],
143 static_libs: [
144 "my_cc_aconfig_library_foo",
145 "my_cc_aconfig_library_bar",
146 ],
147 vendor: true,
148 }
149
150 cc_library {
151 name: "server_configurable_flags",
152 srcs: ["server_configurable_flags.cc"],
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900153 vendor_available: true,
Yu Liu6dc93f92023-12-14 01:19:35 +0000154 }
155 `
156 result := android.GroupFixturePreparers(
157 PrepareForTestWithAconfigBuildComponents,
158 cc.PrepareForTestWithCcDefaultModules).
159 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).RunTestWithBp(t, bp)
160
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900161 module := result.ModuleForTests("my_cc_library", "android_vendor_arm64_armv8-a_shared").Module()
Yu Liu6dc93f92023-12-14 01:19:35 +0000162
163 entry := android.AndroidMkEntriesForTest(t, result.TestContext, module)[0]
164
165 makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"]
166 android.AssertIntEquals(t, "len(LOCAL_ACONFIG_FILES)", 1, len(makeVar))
167 android.EnsureListContainsSuffix(t, makeVar, "my_aconfig_declarations_foo/intermediate.pb")
168}
Dennis Shenc6dc5512024-01-10 14:07:35 +0000169
170func TestForceReadOnly(t *testing.T) {
171 t.Helper()
172 result := android.GroupFixturePreparers(
173 PrepareForTestWithAconfigBuildComponents,
174 cc.PrepareForTestWithCcDefaultModules).
175 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
176 RunTestWithBp(t, fmt.Sprintf(`
177 aconfig_declarations {
178 name: "my_aconfig_declarations",
179 package: "com.example.package",
180 srcs: ["foo.aconfig"],
181 }
182
183 cc_aconfig_library {
184 name: "my_cc_aconfig_library",
185 aconfig_declarations: "my_aconfig_declarations",
186 mode: "force-read-only",
187 }
188 `))
189
190 module := result.ModuleForTests("my_cc_aconfig_library", "android_arm64_armv8-a_shared").Module()
191 dependOnBaseLib := false
192 result.VisitDirectDeps(module, func(dep blueprint.Module) {
193 if dep.Name() == baseLibDep {
194 dependOnBaseLib = true
195 }
196 })
197 android.AssertBoolEquals(t, "should not have dependency on server_configuriable_flags",
198 dependOnBaseLib, false)
199}