Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 1 | // 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 Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 15 | package codegen |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "testing" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | "android/soong/cc" |
Dennis Shen | c6dc551 | 2024-01-10 14:07:35 +0000 | [diff] [blame^] | 23 | |
| 24 | "github.com/google/blueprint" |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 25 | ) |
| 26 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 27 | var ccCodegenModeTestData = []struct { |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 28 | setting, expected string |
| 29 | }{ |
| 30 | {"", "production"}, |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 31 | {"mode: `production`,", "production"}, |
| 32 | {"mode: `test`,", "test"}, |
| 33 | {"mode: `exported`,", "exported"}, |
Zhi Dou | 70e2124 | 2023-12-20 23:14:34 +0000 | [diff] [blame] | 34 | {"mode: `force-read-only`,", "force-read-only"}, |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | func TestCCCodegenMode(t *testing.T) { |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 38 | for _, testData := range ccCodegenModeTestData { |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 39 | testCCCodegenModeHelper(t, testData.setting, testData.expected) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func 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 Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 72 | |
| 73 | var incorrectCCCodegenModeTestData = []struct { |
| 74 | setting, expectedErr string |
| 75 | }{ |
| 76 | {"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"}, |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | func TestIncorrectCCCodegenMode(t *testing.T) { |
| 80 | for _, testData := range incorrectCCCodegenModeTestData { |
| 81 | testIncorrectCCCodegenModeHelper(t, testData.setting, testData.expectedErr) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func 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 Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 110 | |
| 111 | func 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 Kim | b5fdb2e | 2024-01-03 14:24:34 +0900 | [diff] [blame] | 153 | vendor_available: true, |
Yu Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 154 | } |
| 155 | ` |
| 156 | result := android.GroupFixturePreparers( |
| 157 | PrepareForTestWithAconfigBuildComponents, |
| 158 | cc.PrepareForTestWithCcDefaultModules). |
| 159 | ExtendWithErrorHandler(android.FixtureExpectsNoErrors).RunTestWithBp(t, bp) |
| 160 | |
Kiyoung Kim | b5fdb2e | 2024-01-03 14:24:34 +0900 | [diff] [blame] | 161 | module := result.ModuleForTests("my_cc_library", "android_vendor_arm64_armv8-a_shared").Module() |
Yu Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 162 | |
| 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 Shen | c6dc551 | 2024-01-10 14:07:35 +0000 | [diff] [blame^] | 169 | |
| 170 | func 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 | } |