blob: 259a892ab5f7d568f35e6fb3b5535c4760bcf19e [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 (
18 "testing"
Jiyong Park5df7bd32021-08-25 16:18:46 +090019
20 "android/soong/android"
Pete Bentley74c9bba2019-08-16 20:25:06 +010021)
22
Liz Kammer25f369f2021-04-19 12:44:51 -040023func 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 Park5df7bd32021-08-25 16:18:46 +090031 vendor_available: true,
Liz Kammer25f369f2021-04-19 12:44:51 -040032 }`)
33
Jiyong Park5df7bd32021-08-25 16:18:46 +090034 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 Kammer25f369f2021-04-19 12:44:51 -040048 android.AssertStringDoesContain(t, "cflag", cflags, expected)
49 }
50}
51
52func 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 Bentley74c9bba2019-08-16 20:25:06 +010076func TestLinkerScript(t *testing.T) {
Pete Bentley74c9bba2019-08-16 20:25:06 +010077 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 Kammer07bc5f92021-04-08 13:28:56 -040085}
Pete Bentley74c9bba2019-08-16 20:25:06 +010086
Liz Kammer07bc5f92021-04-08 13:28:56 -040087func TestCcObjectWithBazel(t *testing.T) {
88 bp := `
89cc_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 Bentley74c9bba2019-08-16 20:25:06 +0100109}