Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -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 android |
| 16 | |
| 17 | import ( |
| 18 | "io/ioutil" |
| 19 | "os" |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 20 | "path/filepath" |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 21 | "reflect" |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 22 | "testing" |
| 23 | ) |
| 24 | |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 25 | func testPrebuiltEtc(t *testing.T, bp string) (*TestContext, Config) { |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 26 | config, buildDir := setUp(t) |
| 27 | defer tearDown(buildDir) |
| 28 | ctx := NewTestArchContext() |
| 29 | ctx.RegisterModuleType("prebuilt_etc", ModuleFactoryAdaptor(PrebuiltEtcFactory)) |
Jaewoong Jung | 4b44fcd | 2019-02-07 08:28:03 -0800 | [diff] [blame] | 30 | ctx.RegisterModuleType("prebuilt_etc_host", ModuleFactoryAdaptor(PrebuiltEtcHostFactory)) |
Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 31 | ctx.RegisterModuleType("prebuilt_usr_share", ModuleFactoryAdaptor(PrebuiltUserShareFactory)) |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 32 | ctx.RegisterModuleType("prebuilt_usr_share_host", ModuleFactoryAdaptor(PrebuiltUserShareHostFactory)) |
Patrice Arruda | 61583eb | 2019-05-14 08:20:45 -0700 | [diff] [blame] | 33 | ctx.RegisterModuleType("prebuilt_font", ModuleFactoryAdaptor(PrebuiltFontFactory)) |
Patrice Arruda | 057a8b1 | 2019-06-03 15:29:27 -0700 | [diff] [blame] | 34 | ctx.RegisterModuleType("prebuilt_firmware", ModuleFactoryAdaptor(PrebuiltFirmwareFactory)) |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 35 | ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { |
| 36 | ctx.BottomUp("prebuilt_etc", prebuiltEtcMutator).Parallel() |
| 37 | }) |
| 38 | ctx.Register() |
| 39 | mockFiles := map[string][]byte{ |
| 40 | "Android.bp": []byte(bp), |
| 41 | "foo.conf": nil, |
| 42 | "bar.conf": nil, |
| 43 | "baz.conf": nil, |
| 44 | } |
| 45 | ctx.MockFileSystem(mockFiles) |
| 46 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 47 | FailIfErrored(t, errs) |
| 48 | _, errs = ctx.PrepareBuildActions(config) |
| 49 | FailIfErrored(t, errs) |
| 50 | |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 51 | return ctx, config |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | func setUp(t *testing.T) (config Config, buildDir string) { |
| 55 | buildDir, err := ioutil.TempDir("", "soong_prebuilt_etc_test") |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | |
| 60 | config = TestArchConfig(buildDir, nil) |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | func tearDown(buildDir string) { |
| 65 | os.RemoveAll(buildDir) |
| 66 | } |
| 67 | |
| 68 | func TestPrebuiltEtcVariants(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 69 | ctx, _ := testPrebuiltEtc(t, ` |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 70 | prebuilt_etc { |
| 71 | name: "foo.conf", |
| 72 | src: "foo.conf", |
| 73 | } |
| 74 | prebuilt_etc { |
| 75 | name: "bar.conf", |
| 76 | src: "bar.conf", |
| 77 | recovery_available: true, |
| 78 | } |
| 79 | prebuilt_etc { |
| 80 | name: "baz.conf", |
| 81 | src: "baz.conf", |
| 82 | recovery: true, |
| 83 | } |
| 84 | `) |
| 85 | |
| 86 | foo_variants := ctx.ModuleVariantsForTests("foo.conf") |
| 87 | if len(foo_variants) != 1 { |
| 88 | t.Errorf("expected 1, got %#v", foo_variants) |
| 89 | } |
| 90 | |
| 91 | bar_variants := ctx.ModuleVariantsForTests("bar.conf") |
| 92 | if len(bar_variants) != 2 { |
| 93 | t.Errorf("expected 2, got %#v", bar_variants) |
| 94 | } |
| 95 | |
| 96 | baz_variants := ctx.ModuleVariantsForTests("baz.conf") |
| 97 | if len(baz_variants) != 1 { |
| 98 | t.Errorf("expected 1, got %#v", bar_variants) |
| 99 | } |
| 100 | } |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 101 | |
| 102 | func TestPrebuiltEtcOutputPath(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 103 | ctx, _ := testPrebuiltEtc(t, ` |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 104 | prebuilt_etc { |
| 105 | name: "foo.conf", |
| 106 | src: "foo.conf", |
| 107 | filename: "foo.installed.conf", |
| 108 | } |
| 109 | `) |
| 110 | |
Jaewoong Jung | b9a1151 | 2019-01-15 10:47:05 -0800 | [diff] [blame] | 111 | p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 112 | if p.outputFilePath.Base() != "foo.installed.conf" { |
| 113 | t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base()) |
| 114 | } |
| 115 | } |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 116 | |
| 117 | func TestPrebuiltEtcGlob(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 118 | ctx, _ := testPrebuiltEtc(t, ` |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 119 | prebuilt_etc { |
| 120 | name: "my_foo", |
| 121 | src: "foo.*", |
| 122 | } |
| 123 | prebuilt_etc { |
| 124 | name: "my_bar", |
| 125 | src: "bar.*", |
| 126 | filename_from_src: true, |
| 127 | } |
| 128 | `) |
| 129 | |
Jaewoong Jung | b9a1151 | 2019-01-15 10:47:05 -0800 | [diff] [blame] | 130 | p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 131 | if p.outputFilePath.Base() != "my_foo" { |
| 132 | t.Errorf("expected my_foo, got %q", p.outputFilePath.Base()) |
| 133 | } |
| 134 | |
Jaewoong Jung | b9a1151 | 2019-01-15 10:47:05 -0800 | [diff] [blame] | 135 | p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 136 | if p.outputFilePath.Base() != "bar.conf" { |
| 137 | t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base()) |
| 138 | } |
| 139 | } |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 140 | |
| 141 | func TestPrebuiltEtcAndroidMk(t *testing.T) { |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 142 | ctx, config := testPrebuiltEtc(t, ` |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 143 | prebuilt_etc { |
| 144 | name: "foo", |
| 145 | src: "foo.conf", |
| 146 | owner: "abc", |
| 147 | filename_from_src: true, |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 148 | required: ["modA", "moduleB"], |
| 149 | host_required: ["hostModA", "hostModB"], |
| 150 | target_required: ["targetModA"], |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 151 | } |
| 152 | `) |
| 153 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 154 | expected := map[string][]string{ |
| 155 | "LOCAL_MODULE": {"foo"}, |
| 156 | "LOCAL_MODULE_CLASS": {"ETC"}, |
| 157 | "LOCAL_MODULE_OWNER": {"abc"}, |
| 158 | "LOCAL_INSTALLED_MODULE_STEM": {"foo.conf"}, |
| 159 | "LOCAL_REQUIRED_MODULES": {"modA", "moduleB"}, |
| 160 | "LOCAL_HOST_REQUIRED_MODULES": {"hostModA", "hostModB"}, |
| 161 | "LOCAL_TARGET_REQUIRED_MODULES": {"targetModA"}, |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 165 | entries := AndroidMkEntriesForTest(t, config, "", mod) |
| 166 | for k, expectedValue := range expected { |
| 167 | if value, ok := entries.EntryMap[k]; ok { |
| 168 | if !reflect.DeepEqual(value, expectedValue) { |
| 169 | t.Errorf("Incorrect %s '%s', expected '%s'", k, value, expectedValue) |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 170 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 171 | } else { |
| 172 | t.Errorf("No %s defined, saw %q", k, entries.EntryMap) |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame] | 176 | |
| 177 | func TestPrebuiltEtcHost(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 178 | ctx, _ := testPrebuiltEtc(t, ` |
Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame] | 179 | prebuilt_etc_host { |
| 180 | name: "foo.conf", |
| 181 | src: "foo.conf", |
| 182 | } |
| 183 | `) |
| 184 | |
| 185 | buildOS := BuildOs.String() |
| 186 | p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc) |
| 187 | if !p.Host() { |
| 188 | t.Errorf("host bit is not set for a prebuilt_etc_host module.") |
| 189 | } |
| 190 | } |
Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 191 | |
| 192 | func TestPrebuiltUserShareInstallDirPath(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 193 | ctx, _ := testPrebuiltEtc(t, ` |
Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 194 | prebuilt_usr_share { |
| 195 | name: "foo.conf", |
| 196 | src: "foo.conf", |
| 197 | sub_dir: "bar", |
| 198 | } |
| 199 | `) |
| 200 | |
| 201 | p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
| 202 | expected := "target/product/test_device/system/usr/share/bar" |
| 203 | if p.installDirPath.RelPathString() != expected { |
| 204 | t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString()) |
| 205 | } |
| 206 | } |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 207 | |
| 208 | func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) { |
| 209 | ctx, config := testPrebuiltEtc(t, ` |
| 210 | prebuilt_usr_share_host { |
| 211 | name: "foo.conf", |
| 212 | src: "foo.conf", |
| 213 | sub_dir: "bar", |
| 214 | } |
| 215 | `) |
| 216 | |
| 217 | buildOS := BuildOs.String() |
| 218 | p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc) |
| 219 | expected := filepath.Join("host", config.PrebuiltOS(), "usr", "share", "bar") |
| 220 | if p.installDirPath.RelPathString() != expected { |
| 221 | t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString()) |
| 222 | } |
| 223 | } |
Patrice Arruda | 61583eb | 2019-05-14 08:20:45 -0700 | [diff] [blame] | 224 | |
| 225 | func TestPrebuiltFontInstallDirPath(t *testing.T) { |
| 226 | ctx, _ := testPrebuiltEtc(t, ` |
| 227 | prebuilt_font { |
| 228 | name: "foo.conf", |
| 229 | src: "foo.conf", |
| 230 | } |
| 231 | `) |
| 232 | |
| 233 | p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
| 234 | expected := "target/product/test_device/system/fonts" |
| 235 | if p.installDirPath.RelPathString() != expected { |
| 236 | t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString()) |
| 237 | } |
| 238 | } |
Patrice Arruda | 057a8b1 | 2019-06-03 15:29:27 -0700 | [diff] [blame] | 239 | |
| 240 | func TestPrebuiltFirmwareDirPath(t *testing.T) { |
| 241 | targetPath := "target/product/test_device" |
| 242 | tests := []struct { |
| 243 | description string |
| 244 | config string |
| 245 | expectedPath string |
| 246 | }{{ |
| 247 | description: "prebuilt: system firmware", |
| 248 | config: ` |
| 249 | prebuilt_firmware { |
| 250 | name: "foo.conf", |
| 251 | src: "foo.conf", |
| 252 | }`, |
| 253 | expectedPath: filepath.Join(targetPath, "system/etc/firmware"), |
| 254 | }, { |
| 255 | description: "prebuilt: vendor firmware", |
| 256 | config: ` |
| 257 | prebuilt_firmware { |
| 258 | name: "foo.conf", |
| 259 | src: "foo.conf", |
| 260 | soc_specific: true, |
| 261 | sub_dir: "sub_dir", |
| 262 | }`, |
| 263 | expectedPath: filepath.Join(targetPath, "vendor/firmware/sub_dir"), |
| 264 | }} |
| 265 | for _, tt := range tests { |
| 266 | t.Run(tt.description, func(t *testing.T) { |
| 267 | ctx, _ := testPrebuiltEtc(t, tt.config) |
| 268 | p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
| 269 | if p.installDirPath.RelPathString() != tt.expectedPath { |
| 270 | t.Errorf("expected %q, got %q", tt.expectedPath, p.installDirPath) |
| 271 | } |
| 272 | }) |
| 273 | } |
| 274 | } |