blob: d762e9bfee6e1b6cfc6b9186b601c432cb783a20 [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"
23)
24
Zi Wang275f6542023-11-09 14:59:31 -080025var ccCodegenModeTestData = []struct {
Dennis Shenc5e39f52023-09-14 18:52:49 +000026 setting, expected string
27}{
28 {"", "production"},
Zi Wang275f6542023-11-09 14:59:31 -080029 {"mode: `production`,", "production"},
30 {"mode: `test`,", "test"},
31 {"mode: `exported`,", "exported"},
Zhi Dou70e21242023-12-20 23:14:34 +000032 {"mode: `force-read-only`,", "force-read-only"},
Dennis Shenc5e39f52023-09-14 18:52:49 +000033}
34
35func TestCCCodegenMode(t *testing.T) {
Zi Wang275f6542023-11-09 14:59:31 -080036 for _, testData := range ccCodegenModeTestData {
Dennis Shenc5e39f52023-09-14 18:52:49 +000037 testCCCodegenModeHelper(t, testData.setting, testData.expected)
38 }
39}
40
41func testCCCodegenModeHelper(t *testing.T, bpMode string, ruleMode string) {
42 t.Helper()
43 result := android.GroupFixturePreparers(
44 PrepareForTestWithAconfigBuildComponents,
45 cc.PrepareForTestWithCcDefaultModules).
46 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
47 RunTestWithBp(t, fmt.Sprintf(`
48 aconfig_declarations {
49 name: "my_aconfig_declarations",
50 package: "com.example.package",
51 srcs: ["foo.aconfig"],
52 }
53
54 cc_library {
55 name: "server_configurable_flags",
56 srcs: ["server_configurable_flags.cc"],
57 }
58
59 cc_aconfig_library {
60 name: "my_cc_aconfig_library",
61 aconfig_declarations: "my_aconfig_declarations",
62 %s
63 }
64 `, bpMode))
65
66 module := result.ModuleForTests("my_cc_aconfig_library", "android_arm64_armv8-a_shared")
67 rule := module.Rule("cc_aconfig_library")
68 android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode)
69}
Zi Wang275f6542023-11-09 14:59:31 -080070
71var incorrectCCCodegenModeTestData = []struct {
72 setting, expectedErr string
73}{
74 {"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"},
Zi Wang275f6542023-11-09 14:59:31 -080075}
76
77func TestIncorrectCCCodegenMode(t *testing.T) {
78 for _, testData := range incorrectCCCodegenModeTestData {
79 testIncorrectCCCodegenModeHelper(t, testData.setting, testData.expectedErr)
80 }
81}
82
83func testIncorrectCCCodegenModeHelper(t *testing.T, bpMode string, err string) {
84 t.Helper()
85 android.GroupFixturePreparers(
86 PrepareForTestWithAconfigBuildComponents,
87 cc.PrepareForTestWithCcDefaultModules).
88 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)).
89 RunTestWithBp(t, fmt.Sprintf(`
90 aconfig_declarations {
91 name: "my_aconfig_declarations",
92 package: "com.example.package",
93 srcs: ["foo.aconfig"],
94 }
95
96 cc_library {
97 name: "server_configurable_flags",
98 srcs: ["server_configurable_flags.cc"],
99 }
100
101 cc_aconfig_library {
102 name: "my_cc_aconfig_library",
103 aconfig_declarations: "my_aconfig_declarations",
104 %s
105 }
106 `, bpMode))
107}
Yu Liu6dc93f92023-12-14 01:19:35 +0000108
109func TestAndroidMkCcLibrary(t *testing.T) {
110 bp := `
111 aconfig_declarations {
112 name: "my_aconfig_declarations_foo",
113 package: "com.example.package",
114 srcs: ["foo.aconfig"],
115 container: "vendor",
116 }
117
118 cc_aconfig_library {
119 name: "my_cc_aconfig_library_foo",
120 aconfig_declarations: "my_aconfig_declarations_foo",
121 vendor_available: true,
122 }
123
124 aconfig_declarations {
125 name: "my_aconfig_declarations_bar",
126 package: "com.example.package",
127 srcs: ["bar.aconfig"],
128 }
129
130 cc_aconfig_library {
131 name: "my_cc_aconfig_library_bar",
132 aconfig_declarations: "my_aconfig_declarations_bar",
133 vendor_available: true,
134 }
135
136 cc_library {
137 name: "my_cc_library",
138 srcs: [
139 "src/foo.cc",
140 ],
141 static_libs: [
142 "my_cc_aconfig_library_foo",
143 "my_cc_aconfig_library_bar",
144 ],
145 vendor: true,
146 }
147
148 cc_library {
149 name: "server_configurable_flags",
150 srcs: ["server_configurable_flags.cc"],
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900151 vendor_available: true,
Yu Liu6dc93f92023-12-14 01:19:35 +0000152 }
153 `
154 result := android.GroupFixturePreparers(
155 PrepareForTestWithAconfigBuildComponents,
156 cc.PrepareForTestWithCcDefaultModules).
157 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).RunTestWithBp(t, bp)
158
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900159 module := result.ModuleForTests("my_cc_library", "android_vendor_arm64_armv8-a_shared").Module()
Yu Liu6dc93f92023-12-14 01:19:35 +0000160
161 entry := android.AndroidMkEntriesForTest(t, result.TestContext, module)[0]
162
163 makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"]
164 android.AssertIntEquals(t, "len(LOCAL_ACONFIG_FILES)", 1, len(makeVar))
165 android.EnsureListContainsSuffix(t, makeVar, "my_aconfig_declarations_foo/intermediate.pb")
166}