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