blob: 004dfd3e7826e64005ee9c60ad0335a1128c1843 [file] [log] [blame]
Pete Bentley74c9bba2019-08-16 20:25:06 +01001// 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
15package cc
16
17import (
Colin Crossfb44cd22022-09-09 15:11:16 -070018 "fmt"
Pete Bentley74c9bba2019-08-16 20:25:06 +010019 "testing"
Jiyong Park5df7bd32021-08-25 16:18:46 +090020
21 "android/soong/android"
Pete Bentley74c9bba2019-08-16 20:25:06 +010022)
23
Liz Kammer25f369f2021-04-19 12:44:51 -040024func TestMinSdkVersionsOfCrtObjects(t *testing.T) {
Yi-Yo Chiang88960aa2024-01-19 15:02:29 +080025 bp := `
Liz Kammer25f369f2021-04-19 12:44:51 -040026 cc_object {
27 name: "crt_foo",
28 srcs: ["foo.c"],
29 crt: true,
30 stl: "none",
31 min_sdk_version: "28",
Jiyong Park5df7bd32021-08-25 16:18:46 +090032 vendor_available: true,
Yi-Yo Chiang88960aa2024-01-19 15:02:29 +080033 }
34 `
Jiyong Park5df7bd32021-08-25 16:18:46 +090035 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"},
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +090044 {"android_vendor_arm64_armv8-a", "10000"},
Jiyong Park5df7bd32021-08-25 16:18:46 +090045 }
Yi-Yo Chiang88960aa2024-01-19 15:02:29 +080046
47 ctx := prepareForCcTest.RunTestWithBp(t, bp)
Jiyong Park5df7bd32021-08-25 16:18:46 +090048 for _, v := range variants {
49 cflags := ctx.ModuleForTests("crt_foo", v.variant).Rule("cc").Args["cFlags"]
50 expected := "-target aarch64-linux-android" + v.num + " "
Liz Kammer25f369f2021-04-19 12:44:51 -040051 android.AssertStringDoesContain(t, "cflag", cflags, expected)
52 }
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +090053 ctx = prepareForCcTest.RunTestWithBp(t, bp)
Yi-Yo Chiang88960aa2024-01-19 15:02:29 +080054 android.AssertStringDoesContain(t, "cflag",
55 ctx.ModuleForTests("crt_foo", "android_vendor_arm64_armv8-a").Rule("cc").Args["cFlags"],
56 "-target aarch64-linux-android10000 ")
Liz Kammer25f369f2021-04-19 12:44:51 -040057}
58
59func TestUseCrtObjectOfCorrectVersion(t *testing.T) {
60 ctx := testCc(t, `
61 cc_binary {
62 name: "bin",
63 srcs: ["foo.c"],
64 stl: "none",
65 min_sdk_version: "29",
66 sdk_version: "current",
67 }
68 `)
69
70 // Sdk variant uses the crt object of the matching min_sdk_version
71 variant := "android_arm64_armv8-a_sdk"
72 crt := ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
73 android.AssertStringDoesContain(t, "crt dep of sdk variant", crt,
Dan Albert5b0d4f32023-04-04 23:22:11 +000074 "29/crtbegin_dynamic.o")
Liz Kammer25f369f2021-04-19 12:44:51 -040075
76 // platform variant uses the crt object built for platform
77 variant = "android_arm64_armv8-a"
78 crt = ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
79 android.AssertStringDoesContain(t, "crt dep of platform variant", crt,
80 variant+"/crtbegin_dynamic.o")
81}
82
Pete Bentley74c9bba2019-08-16 20:25:06 +010083func TestLinkerScript(t *testing.T) {
Pete Bentley74c9bba2019-08-16 20:25:06 +010084 t.Run("script", func(t *testing.T) {
85 testCc(t, `
86 cc_object {
87 name: "foo",
88 srcs: ["baz.o"],
89 linker_script: "foo.lds",
90 }`)
91 })
Liz Kammer07bc5f92021-04-08 13:28:56 -040092}
Pete Bentley74c9bba2019-08-16 20:25:06 +010093
Colin Crossfb44cd22022-09-09 15:11:16 -070094func TestCcObjectOutputFile(t *testing.T) {
95 testcases := []struct {
96 name string
97 moduleName string
98 bp string
99 }{
100 {
101 name: "normal",
102 moduleName: "foo",
103 bp: `
104 srcs: ["bar.c"],
105 `,
106 },
107 {
108 name: "suffix",
109 moduleName: "foo.o",
110 bp: `
111 srcs: ["bar.c"],
112 `,
113 },
114 {
115 name: "keep symbols",
116 moduleName: "foo",
117 bp: `
118 srcs: ["bar.c"],
119 prefix_symbols: "foo_",
120 `,
121 },
122 {
123 name: "partial linking",
124 moduleName: "foo",
125 bp: `
126 srcs: ["bar.c", "baz.c"],
127 `,
128 },
129 {
130 name: "partial linking and prefix symbols",
131 moduleName: "foo",
132 bp: `
133 srcs: ["bar.c", "baz.c"],
134 prefix_symbols: "foo_",
135 `,
136 },
137 }
138
139 for _, testcase := range testcases {
140 bp := fmt.Sprintf(`
141 cc_object {
142 name: "%s",
143 %s
144 }
145 `, testcase.moduleName, testcase.bp)
146 t.Run(testcase.name, func(t *testing.T) {
147 ctx := PrepareForIntegrationTestWithCc.RunTestWithBp(t, bp)
148 android.AssertPathRelativeToTopEquals(t, "expected output file foo.o",
149 fmt.Sprintf("out/soong/.intermediates/%s/android_arm64_armv8-a/foo.o", testcase.moduleName),
150 ctx.ModuleForTests(testcase.moduleName, "android_arm64_armv8-a").Output("foo.o").Output)
151 })
152 }
153
154}