Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 1 | // Copyright 2018 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "reflect" |
| 19 | "testing" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | ) |
| 23 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 24 | func testGenruleContext(config android.Config) *android.TestContext { |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 25 | ctx := android.NewTestArchContext(config) |
Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 26 | ctx.RegisterModuleType("cc_genrule", GenRuleFactory) |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 27 | ctx.Register() |
Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 28 | |
| 29 | return ctx |
| 30 | } |
| 31 | |
| 32 | func TestArchGenruleCmd(t *testing.T) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 33 | fs := map[string][]byte{ |
| 34 | "tool": nil, |
| 35 | "foo": nil, |
| 36 | "bar": nil, |
| 37 | } |
Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 38 | bp := ` |
| 39 | cc_genrule { |
| 40 | name: "gen", |
| 41 | tool_files: ["tool"], |
| 42 | cmd: "$(location tool) $(in) $(out)", |
Yu Liu | d620101 | 2022-10-17 12:29:15 -0700 | [diff] [blame] | 43 | out: ["out_arm"], |
Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 44 | arch: { |
| 45 | arm: { |
| 46 | srcs: ["foo"], |
Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 47 | }, |
| 48 | arm64: { |
| 49 | srcs: ["bar"], |
Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 50 | }, |
| 51 | }, |
| 52 | } |
| 53 | ` |
Paul Duffin | c3e6ce0 | 2021-03-22 23:21:32 +0000 | [diff] [blame] | 54 | config := android.TestArchConfig(t.TempDir(), nil, bp, fs) |
Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 55 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 56 | ctx := testGenruleContext(config) |
Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 57 | |
| 58 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 59 | if errs == nil { |
| 60 | _, errs = ctx.PrepareBuildActions(config) |
| 61 | } |
| 62 | if errs != nil { |
| 63 | t.Fatal(errs) |
| 64 | } |
| 65 | |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 66 | gen := ctx.ModuleForTests("gen", "android_arm_armv7-a-neon").Output("out_arm") |
Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 67 | expected := []string{"foo"} |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 68 | if !reflect.DeepEqual(expected, gen.Implicits.Strings()[:len(expected)]) { |
| 69 | t.Errorf(`want arm inputs %v, got %v`, expected, gen.Implicits.Strings()) |
Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Yu Liu | d620101 | 2022-10-17 12:29:15 -0700 | [diff] [blame] | 72 | gen = ctx.ModuleForTests("gen", "android_arm64_armv8-a").Output("out_arm") |
Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 73 | expected = []string{"bar"} |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 74 | if !reflect.DeepEqual(expected, gen.Implicits.Strings()[:len(expected)]) { |
| 75 | t.Errorf(`want arm64 inputs %v, got %v`, expected, gen.Implicits.Strings()) |
Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
Colin Cross | 81ca6cd | 2020-08-06 17:46:48 -0700 | [diff] [blame] | 78 | |
| 79 | func TestLibraryGenruleCmd(t *testing.T) { |
| 80 | bp := ` |
| 81 | cc_library { |
| 82 | name: "libboth", |
| 83 | } |
| 84 | |
| 85 | cc_library_shared { |
| 86 | name: "libshared", |
| 87 | } |
| 88 | |
| 89 | cc_library_static { |
| 90 | name: "libstatic", |
| 91 | } |
| 92 | |
| 93 | cc_genrule { |
| 94 | name: "gen", |
| 95 | tool_files: ["tool"], |
| 96 | srcs: [ |
| 97 | ":libboth", |
| 98 | ":libshared", |
| 99 | ":libstatic", |
| 100 | ], |
| 101 | cmd: "$(location tool) $(in) $(out)", |
| 102 | out: ["out"], |
| 103 | } |
| 104 | ` |
| 105 | ctx := testCc(t, bp) |
| 106 | |
| 107 | gen := ctx.ModuleForTests("gen", "android_arm_armv7-a-neon").Output("out") |
| 108 | expected := []string{"libboth.so", "libshared.so", "libstatic.a"} |
| 109 | var got []string |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 110 | for _, input := range gen.Implicits { |
Colin Cross | 81ca6cd | 2020-08-06 17:46:48 -0700 | [diff] [blame] | 111 | got = append(got, input.Base()) |
| 112 | } |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 113 | if !reflect.DeepEqual(expected, got[:len(expected)]) { |
Colin Cross | 81ca6cd | 2020-08-06 17:46:48 -0700 | [diff] [blame] | 114 | t.Errorf(`want inputs %v, got %v`, expected, got) |
| 115 | } |
| 116 | } |
Colin Cross | f3bfd02 | 2021-09-27 15:15:06 -0700 | [diff] [blame] | 117 | |
| 118 | func TestCmdPrefix(t *testing.T) { |
| 119 | bp := ` |
| 120 | cc_genrule { |
| 121 | name: "gen", |
| 122 | cmd: "echo foo", |
| 123 | out: ["out"], |
| 124 | native_bridge_supported: true, |
| 125 | } |
| 126 | ` |
| 127 | |
| 128 | testCases := []struct { |
| 129 | name string |
| 130 | variant string |
| 131 | preparer android.FixturePreparer |
| 132 | |
| 133 | arch string |
| 134 | nativeBridge string |
| 135 | multilib string |
| 136 | }{ |
| 137 | { |
| 138 | name: "arm", |
| 139 | variant: "android_arm_armv7-a-neon", |
| 140 | arch: "arm", |
| 141 | multilib: "lib32", |
| 142 | }, |
| 143 | { |
| 144 | name: "arm64", |
| 145 | variant: "android_arm64_armv8-a", |
| 146 | arch: "arm64", |
| 147 | multilib: "lib64", |
| 148 | }, |
| 149 | { |
| 150 | name: "nativebridge", |
| 151 | variant: "android_native_bridge_arm_armv7-a-neon", |
| 152 | preparer: android.FixtureModifyConfig(func(config android.Config) { |
| 153 | config.Targets[android.Android] = []android.Target{ |
| 154 | { |
| 155 | Os: android.Android, |
| 156 | Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, |
| 157 | NativeBridge: android.NativeBridgeDisabled, |
| 158 | }, |
| 159 | { |
| 160 | Os: android.Android, |
| 161 | Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, |
| 162 | NativeBridge: android.NativeBridgeEnabled, |
| 163 | NativeBridgeHostArchName: "x86", |
| 164 | NativeBridgeRelativePath: "arm", |
| 165 | }, |
| 166 | } |
| 167 | }), |
| 168 | arch: "arm", |
| 169 | multilib: "lib32", |
| 170 | nativeBridge: "arm", |
| 171 | }, |
| 172 | } |
| 173 | |
| 174 | for _, tt := range testCases { |
| 175 | t.Run(tt.name, func(t *testing.T) { |
| 176 | result := android.GroupFixturePreparers( |
| 177 | PrepareForIntegrationTestWithCc, |
| 178 | android.OptionalFixturePreparer(tt.preparer), |
| 179 | ).RunTestWithBp(t, bp) |
| 180 | gen := result.ModuleForTests("gen", tt.variant) |
| 181 | sboxProto := android.RuleBuilderSboxProtoForTests(t, gen.Output("genrule.sbox.textproto")) |
| 182 | cmd := *sboxProto.Commands[0].Command |
| 183 | android.AssertStringDoesContain(t, "incorrect CC_ARCH", cmd, "CC_ARCH="+tt.arch+" ") |
| 184 | android.AssertStringDoesContain(t, "incorrect CC_NATIVE_BRIDGE", cmd, "CC_NATIVE_BRIDGE="+tt.nativeBridge+" ") |
| 185 | android.AssertStringDoesContain(t, "incorrect CC_MULTILIB", cmd, "CC_MULTILIB="+tt.multilib+" ") |
| 186 | }) |
| 187 | } |
| 188 | } |