blob: 860f9487eb4d3c84f988f815f5dadffa9e4038f5 [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",
Yu Liue916a2c2024-03-20 19:47:28 +000053 container: "com.android.foo",
Dennis Shenc5e39f52023-09-14 18:52:49 +000054 srcs: ["foo.aconfig"],
55 }
56
57 cc_library {
58 name: "server_configurable_flags",
59 srcs: ["server_configurable_flags.cc"],
60 }
61
62 cc_aconfig_library {
63 name: "my_cc_aconfig_library",
64 aconfig_declarations: "my_aconfig_declarations",
65 %s
66 }
67 `, bpMode))
68
69 module := result.ModuleForTests("my_cc_aconfig_library", "android_arm64_armv8-a_shared")
70 rule := module.Rule("cc_aconfig_library")
71 android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode)
72}
Zi Wang275f6542023-11-09 14:59:31 -080073
74var incorrectCCCodegenModeTestData = []struct {
75 setting, expectedErr string
76}{
77 {"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"},
Zi Wang275f6542023-11-09 14:59:31 -080078}
79
80func TestIncorrectCCCodegenMode(t *testing.T) {
81 for _, testData := range incorrectCCCodegenModeTestData {
82 testIncorrectCCCodegenModeHelper(t, testData.setting, testData.expectedErr)
83 }
84}
85
86func testIncorrectCCCodegenModeHelper(t *testing.T, bpMode string, err string) {
87 t.Helper()
88 android.GroupFixturePreparers(
89 PrepareForTestWithAconfigBuildComponents,
90 cc.PrepareForTestWithCcDefaultModules).
91 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)).
92 RunTestWithBp(t, fmt.Sprintf(`
93 aconfig_declarations {
94 name: "my_aconfig_declarations",
95 package: "com.example.package",
Yu Liue916a2c2024-03-20 19:47:28 +000096 container: "com.android.foo",
Zi Wang275f6542023-11-09 14:59:31 -080097 srcs: ["foo.aconfig"],
98 }
99
100 cc_library {
101 name: "server_configurable_flags",
102 srcs: ["server_configurable_flags.cc"],
103 }
104
105 cc_aconfig_library {
106 name: "my_cc_aconfig_library",
107 aconfig_declarations: "my_aconfig_declarations",
108 %s
109 }
110 `, bpMode))
111}
Yu Liu6dc93f92023-12-14 01:19:35 +0000112
113func TestAndroidMkCcLibrary(t *testing.T) {
114 bp := `
115 aconfig_declarations {
116 name: "my_aconfig_declarations_foo",
117 package: "com.example.package",
118 srcs: ["foo.aconfig"],
119 container: "vendor",
120 }
121
122 cc_aconfig_library {
123 name: "my_cc_aconfig_library_foo",
124 aconfig_declarations: "my_aconfig_declarations_foo",
125 vendor_available: true,
126 }
127
128 aconfig_declarations {
129 name: "my_aconfig_declarations_bar",
130 package: "com.example.package",
Yu Liue916a2c2024-03-20 19:47:28 +0000131 container: "com.android.foo",
Yu Liu6dc93f92023-12-14 01:19:35 +0000132 srcs: ["bar.aconfig"],
133 }
134
135 cc_aconfig_library {
136 name: "my_cc_aconfig_library_bar",
137 aconfig_declarations: "my_aconfig_declarations_bar",
138 vendor_available: true,
139 }
140
141 cc_library {
142 name: "my_cc_library",
143 srcs: [
144 "src/foo.cc",
145 ],
146 static_libs: [
147 "my_cc_aconfig_library_foo",
148 "my_cc_aconfig_library_bar",
149 ],
150 vendor: true,
151 }
152
153 cc_library {
154 name: "server_configurable_flags",
155 srcs: ["server_configurable_flags.cc"],
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900156 vendor_available: true,
Yu Liu6dc93f92023-12-14 01:19:35 +0000157 }
158 `
159 result := android.GroupFixturePreparers(
160 PrepareForTestWithAconfigBuildComponents,
161 cc.PrepareForTestWithCcDefaultModules).
162 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).RunTestWithBp(t, bp)
163
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900164 module := result.ModuleForTests("my_cc_library", "android_vendor_arm64_armv8-a_shared").Module()
Yu Liu6dc93f92023-12-14 01:19:35 +0000165
166 entry := android.AndroidMkEntriesForTest(t, result.TestContext, module)[0]
167
168 makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"]
Yu Liu6dc93f92023-12-14 01:19:35 +0000169 android.EnsureListContainsSuffix(t, makeVar, "my_aconfig_declarations_foo/intermediate.pb")
170}
Dennis Shenc6dc5512024-01-10 14:07:35 +0000171
172func TestForceReadOnly(t *testing.T) {
173 t.Helper()
174 result := android.GroupFixturePreparers(
175 PrepareForTestWithAconfigBuildComponents,
176 cc.PrepareForTestWithCcDefaultModules).
177 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
178 RunTestWithBp(t, fmt.Sprintf(`
179 aconfig_declarations {
180 name: "my_aconfig_declarations",
181 package: "com.example.package",
Yu Liue916a2c2024-03-20 19:47:28 +0000182 container: "com.android.foo",
Dennis Shenc6dc5512024-01-10 14:07:35 +0000183 srcs: ["foo.aconfig"],
184 }
185
186 cc_aconfig_library {
187 name: "my_cc_aconfig_library",
188 aconfig_declarations: "my_aconfig_declarations",
189 mode: "force-read-only",
190 }
191 `))
192
193 module := result.ModuleForTests("my_cc_aconfig_library", "android_arm64_armv8-a_shared").Module()
194 dependOnBaseLib := false
195 result.VisitDirectDeps(module, func(dep blueprint.Module) {
196 if dep.Name() == baseLibDep {
197 dependOnBaseLib = true
198 }
199 })
200 android.AssertBoolEquals(t, "should not have dependency on server_configuriable_flags",
201 dependOnBaseLib, false)
202}