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" |
| 23 | ) |
| 24 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 25 | var ccCodegenModeTestData = []struct { |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 26 | setting, expected string |
| 27 | }{ |
| 28 | {"", "production"}, |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 29 | {"mode: `production`,", "production"}, |
| 30 | {"mode: `test`,", "test"}, |
| 31 | {"mode: `exported`,", "exported"}, |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | func TestCCCodegenMode(t *testing.T) { |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 35 | for _, testData := range ccCodegenModeTestData { |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 36 | testCCCodegenModeHelper(t, testData.setting, testData.expected) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func 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 Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 69 | |
| 70 | var incorrectCCCodegenModeTestData = []struct { |
| 71 | setting, expectedErr string |
| 72 | }{ |
| 73 | {"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"}, |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | func TestIncorrectCCCodegenMode(t *testing.T) { |
| 77 | for _, testData := range incorrectCCCodegenModeTestData { |
| 78 | testIncorrectCCCodegenModeHelper(t, testData.setting, testData.expectedErr) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func 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 | } |