blob: 0c8a96936ae06d115e01506dd599270f347cfa81 [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}