| 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 | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 25 | ctx := android.NewTestArchContext() | 
| Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 26 | ctx.RegisterModuleType("cc_genrule", genRuleFactory) | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 27 | ctx.Register(config) | 
| 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)", | 
|  | 43 | arch: { | 
|  | 44 | arm: { | 
|  | 45 | srcs: ["foo"], | 
|  | 46 | out: ["out_arm"], | 
|  | 47 | }, | 
|  | 48 | arm64: { | 
|  | 49 | srcs: ["bar"], | 
|  | 50 | out: ["out_arm64"], | 
|  | 51 | }, | 
|  | 52 | }, | 
|  | 53 | } | 
|  | 54 | ` | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 55 | config := android.TestArchConfig(buildDir, nil, bp, fs) | 
| Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 56 |  | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 57 | ctx := testGenruleContext(config) | 
| Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 58 |  | 
|  | 59 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) | 
|  | 60 | if errs == nil { | 
|  | 61 | _, errs = ctx.PrepareBuildActions(config) | 
|  | 62 | } | 
|  | 63 | if errs != nil { | 
|  | 64 | t.Fatal(errs) | 
|  | 65 | } | 
|  | 66 |  | 
| Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 67 | gen := ctx.ModuleForTests("gen", "android_arm_armv7-a-neon").Output("out_arm") | 
| Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 68 | expected := []string{"foo"} | 
|  | 69 | if !reflect.DeepEqual(expected, gen.Inputs.Strings()) { | 
|  | 70 | t.Errorf(`want arm inputs %v, got %v`, expected, gen.Inputs.Strings()) | 
|  | 71 | } | 
|  | 72 |  | 
| Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 73 | gen = ctx.ModuleForTests("gen", "android_arm64_armv8-a").Output("out_arm64") | 
| Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 74 | expected = []string{"bar"} | 
|  | 75 | if !reflect.DeepEqual(expected, gen.Inputs.Strings()) { | 
|  | 76 | t.Errorf(`want arm64 inputs %v, got %v`, expected, gen.Inputs.Strings()) | 
|  | 77 | } | 
|  | 78 | } | 
| Colin Cross | 81ca6cd | 2020-08-06 17:46:48 -0700 | [diff] [blame] | 79 |  | 
|  | 80 | func TestLibraryGenruleCmd(t *testing.T) { | 
|  | 81 | bp := ` | 
|  | 82 | cc_library { | 
|  | 83 | name: "libboth", | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | cc_library_shared { | 
|  | 87 | name: "libshared", | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | cc_library_static { | 
|  | 91 | name: "libstatic", | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | cc_genrule { | 
|  | 95 | name: "gen", | 
|  | 96 | tool_files: ["tool"], | 
|  | 97 | srcs: [ | 
|  | 98 | ":libboth", | 
|  | 99 | ":libshared", | 
|  | 100 | ":libstatic", | 
|  | 101 | ], | 
|  | 102 | cmd: "$(location tool) $(in) $(out)", | 
|  | 103 | out: ["out"], | 
|  | 104 | } | 
|  | 105 | ` | 
|  | 106 | ctx := testCc(t, bp) | 
|  | 107 |  | 
|  | 108 | gen := ctx.ModuleForTests("gen", "android_arm_armv7-a-neon").Output("out") | 
|  | 109 | expected := []string{"libboth.so", "libshared.so", "libstatic.a"} | 
|  | 110 | var got []string | 
|  | 111 | for _, input := range gen.Inputs { | 
|  | 112 | got = append(got, input.Base()) | 
|  | 113 | } | 
|  | 114 | if !reflect.DeepEqual(expected, got) { | 
|  | 115 | t.Errorf(`want inputs %v, got %v`, expected, got) | 
|  | 116 | } | 
|  | 117 | } |