blob: 0e5508a4aa0887874441698afdb3c31b9defb98a [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 (
Liz Kammer07bc5f92021-04-08 13:28:56 -040018 "android/soong/android"
Pete Bentley74c9bba2019-08-16 20:25:06 +010019 "testing"
20)
21
Liz Kammer25f369f2021-04-19 12:44:51 -040022func TestMinSdkVersionsOfCrtObjects(t *testing.T) {
23 ctx := testCc(t, `
24 cc_object {
25 name: "crt_foo",
26 srcs: ["foo.c"],
27 crt: true,
28 stl: "none",
29 min_sdk_version: "28",
30
31 }`)
32
33 arch := "android_arm64_armv8-a"
34 for _, v := range []string{"", "28", "29", "30", "current"} {
35 var variant string
36 // platform variant
37 if v == "" {
38 variant = arch
39 } else {
40 variant = arch + "_sdk_" + v
41 }
42 cflags := ctx.ModuleForTests("crt_foo", variant).Rule("cc").Args["cFlags"]
43 vNum := v
44 if v == "current" || v == "" {
45 vNum = "10000"
46 }
47 expected := "-target aarch64-linux-android" + vNum + " "
48 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}