Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 1 | // Copyright 2019 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 | "testing" |
Jiyong Park | 5df7bd3 | 2021-08-25 16:18:46 +0900 | [diff] [blame] | 19 | |
| 20 | "android/soong/android" |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 21 | ) |
| 22 | |
Liz Kammer | 25f369f | 2021-04-19 12:44:51 -0400 | [diff] [blame] | 23 | func TestMinSdkVersionsOfCrtObjects(t *testing.T) { |
| 24 | ctx := testCc(t, ` |
| 25 | cc_object { |
| 26 | name: "crt_foo", |
| 27 | srcs: ["foo.c"], |
| 28 | crt: true, |
| 29 | stl: "none", |
| 30 | min_sdk_version: "28", |
Jiyong Park | 5df7bd3 | 2021-08-25 16:18:46 +0900 | [diff] [blame] | 31 | vendor_available: true, |
Liz Kammer | 25f369f | 2021-04-19 12:44:51 -0400 | [diff] [blame] | 32 | }`) |
| 33 | |
Jiyong Park | 5df7bd3 | 2021-08-25 16:18:46 +0900 | [diff] [blame] | 34 | variants := []struct { |
| 35 | variant string |
| 36 | num string |
| 37 | }{ |
| 38 | {"android_arm64_armv8-a", "10000"}, |
| 39 | {"android_arm64_armv8-a_sdk_28", "28"}, |
| 40 | {"android_arm64_armv8-a_sdk_29", "29"}, |
| 41 | {"android_arm64_armv8-a_sdk_30", "30"}, |
| 42 | {"android_arm64_armv8-a_sdk_current", "10000"}, |
| 43 | {"android_vendor.29_arm64_armv8-a", "29"}, |
| 44 | } |
| 45 | for _, v := range variants { |
| 46 | cflags := ctx.ModuleForTests("crt_foo", v.variant).Rule("cc").Args["cFlags"] |
| 47 | expected := "-target aarch64-linux-android" + v.num + " " |
Liz Kammer | 25f369f | 2021-04-19 12:44:51 -0400 | [diff] [blame] | 48 | android.AssertStringDoesContain(t, "cflag", cflags, expected) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestUseCrtObjectOfCorrectVersion(t *testing.T) { |
| 53 | ctx := testCc(t, ` |
| 54 | cc_binary { |
| 55 | name: "bin", |
| 56 | srcs: ["foo.c"], |
| 57 | stl: "none", |
| 58 | min_sdk_version: "29", |
| 59 | sdk_version: "current", |
| 60 | } |
| 61 | `) |
| 62 | |
| 63 | // Sdk variant uses the crt object of the matching min_sdk_version |
| 64 | variant := "android_arm64_armv8-a_sdk" |
| 65 | crt := ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"] |
| 66 | android.AssertStringDoesContain(t, "crt dep of sdk variant", crt, |
| 67 | variant+"_29/crtbegin_dynamic.o") |
| 68 | |
| 69 | // platform variant uses the crt object built for platform |
| 70 | variant = "android_arm64_armv8-a" |
| 71 | crt = ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"] |
| 72 | android.AssertStringDoesContain(t, "crt dep of platform variant", crt, |
| 73 | variant+"/crtbegin_dynamic.o") |
| 74 | } |
| 75 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 76 | func TestLinkerScript(t *testing.T) { |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 77 | t.Run("script", func(t *testing.T) { |
| 78 | testCc(t, ` |
| 79 | cc_object { |
| 80 | name: "foo", |
| 81 | srcs: ["baz.o"], |
| 82 | linker_script: "foo.lds", |
| 83 | }`) |
| 84 | }) |
Liz Kammer | 07bc5f9 | 2021-04-08 13:28:56 -0400 | [diff] [blame] | 85 | } |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 86 | |
Liz Kammer | 07bc5f9 | 2021-04-08 13:28:56 -0400 | [diff] [blame] | 87 | func TestCcObjectWithBazel(t *testing.T) { |
| 88 | bp := ` |
| 89 | cc_object { |
| 90 | name: "foo", |
| 91 | srcs: ["baz.o"], |
| 92 | bazel_module: { label: "//foo/bar:bar" }, |
| 93 | }` |
| 94 | config := TestConfig(t.TempDir(), android.Android, nil, bp, nil) |
| 95 | config.BazelContext = android.MockBazelContext{ |
| 96 | OutputBaseDir: "outputbase", |
| 97 | LabelToOutputFiles: map[string][]string{ |
| 98 | "//foo/bar:bar": []string{"bazel_out.o"}}} |
| 99 | ctx := testCcWithConfig(t, config) |
| 100 | |
| 101 | module := ctx.ModuleForTests("foo", "android_arm_armv7-a-neon").Module() |
| 102 | outputFiles, err := module.(android.OutputFileProducer).OutputFiles("") |
| 103 | if err != nil { |
| 104 | t.Errorf("Unexpected error getting cc_object outputfiles %s", err) |
| 105 | } |
| 106 | |
| 107 | expectedOutputFiles := []string{"outputbase/execroot/__main__/bazel_out.o"} |
| 108 | android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings()) |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 109 | } |