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 ( |
Colin Cross | fb44cd2 | 2022-09-09 15:11:16 -0700 | [diff] [blame] | 18 | "fmt" |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 19 | "testing" |
Jiyong Park | 5df7bd3 | 2021-08-25 16:18:46 +0900 | [diff] [blame] | 20 | |
| 21 | "android/soong/android" |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 22 | ) |
| 23 | |
Liz Kammer | 25f369f | 2021-04-19 12:44:51 -0400 | [diff] [blame] | 24 | func TestMinSdkVersionsOfCrtObjects(t *testing.T) { |
| 25 | ctx := testCc(t, ` |
| 26 | cc_object { |
| 27 | name: "crt_foo", |
| 28 | srcs: ["foo.c"], |
| 29 | crt: true, |
| 30 | stl: "none", |
| 31 | min_sdk_version: "28", |
Jiyong Park | 5df7bd3 | 2021-08-25 16:18:46 +0900 | [diff] [blame] | 32 | vendor_available: true, |
Liz Kammer | 25f369f | 2021-04-19 12:44:51 -0400 | [diff] [blame] | 33 | }`) |
| 34 | |
Jiyong Park | 5df7bd3 | 2021-08-25 16:18:46 +0900 | [diff] [blame] | 35 | variants := []struct { |
| 36 | variant string |
| 37 | num string |
| 38 | }{ |
| 39 | {"android_arm64_armv8-a", "10000"}, |
| 40 | {"android_arm64_armv8-a_sdk_28", "28"}, |
| 41 | {"android_arm64_armv8-a_sdk_29", "29"}, |
| 42 | {"android_arm64_armv8-a_sdk_30", "30"}, |
| 43 | {"android_arm64_armv8-a_sdk_current", "10000"}, |
| 44 | {"android_vendor.29_arm64_armv8-a", "29"}, |
| 45 | } |
| 46 | for _, v := range variants { |
| 47 | cflags := ctx.ModuleForTests("crt_foo", v.variant).Rule("cc").Args["cFlags"] |
| 48 | expected := "-target aarch64-linux-android" + v.num + " " |
Liz Kammer | 25f369f | 2021-04-19 12:44:51 -0400 | [diff] [blame] | 49 | android.AssertStringDoesContain(t, "cflag", cflags, expected) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestUseCrtObjectOfCorrectVersion(t *testing.T) { |
| 54 | ctx := testCc(t, ` |
| 55 | cc_binary { |
| 56 | name: "bin", |
| 57 | srcs: ["foo.c"], |
| 58 | stl: "none", |
| 59 | min_sdk_version: "29", |
| 60 | sdk_version: "current", |
| 61 | } |
| 62 | `) |
| 63 | |
| 64 | // Sdk variant uses the crt object of the matching min_sdk_version |
| 65 | variant := "android_arm64_armv8-a_sdk" |
| 66 | crt := ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"] |
| 67 | android.AssertStringDoesContain(t, "crt dep of sdk variant", crt, |
Dan Albert | 5b0d4f3 | 2023-04-04 23:22:11 +0000 | [diff] [blame] | 68 | "29/crtbegin_dynamic.o") |
Liz Kammer | 25f369f | 2021-04-19 12:44:51 -0400 | [diff] [blame] | 69 | |
| 70 | // platform variant uses the crt object built for platform |
| 71 | variant = "android_arm64_armv8-a" |
| 72 | crt = ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"] |
| 73 | android.AssertStringDoesContain(t, "crt dep of platform variant", crt, |
| 74 | variant+"/crtbegin_dynamic.o") |
| 75 | } |
| 76 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 77 | func TestLinkerScript(t *testing.T) { |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 78 | t.Run("script", func(t *testing.T) { |
| 79 | testCc(t, ` |
| 80 | cc_object { |
| 81 | name: "foo", |
| 82 | srcs: ["baz.o"], |
| 83 | linker_script: "foo.lds", |
| 84 | }`) |
| 85 | }) |
Liz Kammer | 07bc5f9 | 2021-04-08 13:28:56 -0400 | [diff] [blame] | 86 | } |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 87 | |
Colin Cross | fb44cd2 | 2022-09-09 15:11:16 -0700 | [diff] [blame] | 88 | func TestCcObjectOutputFile(t *testing.T) { |
| 89 | testcases := []struct { |
| 90 | name string |
| 91 | moduleName string |
| 92 | bp string |
| 93 | }{ |
| 94 | { |
| 95 | name: "normal", |
| 96 | moduleName: "foo", |
| 97 | bp: ` |
| 98 | srcs: ["bar.c"], |
| 99 | `, |
| 100 | }, |
| 101 | { |
| 102 | name: "suffix", |
| 103 | moduleName: "foo.o", |
| 104 | bp: ` |
| 105 | srcs: ["bar.c"], |
| 106 | `, |
| 107 | }, |
| 108 | { |
| 109 | name: "keep symbols", |
| 110 | moduleName: "foo", |
| 111 | bp: ` |
| 112 | srcs: ["bar.c"], |
| 113 | prefix_symbols: "foo_", |
| 114 | `, |
| 115 | }, |
| 116 | { |
| 117 | name: "partial linking", |
| 118 | moduleName: "foo", |
| 119 | bp: ` |
| 120 | srcs: ["bar.c", "baz.c"], |
| 121 | `, |
| 122 | }, |
| 123 | { |
| 124 | name: "partial linking and prefix symbols", |
| 125 | moduleName: "foo", |
| 126 | bp: ` |
| 127 | srcs: ["bar.c", "baz.c"], |
| 128 | prefix_symbols: "foo_", |
| 129 | `, |
| 130 | }, |
| 131 | } |
| 132 | |
| 133 | for _, testcase := range testcases { |
| 134 | bp := fmt.Sprintf(` |
| 135 | cc_object { |
| 136 | name: "%s", |
| 137 | %s |
| 138 | } |
| 139 | `, testcase.moduleName, testcase.bp) |
| 140 | t.Run(testcase.name, func(t *testing.T) { |
| 141 | ctx := PrepareForIntegrationTestWithCc.RunTestWithBp(t, bp) |
| 142 | android.AssertPathRelativeToTopEquals(t, "expected output file foo.o", |
| 143 | fmt.Sprintf("out/soong/.intermediates/%s/android_arm64_armv8-a/foo.o", testcase.moduleName), |
| 144 | ctx.ModuleForTests(testcase.moduleName, "android_arm64_armv8-a").Output("foo.o").Output) |
| 145 | }) |
| 146 | } |
| 147 | |
| 148 | } |